From 20c43e1065b403af7e88d368ebc72c5ce9c50b00 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 29 Nov 2022 10:24:54 -0800 Subject: [PATCH 1/5] more error logging --- experimental/golden_response_checker.go | 12 +++++++----- internal/standalone/standalone.go | 5 ++--- internal/utils.go | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/experimental/golden_response_checker.go b/experimental/golden_response_checker.go index 320f40f3b..2bd2561c7 100644 --- a/experimental/golden_response_checker.go +++ b/experimental/golden_response_checker.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" "log" "os" "path" @@ -160,7 +159,7 @@ func writeGoldenFile(path string, dr *backend.DataResponse) error { } str += "\n" - return ioutil.WriteFile(path, []byte(str), 0600) + return os.WriteFile(path, []byte(str), 0600) } const machineStr = "🌟 This was machine generated. Do not edit. 🌟\n" @@ -220,13 +219,16 @@ func CheckGoldenJSONResponse(t *testing.T, dir string, name string, dr *backend. } func readGoldenJSONFile(fpath string) (string, error) { - raw, err := ioutil.ReadFile(fpath) + raw, err := os.ReadFile(fpath) if err != nil { return "", err } + if len(raw) < 3 { + return "", fmt.Errorf("empty file found: %s", fpath) + } chunks := strings.Split(string(raw), "// "+machineStr) if len(chunks) < 3 { - return "", fmt.Errorf("no golden data found in: %s", fpath) + return "", fmt.Errorf("no golden data found in: %s (%d bytes)", fpath, len(raw)) } return chunks[2], nil } @@ -239,5 +241,5 @@ func writeGoldenJSONFile(fpath string, dr *backend.DataResponse) error { return err } str += string(raw) - return ioutil.WriteFile(fpath, []byte(str), 0600) + return os.WriteFile(fpath, []byte(str), 0600) } diff --git a/internal/standalone/standalone.go b/internal/standalone/standalone.go index a662d13d8..1f169429c 100644 --- a/internal/standalone/standalone.go +++ b/internal/standalone/standalone.go @@ -3,7 +3,6 @@ package standalone import ( "flag" "fmt" - "io/ioutil" "log" "net" "os" @@ -62,7 +61,7 @@ func GetInfo(id string) (Args, error) { } // Check the local file for address - addrBytes, err := ioutil.ReadFile(filePath) + addrBytes, err := os.ReadFile(filePath) if address == "" { if err == nil && len(addrBytes) > 0 { address = string(addrBytes) @@ -75,7 +74,7 @@ func GetInfo(id string) (Args, error) { if info.Address == "" { return info, fmt.Errorf("standalone address must be specified") } - _ = ioutil.WriteFile(filePath, []byte(info.Address), 0600) + _ = os.WriteFile(filePath, []byte(info.Address), 0600) // sadly vs-code can not listen to shutdown events // https://github.com/golang/vscode-go/issues/120 diff --git a/internal/utils.go b/internal/utils.go index c0c32c8b4..d2e12a2d4 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -3,13 +3,13 @@ package internal import ( "encoding/json" "fmt" - "io/ioutil" + "os" "path" "strings" ) func GetStringValueFromJSON(fpath string, key string) (string, error) { - byteValue, err := ioutil.ReadFile(fpath) + byteValue, err := os.ReadFile(fpath) if err != nil { return "", err } From 82823a87bcf9b2db52ab249ee51548bdfed926ee Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 29 Nov 2022 17:21:02 -0800 Subject: [PATCH 2/5] include hash --- experimental/golden_response_checker.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/experimental/golden_response_checker.go b/experimental/golden_response_checker.go index 2bd2561c7..b647f28fc 100644 --- a/experimental/golden_response_checker.go +++ b/experimental/golden_response_checker.go @@ -2,7 +2,9 @@ package experimental import ( "bufio" + "crypto/sha1" "encoding/base64" + "encoding/hex" "encoding/json" "fmt" "log" @@ -223,12 +225,13 @@ func readGoldenJSONFile(fpath string) (string, error) { if err != nil { return "", err } - if len(raw) < 3 { + if len(raw) == 0 { return "", fmt.Errorf("empty file found: %s", fpath) } chunks := strings.Split(string(raw), "// "+machineStr) if len(chunks) < 3 { - return "", fmt.Errorf("no golden data found in: %s (%d bytes)", fpath, len(raw)) + hash := sha1.Sum(raw) + return "", fmt.Errorf("no golden data found in: %s (%d bytes, sha1: %s)", fpath, len(raw), hex.EncodeToString(hash[:])) } return chunks[2], nil } From ee92a189a1ddc62a260a8005a20f39980a72a078 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 29 Nov 2022 18:24:53 -0800 Subject: [PATCH 3/5] avoid second error --- experimental/golden_response_checker.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/experimental/golden_response_checker.go b/experimental/golden_response_checker.go index b647f28fc..115c3e2fc 100644 --- a/experimental/golden_response_checker.go +++ b/experimental/golden_response_checker.go @@ -208,6 +208,9 @@ func CheckGoldenJSONResponse(t *testing.T, dir string, name string, dr *backend. expected, err := readGoldenJSONFile(fpath) assert.NoError(t, err) + if err != nil { + return + } actual, err := json.Marshal(dr) assert.NoError(t, err) From 27b2e22f8119c5e216e9190a509f79087a043938 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 29 Nov 2022 18:44:39 -0800 Subject: [PATCH 4/5] lint fix --- experimental/golden_response_checker.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/golden_response_checker.go b/experimental/golden_response_checker.go index 115c3e2fc..fbf3ac38a 100644 --- a/experimental/golden_response_checker.go +++ b/experimental/golden_response_checker.go @@ -233,6 +233,8 @@ func readGoldenJSONFile(fpath string) (string, error) { } chunks := strings.Split(string(raw), "// "+machineStr) if len(chunks) < 3 { + // ignoring the G401 so that the checksum matches git hash + // nolint:gosec hash := sha1.Sum(raw) return "", fmt.Errorf("no golden data found in: %s (%d bytes, sha1: %s)", fpath, len(raw), hex.EncodeToString(hash[:])) } From 36810b6543a55b09ed223fbf8c433cb4de763871 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 29 Nov 2022 18:45:18 -0800 Subject: [PATCH 5/5] lint fix --- experimental/golden_response_checker.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/experimental/golden_response_checker.go b/experimental/golden_response_checker.go index fbf3ac38a..6f2fd7cbc 100644 --- a/experimental/golden_response_checker.go +++ b/experimental/golden_response_checker.go @@ -2,6 +2,8 @@ package experimental import ( "bufio" + // ignoring the G505 so that the checksum matches git hash + // nolint:gosec "crypto/sha1" "encoding/base64" "encoding/hex"