Fix: Test TestSafeWriteToDisk now works

Oops, my bad.
Despite the function intending to test SafeWriteToDisk it was actually
calling WriteToDisk. WriteToDisk does not return the file exists error
that SafeWriteToDisk does, which the test checks for.
This commit is contained in:
Owen Waller 2014-09-10 19:47:31 +01:00 committed by spf13
parent e8bbc44ab0
commit a54ad2bf39

View file

@ -457,44 +457,42 @@ func TestFindCWD(t *testing.T) {
} }
func TestSafeWriteToDisk(t *testing.T) { func TestSafeWriteToDisk(t *testing.T) {
/* emptyFile, _ := createZeroSizedFileInTempDir()
emptyFile, _ := createZeroSizedFileInTempDir() defer deleteFileInTempDir(emptyFile)
defer deleteFileInTempDir(emptyFile) tmpDir, _ := createEmptyTempDir()
tmpDir, _ := createEmptyTempDir() defer deleteTempDir(tmpDir)
defer deleteTempDir(tmpDir) os.MkdirAll(tmpDir+"/this/dir/does/not/exist/", 0644)
os.MkdirAll(tmpDir+"/this/dir/does/not/exist/", 0644)
randomString := "This is a random string!" randomString := "This is a random string!"
reader := strings.NewReader(randomString) reader := strings.NewReader(randomString)
fileExists := fmt.Errorf("%v already exists", emptyFile.Name()) fileExists := fmt.Errorf("%v already exists", emptyFile.Name())
type test struct { type test struct {
filename string filename string
expectedErr error expectedErr error
} }
data := []test{ data := []test{
{emptyFile.Name(), fileExists}, {emptyFile.Name(), fileExists},
{tmpDir + "/" + emptyFile.Name(), nil}, {tmpDir + "/" + emptyFile.Name(), nil},
} }
for i, d := range data { for i, d := range data {
e := WriteToDisk(d.filename, reader) e := SafeWriteToDisk(d.filename, reader)
t.Errorf("Failed: e is %q %#v", e, e) t.Errorf("Failed: e is %q %#v", e, e)
if d.expectedErr != nil { if d.expectedErr != nil {
if d.expectedErr.Error() != e.Error() { if d.expectedErr.Error() != e.Error() {
t.Errorf("Test %d failed. Expected error %q but got %q", i, d.expectedErr.Error(), e.Error()) t.Errorf("Test %d failed. Expected error %q but got %q", i, d.expectedErr.Error(), e.Error())
}
}
if d.expectedErr != e {
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, e)
}
contents, e := ioutil.ReadFile(d.filename)
if randomString != string(contents) {
t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents))
} }
} }
*/ if d.expectedErr != e {
t.Errorf("Test %d failed. Expected %q but got %q", i, d.expectedErr, e)
}
contents, e := ioutil.ReadFile(d.filename)
if randomString != string(contents) {
t.Errorf("Test %d failed. Expected contents %q but got %q", i, randomString, string(contents))
}
}
} }
func TestWriteToDisk(t *testing.T) { func TestWriteToDisk(t *testing.T) {