mirror of
https://github.com/luxfi/utils.git
synced 2026-10-30 15:13:57 +00:00
Lux Partners Limited is not the entity behind this work. Every place the old name stood — copyright lines, site footers, brand config, legal and compliance text — now names the company that actually holds it. lux.fund is the exception and is not renamed to match: that site belongs to Lux Assets Management LLC, a different company, and it says so. Two kinds of line were left for a person rather than rewritten. A dated news item recording that Lux Partners Limited was established in the Isle of Man is a historical fact, and renaming it would falsify the record. And a sentence asserting that the company is domiciled in the Isle of Man is a claim about a jurisdiction, not a name — an Inc or an LLC is not an Isle of Man company, so swapping the name there would assert something untrue. Co-authored-by: Hanzo Dev <[email protected]>
137 lines
3.9 KiB
Go
137 lines
3.9 KiB
Go
// Copyright (C) 2025, Lux Industries Inc 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)
|
|
})
|
|
}
|
|
}
|