Fix tests path fragility
This commit is contained in:
@@ -3,6 +3,7 @@ package backer
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -16,24 +17,32 @@ func normalizePath(p string) string {
|
|||||||
|
|
||||||
// TestGetFilesFromDirectories checks if GetFilesFromDirectories returns the proper amount of strings, correct strings and no error.
|
// TestGetFilesFromDirectories checks if GetFilesFromDirectories returns the proper amount of strings, correct strings and no error.
|
||||||
func TestGetFilesFromDirectories(t *testing.T) {
|
func TestGetFilesFromDirectories(t *testing.T) {
|
||||||
// Ensure empty_dir exists for the test.
|
testDataPath := filepath.Join("..", "test_data")
|
||||||
os.MkdirAll("../../test_data/test1/foo/empty_dir", 0755)
|
|
||||||
|
// Log the paths we're checking for debugging
|
||||||
|
t.Logf("Checking directories: %v", []string{
|
||||||
|
filepath.Join(testDataPath, "test1", "foo"),
|
||||||
|
filepath.Join(testDataPath, "test1", "bar"),
|
||||||
|
})
|
||||||
|
|
||||||
var (
|
var (
|
||||||
input = Config{
|
input = Config{
|
||||||
Directories: []string{"../../test_data/test1/foo", "../../test_data/test1/bar"},
|
Directories: []string{
|
||||||
|
filepath.Join(testDataPath, "test1", "foo"),
|
||||||
|
filepath.Join(testDataPath, "test1", "bar"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
expectedData = []string{
|
expectedData = []string{
|
||||||
"../../test_data/test1/foo",
|
filepath.Join(testDataPath, "test1", "foo"),
|
||||||
"../../test_data/test1/foo/empty_dir",
|
filepath.Join(testDataPath, "test1", "foo", "empty_dir"),
|
||||||
"../../test_data/test1/foo/hello_breakfast.txt",
|
filepath.Join(testDataPath, "test1", "foo", "hello_breakfast.txt"),
|
||||||
"../../test_data/test1/foo/mydir",
|
filepath.Join(testDataPath, "test1", "foo", "mydir"),
|
||||||
"../../test_data/test1/foo/mydir/myfile.txt",
|
filepath.Join(testDataPath, "test1", "foo", "mydir", "myfile.txt"),
|
||||||
"../../test_data/test1/foo/some_text.txt",
|
filepath.Join(testDataPath, "test1", "foo", "some_text.txt"),
|
||||||
"../../test_data/test1/bar",
|
filepath.Join(testDataPath, "test1", "bar"),
|
||||||
"../../test_data/test1/bar/goodbye.txt",
|
filepath.Join(testDataPath, "test1", "bar", "goodbye.txt"),
|
||||||
"../../test_data/test1/bar/goodbye.txt/text.txt",
|
filepath.Join(testDataPath, "test1", "bar", "goodbye.txt", "text.txt"),
|
||||||
"../../test_data/test1/bar/hello.txt",
|
filepath.Join(testDataPath, "test1", "bar", "hello.txt"),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,7 +69,8 @@ func TestGetFilesFromDirectories(t *testing.T) {
|
|||||||
|
|
||||||
// TestGetFilesFromDirectoriesSingleDir tests with a single directory.
|
// TestGetFilesFromDirectoriesSingleDir tests with a single directory.
|
||||||
func TestGetFilesFromDirectoriesSingleDir(t *testing.T) {
|
func TestGetFilesFromDirectoriesSingleDir(t *testing.T) {
|
||||||
output, err := GetFilesFromDirectories(context.Background(), []string{"../../test_data/test1/foo"})
|
testDataPath := filepath.Join("test_data")
|
||||||
|
output, err := GetFilesFromDirectories(context.Background(), []string{filepath.Join(testDataPath, "test1", "foo")})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
@@ -71,7 +81,7 @@ func TestGetFilesFromDirectoriesSingleDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Should contain the directory itself
|
// Should contain the directory itself
|
||||||
if !slices.Contains(output, "../../test_data/test1/foo") {
|
if !slices.Contains(output, filepath.Join(testDataPath, "test1", "foo")) {
|
||||||
t.Error("Output should contain the root directory")
|
t.Error("Output should contain the root directory")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,7 +106,9 @@ func TestGetFilesFromDirectoriesEmptyDir(t *testing.T) {
|
|||||||
// TestGetFilesFromDirectoriesNonExistent tests with non-existent directory.
|
// TestGetFilesFromDirectoriesNonExistent tests with non-existent directory.
|
||||||
// Should log error but not fail - returns empty list.
|
// Should log error but not fail - returns empty list.
|
||||||
func TestGetFilesFromDirectoriesNonExistent(t *testing.T) {
|
func TestGetFilesFromDirectoriesNonExistent(t *testing.T) {
|
||||||
output, err := GetFilesFromDirectories(context.Background(), []string{"/nonexistent/path/that/does/not/exist"})
|
testDataPath := filepath.Join("test_data")
|
||||||
|
nonExistentPath := filepath.Join(testDataPath, "nonexistent", "path", "that", "does", "not", "exist")
|
||||||
|
output, err := GetFilesFromDirectories(context.Background(), []string{nonExistentPath})
|
||||||
|
|
||||||
// Should not return error, but logs error
|
// Should not return error, but logs error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -111,10 +123,11 @@ func TestGetFilesFromDirectoriesNonExistent(t *testing.T) {
|
|||||||
|
|
||||||
// TestGetFilesFromDirectoriesPartialFailure tests with mix of valid and invalid directories.
|
// TestGetFilesFromDirectoriesPartialFailure tests with mix of valid and invalid directories.
|
||||||
func TestGetFilesFromDirectoriesPartialFailure(t *testing.T) {
|
func TestGetFilesFromDirectoriesPartialFailure(t *testing.T) {
|
||||||
|
testDataPath := filepath.Join("test_data")
|
||||||
// Create a temp directory that exists
|
// Create a temp directory that exists
|
||||||
tmpDir := t.TempDir()
|
tmpDir := t.TempDir()
|
||||||
|
|
||||||
output, err := GetFilesFromDirectories(context.Background(), []string{tmpDir, "/nonexistent/path/that/does/not/exist"})
|
output, err := GetFilesFromDirectories(context.Background(), []string{tmpDir, filepath.Join(testDataPath, "nonexistent", "path", "that", "does", "not", "exist")})
|
||||||
|
|
||||||
// Should not return error, but logs error
|
// Should not return error, but logs error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -129,9 +142,10 @@ func TestGetFilesFromDirectoriesPartialFailure(t *testing.T) {
|
|||||||
|
|
||||||
// TestGetFilesFromDirectoriesMultipleDirs tests multiple directories.
|
// TestGetFilesFromDirectoriesMultipleDirs tests multiple directories.
|
||||||
func TestGetFilesFromDirectoriesMultipleDirs(t *testing.T) {
|
func TestGetFilesFromDirectoriesMultipleDirs(t *testing.T) {
|
||||||
|
testDataPath := filepath.Join("test_data")
|
||||||
output, err := GetFilesFromDirectories(context.Background(), []string{
|
output, err := GetFilesFromDirectories(context.Background(), []string{
|
||||||
"../../test_data/test1/foo",
|
filepath.Join(testDataPath, "test1", "foo"),
|
||||||
"../../test_data/test1/bar",
|
filepath.Join(testDataPath, "test1", "bar"),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -140,10 +154,10 @@ func TestGetFilesFromDirectoriesMultipleDirs(t *testing.T) {
|
|||||||
|
|
||||||
// Should contain entries from both directories
|
// Should contain entries from both directories
|
||||||
hasFoo := slices.ContainsFunc(output, func(s string) bool {
|
hasFoo := slices.ContainsFunc(output, func(s string) bool {
|
||||||
return s == "../../test_data/test1/foo" || s == "../../test_data/test1/foo/hello_breakfast.txt"
|
return s == filepath.Join(testDataPath, "test1", "foo") || s == filepath.Join(testDataPath, "test1", "foo", "hello_breakfast.txt")
|
||||||
})
|
})
|
||||||
hasBar := slices.ContainsFunc(output, func(s string) bool {
|
hasBar := slices.ContainsFunc(output, func(s string) bool {
|
||||||
return s == "../../test_data/test1/bar" || s == "../../test_data/test1/bar/hello.txt"
|
return s == filepath.Join(testDataPath, "test1", "bar") || s == filepath.Join(testDataPath, "test1", "bar", "hello.txt")
|
||||||
})
|
})
|
||||||
|
|
||||||
if !hasFoo {
|
if !hasFoo {
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package backer
|
package backer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -10,18 +12,46 @@ func TestLoadConfig(t *testing.T) {
|
|||||||
// Reset global config
|
// Reset global config
|
||||||
C = Config{}
|
C = Config{}
|
||||||
|
|
||||||
var (
|
testDataPath := filepath.Join(t.TempDir(), "test_data")
|
||||||
testConfigPath = "./../../test_data/test_config.json"
|
os.MkdirAll(filepath.Join(testDataPath, "test1", "foo"), 0755)
|
||||||
expectedData = Config{
|
os.MkdirAll(filepath.Join(testDataPath, "test1", "bar"), 0755)
|
||||||
Address: "127.0.0.1",
|
|
||||||
Port: 8086,
|
// Create test config file
|
||||||
Directories: []string{
|
testConfigPath := filepath.Join(testDataPath, "test_config.json")
|
||||||
"../../test_data/test1/foo",
|
testStruct := struct {
|
||||||
"../../test_data/test1/bar",
|
Address string `json:"address"`
|
||||||
},
|
Port int `json:"port"`
|
||||||
Location: "/archive",
|
Directories []string `json:"directories"`
|
||||||
}
|
Location string `json:"location"`
|
||||||
)
|
User string `json:"user"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
NoHTTPS bool `json:"nohttps"`
|
||||||
|
}{
|
||||||
|
Address: "127.0.0.1",
|
||||||
|
Port: 8086,
|
||||||
|
Directories: []string{filepath.Join(testDataPath, "test1", "foo"), filepath.Join(testDataPath, "test1", "bar")},
|
||||||
|
Location: "/archive",
|
||||||
|
User: "test",
|
||||||
|
Password: "test",
|
||||||
|
NoHTTPS: true,
|
||||||
|
}
|
||||||
|
testConfigBytes, _ := json.MarshalIndent(testStruct, "", " ")
|
||||||
|
if err := os.WriteFile(testConfigPath, testConfigBytes, 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedData := Config{
|
||||||
|
Address: "127.0.0.1",
|
||||||
|
Port: 8086,
|
||||||
|
Directories: []string{
|
||||||
|
filepath.Join(testDataPath, "test1", "foo"),
|
||||||
|
filepath.Join(testDataPath, "test1", "bar"),
|
||||||
|
},
|
||||||
|
Location: "/archive",
|
||||||
|
User: "test",
|
||||||
|
Password: "test",
|
||||||
|
NoHTTPS: true,
|
||||||
|
}
|
||||||
|
|
||||||
err := LoadConfig(testConfigPath)
|
err := LoadConfig(testConfigPath)
|
||||||
|
|
||||||
@@ -57,6 +87,8 @@ func TestLoadConfig(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigMissingUser tests that config fails when user is missing.
|
// TestLoadConfigMissingUser tests that config fails when user is missing.
|
||||||
func TestLoadConfigMissingUser(t *testing.T) {
|
func TestLoadConfigMissingUser(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 8086,
|
"port": 8086,
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -72,6 +104,8 @@ func TestLoadConfigMissingUser(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigMissingPassword tests that config fails when password is missing.
|
// TestLoadConfigMissingPassword tests that config fails when password is missing.
|
||||||
func TestLoadConfigMissingPassword(t *testing.T) {
|
func TestLoadConfigMissingPassword(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 8086,
|
"port": 8086,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
@@ -87,6 +121,8 @@ func TestLoadConfigMissingPassword(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigMissingDirectories tests that config fails when directories is missing.
|
// TestLoadConfigMissingDirectories tests that config fails when directories is missing.
|
||||||
func TestLoadConfigMissingDirectories(t *testing.T) {
|
func TestLoadConfigMissingDirectories(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 8086,
|
"port": 8086,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
@@ -102,6 +138,8 @@ func TestLoadConfigMissingDirectories(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigInvalidPort tests that config fails with invalid port.
|
// TestLoadConfigInvalidPort tests that config fails with invalid port.
|
||||||
func TestLoadConfigInvalidPort(t *testing.T) {
|
func TestLoadConfigInvalidPort(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 99999,
|
"port": 99999,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
@@ -118,6 +156,8 @@ func TestLoadConfigInvalidPort(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigNonExistentDirectory tests that config fails with non-existent directory.
|
// TestLoadConfigNonExistentDirectory tests that config fails with non-existent directory.
|
||||||
func TestLoadConfigNonExistentDirectory(t *testing.T) {
|
func TestLoadConfigNonExistentDirectory(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 8086,
|
"port": 8086,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
@@ -134,6 +174,8 @@ func TestLoadConfigNonExistentDirectory(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigHTTPSWithoutCert tests that config fails when HTTPS enabled but no cert.
|
// TestLoadConfigHTTPSWithoutCert tests that config fails when HTTPS enabled but no cert.
|
||||||
func TestLoadConfigHTTPSWithoutCert(t *testing.T) {
|
func TestLoadConfigHTTPSWithoutCert(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 8086,
|
"port": 8086,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
@@ -151,6 +193,8 @@ func TestLoadConfigHTTPSWithoutCert(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigNoHTTPS tests that config succeeds with nohttps=true and no cert.
|
// TestLoadConfigNoHTTPS tests that config succeeds with nohttps=true and no cert.
|
||||||
func TestLoadConfigNoHTTPS(t *testing.T) {
|
func TestLoadConfigNoHTTPS(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"port": 8086,
|
"port": 8086,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
@@ -172,6 +216,8 @@ func TestLoadConfigNoHTTPS(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigFilenamePrefixCustom tests custom filename prefix.
|
// TestLoadConfigFilenamePrefixCustom tests custom filename prefix.
|
||||||
func TestLoadConfigFilenamePrefixCustom(t *testing.T) {
|
func TestLoadConfigFilenamePrefixCustom(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -193,6 +239,8 @@ func TestLoadConfigFilenamePrefixCustom(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigDefaultValues tests default values are set correctly.
|
// TestLoadConfigDefaultValues tests default values are set correctly.
|
||||||
func TestLoadConfigDefaultValues(t *testing.T) {
|
func TestLoadConfigDefaultValues(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -234,6 +282,8 @@ func TestLoadConfigDefaultValues(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigCompressionAlgorithmBzip2 tests bzip2 compression algorithm.
|
// TestLoadConfigCompressionAlgorithmBzip2 tests bzip2 compression algorithm.
|
||||||
func TestLoadConfigCompressionAlgorithmBzip2(t *testing.T) {
|
func TestLoadConfigCompressionAlgorithmBzip2(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -255,6 +305,8 @@ func TestLoadConfigCompressionAlgorithmBzip2(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigCompressionAlgorithmZstd tests zstd compression algorithm.
|
// TestLoadConfigCompressionAlgorithmZstd tests zstd compression algorithm.
|
||||||
func TestLoadConfigCompressionAlgorithmZstd(t *testing.T) {
|
func TestLoadConfigCompressionAlgorithmZstd(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -276,6 +328,8 @@ func TestLoadConfigCompressionAlgorithmZstd(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigCompressionAlgorithmLz4 tests lz4 compression algorithm.
|
// TestLoadConfigCompressionAlgorithmLz4 tests lz4 compression algorithm.
|
||||||
func TestLoadConfigCompressionAlgorithmLz4(t *testing.T) {
|
func TestLoadConfigCompressionAlgorithmLz4(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -297,6 +351,8 @@ func TestLoadConfigCompressionAlgorithmLz4(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigCompressionAlgorithmXz tests xz compression algorithm.
|
// TestLoadConfigCompressionAlgorithmXz tests xz compression algorithm.
|
||||||
func TestLoadConfigCompressionAlgorithmXz(t *testing.T) {
|
func TestLoadConfigCompressionAlgorithmXz(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -318,6 +374,8 @@ func TestLoadConfigCompressionAlgorithmXz(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigCompressionAlgorithmPgzip tests pgzip compression algorithm.
|
// TestLoadConfigCompressionAlgorithmPgzip tests pgzip compression algorithm.
|
||||||
func TestLoadConfigCompressionAlgorithmPgzip(t *testing.T) {
|
func TestLoadConfigCompressionAlgorithmPgzip(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -339,6 +397,8 @@ func TestLoadConfigCompressionAlgorithmPgzip(t *testing.T) {
|
|||||||
|
|
||||||
// TestLoadConfigCompressionAlgorithmInvalid tests invalid compression algorithm.
|
// TestLoadConfigCompressionAlgorithmInvalid tests invalid compression algorithm.
|
||||||
func TestLoadConfigCompressionAlgorithmInvalid(t *testing.T) {
|
func TestLoadConfigCompressionAlgorithmInvalid(t *testing.T) {
|
||||||
|
// Reset global config
|
||||||
|
C = Config{}
|
||||||
config := `{
|
config := `{
|
||||||
"user": "test",
|
"user": "test",
|
||||||
"password": "test",
|
"password": "test",
|
||||||
@@ -367,7 +427,6 @@ func writeTempConfig(t *testing.T, content string) string {
|
|||||||
tmpFile.Close()
|
tmpFile.Close()
|
||||||
// Reset global config before loading
|
// Reset global config before loading
|
||||||
C = Config{}
|
C = Config{}
|
||||||
excludePatterns = nil
|
|
||||||
return tmpFile.Name()
|
return tmpFile.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -351,7 +351,7 @@ func TestHandlerArchiveStream(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
files, err := GetFilesFromDirectories(C.Directories)
|
files, err := GetFilesFromDirectories(ctx, C.Directories)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to get files", http.StatusInternalServerError)
|
http.Error(w, "Failed to get files", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user