From e2701d8a1f9e5bfc056609bbc345f0adac14a65d Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Sat, 28 Sep 2024 20:31:54 +0200
Subject: [PATCH] fix: test should use temp (flake is readonly

---
 hash_test.go           | 41 +++++++++++++++++++----------------------
 nix/config/release.nix |  2 +-
 2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/hash_test.go b/hash_test.go
index f1362cf..7a75cbb 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 c2b2676..cb76205 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";
-- 
GitLab