|  | 
|  | 1 | +package pathutil | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"os" | 
|  | 5 | +	"path/filepath" | 
|  | 6 | +	"testing" | 
|  | 7 | +) | 
|  | 8 | + | 
|  | 9 | +func TestExpandTilde(t *testing.T) { | 
|  | 10 | +	// Get current user's home directory for testing | 
|  | 11 | +	home, err := os.UserHomeDir() | 
|  | 12 | +	if err != nil { | 
|  | 13 | +		t.Fatalf("Failed to get home directory: %v", err) | 
|  | 14 | +	} | 
|  | 15 | + | 
|  | 16 | +	tests := []struct { | 
|  | 17 | +		name     string | 
|  | 18 | +		input    string | 
|  | 19 | +		expected string | 
|  | 20 | +	}{ | 
|  | 21 | +		{ | 
|  | 22 | +			name:     "no tilde", | 
|  | 23 | +			input:    "/absolute/path", | 
|  | 24 | +			expected: "/absolute/path", | 
|  | 25 | +		}, | 
|  | 26 | +		{ | 
|  | 27 | +			name:     "relative path no tilde", | 
|  | 28 | +			input:    "relative/path", | 
|  | 29 | +			expected: "relative/path", | 
|  | 30 | +		}, | 
|  | 31 | +		{ | 
|  | 32 | +			name:     "just tilde", | 
|  | 33 | +			input:    "~", | 
|  | 34 | +			expected: home, | 
|  | 35 | +		}, | 
|  | 36 | +		{ | 
|  | 37 | +			name:     "tilde with path", | 
|  | 38 | +			input:    "~/projects/test", | 
|  | 39 | +			expected: filepath.Join(home, "projects/test"), | 
|  | 40 | +		}, | 
|  | 41 | +		{ | 
|  | 42 | +			name:     "tilde with dotfile", | 
|  | 43 | +			input:    "~/.npmrc", | 
|  | 44 | +			expected: filepath.Join(home, ".npmrc"), | 
|  | 45 | +		}, | 
|  | 46 | +		{ | 
|  | 47 | +			name:     "invalid username", | 
|  | 48 | +			input:    "~nonexistentuser99999/path", | 
|  | 49 | +			expected: "~nonexistentuser99999/path", // Should return original | 
|  | 50 | +		}, | 
|  | 51 | +		{ | 
|  | 52 | +			name:     "invalid username no slash", | 
|  | 53 | +			input:    "~nonexistentuser99999", | 
|  | 54 | +			expected: "~nonexistentuser99999", // Should return original | 
|  | 55 | +		}, | 
|  | 56 | +		{ | 
|  | 57 | +			name:     "empty path", | 
|  | 58 | +			input:    "", | 
|  | 59 | +			expected: "", | 
|  | 60 | +		}, | 
|  | 61 | +		{ | 
|  | 62 | +			name:     "special prefixes not affected", | 
|  | 63 | +			input:    "docker-image://something", | 
|  | 64 | +			expected: "docker-image://something", | 
|  | 65 | +		}, | 
|  | 66 | +		{ | 
|  | 67 | +			name:     "git url not affected", | 
|  | 68 | +			input:    "[email protected]:user/repo.git" , | 
|  | 69 | +			expected: "[email protected]:user/repo.git" , | 
|  | 70 | +		}, | 
|  | 71 | +	} | 
|  | 72 | + | 
|  | 73 | +	for _, tt := range tests { | 
|  | 74 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 75 | +			result := ExpandTilde(tt.input) | 
|  | 76 | +			if result != tt.expected { | 
|  | 77 | +				t.Errorf("ExpandTilde(%q) = %q, want %q", tt.input, result, tt.expected) | 
|  | 78 | +			} | 
|  | 79 | +		}) | 
|  | 80 | +	} | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +func TestExpandTildePaths(t *testing.T) { | 
|  | 84 | +	home, err := os.UserHomeDir() | 
|  | 85 | +	if err != nil { | 
|  | 86 | +		t.Fatalf("Failed to get home directory: %v", err) | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	tests := []struct { | 
|  | 90 | +		name     string | 
|  | 91 | +		input    []string | 
|  | 92 | +		expected []string | 
|  | 93 | +	}{ | 
|  | 94 | +		{ | 
|  | 95 | +			name:     "nil input", | 
|  | 96 | +			input:    nil, | 
|  | 97 | +			expected: nil, | 
|  | 98 | +		}, | 
|  | 99 | +		{ | 
|  | 100 | +			name:     "empty slice", | 
|  | 101 | +			input:    []string{}, | 
|  | 102 | +			expected: []string{}, | 
|  | 103 | +		}, | 
|  | 104 | +		{ | 
|  | 105 | +			name:     "mixed paths", | 
|  | 106 | +			input:    []string{"~/path1", "/absolute/path", "relative/path", "~/.ssh/id_rsa"}, | 
|  | 107 | +			expected: []string{filepath.Join(home, "path1"), "/absolute/path", "relative/path", filepath.Join(home, ".ssh/id_rsa")}, | 
|  | 108 | +		}, | 
|  | 109 | +		{ | 
|  | 110 | +			name:     "all tildes", | 
|  | 111 | +			input:    []string{"~/a", "~/b", "~/c"}, | 
|  | 112 | +			expected: []string{filepath.Join(home, "a"), filepath.Join(home, "b"), filepath.Join(home, "c")}, | 
|  | 113 | +		}, | 
|  | 114 | +		{ | 
|  | 115 | +			name:     "with invalid usernames", | 
|  | 116 | +			input:    []string{"~/valid", "~invaliduser/path", "~/another"}, | 
|  | 117 | +			expected: []string{filepath.Join(home, "valid"), "~invaliduser/path", filepath.Join(home, "another")}, | 
|  | 118 | +		}, | 
|  | 119 | +	} | 
|  | 120 | + | 
|  | 121 | +	for _, tt := range tests { | 
|  | 122 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 123 | +			result := ExpandTildePaths(tt.input) | 
|  | 124 | +			if len(result) != len(tt.expected) { | 
|  | 125 | +				t.Errorf("ExpandTildePaths(%v) returned %d items, want %d", tt.input, len(result), len(tt.expected)) | 
|  | 126 | +				return | 
|  | 127 | +			} | 
|  | 128 | +			for i := range result { | 
|  | 129 | +				if result[i] != tt.expected[i] { | 
|  | 130 | +					t.Errorf("ExpandTildePaths[%d] = %q, want %q", i, result[i], tt.expected[i]) | 
|  | 131 | +				} | 
|  | 132 | +			} | 
|  | 133 | +		}) | 
|  | 134 | +	} | 
|  | 135 | +} | 
0 commit comments