Skip to content

Commit

Permalink
snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 19, 2020
1 parent fd7fe71 commit 176d367
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 74 deletions.
4 changes: 2 additions & 2 deletions next/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,8 @@ func (c *Config) init(rootCmd *cobra.Command) error {
}

func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error {
c.SourceDir = makeCleanAbsSlashPath(c.workingDir, c.SourceDir)
c.DestDir = makeCleanAbsSlashPath(c.workingDir, c.DestDir)
c.SourceDir = chezmoi.PathJoin(c.workingDir, c.SourceDir)
c.DestDir = chezmoi.PathJoin(c.workingDir, c.DestDir)

if !getBoolAnnotation(cmd, doesNotRequireValidConfig) {
if c.err != nil {
Expand Down
10 changes: 0 additions & 10 deletions next/cmd/util_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package cmd

import (
"io"
"path"
"syscall"
)

Expand All @@ -21,15 +20,6 @@ func getUmask() int {
return umask
}

// makeCleanAbsSlashPath returns a clean, absolute path separated with forward
// slashes. If file is not an absolute path then it is joined on to dir.
func makeCleanAbsSlashPath(dir, file string) string {
if !path.IsAbs(file) {
file = path.Join(dir, file)
}
return path.Clean(file)
}

func trimExecutableSuffix(s string) string {
return s
}
46 changes: 0 additions & 46 deletions next/cmd/util_test.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
package cmd

import (
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMakeCleanAbsSlashPath(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, makeCleanAbsSlashPath(tc.dir, tc.file))
})
}
}

func TestUpperSnakeCaseToCamelCaseMap(t *testing.T) {
actual := upperSnakeCaseToCamelCaseMap(map[string]string{
"BUG_REPORT_URL": "",
Expand Down
9 changes: 0 additions & 9 deletions next/cmd/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ func getUmask() int {
return 0
}

// makeCleanAbsSlashPath returns a clean, absolute path separated with forward
// slashes. If file is not an absolute path then it is joined on to dir.
func makeCleanAbsSlashPath(dir, file string) string {
if !filepath.IsAbs(file) {
file = filepath.Join(dir, file)
}
return filepath.ToSlash(filepath.Clean(file))
}

func trimExecutableSuffix(s string) string {
if strings.EqualFold(filepath.Ext(s), ".exe") {
return s[:len(s)-4]
Expand Down
21 changes: 21 additions & 0 deletions next/internal/chezmoi/path_posix.go
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
}
53 changes: 53 additions & 0 deletions next/internal/chezmoi/path_test.go
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))
})
}
}
23 changes: 23 additions & 0 deletions next/internal/chezmoi/path_windows.go
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
}
2 changes: 1 addition & 1 deletion next/internal/chezmoi/realsystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestRealSystemGlob(t *testing.T) {
actualMatches, err := s.Glob(tc.pattern)
require.NoError(t, err)
sort.Strings(actualMatches)
assert.Equal(t, tc.expectedMatches, actualMatches)
assert.Equal(t, tc.expectedMatches, PathsToSlashes(actualMatches))
})
}
}
Expand Down
13 changes: 7 additions & 6 deletions next/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/twpayne/go-vfs/vfst"

"github.com/twpayne/chezmoi/next/cmd"
"github.com/twpayne/chezmoi/next/internal/chezmoi"
)

//nolint:interfacer
Expand Down Expand Up @@ -76,8 +77,8 @@ func cmdChHome(ts *testscript.TestScript, neg bool, args []string) {
}
var (
homeDir = ts.MkAbs(args[0])
chezmoiConfigDir = filepath.Join(homeDir, ".config", "chezmoi")
chezmoiSourceDir = filepath.Join(homeDir, ".local", "share", "chezmoi")
chezmoiConfigDir = chezmoi.PathJoin(homeDir, ".config/chezmoi")
chezmoiSourceDir = chezmoi.PathJoin(homeDir, ".local/share/chezmoi")
)
ts.Check(os.MkdirAll(homeDir, 0o777))
ts.Setenv("HOME", homeDir)
Expand Down Expand Up @@ -225,10 +226,10 @@ func cmdMkSourceDir(ts *testscript.TestScript, neg bool, args []string) {

func setup(env *testscript.Env) error {
var (
binDir = filepath.Join(env.WorkDir, "bin")
homeDir = filepath.Join(env.WorkDir, "home", "user")
chezmoiConfigDir = filepath.Join(homeDir, ".config", "chezmoi")
chezmoiSourceDir = filepath.Join(homeDir, ".local", "share", "chezmoi")
binDir = chezmoi.PathJoin(env.WorkDir, "bin")
homeDir = chezmoi.PathJoin(env.WorkDir, "home/user")
chezmoiConfigDir = chezmoi.PathJoin(homeDir, ".config/chezmoi")
chezmoiSourceDir = chezmoi.PathJoin(homeDir, ".local/share/chezmoi")
)

env.Setenv("HOME", homeDir)
Expand Down

0 comments on commit 176d367

Please sign in to comment.