-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_test.go
147 lines (123 loc) · 2.83 KB
/
compile_test.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package fab
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/otiai10/copy"
)
func TestCompile(t *testing.T) {
t.Parallel()
tbCompile(t, func(tmpdir string) {
var (
fabdir = filepath.Join(tmpdir, "fab")
compiledir = filepath.Join(tmpdir, "compile")
pkgdir = filepath.Join(compiledir, "_fab")
rulesgo = filepath.Join(pkgdir, "rules.go")
)
m := Main{
Fabdir: fabdir,
Verbose: testing.Verbose(),
Topdir: compiledir,
}
ctx := context.Background()
ctx = WithVerbose(ctx, true)
driver, err := m.getDriver(ctx, true)
if err != nil {
t.Fatal(err)
}
info, err := os.Stat(driver)
if err != nil {
t.Fatal(err)
}
modtime := info.ModTime()
cmd := exec.CommandContext(ctx, driver, "Noop")
cmd.Dir = tmpdir
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err = cmd.Run(); err != nil {
t.Fatal(err)
}
// If the driver is (wrongly) recompiled here,
// this sleep forces the modtime to be different
// (on systems where file-modtime granularity
// is no better than one second).
time.Sleep(time.Second)
driver, err = m.getDriver(ctx, true)
if err != nil {
t.Fatal(err)
}
info, err = os.Stat(driver)
if err != nil {
t.Fatal(err)
}
if !modtime.Equal(info.ModTime()) {
t.Error("driver got rebuilt unexpectedly")
}
f, err := os.OpenFile(rulesgo, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
t.Fatal(err)
}
defer f.Close()
fmt.Fprintln(f, "// comment")
if err = f.Close(); err != nil {
t.Fatal(err)
}
driver, err = m.getDriver(ctx, true)
if err != nil {
t.Fatal(err)
}
info, err = os.Stat(driver)
if err != nil {
t.Fatal(err)
}
if modtime.Equal(info.ModTime()) {
t.Error("driver did not get rebuilt but should have")
}
cmd = exec.CommandContext(ctx, driver, "Noop")
cmd.Dir = tmpdir
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
if err = cmd.Run(); err != nil {
t.Fatal(err)
}
})
}
func BenchmarkCompile(b *testing.B) {
tbCompile(b, func(tmpdir string) {
pkgdir := filepath.Join(tmpdir, "compile/_fab")
tmpfile, err := os.CreateTemp("", "fab")
if err != nil {
b.Fatal(err)
}
tmpname := tmpfile.Name()
defer os.Remove(tmpname)
if err = tmpfile.Close(); err != nil {
b.Fatal(err)
}
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err = Compile(ctx, pkgdir, tmpname); err != nil {
b.Fatal(err)
}
}
})
}
// Test or benchmark the compiler.
func tbCompile(tb testing.TB, f func(tmpdir string)) {
tmpdir, err := os.MkdirTemp("", "fab")
if err != nil {
tb.Fatal(err)
}
defer os.RemoveAll(tmpdir)
if err = populateFabDir(tmpdir); err != nil {
tb.Fatal(err)
}
compiledir := filepath.Join(tmpdir, "compile")
if err = copy.Copy("_testdata/compile", compiledir); err != nil {
tb.Fatal(err)
}
f(tmpdir)
}