package file import ( "bytes" "io/ioutil" "os" "path" "testing" ) func TestContent(t *testing.T) { tmpDir, err := ioutil.TempDir("", "file.go") if err != nil { t.Fatalf("could not create test directory") } defer os.RemoveAll(tmpDir) t1 := &F{ Content: []byte("this is a test"), Path: path.Join(tmpDir, "test1"), } if err := t1.Ensure(); err != nil { t.Fatalf("file failed with: %s", err) } raw, err := ioutil.ReadFile(t1.Path) if err != nil { t.Fatalf("could not read file: %s", err) } if bytes.Compare(raw, t1.Content) != 0 { t.Fatalf("content mismatch") } t2 := &F{ Content: []byte("other content"), Path: path.Join(tmpDir, "test1"), } if t2.Is() { t.Fatalf("Is should have returned false") } if err := t2.Ensure(); err != nil { t.Fatalf("it should enforce new content: %s", err) } if t1.Is() { t.Fatalf("changes not recognized") } } func TestDir(t *testing.T) { tmpDir, err := ioutil.TempDir("", "file.go") if err != nil { t.Fatalf("could not create test directory") } defer os.RemoveAll(tmpDir) t1 := &F{IsDir: true} if err := t1.Ensure(); err == nil { t.Fatalf("a file without path should not work") } t1.Path = path.Join(tmpDir, "t1") if err := t1.Ensure(); err != nil { t.Fatalf("directory should have been created, instead got: %s", err) } t1.Content = []byte("just a check") if err := t1.Ensure(); err == nil { t.Fatalf("content and IsDir should not work") } }