From a270c4c737c8f83e7fc761b211f6655d0e153ce6 Mon Sep 17 00:00:00 2001 From: Michael Marineau Date: Fri, 11 Jul 2014 11:27:05 -0700 Subject: [PATCH] env_file: fix broken test cases TestWriteEnvFileDos2Unix had a copy/paste bug, it shouldn't have asserted that mtime doesn't change because the file is actually being modified in this test. This didn't come up earlier because the actual comparison wasn't using Time.Equal as it should have. Instead switch to comparing inode numbers which is the actual thing I wanted to test for in the first place, just accessing them is much more awkard. Now all tests where it is relevant check the inode in addition to the contents. --- system/env_file_test.go | 57 +++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/system/env_file_test.go b/system/env_file_test.go index c3fc0a4..1654e39 100644 --- a/system/env_file_test.go +++ b/system/env_file_test.go @@ -5,6 +5,7 @@ import ( "os" "path" "strings" + "syscall" "testing" ) @@ -41,6 +42,11 @@ func TestWriteEnvFileUpdate(t *testing.T) { fullPath := path.Join(dir, name) ioutil.WriteFile(fullPath, []byte(base), 0644) + oldStat, err := os.Stat(fullPath) + if err != nil { + t.Fatal("Unable to stat file: %v", err) + } + ef := EnvFile{ File: &File{ Path: name, @@ -61,6 +67,15 @@ func TestWriteEnvFileUpdate(t *testing.T) { if string(contents) != expectUpdate { t.Fatalf("File has incorrect contents: %q", contents) } + + newStat, err := os.Stat(fullPath) + if err != nil { + t.Fatal("Unable to stat file: %v", err) + } + + if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino { + t.Fatal("File was not replaced: %s", fullPath) + } } func TestWriteEnvFileUpdateNoNewline(t *testing.T) { @@ -74,6 +89,11 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) { fullPath := path.Join(dir, name) ioutil.WriteFile(fullPath, []byte(baseNoNewline), 0644) + oldStat, err := os.Stat(fullPath) + if err != nil { + t.Fatal("Unable to stat file: %v", err) + } + ef := EnvFile{ File: &File{ Path: name, @@ -94,6 +114,15 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) { if string(contents) != expectUpdate { t.Fatalf("File has incorrect contents: %q", contents) } + + newStat, err := os.Stat(fullPath) + if err != nil { + t.Fatal("Unable to stat file: %v", err) + } + + if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino { + t.Fatal("File was not replaced: %s", fullPath) + } } func TestWriteEnvFileCreate(t *testing.T) { @@ -170,8 +199,8 @@ func TestWriteEnvFileNoop(t *testing.T) { t.Fatal("Unable to stat file: %v", err) } - if oldStat.ModTime() != newStat.ModTime() { - t.Fatal("File mtime changed.") + if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino { + t.Fatal("File was replaced: %s", fullPath) } } @@ -186,6 +215,11 @@ func TestWriteEnvFileUpdateDos(t *testing.T) { fullPath := path.Join(dir, name) ioutil.WriteFile(fullPath, []byte(baseDos), 0644) + oldStat, err := os.Stat(fullPath) + if err != nil { + t.Fatal("Unable to stat file: %v", err) + } + ef := EnvFile{ File: &File{ Path: name, @@ -206,6 +240,15 @@ func TestWriteEnvFileUpdateDos(t *testing.T) { if string(contents) != expectUpdate { t.Fatalf("File has incorrect contents: %q", contents) } + + newStat, err := os.Stat(fullPath) + if err != nil { + t.Fatal("Unable to stat file: %v", err) + } + + if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino { + t.Fatal("File was not replaced: %s", fullPath) + } } // A middle ground noop, values are unchanged but we did have a value. @@ -252,8 +295,8 @@ func TestWriteEnvFileDos2Unix(t *testing.T) { t.Fatal("Unable to stat file: %v", err) } - if oldStat.ModTime() != newStat.ModTime() { - t.Fatal("File mtime changed.") + if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino { + t.Fatal("File was not replaced: %s", fullPath) } } @@ -300,8 +343,8 @@ func TestWriteEnvFileEmpty(t *testing.T) { t.Fatal("Unable to stat file: %v", err) } - if oldStat.ModTime() != newStat.ModTime() { - t.Fatal("File mtime changed.") + if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino { + t.Fatal("File was replaced: %s", fullPath) } } @@ -368,8 +411,6 @@ func TestWriteEnvFileNameFailure(t *testing.T) { defer os.RemoveAll(dir) name := "foo.conf" - fullPath := path.Join(dir, name) - ioutil.WriteFile(fullPath, []byte(base), 0000) ef := EnvFile{ File: &File{