From a19ee75bd15140c73a3e5a91e25964a00a9f0559 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 7 Oct 2022 16:17:11 +0200 Subject: [PATCH] pkg/system: fix missing assertions and use sub-tests for ChTimes These tests were effectively doing "subtests", using comments to describe each, however; - due to the use of `t.Fatal()` would terminate before completing all "subtests" - The error returned by the function being tested (`Chtimes`), was not checked, and the test used "indirect" checks to verify if it worked correctly. Adding assertions to check if the function didn't produce an error. Signed-off-by: Sebastiaan van Stijn --- pkg/system/chtimes_linux_test.go | 120 +++++++++++++++++------------ pkg/system/chtimes_test.go | 100 ++++++++++++++---------- pkg/system/chtimes_windows_test.go | 110 +++++++++++++++----------- 3 files changed, 195 insertions(+), 135 deletions(-) diff --git a/pkg/system/chtimes_linux_test.go b/pkg/system/chtimes_linux_test.go index 091b8aba44..662aaf147d 100644 --- a/pkg/system/chtimes_linux_test.go +++ b/pkg/system/chtimes_linux_test.go @@ -19,72 +19,92 @@ func TestChtimesATime(t *testing.T) { afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) // Test both aTime and mTime set to Unix Epoch - Chtimes(file, unixEpochTime, unixEpochTime) + t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil { + t.Error(err) + } - f, err := os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - stat := f.Sys().(*syscall.Stat_t) - aTime := time.Unix(stat.Atim.Unix()) - if aTime != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) - } + stat := f.Sys().(*syscall.Stat_t) + aTime := time.Unix(stat.Atim.Unix()) + if aTime != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) + } + }) // Test aTime before Unix Epoch and mTime set to Unix Epoch - Chtimes(file, beforeUnixEpochTime, unixEpochTime) + t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(stat.Atim.Unix()) - if aTime != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) - } + stat := f.Sys().(*syscall.Stat_t) + aTime := time.Unix(stat.Atim.Unix()) + if aTime != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) + } + }) // Test aTime set to Unix Epoch and mTime before Unix Epoch - Chtimes(file, unixEpochTime, beforeUnixEpochTime) + t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(stat.Atim.Unix()) - if aTime != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) - } + stat := f.Sys().(*syscall.Stat_t) + aTime := time.Unix(stat.Atim.Unix()) + if aTime != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) + } + }) // Test both aTime and mTime set to after Unix Epoch (valid time) - Chtimes(file, afterUnixEpochTime, afterUnixEpochTime) + t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) { + if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(stat.Atim.Unix()) - if aTime != afterUnixEpochTime { - t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) - } + stat := f.Sys().(*syscall.Stat_t) + aTime := time.Unix(stat.Atim.Unix()) + if aTime != afterUnixEpochTime { + t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) + } + }) // Test both aTime and mTime set to Unix max time - Chtimes(file, unixMaxTime, unixMaxTime) + t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) { + if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - stat = f.Sys().(*syscall.Stat_t) - aTime = time.Unix(stat.Atim.Unix()) - if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { - t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) - } + stat := f.Sys().(*syscall.Stat_t) + aTime := time.Unix(stat.Atim.Unix()) + if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { + t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) + } + }) } diff --git a/pkg/system/chtimes_test.go b/pkg/system/chtimes_test.go index eb85110a45..ad95df2f09 100644 --- a/pkg/system/chtimes_test.go +++ b/pkg/system/chtimes_test.go @@ -19,62 +19,82 @@ func TestChtimesModTime(t *testing.T) { afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) // Test both aTime and mTime set to Unix Epoch - Chtimes(file, unixEpochTime, unixEpochTime) + t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil { + t.Error(err) + } - f, err := os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - if f.ModTime() != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) - } + if f.ModTime() != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) + } + }) // Test aTime before Unix Epoch and mTime set to Unix Epoch - Chtimes(file, beforeUnixEpochTime, unixEpochTime) + t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - if f.ModTime() != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) - } + if f.ModTime() != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) + } + }) // Test aTime set to Unix Epoch and mTime before Unix Epoch - Chtimes(file, unixEpochTime, beforeUnixEpochTime) + t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - if f.ModTime() != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) - } + if f.ModTime() != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, f.ModTime()) + } + }) // Test both aTime and mTime set to after Unix Epoch (valid time) - Chtimes(file, afterUnixEpochTime, afterUnixEpochTime) + t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) { + if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - if f.ModTime() != afterUnixEpochTime { - t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, f.ModTime()) - } + if f.ModTime() != afterUnixEpochTime { + t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, f.ModTime()) + } + }) // Test both aTime and mTime set to Unix max time - Chtimes(file, unixMaxTime, unixMaxTime) + t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) { + if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - if f.ModTime().Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { - t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), f.ModTime().Truncate(time.Second)) - } + if f.ModTime().Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { + t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), f.ModTime().Truncate(time.Second)) + } + }) } diff --git a/pkg/system/chtimes_windows_test.go b/pkg/system/chtimes_windows_test.go index cd84f5643f..6184336f3e 100644 --- a/pkg/system/chtimes_windows_test.go +++ b/pkg/system/chtimes_windows_test.go @@ -22,67 +22,87 @@ func TestChtimesATimeWindows(t *testing.T) { afterUnixEpochTime := unixEpochTime.Add(100 * time.Second) // Test both aTime and mTime set to Unix Epoch - Chtimes(file, unixEpochTime, unixEpochTime) + t.Run("both aTime and mTime set to Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, unixEpochTime, unixEpochTime); err != nil { + t.Error(err) + } - f, err := os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) - if aTime != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) - } + aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) + if aTime != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) + } + }) // Test aTime before Unix Epoch and mTime set to Unix Epoch - Chtimes(file, beforeUnixEpochTime, unixEpochTime) + t.Run("aTime before Unix Epoch and mTime set to Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, beforeUnixEpochTime, unixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) - if aTime != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) - } + aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) + if aTime != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) + } + }) // Test aTime set to Unix Epoch and mTime before Unix Epoch - Chtimes(file, unixEpochTime, beforeUnixEpochTime) + t.Run("aTime set to Unix Epoch and mTime before Unix Epoch", func(t *testing.T) { + if err := Chtimes(file, unixEpochTime, beforeUnixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) - if aTime != unixEpochTime { - t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) - } + aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) + if aTime != unixEpochTime { + t.Fatalf("Expected: %s, got: %s", unixEpochTime, aTime) + } + }) // Test both aTime and mTime set to after Unix Epoch (valid time) - Chtimes(file, afterUnixEpochTime, afterUnixEpochTime) + t.Run("both aTime and mTime set to after Unix Epoch (valid time)", func(t *testing.T) { + if err := Chtimes(file, afterUnixEpochTime, afterUnixEpochTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) - if aTime != afterUnixEpochTime { - t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) - } + aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) + if aTime != afterUnixEpochTime { + t.Fatalf("Expected: %s, got: %s", afterUnixEpochTime, aTime) + } + }) // Test both aTime and mTime set to Unix max time - Chtimes(file, unixMaxTime, unixMaxTime) + t.Run("both aTime and mTime set to Unix max time", func(t *testing.T) { + if err := Chtimes(file, unixMaxTime, unixMaxTime); err != nil { + t.Error(err) + } - f, err = os.Stat(file) - if err != nil { - t.Fatal(err) - } + f, err := os.Stat(file) + if err != nil { + t.Fatal(err) + } - aTime = time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) - if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { - t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) - } + aTime := time.Unix(0, f.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds()) + if aTime.Truncate(time.Second) != unixMaxTime.Truncate(time.Second) { + t.Fatalf("Expected: %s, got: %s", unixMaxTime.Truncate(time.Second), aTime.Truncate(time.Second)) + } + }) }