5
5
package fsnotify
6
6
7
7
import (
8
+
"fmt"
9
+
"math/rand"
8
10
"os"
9
11
"os/exec"
10
12
"path/filepath"
@@ -14,6 +16,18 @@ import (
14
16
"time"
15
17
)
16
18
19
+
var r *rand.Rand
20
+
21
+
func init() {
22
+
r = rand.New(rand.NewSource(time.Now().UnixNano()))
23
+
}
24
+
25
+
func testTempDir() string {
26
+
osTempDir := os.TempDir()
27
+
randDir := fmt.Sprintf("%d", r.Int())
28
+
return filepath.Join(osTempDir, randDir)
29
+
}
30
+
17
31
// An atomic counter
18
32
type counter struct {
19
33
val int32
@@ -45,8 +59,8 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
45
59
}
46
60
}()
47
61
48
-
const testDir string = "_test"
49
-
const testDirToMoveFiles string = "_test2"
62
+
var testDir string = testTempDir()
63
+
var testDirToMoveFiles string = testTempDir()
50
64
51
65
// Create directory to watch
52
66
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -60,8 +74,8 @@ func TestFsnotifyMultipleOperations(t *testing.T) {
60
74
}
61
75
defer os.RemoveAll(testDirToMoveFiles)
62
76
63
-
const testFile string = "_test/TestFsnotifySeq.testfile"
64
-
const testFileRenamed string = "_test2/TestFsnotifySeqRename.testfile"
77
+
var testFile string = filepath.Join(testDir, "TestFsnotifySeq.testfile")
78
+
var testFileRenamed string = filepath.Join(testDirToMoveFiles, "TestFsnotifySeqRename.testfile")
65
79
66
80
// Add a watch for testDir
67
81
err = watcher.Watch(testDir)
@@ -178,15 +192,15 @@ func TestFsnotifyMultipleCreates(t *testing.T) {
178
192
}
179
193
}()
180
194
181
-
const testDir string = "_test"
195
+
var testDir string = testTempDir()
182
196
183
197
// Create directory to watch
184
198
if err := os.Mkdir(testDir, 0777); err != nil {
185
199
t.Fatalf("failed to create test directory: %s", err)
186
200
}
187
201
defer os.RemoveAll(testDir)
188
202
189
-
const testFile string = "_test/TestFsnotifySeq.testfile"
203
+
var testFile string = filepath.Join(testDir, "TestFsnotifySeq.testfile")
190
204
191
205
// Add a watch for testDir
192
206
err = watcher.Watch(testDir)
@@ -308,7 +322,7 @@ func TestFsnotifyDirOnly(t *testing.T) {
308
322
t.Fatalf("NewWatcher() failed: %s", err)
309
323
}
310
324
311
-
const testDir string = "_test"
325
+
var testDir string = testTempDir()
312
326
313
327
// Create directory to watch
314
328
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -318,7 +332,7 @@ func TestFsnotifyDirOnly(t *testing.T) {
318
332
319
333
// Create a file before watching directory
320
334
// This should NOT add any events to the fsnotify event queue
321
-
const testFileAlreadyExists string = "_test/TestFsnotifyEventsExisting.testfile"
335
+
var testFileAlreadyExists string = filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
322
336
{
323
337
var f *os.File
324
338
f, err = os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666)
@@ -342,7 +356,7 @@ func TestFsnotifyDirOnly(t *testing.T) {
342
356
}
343
357
}()
344
358
345
-
const testFile string = "_test/TestFsnotifyDirOnly.testfile"
359
+
var testFile string = filepath.Join(testDir, "TestFsnotifyDirOnly.testfile")
346
360
347
361
// Receive events on the event channel on a separate goroutine
348
362
eventstream := watcher.Event
@@ -423,15 +437,15 @@ func TestFsnotifyDeleteWatchedDir(t *testing.T) {
423
437
}
424
438
defer watcher.Close()
425
439
426
-
const testDir string = "_test"
440
+
var testDir string = testTempDir()
427
441
428
442
// Create directory to watch
429
443
if err := os.Mkdir(testDir, 0777); err != nil {
430
444
t.Fatalf("failed to create test directory: %s", err)
431
445
}
432
446
433
447
// Create a file before watching directory
434
-
const testFileAlreadyExists string = "_test/TestFsnotifyEventsExisting.testfile"
448
+
var testFileAlreadyExists string = filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
435
449
{
436
450
var f *os.File
437
451
f, err = os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666)
@@ -495,10 +509,10 @@ func TestFsnotifySubDir(t *testing.T) {
495
509
t.Fatalf("NewWatcher() failed: %s", err)
496
510
}
497
511
498
-
const testDir string = "_test"
499
-
const testFile1 string = "_test/TestFsnotifyFile1.testfile"
500
-
const testSubDir string = "_test/sub"
501
-
const testSubDirFile string = "_test/sub/TestFsnotifyFile1.testfile"
512
+
var testDir string = testTempDir()
513
+
var testFile1 string = filepath.Join(testDir, "TestFsnotifyFile1.testfile")
514
+
var testSubDir string = filepath.Join(testDir, "sub")
515
+
var testSubDirFile string = filepath.Join(testDir, "sub/TestFsnotifyFile1.testfile")
502
516
503
517
// Create directory to watch
504
518
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -600,7 +614,7 @@ func TestFsnotifyRename(t *testing.T) {
600
614
t.Fatalf("NewWatcher() failed: %s", err)
601
615
}
602
616
603
-
const testDir string = "_test"
617
+
var testDir string = testTempDir()
604
618
605
619
// Create directory to watch
606
620
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -621,8 +635,8 @@ func TestFsnotifyRename(t *testing.T) {
621
635
}
622
636
}()
623
637
624
-
const testFile string = "_test/TestFsnotifyEvents.testfile"
625
-
const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed"
638
+
var testFile string = filepath.Join(testDir, "TestFsnotifyEvents.testfile")
639
+
var testFileRenamed string = filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")
626
640
627
641
// Receive events on the event channel on a separate goroutine
628
642
eventstream := watcher.Event
@@ -694,8 +708,8 @@ func TestFsnotifyRenameToCreate(t *testing.T) {
694
708
t.Fatalf("NewWatcher() failed: %s", err)
695
709
}
696
710
697
-
const testDir string = "_test"
698
-
const testDirFrom string = "_testfrom"
711
+
var testDir string = testTempDir()
712
+
var testDirFrom string = testTempDir()
699
713
700
714
// Create directory to watch
701
715
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -722,8 +736,8 @@ func TestFsnotifyRenameToCreate(t *testing.T) {
722
736
}
723
737
}()
724
738
725
-
const testFile string = "_testfrom/TestFsnotifyEvents.testfile"
726
-
const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed"
739
+
var testFile string = filepath.Join(testDirFrom, "TestFsnotifyEvents.testfile")
740
+
var testFileRenamed string = filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")
727
741
728
742
// Receive events on the event channel on a separate goroutine
729
743
eventstream := watcher.Event
@@ -790,11 +804,11 @@ func TestFsnotifyRenameToOverwrite(t *testing.T) {
790
804
t.Fatalf("NewWatcher() failed: %s", err)
791
805
}
792
806
793
-
const testDir string = "_test"
794
-
const testDirFrom string = "_testfrom"
807
+
var testDir string = testTempDir()
808
+
var testDirFrom string = testTempDir()
795
809
796
-
const testFile string = "_testfrom/TestFsnotifyEvents.testfile"
797
-
const testFileRenamed string = "_test/TestFsnotifyEvents.testfileRenamed"
810
+
var testFile string = filepath.Join(testDirFrom, "TestFsnotifyEvents.testfile")
811
+
var testFileRenamed string = filepath.Join(testDir, "TestFsnotifyEvents.testfileRenamed")
798
812
799
813
// Create directory to watch
800
814
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -892,7 +906,7 @@ func TestFsnotifyAttrib(t *testing.T) {
892
906
t.Fatalf("NewWatcher() failed: %s", err)
893
907
}
894
908
895
-
const testDir string = "_test"
909
+
var testDir string = testTempDir()
896
910
897
911
// Create directory to watch
898
912
if err := os.Mkdir(testDir, 0777); err != nil {
@@ -907,7 +921,7 @@ func TestFsnotifyAttrib(t *testing.T) {
907
921
}
908
922
}()
909
923
910
-
const testFile string = "_test/TestFsnotifyAttrib.testfile"
924
+
var testFile string = filepath.Join(testDir, "TestFsnotifyAttrib.testfile")
911
925
912
926
// Receive events on the event channel on a separate goroutine
913
927
eventstream := watcher.Event
@@ -987,7 +1001,7 @@ func TestFsnotifyClose(t *testing.T) {
987
1001
t.Fatal("double Close() test failed: second Close() call didn't return")
988
1002
}
989
1003
990
-
err := watcher.Watch("_test")
1004
+
err := watcher.Watch(testTempDir())
991
1005
if err == nil {
992
1006
t.Fatal("expected error on Watch() after Close(), got nil")
993
1007
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4