1
0
mirror of synced 2026-02-13 13:06:56 +00:00

Add test flag -update to update the golden files

Test flag `-update` can be used to update all the golden files whenever
the CLI output changes.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
Sunny
2022-02-24 04:26:18 +05:30
parent 21f0d5d82c
commit edaf6ca522
7 changed files with 27 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ import (
"bufio"
"bytes"
"context"
"flag"
"fmt"
"io"
"os"
@@ -42,6 +43,9 @@ import (
var nextNamespaceId int64
// update allows golden files to be updated based on the current output.
var update = flag.Bool("update", false, "update golden files")
// Return a unique namespace with the specified prefix, for tests to create
// objects that won't collide with each other.
func allocateNamespace(prefix string) string {
@@ -298,6 +302,13 @@ func assertGoldenTemplateFile(goldenFile string, templateValues map[string]strin
expectedOutput = string(goldenFileContents)
}
if assertErr := assertGoldenValue(expectedOutput)(output, err); assertErr != nil {
// Update the golden files if comparision fails and the update flag is set.
if *update && output != "" {
if err := os.WriteFile(goldenFile, []byte(output), 0644); err != nil {
return fmt.Errorf("failed to update golden file '%s': %v", goldenFile, err)
}
return nil
}
return fmt.Errorf("Mismatch from golden file '%s': %v", goldenFile, assertErr)
}
return nil