internal/scan: add -update to print tests

Change-Id: Ib267ee9ca3211f178a233449dd00552e5dd5feed
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/496164
Auto-Submit: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Julie Qiu <julieqiu@google.com>
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/internal/scan/print_test.go b/internal/scan/print_test.go
index 0b8722f..3b352fa 100644
--- a/internal/scan/print_test.go
+++ b/internal/scan/print_test.go
@@ -5,8 +5,10 @@
 
 import (
 	"bytes"
+	"flag"
 	"io/fs"
 	"os"
+	"path/filepath"
 	"strings"
 	"testing"
 
@@ -15,6 +17,8 @@
 	"golang.org/x/vuln/internal/scan"
 )
 
+var update = flag.Bool("update", false, "update test files with results")
+
 func TestPrinting(t *testing.T) {
 	testdata := os.DirFS("testdata")
 	inputs, err := fs.Glob(testdata, "*.json")
@@ -32,11 +36,16 @@
 			textname := strings.TrimSuffix(textfile, ".txt")
 			t.Run(textname, func(t *testing.T) {
 				wantText, _ := fs.ReadFile(testdata, textfile)
-				got := &strings.Builder{}
+				got := &bytes.Buffer{}
 				handler := scan.NewTextHandler(got)
 				handler.Show = strings.Split(textname, "_")[1:]
 				testRunHandler(t, rawJSON, handler)
 				if diff := cmp.Diff(string(wantText), got.String()); diff != "" {
+					if *update {
+						// write the output back to the file
+						os.WriteFile(filepath.Join("testdata", textfile), got.Bytes(), 0644)
+						return
+					}
 					t.Errorf("Readable mismatch (-want, +got):\n%s", diff)
 				}
 			})