@@ -95,26 +95,30 @@ func TestAll(t *testing.T) {
95
95
// * directories with the xattr com.apple.FinderInfo (like everything in /tmp) are hidden
96
96
// In order to test this, we must create a log file somewhere outside of /tmp, so we use $HOME.
97
97
func TestVisibleInOSXFinder (t * testing.T ) {
98
+ ctx := setUp (t , "visible in macOS finder" , closeFileAfterEachLine , fseventTailer , _nocreate , mv )
99
+
100
+ // replace ctx.basedir with a directory in $HOME
98
101
currentUser , err := user .Current ()
99
102
if err != nil {
100
- t . Fatalf ( "failed to get current user: %v" , err )
103
+ fatalf ( t , ctx , "failed to get current user: %v" , err )
101
104
}
102
105
testDir , err := ioutil .TempDir (currentUser .HomeDir , "grok_exporter_test_dir_" )
103
106
if err != nil {
104
- t . Fatalf ( "failed to create test directory: %v" , err .Error ())
107
+ fatalf ( t , ctx , "failed to create test directory: %v" , err .Error ())
105
108
}
106
109
defer func () {
107
110
err := os .RemoveAll (testDir )
108
111
if err != nil {
109
- t . Fatalf ( "%v: Failed to remove test directory after running the tests: %v" , testDir , err .Error ())
112
+ fatalf ( t , ctx , "%v: Failed to remove test directory after running the tests: %v" , testDir , err .Error ())
110
113
}
111
114
}()
112
- ctx := setUp (t , "visible in macOS finder" , closeFileAfterEachLine , fseventTailer , _nocreate , mv )
113
115
err = os .RemoveAll (ctx .basedir )
114
116
if err != nil {
115
- t . Fatalf ( "%v: failed to remove temp dir: %v" , ctx .basedir , err )
117
+ fatalf ( t , ctx , "%v: failed to remove temp dir: %v" , ctx .basedir , err )
116
118
}
117
119
ctx .basedir = testDir
120
+
121
+ // run simple test in the new directory
118
122
test := [][]string {
119
123
{"log" , "line 1" , "test.log" },
120
124
{"start file tailer" , "test.log" },
@@ -163,14 +167,10 @@ func closeTailer(t *testing.T, ctx *context) {
163
167
select {
164
168
case line , open := <- ctx .tailer .Lines ():
165
169
if open {
166
- msg := fmt .Sprintf ("read unexpected line line from file %q: %q" , line .File , line .Line )
167
- ctx .log .Error (msg )
168
- t .Fatal (msg )
170
+ fatalf (t , ctx , "read unexpected line line from file %q: %q" , line .File , line .Line )
169
171
}
170
172
case <- time .After (timeout ):
171
- msg := fmt .Sprintf ("failed to shut down the tailer. timeout after %v seconds" , timeout )
172
- ctx .log .Error (msg )
173
- fatalf (t , ctx , msg )
173
+ fatalf (t , ctx , "failed to shut down the tailer. timeout after %v seconds" , timeout )
174
174
}
175
175
}
176
176
}
@@ -468,7 +468,7 @@ func startFileTailer(t *testing.T, ctx *context, params []string) {
468
468
for _ , g := range globs {
469
469
parsedGlob , err := glob .Parse (filepath .Join (ctx .basedir , g ))
470
470
if err != nil {
471
- t . Fatal ( err )
471
+ fatalf ( t , ctx , "%v" , err )
472
472
}
473
473
parsedGlobs = append (parsedGlobs , parsedGlob )
474
474
}
@@ -723,10 +723,59 @@ func (l *keepOpenLogFileWriter) close(t *testing.T, ctx *context) {
723
723
}
724
724
725
725
func tearDown (t * testing.T , ctx * context ) {
726
- err := os .RemoveAll (ctx .basedir )
726
+ deleteRecursively (t , ctx , ctx .basedir )
727
+ }
728
+
729
+ func deleteRecursively (t * testing.T , ctx * context , file string ) {
730
+ // os.RemoveAll() fails on Windows with "Access is denied".
731
+ // Remove each file individually to learn which file cannot be removed.
732
+ fileInfo , err := os .Stat (file )
727
733
if err != nil {
728
- fatalf (t , ctx , "%v: failed to remove the test directory after running the tests: %v" , ctx .basedir , err )
734
+ fatalf (t , ctx , "tearDown: stat(%q) failed: %v" , file , err )
735
+ }
736
+ if fileInfo .IsDir () {
737
+ for _ , childInfo := range ls (t , ctx , file ) {
738
+ deleteRecursively (t , ctx , path .Join (file , childInfo .Name ()))
739
+ }
740
+ }
741
+ ctx .log .Debugf ("tearDown: removing %q" , file )
742
+ delete (t , ctx , file )
743
+ //if err != nil {
744
+ // // We get "The directory is not empty" here on Windows.
745
+ // // Let's print what files are in here.
746
+ // var filesInDir []string
747
+ // for _, fileInDir := range ls(t, ctx, file) {
748
+ // filesInDir = append(filesInDir, fileInDir.Name())
749
+ // }
750
+ // fatalf(t, ctx, "tearDown: %q: remove failed: %v. number of files in dir: %v, list of file in dir: %#v", file, err, len(filesInDir), filesInDir)
751
+ //}
752
+ }
753
+
754
+ func delete (t * testing.T , ctx * context , file string ) {
755
+ // os.Remove() seems to fail on Windows without returning an error (file or directory is still there after os.Remove()).
756
+ // Maybe it's a timing problem with the watcher still keeping the file or directory alive.
757
+ // This function checks if the file or directory was deleted and tries again if it is still there.
758
+ timeout := 5 * time .Second
759
+ timePassed := 0 * time .Second
760
+ for timePassed < timeout {
761
+ err := os .Remove (file )
762
+ if err != nil {
763
+ fatalf (t , ctx , "tearDown: %q: remove failed: %v" , file , err )
764
+ }
765
+ _ , err = os .Stat (file )
766
+ if err != nil {
767
+ if os .IsNotExist (err ) {
768
+ // os.Remove(file) was successful, the file or directory is gone.
769
+ return
770
+ } else {
771
+ fatalf (t , ctx , "tearDown: %q: stat failed: %v" , file , err )
772
+ }
773
+ }
774
+ // os.Stat() successful. The file or directory is still there. Try again.
775
+ time .Sleep (200 * time .Millisecond )
776
+ timePassed += 200 * time .Millisecond
729
777
}
778
+ fatalf (t , ctx , "tearDown: %q: failed to remove file or directory" , file )
730
779
}
731
780
732
781
func printCmd (cmd []string ) string {
@@ -803,18 +852,18 @@ func runTestShutdown(t *testing.T, mode string) {
803
852
select {
804
853
case _ , open := <- tailer .Errors ():
805
854
if open {
806
- t . Fatalf ( "error channel not closed" )
855
+ fatalf ( t , ctx , "error channel not closed" )
807
856
}
808
857
case <- time .After (5 * time .Second ):
809
- t . Fatalf ( "timeout while waiting for errors channel to be closed." )
858
+ fatalf ( t , ctx , "timeout while waiting for errors channel to be closed." )
810
859
}
811
860
select {
812
861
case _ , open := <- tailer .Lines ():
813
862
if open {
814
- t . Fatalf ( "lines channel not closed" )
863
+ fatalf ( t , ctx , "lines channel not closed" )
815
864
}
816
865
case <- time .After (5 * time .Second ):
817
- t . Fatalf ( "timeout while waiting for errors channel to be closed." )
866
+ fatalf ( t , ctx , "timeout while waiting for errors channel to be closed." )
818
867
}
819
868
assertGoroutinesTerminated (t , ctx , nGoroutinesBefore )
820
869
}
0 commit comments