internal/control/plain_test.go

v1.43.0
gitbay/internal/control/plain_test.go history · blame · raw

38 lines · 1064 bytes

 1package control
 2
 3import (
 4	"flag"
 5	"os"
 6	"path/filepath"
 7	"regexp"
 8	"testing"
 9)
10
11var updatePlain = flag.Bool("update-plain", false, "rewrite testdata/plain from the current output")
12
13var plainStamp = regexp.MustCompile(`\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z| UTC)?`)
14
15// pinPlain compares a command's piped output with the copy taken before
16// its terminal screen was rewritten. Timestamps are masked, since
17// fixtures are created at test time.
18func pinPlain(t *testing.T, name, got string) {
19	t.Helper()
20	got = plainStamp.ReplaceAllString(got, "<time>")
21	path := filepath.Join("testdata", "plain", name+".txt")
22	if *updatePlain {
23		if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
24			t.Fatal(err)
25		}
26		if err := os.WriteFile(path, []byte(got), 0o644); err != nil {
27			t.Fatal(err)
28		}
29		return
30	}
31	want, err := os.ReadFile(path)
32	if err != nil {
33		t.Fatalf("%v: capture it with -update-plain before changing the command", err)
34	}
35	if got != string(want) {
36		t.Errorf("piped output changed:\n--- want\n%s--- got\n%s", want, got)
37	}
38}