Debian Codegraph @

watch_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package godo
2
3 import (
4 "strings"
5 "testing"
6 "time"
7
•8 "github.com/stretchr/testify/assert"
9 )
10
11 /*
12 NOTE: Watching tests
13
14 // touch initial modtime of files
15 touch("tmp/foo.txt", 0)
16
17 // start project
18 func tasks(p *Project) {
19 // ...
20 }
21 execClI(tasks, ...)
22
23 // give the project time to initialize
24 <- time.After(testProjectDelay)
25
26 // use touch to change files. Modtime has to be after the initial
27 // modtime, so the watcher can pick up the change. To make one file
28 // newer than the other:
29 touch("tmp/older.txt", 1*time.Second)
30 touch("tmp/newer.txt", 2*time.Second)
31
32 // wait for at least the watch delay, which is the interval
33 // the watchers polls the file system
34 <- time.After(testWatchDelay)
35
36 // finally, do assertions
37 */
38
39 // default [templates:*go.html compile]
40 //
41 // if any go.html changes then run "templates", "compile", "default"
42 func TestWatchTasksWithoutSrcShouldAlwaysRun(t *testing.T) {
43 trace := ""
44 pass := 1
45 tasks := func(p *Project) {
46 p.Task("A", nil, func(*Context) {
47 trace += "A"
48 })
49 p.Task("B", nil, func(*Context) {
50 trace += "B"
51 })
52 p.Task("C", nil, func(*Context) {
53 trace += "C"
54 }).Src("test/sub/foo.txt")
55
56 p.Task("default", S{"A", "B", "C"}, func(*Context) {
57 // on watch, the task is run once before watching
58 if pass == 2 {
59 p.Exit(0)
60 }
61 pass++
62 })
63 }
64
65 go func() {
66 execCLI(tasks, []string{"-w"}, nil)
67 }()
68
69 <-time.After(testProjectDelay)
70
71 touch("test/sub/foo.txt", 0)
72
73 <-time.After(testWatchDelay)
74
75 assert.Equal(t, "ABCABC", trace)
76 }
77
78 // default [templates:*go.html styles:*.scss compile ]
79 //
80 // if any go.html changes then run "templates", "compile", "default".
81 // styles is not run since no changes to SCSS files occurred.
82 func TestWatchWithSrc(t *testing.T) {
83 trace := ""
84 pass := 1
85 tasks := func(p *Project) {
86 p.Task("compile", nil, func(*Context) {
87 trace += "C"
88 })
89
90 p.Task("styles", nil, func(*Context) {
91 trace += "S"
92 }).Src("test/styles/*scss")
93
94 p.Task("templates", nil, func(*Context) {
95 trace += "T"
96 }).Src("test/templates/*go.html")
97
98 p.Task("default", S{"templates", "styles", "compile"}, func(*Context) {
99 // on watch, the task is run once before watching
100 if pass == 2 {
101 p.Exit(0)
102 }
103 pass++
104 })
105 }
106
107 go func() {
108 execCLI(tasks, []string{"-w"}, nil)
109 }()
110
111 <-time.After(testProjectDelay)
112
113 touch("test/templates/1.go.html", 100*time.Millisecond)
114
115 <-time.After(testWatchDelay)
116
117 assert.Equal(t, "TSCTC", trace)
118 }
119
120 func TestWatchShouldWatchNamespaceTasks(t *testing.T) {
121 done := make(chan bool)
122 trace := ""
123 pass := 0
124
125 dbTasks := func(p *Project) {
126 p.Task("default", S{"models"}, func(*Context) {
127 trace += "D"
128 pass++
129 if pass == 2 {
130 p.Exit(0)
131 }
132 })
133 p.Task("models", nil, func(*Context) {
134 trace += "M"
135 }).Src("test/sub/*.txt")
136 }
137
138 tasks := func(p *Project) {
139 p.Use("db", dbTasks)
140 }
141
142 go func() {
143 execCLI(tasks, []string{"db:default", "-w"}, func(code int) {
144 done <- true
145 })
146 }()
147
148 touchTil("test/sub/sub1.txt", 200*time.Millisecond, done)
149 assert.Equal(t, "MDMD", trace)
150
151 // test non-watch
152 trace = ""
153 runTask(tasks, "db:default")
154 assert.Equal(t, "MD", trace)
155 }
156
157 func TestWatch(t *testing.T) {
158 done := make(chan bool)
159 ran := 0
160 tasks := func(p *Project) {
161 // this should run twice, watch always runs all tasks first then
162 // the touch below
163 p.Task("txt", nil, func(*Context) {
164 ran++
165 if ran == 2 {
166 p.Exit(0)
167 }
168 }).Src("test/*.txt")
169 }
170
171 status := -1
172 go func() {
173 argv := []string{"txt", "-w"}
174 execCLI(tasks, argv, func(code int) {
175 status = code
176 done <- true
177 })
178 }()
179
180 touchTil("test/bar.txt", 100*time.Millisecond, done)
181 assert.Equal(t, 2, ran)
182 }
183
184 func TestOutdatedNoDest(t *testing.T) {
185 done := make(chan bool)
186 ran := ""
187 // each task will run once before watch is called
188 tasks := func(p *Project) {
189 p.Task("txt", nil, func(*Context) {
190 ran += "T"
191 }).
192 Src("test/*.txt").
193 Dest("tmp/*.foo2")
194
195 p.Task("parent", S{"txt"}, func(*Context) {
196 ran += "P"
197 if strings.Count(ran, "P") == 2 {
198 p.Exit(0)
199 }
200 }).Src("test/sub/*.txt")
201 }
202
203 status := -1
204 go func() {
205 argv := []string{"parent", "-w"}
206 execCLI(tasks, argv, func(code int) {
207 status = code
208 done <- true
209 })
210 }()
211
212 touchTil("test/sub/sub1.txt", 100*time.Millisecond, done)
213 assert.Equal(t, "TPP", ran)
214 }
215
216 func TestOutdated(t *testing.T) {
217 // force txt to be newer than foo, which should run txt
218 touch("tmp/sub/1.foo", -1*time.Second)
219 touch("tmp/sub/foo.txt", 0)
220
221 ran := ""
222 var project *Project
223 tasks := func(p *Project) {
224 project = p
225
226 p.Task("txt", nil, func(*Context) {
227 ran += "T"
228 }).
229 Src("tmp/sub/*.txt").
230 Dest("tmp/sub/*.foo")
231
232 p.Task("parent", S{"txt"}, func(*Context) {
233 ran += "P"
234 }).Src("tmp/*.txt")
235 }
236
237 go func() {
238 argv := []string{"parent", "-w"}
239 execCLI(tasks, argv, func(code int) {
240 assert.Fail(t, "should not have exited")
241 })
242 }()
243
244 // give the task enough time to setup watches
245 <-time.After(testProjectDelay)
246
247 // force txt to have older modtime than foo which should not run "text but run "parent"
248 touch("tmp/1.foo", 3*time.Second) // is not watched
249 touch("tmp/foo.txt", 1*time.Second) // txt is watched
250
251 // wait at least watchDelay which is the interval for updating watches
252 <-time.After(testWatchDelay)
253
254 assert.Equal(t, "TPP", ran)
255 }