Files
utils/file_test.go

137 lines
3.9 KiB
Go

// Copyright (C) 2025, Lux Partners Limited All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestExpandHome(t *testing.T) {
// Test case 1: Absolute path
absolutePath := "/tmp/testfile.txt"
expandedAbsolutePath := ExpandHome(absolutePath)
if expandedAbsolutePath != absolutePath {
t.Errorf("ExpandHome failed for absolute path: expected %s, got %s", absolutePath, expandedAbsolutePath)
}
// Test case 2: Relative path
relativePath := "testfile.txt"
expectedRelativePath := filepath.Join(".", relativePath)
expandedRelativePath := ExpandHome(relativePath)
if expandedRelativePath != expectedRelativePath {
t.Errorf("ExpandHome failed for relative path: expected %s, got %s", expectedRelativePath, expandedRelativePath)
}
// Test case 3: Path starting with ~
homeDir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Error getting user home directory: %v", err)
}
tildePath := "~/testfile.txt"
expectedTildePath := filepath.Join(homeDir, "testfile.txt")
expandedTildePath := ExpandHome(tildePath)
if expandedTildePath != expectedTildePath {
t.Errorf("ExpandHome failed for path starting with ~: expected %s, got %s", expectedTildePath, expandedTildePath)
}
// Test case 4: Empty path
emptyPath := ""
expectedEmptyPath := homeDir
expandedEmptyPath := ExpandHome(emptyPath)
if expandedEmptyPath != expectedEmptyPath {
t.Errorf("ExpandHome failed for empty path: expected %s, got %s", expectedEmptyPath, expandedEmptyPath)
}
}
func TestFileExists(t *testing.T) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "testfile")
require.NoError(t, err)
defer os.Remove(tempFile.Name())
// Test that the file exists
require.True(t, FileExists(tempFile.Name()))
// Test that a non-existent file does not exist
require.False(t, FileExists("non_existent_file.txt"))
}
func TestDirExists(t *testing.T) {
// Create a temporary directory
tempDir, err := os.MkdirTemp("", "testdir")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
// Test that the directory exists
require.True(t, DirExists(tempDir))
// Test that a non-existent directory does not exist
require.False(t, DirExists("non_existent_dir"))
}
func TestIsSubPath(t *testing.T) {
tests := []struct {
name string
childPath string
parentPath string
expected bool
}{
{
name: "child inside parent",
childPath: "/home/user/.lux/chains/zoo",
parentPath: "/home/user/.lux/chains",
expected: true,
},
{
name: "deeply nested child",
childPath: "/home/user/.lux/chains/zoo/config.json",
parentPath: "/home/user/.lux/chains",
expected: true,
},
{
name: "same path (not a subpath)",
childPath: "/home/user/.lux/chains",
parentPath: "/home/user/.lux/chains",
expected: false,
},
{
name: "child outside parent",
childPath: "/home/user/.lux/snapshots",
parentPath: "/home/user/.lux/chains",
expected: false,
},
{
name: "parent inside would-be child",
childPath: "/home/user/.lux",
parentPath: "/home/user/.lux/chains",
expected: false,
},
{
name: "sibling path",
childPath: "/home/user/.lux/plugins",
parentPath: "/home/user/.lux/chains",
expected: false,
},
{
name: "with trailing slash",
childPath: "/home/user/.lux/chains/zoo/",
parentPath: "/home/user/.lux/chains/",
expected: true,
},
{
name: "relative path traversal attempt",
childPath: "/home/user/.lux/chains/../keys",
parentPath: "/home/user/.lux/chains",
expected: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := IsSubPath(tc.childPath, tc.parentPath)
require.Equal(t, tc.expected, result, "IsSubPath(%q, %q) = %v, expected %v",
tc.childPath, tc.parentPath, result, tc.expected)
})
}
}