gunk-dev/the-valley

walk_test.go

sourcee2dd787b4f4fmain's published tip history
1.9 KiB
1package main
2
3import (
4 "os"
5 "path/filepath"
6 "slices"
7 "testing"
8)
9
10// The configuration a step borrows goes after the entries the caller's
11// environment gives git already, which are kept, and the count covers
12// both.
13func TestWithConfigAppendsToTheCallersEntries(t *testing.T) {
14 env := []string{"HOME=/home/operator", "GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=core.pager", "GIT_CONFIG_VALUE_0=cat"}
15 got := withConfig(env, [][2]string{{"valley.principal", "alice"}, {"user.name", "alice"}})
16 want := []string{"HOME=/home/operator", "GIT_CONFIG_KEY_0=core.pager", "GIT_CONFIG_VALUE_0=cat",
17 "GIT_CONFIG_KEY_1=valley.principal", "GIT_CONFIG_VALUE_1=alice",
18 "GIT_CONFIG_KEY_2=user.name", "GIT_CONFIG_VALUE_2=alice", "GIT_CONFIG_COUNT=3"}
19 if !slices.Equal(got, want) {
20 t.Fatalf("withConfig gave\n%q\nwant\n%q", got, want)
21 }
22 if got := withConfig(env, nil); !slices.Equal(got, env) {
23 t.Fatalf("withConfig with nothing to borrow changed the environment: %q", got)
24 }
25}
26
27// An outcome file that is missing, names no outcome, or gives an outcome
28// the wrong fields is no outcome, so the walk counts the step an error.
29func TestOutcomeFileReadsOnlyAKnownOutcome(t *testing.T) {
30 dir := t.TempDir()
31 for _, c := range []struct {
32 name, content string
33 ok bool
34 }{
35 {"approved", "approved\tkmkmid\tkmkm\t2\t0123abcd\n", true},
36 {"skipped", "skipped\n", true},
37 {"not-reviewed", "not-reviewed\tit is a draft\n", true},
38 {"unknown", "landed\n", false},
39 {"short", "approved\tkmkmid\tkmkm\n", false},
40 {"empty", "", false},
41 } {
42 path := filepath.Join(dir, c.name)
43 if err := os.WriteFile(path, []byte(c.content), 0o644); err != nil {
44 t.Fatal(err)
45 }
46 if _, ok := readOutcome(path); ok != c.ok {
47 t.Errorf("%s: read as an outcome: %v, want %v", c.name, ok, c.ok)
48 }
49 }
50 if _, ok := readOutcome(filepath.Join(dir, "missing")); ok {
51 t.Error("a missing outcome file read as an outcome")
52 }
53}