Merge pull request #182 from marineam/fix

env_file: fix broken test cases
This commit is contained in:
Michael Marineau 2014-07-11 14:38:56 -07:00
commit 5e4cbcd909

View File

@ -5,6 +5,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"syscall"
"testing" "testing"
) )
@ -41,6 +42,11 @@ func TestWriteEnvFileUpdate(t *testing.T) {
fullPath := path.Join(dir, name) fullPath := path.Join(dir, name)
ioutil.WriteFile(fullPath, []byte(base), 0644) ioutil.WriteFile(fullPath, []byte(base), 0644)
oldStat, err := os.Stat(fullPath)
if err != nil {
t.Fatal("Unable to stat file: %v", err)
}
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{
Path: name, Path: name,
@ -61,6 +67,15 @@ func TestWriteEnvFileUpdate(t *testing.T) {
if string(contents) != expectUpdate { if string(contents) != expectUpdate {
t.Fatalf("File has incorrect contents: %q", contents) 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) { func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
@ -74,6 +89,11 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
fullPath := path.Join(dir, name) fullPath := path.Join(dir, name)
ioutil.WriteFile(fullPath, []byte(baseNoNewline), 0644) ioutil.WriteFile(fullPath, []byte(baseNoNewline), 0644)
oldStat, err := os.Stat(fullPath)
if err != nil {
t.Fatal("Unable to stat file: %v", err)
}
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{
Path: name, Path: name,
@ -94,6 +114,15 @@ func TestWriteEnvFileUpdateNoNewline(t *testing.T) {
if string(contents) != expectUpdate { if string(contents) != expectUpdate {
t.Fatalf("File has incorrect contents: %q", contents) 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) { func TestWriteEnvFileCreate(t *testing.T) {
@ -170,8 +199,8 @@ func TestWriteEnvFileNoop(t *testing.T) {
t.Fatal("Unable to stat file: %v", err) t.Fatal("Unable to stat file: %v", err)
} }
if oldStat.ModTime() != newStat.ModTime() { if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino {
t.Fatal("File mtime changed.") t.Fatal("File was replaced: %s", fullPath)
} }
} }
@ -186,6 +215,11 @@ func TestWriteEnvFileUpdateDos(t *testing.T) {
fullPath := path.Join(dir, name) fullPath := path.Join(dir, name)
ioutil.WriteFile(fullPath, []byte(baseDos), 0644) ioutil.WriteFile(fullPath, []byte(baseDos), 0644)
oldStat, err := os.Stat(fullPath)
if err != nil {
t.Fatal("Unable to stat file: %v", err)
}
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{
Path: name, Path: name,
@ -206,6 +240,15 @@ func TestWriteEnvFileUpdateDos(t *testing.T) {
if string(contents) != expectUpdate { if string(contents) != expectUpdate {
t.Fatalf("File has incorrect contents: %q", contents) 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. // 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) t.Fatal("Unable to stat file: %v", err)
} }
if oldStat.ModTime() != newStat.ModTime() { if oldStat.Sys().(*syscall.Stat_t).Ino == newStat.Sys().(*syscall.Stat_t).Ino {
t.Fatal("File mtime changed.") 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) t.Fatal("Unable to stat file: %v", err)
} }
if oldStat.ModTime() != newStat.ModTime() { if oldStat.Sys().(*syscall.Stat_t).Ino != newStat.Sys().(*syscall.Stat_t).Ino {
t.Fatal("File mtime changed.") t.Fatal("File was replaced: %s", fullPath)
} }
} }
@ -368,8 +411,6 @@ func TestWriteEnvFileNameFailure(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
name := "foo.conf" name := "foo.conf"
fullPath := path.Join(dir, name)
ioutil.WriteFile(fullPath, []byte(base), 0000)
ef := EnvFile{ ef := EnvFile{
File: &File{ File: &File{