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
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)
}
}
}
......@@ -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";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment