Skip to content
Snippets Groups Projects
Verified Commit e2701d8a authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: test should use temp (flake is readonly

parent 7d8ca14e
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ package watch ...@@ -5,6 +5,7 @@ package watch
import ( import (
"os" "os"
"path/filepath"
"testing" "testing"
"time" "time"
) )
...@@ -95,11 +96,10 @@ func TestFileHashing(t *testing.T) { ...@@ -95,11 +96,10 @@ func TestFileHashing(t *testing.T) {
t.Errorf("Expected Has to return true, got false") t.Errorf("Expected Has to return true, got false")
} }
} }
func TestAddAndRemoveFileHash(t *testing.T) { func TestAddAndRemoveFileHash(t *testing.T) {
// Test cases // Test cases
testCases := []struct { testCases := []struct {
path string fileName string
shouldExist bool shouldExist bool
sleepTime time.Duration sleepTime time.Duration
expectedErr error expectedErr error
...@@ -110,10 +110,18 @@ func TestAddAndRemoveFileHash(t *testing.T) { ...@@ -110,10 +110,18 @@ func TestAddAndRemoveFileHash(t *testing.T) {
{"testfile1.txt", true, 2 * time.Second, nil}, // Test expiration {"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 { for _, tc := range testCases {
if tc.shouldExist { if tc.shouldExist {
file, err := os.Create(tc.path) filePath := filepath.Join(tempDir, tc.fileName)
file, err := os.Create(filePath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -122,18 +130,12 @@ func TestAddAndRemoveFileHash(t *testing.T) { ...@@ -122,18 +130,12 @@ func TestAddAndRemoveFileHash(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
if tc.shouldExist { filePath := filepath.Join(tempDir, tc.fileName)
file, err := os.Create(tc.path)
if err != nil {
t.Fatal(err) // Stop the test if the file cannot be created
}
_ = file.Close()
}
//Add hash //Add hash
err := AddFileHash(tc.path) err := AddFileHash(filePath)
if err != tc.expectedErr { 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 { if tc.sleepTime > 0 {
...@@ -141,19 +143,14 @@ func TestAddAndRemoveFileHash(t *testing.T) { ...@@ -141,19 +143,14 @@ func TestAddAndRemoveFileHash(t *testing.T) {
} }
// Remove hash // Remove hash
err = RemoveFileHash(tc.path) err = RemoveFileHash(filePath)
if err != tc.expectedErr { 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 { if ok {
t.Errorf("Remove(%q) failed: value still exists in cache", tc.path) t.Errorf("Remove(%q) failed: value still exists in cache", filePath)
}
// Delete test files
if tc.shouldExist {
_ = os.Remove(tc.path)
} }
} }
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
# please don't edit it manually # please don't edit it manually
version = "0.4.0"; version = "0.4.0";
commit = "06935e5d8a7a9cae337241a19903c7a4bd149002"; commit = "7d8ca14e254c24b813596cbe516da7233f3ae83d";
projectURL = "https://gitlab.schukai.com/oss/libraries/go/utilities/watch"; projectURL = "https://gitlab.schukai.com/oss/libraries/go/utilities/watch";
name = "Watch"; name = "Watch";
mnemonic = "watch"; mnemonic = "watch";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment