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

Use sebdah/goldie for Golden File Testing #19

Closed
tri-adam opened this issue Jun 12, 2021 · 0 comments · Fixed by #20
Closed

Use sebdah/goldie for Golden File Testing #19

tri-adam opened this issue Jun 12, 2021 · 0 comments · Fixed by #20
Labels
enhancement New feature or request

Comments

@tri-adam
Copy link
Member

Use sebdah/goldie for golden file testing, to replace custom code that does the same thing:

// goldenPath returns the path of the golden file corresponding to name.
func goldenPath(name string) string {
// Replace test name separator with OS-specific path separator.
name = path.Join(strings.Split(name, "/")...)
return path.Join("testdata", name) + ".golden"
}
// updateGolden writes b to a golden file associated with name.
func updateGolden(name string, b []byte) error {
p := goldenPath(name)
if err := os.MkdirAll(path.Dir(p), 0755); err != nil {
return err
}
return ioutil.WriteFile(p, b, 0644) // nolint:gosec
}
// verifyGolden compares b to the contents golden file associated with name.
func verifyGolden(name string, r io.Reader) error {
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if *update {
if err := updateGolden(name, b); err != nil {
return err
}
}
g, err := ioutil.ReadFile(goldenPath(name))
if err != nil {
return err
}
if !bytes.Equal(b, g) {
return errors.New("output does not match golden file")
}
return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant