-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
107 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// +build !windows | ||
|
||
package chezmoi | ||
|
||
import ( | ||
"path" | ||
) | ||
|
||
// PathJoin returns a clean, absolute path. If file is not an absolute path then | ||
// it is joined on to dir. | ||
func PathJoin(dir, file string) string { | ||
if !path.IsAbs(file) { | ||
file = path.Join(dir, file) | ||
} | ||
return path.Clean(file) | ||
} | ||
|
||
// PathsToSlashes returns paths. | ||
func PathsToSlashes(paths []string) []string { | ||
return paths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPathJoin(t *testing.T) { | ||
type testCase struct { | ||
dir string | ||
file string | ||
expected string | ||
} | ||
testCases := []testCase{ | ||
{ | ||
dir: "/home/user", | ||
file: "file", | ||
expected: "/home/user/file", | ||
}, | ||
{ | ||
dir: "/home/user", | ||
file: "/tmp/file", | ||
expected: "/tmp/file", | ||
}, | ||
} | ||
if runtime.GOOS == "windows" { | ||
testCases = append(testCases, | ||
testCase{ | ||
dir: `C:\Users\user`, | ||
file: "file", | ||
expected: `C:/Users/user/file`, | ||
}, | ||
testCase{ | ||
dir: `C:\Users\user`, | ||
file: `dir/file`, | ||
expected: `C:/Users/user/dir/file`, | ||
}, | ||
testCase{ | ||
dir: `C:\Users\user`, | ||
file: `D:\Users\user\file`, | ||
expected: `D:/Users/user/file`, | ||
}, | ||
) | ||
} | ||
for _, tc := range testCases { | ||
t.Run(strings.Join([]string{tc.dir, tc.file}, "_"), func(t *testing.T) { | ||
assert.Equal(t, tc.expected, PathJoin(tc.dir, tc.file)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"path/filepath" | ||
) | ||
|
||
// PathJoin returns a clean, absolute path separated with forward | ||
// slashes. If file is not an absolute path then it is joined on to dir. | ||
func PathJoin(dir, file string) string { | ||
if !filepath.IsAbs(file) { | ||
file = filepath.Join(dir, file) | ||
} | ||
return filepath.ToSlash(filepath.Clean(file)) | ||
} | ||
|
||
// PathsToSlashes returns a copy of paths with filepath.ToSlash to each element. | ||
func PathsToSlashes(paths []string) []string { | ||
result := make([]string, 0, len(paths)) | ||
for _, path := range paths { | ||
result = append(result, filepath.ToSlash(path)) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters