-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_interface_test.go
464 lines (428 loc) · 11.5 KB
/
disk_interface_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nin
import (
"errors"
"fmt"
"os"
"runtime"
"strings"
"testing"
)
func DiskInterfaceTest(t *testing.T) RealDiskInterface {
CreateTempDirAndEnter(t)
return RealDiskInterface{}
}
func Touch(path string) bool {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600)
if f != nil {
f.Close()
}
return err == nil
}
func TestDiskInterfaceTest_StatMissingFile(t *testing.T) {
disk := DiskInterfaceTest(t)
if mtime, err := disk.Stat("nosuchfile"); mtime != 0 || err != nil {
t.Fatal(mtime, err)
}
// On Windows, the errno for a file in a nonexistent directory
// is different.
if mtime, err := disk.Stat("nosuchdir/nosuchfile"); mtime != 0 || err != nil {
t.Fatal(mtime, err)
}
// On POSIX systems, the errno is different (ENOTDIR) if a component of the
// path prefix is not a directory.
if !Touch("notadir") {
t.Fatal(1)
}
if mtime, err := disk.Stat("notadir/nosuchfile"); mtime != 0 || err != nil {
t.Fatal(mtime, err)
}
}
func TestDiskInterfaceTest_StatBadPath(t *testing.T) {
disk := DiskInterfaceTest(t)
badPath := strings.Repeat("x", 512)
if runtime.GOOS == "windows" {
badPath = "cc:\\foo"
}
if mtime, err := disk.Stat(badPath); mtime != -1 || err == nil {
t.Fatal(mtime, err)
}
}
func TestDiskInterfaceTest_StatExistingFile(t *testing.T) {
disk := DiskInterfaceTest(t)
if !Touch("file") {
t.Fatal("failed")
}
if mtime, err := disk.Stat("file"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
}
func TestDiskInterfaceTest_StatExistingDir(t *testing.T) {
disk := DiskInterfaceTest(t)
if err := disk.MakeDir("subdir"); err != nil {
t.Fatal(err)
}
if err := disk.MakeDir("subdir/subsubdir"); err != nil {
t.Fatal(err)
}
if mtime, err := disk.Stat(".."); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("."); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("subdir"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("subdir/subsubdir"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
mtime1, err1 := disk.Stat("subdir")
mtime2, err2 := disk.Stat("subdir/.")
if mtime1 != mtime2 || err1 != err2 {
t.Fatal(mtime1, err1, mtime2, err2)
}
mtime3, err3 := disk.Stat("subdir/subsubdir/..")
if mtime1 != mtime3 || err1 != err3 {
t.Fatal(mtime1, err1, mtime3, err3)
}
mtime4, err4 := disk.Stat("subdir/subsubdir")
mtime5, err5 := disk.Stat("subdir/subsubdir/.")
if mtime4 != mtime5 || err4 != err5 {
t.Fatal(mtime4, err4, mtime5, err5)
}
}
func TestDiskInterfaceTest_StatCache(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("windows-only test")
}
t.Skip("TODO")
disk := DiskInterfaceTest(t)
if !Touch("file1") {
t.Fatal("expected true")
}
if !Touch("fiLE2") {
t.Fatal("expected true")
}
if err := disk.MakeDir("subdir"); err != nil {
t.Fatal(err)
}
if err := disk.MakeDir("subdir/subsubdir"); err != nil {
t.Fatal(err)
}
if !Touch("subdir\\subfile1") {
t.Fatal("expected true")
}
if !Touch("subdir\\SUBFILE2") {
t.Fatal("expected true")
}
if !Touch("subdir\\SUBFILE3") {
t.Fatal("expected true")
}
disk.AllowStatCache(false)
parentStatUncached, erru := disk.Stat("..")
if erru != nil {
t.Fatal(erru)
}
disk.AllowStatCache(true)
if mtime, err := disk.Stat("FIle1"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("file1"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("subdir/subfile2"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("sUbdir\\suBFile1"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat(".."); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("."); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("subdir"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("subdir/subsubdir"); mtime <= 0 || err != nil {
t.Fatal(mtime, err)
}
mtime1, err1 := disk.Stat("subdir")
mtime2, err2 := disk.Stat("subdir/.")
if mtime1 != mtime2 || err1 != err2 {
t.Fatal(mtime1, err1, mtime2, err2)
}
mtime3, err3 := disk.Stat("subdir/subsubdir/..")
if mtime1 != mtime3 || err1 != err3 {
t.Fatal(mtime1, err1, mtime3, err3)
}
if mtime, err := disk.Stat(".."); mtime != parentStatUncached || err != nil {
t.Fatal(mtime, err)
}
mtime4, err4 := disk.Stat("subdir/subsubdir")
mtime5, err5 := disk.Stat("subdir/subsubdir/.")
if mtime4 != mtime5 || err4 != err5 {
t.Fatal(mtime4, err4, mtime5, err5)
}
// Test error cases.
badPath := "cc:\\foo"
if mtime, err := disk.Stat(badPath); mtime != -1 || err == nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat(badPath); mtime != -1 || err == nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("nosuchfile"); mtime != 0 || err != nil {
t.Fatal(mtime, err)
}
if mtime, err := disk.Stat("nosuchdir/nosuchfile"); mtime != 0 || err != nil {
t.Fatal(mtime, err)
}
}
func TestDiskInterfaceTest_ReadFile(t *testing.T) {
disk := DiskInterfaceTest(t)
if content, err := disk.ReadFile("foobar"); content != nil || !os.IsNotExist(err) {
t.Fatal(content, err)
}
testFile := "testfile"
f, err := os.OpenFile(testFile, os.O_CREATE|os.O_RDWR, 0o600)
if f == nil || err != nil {
t.Fatal(err)
}
testContent := "test content\nok"
fmt.Fprintf(f, "%s", testContent)
if nil != f.Close() {
t.Fatal("expected equal")
}
if content, err := disk.ReadFile(testFile); string(content) != testContent+"\x00" || err != nil {
t.Fatal(content, err)
}
}
func TestDiskInterfaceTest_MakeDirs(t *testing.T) {
disk := DiskInterfaceTest(t)
path := "path/with/double//slash/"
if err := MakeDirs(&disk, path); err != nil {
t.Fatal(err)
}
f, _ := os.OpenFile(path+"a_file", os.O_CREATE|os.O_RDWR, 0o600)
if f == nil {
t.Fatal("expected true")
}
if nil != f.Close() {
t.Fatal("expected equal")
}
path2 := "another\\with\\back\\\\slashes\\"
if err := MakeDirs(&disk, path2); err != nil {
t.Fatal(err)
}
f2, _ := os.OpenFile(path2+"a_file", os.O_CREATE|os.O_RDWR, 0o600)
if f2 == nil {
t.Fatal("expected true")
}
if err := f2.Close(); err != nil {
t.Fatal("expected equal")
}
}
func TestDiskInterfaceTest_RemoveFile(t *testing.T) {
// The Go os.Remove() function does much more than C++'s version, so we
// cannot disambiguate between file and directory removal.
disk := DiskInterfaceTest(t)
kFileName := "file-to-remove"
if !Touch(kFileName) {
t.Fatal("expected true")
}
if err := disk.RemoveFile(kFileName); err != nil {
t.Fatal(err)
}
if err := disk.RemoveFile(kFileName); err == nil || !os.IsNotExist(err) {
t.Fatal(err)
}
if err := disk.RemoveFile("does not exist"); err == nil || !os.IsNotExist(err) {
t.Fatal(err)
}
if !Touch(kFileName) {
t.Fatal("expected true")
}
// Make it read-only.
if err := os.Chmod(kFileName, 0o400); err != nil {
t.Fatal(err)
}
if err := disk.RemoveFile(kFileName); err != nil {
t.Fatal(err)
}
if err := disk.RemoveFile(kFileName); err == nil || !os.IsNotExist(err) {
t.Fatal(err)
}
}
func TestDiskInterfaceTest_RemoveDirectory(t *testing.T) {
disk := DiskInterfaceTest(t)
kDirectoryName := "directory-to-remove"
if err := disk.MakeDir(kDirectoryName); err != nil {
t.Fatal(err)
}
if err := disk.RemoveFile(kDirectoryName); err != nil {
t.Fatal(err)
}
if err := disk.RemoveFile(kDirectoryName); err == nil || !os.IsNotExist(err) {
t.Fatal(err)
}
if err := disk.RemoveFile("does not exist"); err == nil || !os.IsNotExist(err) {
t.Fatal(err)
}
}
type StatTest struct {
StateTestWithBuiltinRules
scan DependencyScan
mtimes map[string]TimeStamp
stats []string
}
func (s *StatTest) WriteFile(path string, contents string) error {
s.t.Fatal("Unexpected function call")
return errors.New("not implemented")
}
func (s *StatTest) MakeDir(path string) error {
s.t.Fatal("Unexpected function call")
return errors.New("not implemented")
}
func (s *StatTest) ReadFile(path string) ([]byte, error) {
s.t.Fatal("Unexpected function call")
return nil, errors.New("not implemented")
}
func (s *StatTest) RemoveFile(path string) error {
s.t.Fatal("Unexpected function call")
return errors.New("not implemented")
}
// DiskInterface implementation.
func (s *StatTest) Stat(path string) (TimeStamp, error) {
s.stats = append(s.stats, path)
return s.mtimes[path], nil
}
func NewStatTest(t *testing.T) *StatTest {
s := &StatTest{
StateTestWithBuiltinRules: NewStateTestWithBuiltinRules(t),
mtimes: map[string]TimeStamp{},
}
s.scan = NewDependencyScan(&s.state, nil, nil, s)
return s
}
func TestStatTest_Simple(t *testing.T) {
s := NewStatTest(t)
s.AssertParse(&s.state, "build out: cat in\n", ParseManifestOpts{})
out := s.GetNode("out")
if err := out.Stat(s); err != nil {
t.Fatal(err)
}
if 1 != len(s.stats) {
t.Fatal("expected equal")
}
if _, err := s.scan.RecomputeDirty(out); err != nil {
t.Fatal(err)
}
if 2 != len(s.stats) {
t.Fatal("expected equal")
}
if "out" != s.stats[0] {
t.Fatal("expected equal")
}
if "in" != s.stats[1] {
t.Fatal("expected equal")
}
}
func TestStatTest_TwoStep(t *testing.T) {
s := NewStatTest(t)
s.AssertParse(&s.state, "build out: cat mid\nbuild mid: cat in\n", ParseManifestOpts{})
out := s.GetNode("out")
if err := out.Stat(s); err != nil {
t.Fatal(err)
}
if 1 != len(s.stats) {
t.Fatal("expected equal")
}
if _, err := s.scan.RecomputeDirty(out); err != nil {
t.Fatal(err)
}
if 3 != len(s.stats) {
t.Fatal("expected equal")
}
if "out" != s.stats[0] {
t.Fatal("expected equal")
}
if !s.GetNode("out").Dirty {
t.Fatal("expected true")
}
if "mid" != s.stats[1] {
t.Fatal("expected equal")
}
if !s.GetNode("mid").Dirty {
t.Fatal("expected true")
}
if "in" != s.stats[2] {
t.Fatal("expected equal")
}
}
func TestStatTest_Tree(t *testing.T) {
s := NewStatTest(t)
s.AssertParse(&s.state, "build out: cat mid1 mid2\nbuild mid1: cat in11 in12\nbuild mid2: cat in21 in22\n", ParseManifestOpts{})
out := s.GetNode("out")
if err := out.Stat(s); err != nil {
t.Fatal(err)
}
if 1 != len(s.stats) {
t.Fatal("expected equal")
}
if _, err := s.scan.RecomputeDirty(out); err != nil {
t.Fatal(err)
}
if 1+6 != len(s.stats) {
t.Fatal("expected equal")
}
if "mid1" != s.stats[1] {
t.Fatal("expected equal")
}
if !s.GetNode("mid1").Dirty {
t.Fatal("expected true")
}
if "in11" != s.stats[2] {
t.Fatal("expected equal")
}
}
func TestStatTest_Middle(t *testing.T) {
s := NewStatTest(t)
s.AssertParse(&s.state, "build out: cat mid\nbuild mid: cat in\n", ParseManifestOpts{})
s.mtimes["in"] = 1
s.mtimes["mid"] = 0 // missing
s.mtimes["out"] = 1
out := s.GetNode("out")
if err := out.Stat(s); err != nil {
t.Fatal(err)
}
if 1 != len(s.stats) {
t.Fatal("expected equal")
}
if _, err := s.scan.RecomputeDirty(out); err != nil {
t.Fatal(err)
}
if s.GetNode("in").Dirty {
t.Fatal("expected false")
}
if !s.GetNode("mid").Dirty {
t.Fatal("expected true")
}
if !s.GetNode("out").Dirty {
t.Fatal("expected true")
}
}