diff --git a/hash_test.go b/hash_test.go index f1362cf60a446f173e84a1dcb886766311b455d0..7a75cbba599baa5de1800dd252a4aaba625486c0 100644 --- a/hash_test.go +++ b/hash_test.go @@ -5,6 +5,7 @@ package watch import ( "os" + "path/filepath" "testing" "time" ) @@ -95,11 +96,10 @@ func TestFileHashing(t *testing.T) { t.Errorf("Expected Has to return true, got false") } } - func TestAddAndRemoveFileHash(t *testing.T) { // Test cases testCases := []struct { - path string + fileName string shouldExist bool sleepTime time.Duration expectedErr error @@ -110,10 +110,18 @@ func TestAddAndRemoveFileHash(t *testing.T) { {"testfile1.txt", true, 2 * time.Second, nil}, // Test expiration } - // Create test files + // Create a temporary directory for test files + tempDir, err := os.MkdirTemp("", "testdir") + if err != nil { + t.Fatalf("Failed to create temp directory: %v", err) + } + defer os.RemoveAll(tempDir) // Clean up the temp directory at the end + + // Create test files in the temp directory for _, tc := range testCases { if tc.shouldExist { - file, err := os.Create(tc.path) + filePath := filepath.Join(tempDir, tc.fileName) + file, err := os.Create(filePath) if err != nil { t.Fatal(err) } @@ -122,18 +130,12 @@ func TestAddAndRemoveFileHash(t *testing.T) { } for _, tc := range testCases { - if tc.shouldExist { - file, err := os.Create(tc.path) - if err != nil { - t.Fatal(err) // Stop the test if the file cannot be created - } - _ = file.Close() - } + filePath := filepath.Join(tempDir, tc.fileName) //Add hash - err := AddFileHash(tc.path) + err := AddFileHash(filePath) if err != tc.expectedErr { - t.Errorf("Add(%q) error: got %v, want %v", tc.path, err, tc.expectedErr) + t.Errorf("Add(%q) error: got %v, want %v", filePath, err, tc.expectedErr) } if tc.sleepTime > 0 { @@ -141,19 +143,14 @@ func TestAddAndRemoveFileHash(t *testing.T) { } // Remove hash - err = RemoveFileHash(tc.path) + err = RemoveFileHash(filePath) if err != tc.expectedErr { - t.Errorf("Remove(%q) error: got %v, want %v", tc.path, err, tc.expectedErr) + t.Errorf("Remove(%q) error: got %v, want %v", filePath, err, tc.expectedErr) } - _, ok := watcherImpl.cache.Load(tc.path) + _, ok := watcherImpl.cache.Load(filePath) if ok { - t.Errorf("Remove(%q) failed: value still exists in cache", tc.path) - } - - // Delete test files - if tc.shouldExist { - _ = os.Remove(tc.path) + t.Errorf("Remove(%q) failed: value still exists in cache", filePath) } } } diff --git a/nix/config/release.nix b/nix/config/release.nix index c2b2676f011e95f4804f0c1cfe76cf14724cdf49..cb76205b0c15a61cff2f5fb116e8b0e9eb1c3d34 100644 --- a/nix/config/release.nix +++ b/nix/config/release.nix @@ -3,7 +3,7 @@ # please don't edit it manually version = "0.4.0"; - commit = "06935e5d8a7a9cae337241a19903c7a4bd149002"; + commit = "7d8ca14e254c24b813596cbe516da7233f3ae83d"; projectURL = "https://gitlab.schukai.com/oss/libraries/go/utilities/watch"; name = "Watch"; mnemonic = "watch";