Skip to content

Commit

Permalink
🎈 perf:modify deprecated methods (#328)
Browse files Browse the repository at this point in the history
Signed-off-by: xiaok29 <[email protected]>
  • Loading branch information
XiaoK29 authored Jun 17, 2024
1 parent 8d92774 commit a831afd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/3rdparty/jsonschema/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -72,7 +72,7 @@ func FetchSchema(ctx context.Context, uri string, schema *Schema) error {
if err != nil {
return err
}
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
Expand All @@ -82,7 +82,7 @@ func FetchSchema(ctx context.Context, uri string, schema *Schema) error {
return json.Unmarshal(body, schema)
}
if u.Scheme == "file" {
body, err := ioutil.ReadFile(u.Path)
body, err := os.ReadFile(u.Path)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/http_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -38,7 +38,7 @@ func httpPost(urlpath string, input, output interface{}) error {
}
defer r.Body.Close()

bodyData, err := ioutil.ReadAll(r.Body)
bodyData, err := io.ReadAll(r.Body)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/tools/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -47,17 +47,17 @@ func TestFormatCode(t *testing.T) {
func TestFormatPath(t *testing.T) {
successDir := filepath.Join("testdata", "success")
expectedFileSuffix := ".formatted"
expectedFiles := findFiles(t, successDir, func(info fs.FileInfo) bool {
expectedFiles := findFiles(t, successDir, func(info fs.DirEntry) bool {
return strings.HasSuffix(info.Name(), expectedFileSuffix)
})

sourceFiles := findFiles(t, successDir, func(info fs.FileInfo) bool {
sourceFiles := findFiles(t, successDir, func(info fs.DirEntry) bool {
return strings.HasSuffix(info.Name(), ".k")
})
var sourceFilesBackup []kclFile

for _, sourceFile := range sourceFiles {
content, err := ioutil.ReadFile(sourceFile)
content, err := os.ReadFile(sourceFile)
if err != nil {
t.Fatalf("read source file content failed: %s", sourceFile)
}
Expand All @@ -82,26 +82,26 @@ func TestFormatPath(t *testing.T) {
assert.ElementsMatchf(t, expectedFiles, changedPathsRelative, "format path get wrong result. changedPath mismatch, expect: %s, get: %s", expectedFiles, changedPathsRelative)

for _, expectedFile := range expectedFiles {
expected, err := ioutil.ReadFile(expectedFile)
expected, err := os.ReadFile(expectedFile)
if runtime.GOOS == "windows" {
expected = bytes.Replace(expected, []byte{0xd, 0xa}, []byte{0xa}, -1)
}
if err != nil {
t.Fatalf("read expected formatted file failed: %s", expectedFile)
}
actualFile := strings.TrimSuffix(expectedFile, expectedFileSuffix) + ".k"
get, err := ioutil.ReadFile(actualFile)
get, err := os.ReadFile(actualFile)
if err != nil {
t.Fatalf("read actual formatted file failed: %s", actualFile)
}
assert.Equal(t, expected, get, fmt.Sprintf("format path get wrong result. formatted content mismatch, file: %s, expect: %s, get: %s", actualFile, expected, get))
}
}

type filterFile func(fs.FileInfo) bool
type filterFile func(fs.DirEntry) bool

func findFiles(t testing.TB, testDir string, filter filterFile) (names []string) {
files, err := ioutil.ReadDir(testDir)
files, err := os.ReadDir(testDir)
if err != nil {
t.Fatalf("ReadDir failed: %v", err)
}
Expand All @@ -122,7 +122,7 @@ type kclFile struct {

func writeFile(t *testing.T, kclfiles []kclFile) {
for _, backUpFile := range kclfiles {
err := ioutil.WriteFile(backUpFile.name, backUpFile.content, 0666)
err := os.WriteFile(backUpFile.name, backUpFile.content, 0666)
if err != nil {
t.Logf("write back formatted source file failed: %v", err)
}
Expand Down

0 comments on commit a831afd

Please sign in to comment.