mirror of
https://github.com/vcscsvcscs/GenerationsHeritage.git
synced 2025-08-13 14:29:05 +02:00
Add fileExists utility function
This commit is contained in:
11
backend/utilities/fileExists.go
Normal file
11
backend/utilities/fileExists.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// Checks if file on path exists or not
|
||||
func FileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
27
backend/utilities/fileExists_test.go
Normal file
27
backend/utilities/fileExists_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
// Create a temporary file for testing
|
||||
file, err := os.CreateTemp("", "testfile")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temporary file: %v", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
|
||||
// Test with an existing file
|
||||
exists := FileExists(file.Name())
|
||||
if !exists {
|
||||
t.Errorf("FileExists(%q) = false, want true", file.Name())
|
||||
}
|
||||
|
||||
// Test with a non-existing file
|
||||
exists = FileExists("non_existing_file.txt")
|
||||
if exists {
|
||||
t.Errorf("FileExists(%q) = true, want false", "non_existing_file.txt")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user