From 920d458a4a8d6cf39b856ce3145177663ed7cf0e Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 5 May 2019 14:57:38 +0200 Subject: [PATCH] archiver: Use untyped constants for testing FileInfo --- internal/archiver/archiver_test.go | 12 ++++++++++-- internal/archiver/archiver_unix_test.go | 10 +++++----- internal/archiver/archiver_windows_test.go | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index ce6bb7fa7..3335d3cbb 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -2022,6 +2022,14 @@ func (f fileStat) Stat() (os.FileInfo, error) { return f.fi, nil } +// used by wrapFileInfo, use untyped const in order to avoid having a version +// of wrapFileInfo for each OS +const ( + mockFileInfoMode = 0400 + mockFileInfoUID = 51234 + mockFileInfoGID = 51235 +) + func TestMetadataChanged(t *testing.T) { files := TestDir{ "testfile": TestFile{ @@ -2061,8 +2069,8 @@ func TestMetadataChanged(t *testing.T) { t.Fatalf("metadata does not match:\n%v", cmp.Diff(want, node2)) } - // modify the mode by wrapping it in a new struct - fs.OverrideLstat["testfile"] = wrapFileInfo(t, fi, 0400, 51234, 51235) + // modify the mode by wrapping it in a new struct, uses the consts defined above + fs.OverrideLstat["testfile"] = wrapFileInfo(t, fi) // set the override values in the 'want' node which want.Mode = 0400 diff --git a/internal/archiver/archiver_unix_test.go b/internal/archiver/archiver_unix_test.go index 4af972577..f7e827e7e 100644 --- a/internal/archiver/archiver_unix_test.go +++ b/internal/archiver/archiver_unix_test.go @@ -23,18 +23,18 @@ func (fi wrappedFileInfo) Mode() os.FileMode { } // wrapFileInfo returns a new os.FileInfo with the mode, owner, and group fields changed. -func wrapFileInfo(t testing.TB, fi os.FileInfo, mode os.FileMode, uid, gid uint) os.FileInfo { +func wrapFileInfo(t testing.TB, fi os.FileInfo) os.FileInfo { // get the underlying stat_t and modify the values stat := fi.Sys().(*syscall.Stat_t) - stat.Mode = uint32(mode) - stat.Uid = uint32(uid) - stat.Gid = uint32(gid) + stat.Mode = mockFileInfoMode + stat.Uid = mockFileInfoUID + stat.Gid = mockFileInfoGID // wrap the os.FileInfo so we can return a modified stat_t res := wrappedFileInfo{ FileInfo: fi, sys: stat, - mode: mode, + mode: mockFileInfoMode, } return res diff --git a/internal/archiver/archiver_windows_test.go b/internal/archiver/archiver_windows_test.go index 6a0100f49..9b3d77898 100644 --- a/internal/archiver/archiver_windows_test.go +++ b/internal/archiver/archiver_windows_test.go @@ -17,11 +17,11 @@ func (fi wrappedFileInfo) Mode() os.FileMode { } // wrapFileInfo returns a new os.FileInfo with the mode, owner, and group fields changed. -func wrapFileInfo(t testing.TB, fi os.FileInfo, mode os.FileMode, uid, gid uint) os.FileInfo { +func wrapFileInfo(t testing.TB, fi os.FileInfo) os.FileInfo { // wrap the os.FileInfo and return the modified mode, uid and gid are ignored on Windows res := wrappedFileInfo{ FileInfo: fi, - mode: mode, + mode: mockFileInfoMode, } return res