Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: Add more error logging to golden checker #571

Merged
merged 5 commits into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions experimental/golden_response_checker.go
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm probably missing something obvious, but why wouldn't this be if len(raw) == 0 {?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i changed it to == 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)
return "", fmt.Errorf("no golden data found in: %s (%d bytes)", fpath, len(raw))
}
return chunks[2], nil
}
Expand All @@ -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)
}
5 changes: 2 additions & 3 deletions internal/standalone/standalone.go
Expand Up @@ -3,7 +3,6 @@ package standalone
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions internal/utils.go
Expand Up @@ -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
}
Expand Down