Created
October 29, 2012 08:48
-
-
Save jakerr/3972447 to your computer and use it in GitHub Desktop.
FSNotify Bug TestCase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/** | |
* Exhibits a bug in fsnotify where removing with "rm -r" a watched dir which includes a watched file | |
* causes an error. Rather than delete events for each item and the containing folder. | |
* | |
* Steps to repro: | |
* 1.) Watch a directory | |
* 2.) Watch a file in the directory | |
* 3.) Rm -rf the directory | |
**/ | |
import ( | |
"github.com/howeyc/fsnotify" | |
"log" | |
"path/filepath" | |
"io/ioutil" | |
"time" | |
"os/exec" | |
) | |
func panic_if(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func main() { | |
done := make(chan bool) | |
w, err := fsnotify.NewWatcher() | |
panic_if(err) | |
tempDir, err := ioutil.TempDir("", "a_dir") | |
panic_if(err) | |
log.Println(tempDir) | |
go func() { | |
for { | |
select { | |
case ev := <-w.Event: | |
log.Println("event:", ev) | |
case err := <-w.Error: | |
panic(err) | |
done <- true | |
} | |
} | |
}() | |
go func() { | |
// Step 1.) | |
log.Println("Watching new dir at", tempDir) | |
w.Watch(tempDir); | |
time.Sleep(1 * time.Second) | |
// Step 2.) | |
f, err := ioutil.TempFile(tempDir, "a_file") | |
panic_if(err) | |
f_info, err := f.Stat() | |
panic_if(err) | |
f_path := filepath.Join(tempDir, f_info.Name()) | |
log.Println("Watch new file at", f_path) | |
w.Watch(f_path); | |
err = f.Close() | |
panic_if(err) | |
time.Sleep(1 * time.Second) | |
// Step 3.) | |
letTestRunRm := false | |
if (letTestRunRm) { | |
// I left this behind a flag because I don't | |
// feel comfortable running rm -rf on other people's | |
// machine's without asking. | |
rmCmd := exec.Command("rm", "-rf", tempDir) | |
log.Printf("\n\nTry to delete watched dir with %v", rmCmd.Args) | |
err = rmCmd.Run() | |
panic_if(err) | |
} else { | |
log.Println("Please go run ( rm -rf ", | |
tempDir, | |
") and notice the failure.") | |
} | |
}() | |
<- done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment