// Package golden compares test output against stored golden files, rewriting // them instead when the PLUMB_GOLDEN_UPDATE environment variable is set.
package golden import ( ) // updateEnv, when set to a non-empty value, switches the helpers from comparing // against golden files to rewriting them. const updateEnv = "PLUMB_GOLDEN_UPDATE" // updating reports whether golden files should be rewritten this run. func () bool { return os.Getenv(updateEnv) != "" } // Check compares got against the golden file at path and fails the test on a // mismatch. When updating() is true it rewrites the file instead (creating it if // absent); otherwise a missing file is fatal and names the env var to set. func ( *testing.T, , string) { .Helper() if updating() { if := writefile.Write(, ); != nil { .Fatalf("writing golden %s: %v", , ) } return } , := os.ReadFile() if != nil { .Fatalf("reading golden %s (run with %s=1 to create it): %v", , updateEnv, ) } if != string() { .Errorf("output differs from golden %s:\n--- got ---\n%s\n--- want ---\n%s", , , ) } }