diff --git a/experimental/golden_response_checker.go b/experimental/golden_response_checker.go index 320f40f3b..6f2fd7cbc 100644 --- a/experimental/golden_response_checker.go +++ b/experimental/golden_response_checker.go @@ -2,10 +2,13 @@ package experimental import ( "bufio" + // ignoring the G505 so that the checksum matches git hash + // nolint:gosec + "crypto/sha1" "encoding/base64" + "encoding/hex" "encoding/json" "fmt" - "io/ioutil" "log" "os" "path" @@ -160,7 +163,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" @@ -207,6 +210,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) @@ -220,13 +226,19 @@ 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) == 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", fpath) + // 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[:])) } return chunks[2], nil } @@ -239,5 +251,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 }