From ee9667e004f8fbdcac54919796a4141c2210d5db Mon Sep 17 00:00:00 2001 From: zachary-walters Date: Sun, 29 Jan 2023 16:32:21 -0600 Subject: [PATCH 1/4] [skip changelog] Updated the deprecated ioutil dependency --- arduino/builder/sketch_test.go | 5 ++--- arduino/httpclient/httpclient_test.go | 4 ++-- arduino/resources/checksums.go | 3 +-- arduino/resources/helpers_test.go | 4 ++-- client_example/main.go | 3 +-- configuration/configuration_test.go | 3 +-- i18n/cmd/commands/transifex/pull_transifex.go | 3 +-- internal/cli/cli.go | 4 ++-- legacy/builder/ctags/ctags_parser_test.go | 6 +++--- legacy/builder/ctags/ctags_to_prototypes_test.go | 4 ++-- legacy/builder/gohasissues/go_has_issues.go | 16 +++++++++++----- legacy/builder/utils/utils.go | 13 +++++++++++-- 12 files changed, 39 insertions(+), 29 deletions(-) diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index b47a09c02cb..a8f1cb6b1b0 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -17,7 +17,6 @@ package builder_test import ( "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -31,7 +30,7 @@ import ( ) func tmpDirOrDie() *paths.Path { - dir, err := ioutil.TempDir(os.TempDir(), "builder_test") + dir, err := os.MkdirTemp(os.TempDir(), "builder_test") if err != nil { panic(fmt.Sprintf("error creating tmp dir: %v", err)) } @@ -44,7 +43,7 @@ func TestSaveSketch(t *testing.T) { sketchFile := filepath.Join("testdata", sketchName) tmp := tmpDirOrDie() defer tmp.RemoveAll() - source, err := ioutil.ReadFile(sketchFile) + source, err := os.ReadFile(sketchFile) if err != nil { t.Fatalf("unable to read golden file %s: %v", sketchFile, err) } diff --git a/arduino/httpclient/httpclient_test.go b/arduino/httpclient/httpclient_test.go index 010363a652d..a2dbc61c291 100644 --- a/arduino/httpclient/httpclient_test.go +++ b/arduino/httpclient/httpclient_test.go @@ -17,7 +17,7 @@ package httpclient import ( "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -42,7 +42,7 @@ func TestUserAgentHeader(t *testing.T) { response, err := client.Do(request) require.NoError(t, err) - b, err := ioutil.ReadAll(response.Body) + b, err := io.ReadAll(response.Body) require.NoError(t, err) require.Equal(t, "test-user-agent", string(b)) diff --git a/arduino/resources/checksums.go b/arduino/resources/checksums.go index 5e816b93c28..cadd3ea6685 100644 --- a/arduino/resources/checksums.go +++ b/arduino/resources/checksums.go @@ -24,7 +24,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -154,7 +153,7 @@ func computeDirChecksum(root string) (string, error) { // CheckDirChecksum reads checksum from the package.json and compares it with a recomputed value. func CheckDirChecksum(root string) (bool, error) { - packageJSON, err := ioutil.ReadFile(filepath.Join(root, packageFileName)) + packageJSON, err := os.ReadFile(filepath.Join(root, packageFileName)) if err != nil { return false, err } diff --git a/arduino/resources/helpers_test.go b/arduino/resources/helpers_test.go index d277cecbfcb..79f17548151 100644 --- a/arduino/resources/helpers_test.go +++ b/arduino/resources/helpers_test.go @@ -16,7 +16,7 @@ package resources import ( - "io/ioutil" + "os" "net/http" "net/http/httptest" "strings" @@ -67,7 +67,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) { // User-Agent: arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111 // Accept-Encoding: gzip - b, err := ioutil.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name + b, err := os.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name require.NoError(t, err) requestLines := strings.Split(string(b), "\r\n") diff --git a/client_example/main.go b/client_example/main.go index f0fcceffa57..b8d801ae6c8 100644 --- a/client_example/main.go +++ b/client_example/main.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "log" "os" "path" @@ -53,7 +52,7 @@ func main() { // To avoid polluting an existing arduino-cli installation, the example // client uses a temp folder to keep cores, libraries and the like. // You can point `dataDir` to a location that better fits your needs. - dataDir, err = ioutil.TempDir("", "arduino-rpc-client") + dataDir, err = os.MkdirTemp("", "arduino-rpc-client") if err != nil { log.Fatal(err) } diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index 0282f55b641..880b40a32f2 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -17,7 +17,6 @@ package configuration import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -27,7 +26,7 @@ import ( ) func tmpDirOrDie() string { - dir, err := ioutil.TempDir(os.TempDir(), "cli_test") + dir, err := os.MkdirTemp(os.TempDir(), "cli_test") if err != nil { panic(fmt.Sprintf("error creating tmp dir: %v", err)) } diff --git a/i18n/cmd/commands/transifex/pull_transifex.go b/i18n/cmd/commands/transifex/pull_transifex.go index cabfb8d6fee..b8b3cdded4c 100644 --- a/i18n/cmd/commands/transifex/pull_transifex.go +++ b/i18n/cmd/commands/transifex/pull_transifex.go @@ -20,7 +20,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" "sync" @@ -54,7 +53,7 @@ func getLanguages() []string { } defer res.Body.Close() - b, err := ioutil.ReadAll(res.Body) + b, err := io.ReadAll(res.Body) if err != nil { fmt.Println(err.Error()) os.Exit(1) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 61a33e75464..c7f6753182f 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -17,7 +17,7 @@ package cli import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -185,7 +185,7 @@ func preRun(cmd *cobra.Command, args []string) { DisableColors: color.NoColor, }) } else { - logrus.SetOutput(ioutil.Discard) + logrus.SetOutput(io.Discard) } // set the Logger format diff --git a/legacy/builder/ctags/ctags_parser_test.go b/legacy/builder/ctags/ctags_parser_test.go index 13e10e8e79c..dca1d037ed4 100644 --- a/legacy/builder/ctags/ctags_parser_test.go +++ b/legacy/builder/ctags/ctags_parser_test.go @@ -11,12 +11,12 @@ // a commercial license. Buying such a license is mandatory if you want to // modify or otherwise use the software for commercial activities involving the // Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. +// To purchase a commercial license, send an email to license@arduino.c`c. package ctags import ( - "io/ioutil" + "os" "path/filepath" "testing" @@ -26,7 +26,7 @@ import ( ) func produceTags(t *testing.T, filename string) []*types.CTag { - bytes, err := ioutil.ReadFile(filepath.Join("test_data", filename)) + bytes, err := os.ReadFile(filepath.Join("test_data", filename)) require.NoError(t, err) parser := CTagsParser{} diff --git a/legacy/builder/ctags/ctags_to_prototypes_test.go b/legacy/builder/ctags/ctags_to_prototypes_test.go index 94a1ae93710..e73a1312069 100644 --- a/legacy/builder/ctags/ctags_to_prototypes_test.go +++ b/legacy/builder/ctags/ctags_to_prototypes_test.go @@ -16,7 +16,7 @@ package ctags import ( - "io/ioutil" + "os" "path/filepath" "testing" @@ -26,7 +26,7 @@ import ( ) func producePrototypes(t *testing.T, filename string, mainFile string) ([]*types.Prototype, int) { - bytes, err := ioutil.ReadFile(filepath.Join("test_data", filename)) + bytes, err := os.ReadFile(filepath.Join("test_data", filename)) require.NoError(t, err) parser := &CTagsParser{} diff --git a/legacy/builder/gohasissues/go_has_issues.go b/legacy/builder/gohasissues/go_has_issues.go index 5b2ca8f1ff8..316308de429 100644 --- a/legacy/builder/gohasissues/go_has_issues.go +++ b/legacy/builder/gohasissues/go_has_issues.go @@ -16,7 +16,7 @@ package gohasissues import ( - "io/ioutil" + "io/fs" "os" "path/filepath" "sort" @@ -84,18 +84,24 @@ func readDirNames(dirname string) ([]string, error) { } func ReadDir(dirname string) ([]os.FileInfo, error) { - infos, err := ioutil.ReadDir(dirname) + entries, err := os.ReadDir(dirname) if err != nil { return nil, err } - for idx, info := range infos { - info, err := resolveSymlink(dirname, info) + infos := make([]fs.FileInfo, 0, len(entries)) + for _, entry := range entries { + info, err := entry.Info() + if err != nil { + return nil, err + } + + info, err = resolveSymlink(dirname, info) if err != nil { // unresolvable symlinks should be skipped silently continue } - infos[idx] = info + infos = append(infos, info) } return infos, nil diff --git a/legacy/builder/utils/utils.go b/legacy/builder/utils/utils.go index c7ef7524ac0..a4885d6725c 100644 --- a/legacy/builder/utils/utils.go +++ b/legacy/builder/utils/utils.go @@ -21,7 +21,7 @@ import ( "encoding/hex" "fmt" "io" - "io/ioutil" + "io/fs" "os" "os/exec" "path/filepath" @@ -443,12 +443,21 @@ func CopyDir(src string, dst string, extensions []string) (err error) { return } - entries, err := ioutil.ReadDir(src) + entries, err := os.ReadDir(src) if err != nil { return } + infos := make([]fs.FileInfo, 0, len(entries)) for _, entry := range entries { + info, scopeErr := entry.Info() + if scopeErr != nil { + return + } + infos = append(infos, info) + } + + for _, entry := range infos { srcPath := filepath.Join(src, entry.Name()) dstPath := filepath.Join(dst, entry.Name()) From b82b13c1e1caf33dd0636bb8469b44af1351030f Mon Sep 17 00:00:00 2001 From: zachary-walters Date: Mon, 30 Jan 2023 07:43:22 -0600 Subject: [PATCH 2/4] Resolved the go fmt violation that causes the check go CI workflow run failure --- arduino/resources/helpers_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino/resources/helpers_test.go b/arduino/resources/helpers_test.go index 79f17548151..f8dea9abf43 100644 --- a/arduino/resources/helpers_test.go +++ b/arduino/resources/helpers_test.go @@ -16,9 +16,9 @@ package resources import ( - "os" "net/http" "net/http/httptest" + "os" "strings" "testing" From f176565d0b5763ee58ae49599197c9530f9203f4 Mon Sep 17 00:00:00 2001 From: zachary-walters Date: Tue, 31 Jan 2023 08:20:13 -0600 Subject: [PATCH 3/4] Removed the typo grave accent from ctags_parser_test --- legacy/builder/ctags/ctags_parser_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/legacy/builder/ctags/ctags_parser_test.go b/legacy/builder/ctags/ctags_parser_test.go index dca1d037ed4..d3bb03b23a0 100644 --- a/legacy/builder/ctags/ctags_parser_test.go +++ b/legacy/builder/ctags/ctags_parser_test.go @@ -11,7 +11,7 @@ // a commercial license. Buying such a license is mandatory if you want to // modify or otherwise use the software for commercial activities involving the // Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.c`c. +// To purchase a commercial license, send an email to license@arduino.cc. package ctags From 6715f3969c5dc3441469516fa4b0ff9f7f26abc4 Mon Sep 17 00:00:00 2001 From: zachary-walters Date: Tue, 31 Jan 2023 08:24:28 -0600 Subject: [PATCH 4/4] Removed the arbitrary fs.FileInfo array and its iterations in legacy/builder/utils/utils.go --- legacy/builder/utils/utils.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/legacy/builder/utils/utils.go b/legacy/builder/utils/utils.go index a4885d6725c..f2a13c1f254 100644 --- a/legacy/builder/utils/utils.go +++ b/legacy/builder/utils/utils.go @@ -21,7 +21,6 @@ import ( "encoding/hex" "fmt" "io" - "io/fs" "os" "os/exec" "path/filepath" @@ -448,16 +447,12 @@ func CopyDir(src string, dst string, extensions []string) (err error) { return } - infos := make([]fs.FileInfo, 0, len(entries)) - for _, entry := range entries { - info, scopeErr := entry.Info() + for _, dirEntry := range entries { + entry, scopeErr := dirEntry.Info() if scopeErr != nil { return } - infos = append(infos, info) - } - for _, entry := range infos { srcPath := filepath.Join(src, entry.Name()) dstPath := filepath.Join(dst, entry.Name())