@@ -95,26 +95,30 @@ func TestAll(t *testing.T) {
9595// * directories with the xattr com.apple.FinderInfo (like everything in /tmp) are hidden
9696// In order to test this, we must create a log file somewhere outside of /tmp, so we use $HOME.
9797func 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
98101 currentUser , err := user .Current ()
99102 if err != nil {
100- t . Fatalf ( "failed to get current user: %v" , err )
103+ fatalf ( t , ctx , "failed to get current user: %v" , err )
101104 }
102105 testDir , err := ioutil .TempDir (currentUser .HomeDir , "grok_exporter_test_dir_" )
103106 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 ())
105108 }
106109 defer func () {
107110 err := os .RemoveAll (testDir )
108111 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 ())
110113 }
111114 }()
112- ctx := setUp (t , "visible in macOS finder" , closeFileAfterEachLine , fseventTailer , _nocreate , mv )
113115 err = os .RemoveAll (ctx .basedir )
114116 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 )
116118 }
117119 ctx .basedir = testDir
120+
121+ // run simple test in the new directory
118122 test := [][]string {
119123 {"log" , "line 1" , "test.log" },
120124 {"start file tailer" , "test.log" },
@@ -163,14 +167,10 @@ func closeTailer(t *testing.T, ctx *context) {
163167 select {
164168 case line , open := <- ctx .tailer .Lines ():
165169 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 )
169171 }
170172 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 )
174174 }
175175 }
176176}
@@ -468,7 +468,7 @@ func startFileTailer(t *testing.T, ctx *context, params []string) {
468468 for _ , g := range globs {
469469 parsedGlob , err := glob .Parse (filepath .Join (ctx .basedir , g ))
470470 if err != nil {
471- t . Fatal ( err )
471+ fatalf ( t , ctx , "%v" , err )
472472 }
473473 parsedGlobs = append (parsedGlobs , parsedGlob )
474474 }
@@ -723,10 +723,59 @@ func (l *keepOpenLogFileWriter) close(t *testing.T, ctx *context) {
723723}
724724
725725func 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 )
727733 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
729777 }
778+ fatalf (t , ctx , "tearDown: %q: failed to remove file or directory" , file )
730779}
731780
732781func printCmd (cmd []string ) string {
@@ -803,18 +852,18 @@ func runTestShutdown(t *testing.T, mode string) {
803852 select {
804853 case _ , open := <- tailer .Errors ():
805854 if open {
806- t . Fatalf ( "error channel not closed" )
855+ fatalf ( t , ctx , "error channel not closed" )
807856 }
808857 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." )
810859 }
811860 select {
812861 case _ , open := <- tailer .Lines ():
813862 if open {
814- t . Fatalf ( "lines channel not closed" )
863+ fatalf ( t , ctx , "lines channel not closed" )
815864 }
816865 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." )
818867 }
819868 assertGoroutinesTerminated (t , ctx , nGoroutinesBefore )
820869}
0 commit comments