Close files so that tests pass on windows

This commit is contained in:
Justin Hawkins 2021-10-16 16:18:02 +10:30
parent 79d14c00bc
commit c3f1813f6e
2 changed files with 18 additions and 7 deletions

View File

@ -10,10 +10,14 @@ func TestNoConfig(t *testing.T) {
c := ConfigService{} c := ConfigService{}
c.ConfigFilename = emptyTempFile() c.ConfigFilename = emptyTempFile()
os.Remove(c.ConfigFilename) err := os.Remove(c.ConfigFilename)
if err != nil {
t.Fatalf("could not remove file: %v", err)
}
defer os.Remove(c.ConfigFilename) // because we are about to create it defer os.Remove(c.ConfigFilename) // because we are about to create it
err := c.LoadOrInit() err = c.LoadOrInit()
if err != nil { if err != nil {
t.Errorf("unexpected failure from load: %s", err) t.Errorf("unexpected failure from load: %s", err)
} }
@ -84,6 +88,7 @@ func emptyTempFile() string {
if err != nil { if err != nil {
panic(err) panic(err)
} }
f.Close()
return f.Name() return f.Name()
} }

View File

@ -80,7 +80,11 @@ func TestCheckPath(t *testing.T) {
if !w.checkPath() { if !w.checkPath() {
t.Error("checkPath failed?") t.Error("checkPath failed?")
} }
os.RemoveAll(dir)
err := os.RemoveAll(dir)
if err != nil {
t.Fatalf("could not remove test dir: %v", err)
}
if w.checkPath() { if w.checkPath() {
t.Error("checkPath succeeded when shouldn't?") t.Error("checkPath succeeded when shouldn't?")
} }
@ -91,9 +95,11 @@ func createFileTree() string {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
os.Create(fmt.Sprintf("%s%c%s", dir, os.PathSeparator, "a.gif")) f1, _ := os.Create(fmt.Sprintf("%s%c%s", dir, os.PathSeparator, "a.gif"))
os.Create(fmt.Sprintf("%s%c%s", dir, os.PathSeparator, "a.jpg")) f2, _ := os.Create(fmt.Sprintf("%s%c%s", dir, os.PathSeparator, "a.jpg"))
os.Create(fmt.Sprintf("%s%c%s", dir, os.PathSeparator, "a.png")) f3, _ := os.Create(fmt.Sprintf("%s%c%s", dir, os.PathSeparator, "a.png"))
f1.Close()
f2.Close()
f3.Close()
return dir return dir
} }