Repository navigation
Expand file tree
/
Copy pathmakefile_test.go
More file actions
108 lines (105 loc) · 3.38 KB
/
Copy pathmakefile_test.go
File metadata and controls
108 lines (105 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package fdx
import (
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
)
/** TestMakefileProgramsMatchManPages checks that the Makefile's PROGRAMS line
* names every cmd/ directory and every section 1 man page source. The man page
* list is derived from PROGRAMS, so a program left out would drop its page from
* `make man` and `make install` while the build still succeeds.
*/
func TestMakefileProgramsMatchManPages(t *testing.T) {
mk, err := os.ReadFile("Makefile")
if err != nil {
t.Fatal(err)
}
m := regexp.MustCompile(`(?m)^PROGRAMS *= *(.*)$`).FindSubmatch(mk)
if m == nil {
t.Fatal("Makefile has no PROGRAMS line")
}
got := strings.Fields(string(m[1]))
var cmds, pages []string
dirs, _ := filepath.Glob("cmd/*")
for _, d := range dirs {
cmds = append(cmds, filepath.Base(d))
}
md, _ := filepath.Glob("*.1.md")
for _, f := range md {
pages = append(pages, strings.TrimSuffix(f, ".1.md"))
}
sort.Strings(got)
sort.Strings(cmds)
sort.Strings(pages)
if strings.Join(got, " ") != strings.Join(cmds, " ") {
t.Errorf("Makefile PROGRAMS = %v, cmd/ holds %v", got, cmds)
}
if strings.Join(got, " ") != strings.Join(pages, " ") {
t.Errorf("Makefile PROGRAMS = %v, *.1.md pages = %v", got, pages)
}
}
/** TestCheckCommitted runs `make check-committed` in a scratch repository with
* a bare origin: it passes when the tree is clean and HEAD is pushed, and fails
* for an uncommitted change to a tracked file and for an unpushed commit.
*/
func TestCheckCommitted(t *testing.T) {
for _, tool := range []string{"git", "make"} {
if _, err := exec.LookPath(tool); err != nil {
t.Skipf("%s not available", tool)
}
}
mk, err := filepath.Abs("Makefile")
if err != nil {
t.Fatal(err)
}
root := t.TempDir()
origin, work := filepath.Join(root, "origin.git"), filepath.Join(root, "work")
run := func(dir string, name string, args ...string) error {
c := exec.Command(name, args...)
c.Dir = dir
c.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@example.org",
"GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@example.org")
if b, err := c.CombinedOutput(); err != nil {
t.Logf("%s %v: %s", name, args, b)
return err
}
return nil
}
must := func(dir, name string, args ...string) {
t.Helper()
if err := run(dir, name, args...); err != nil {
t.Fatalf("%s %v: %v", name, args, err)
}
}
must(root, "git", "init", "-q", "--bare", origin)
must(root, "git", "init", "-q", "-b", "main", work)
must(work, "git", "remote", "add", "origin", origin)
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("one\n"), 0o644); err != nil {
t.Fatal(err)
}
must(work, "git", "add", "a.txt")
must(work, "git", "commit", "-q", "-m", "one")
must(work, "git", "push", "-q", "origin", "main")
check := func() error { return run(work, "make", "-s", "-f", mk, "check-committed") }
if err := check(); err != nil {
t.Errorf("clean and pushed: want pass, got %v", err)
}
if err := os.WriteFile(filepath.Join(work, "a.txt"), []byte("two\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := check(); err == nil {
t.Error("uncommitted change to a tracked file: want failure")
}
must(work, "git", "commit", "-q", "-am", "two")
if err := check(); err == nil {
t.Error("committed but unpushed: want failure")
}
must(work, "git", "push", "-q", "origin", "main")
if err := check(); err != nil {
t.Errorf("pushed again: want pass, got %v", err)
}
}