Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more integration tests #747

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestArchiveCmd(t *testing.T) {

h, err := r.Next()
assert.NoError(t, err)
assert.Equal(t, "dir", h.Name)
assert.Equal(t, "dir/", h.Name)

h, err = r.Next()
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Config) runManagedCmd(cmd *cobra.Command, args []string) error {
if ts.TargetIgnore.Match(targetName) {
continue
}
fmt.Fprintln(c.Stdout, filepath.Join(ts.DestDir, targetName))
fmt.Fprintln(c.Stdout, filepath.FromSlash(filepath.Join(ts.DestDir, targetName)))
}

return nil
Expand Down
17 changes: 5 additions & 12 deletions cmd/managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,26 @@ func TestManagedCmd(t *testing.T) {
}),
)
assert.NoError(t, c.runManagedCmd(nil, nil))
posixTargetNames, err := extractPOSIXTargetNames(stdout.Bytes())
require.NoError(t, err)
assert.Equal(t, tc.expectedTargetNames, posixTargetNames)
actualTargetNames, err := extractTargetNames(stdout.Bytes())
require.NoError(t, err)
assert.Equal(t, tc.expectedTargetNames, actualTargetNames)
})
}
}

// extractPOSIXTargetNames extracts all target names from b and coverts them to
// POSIX-like names.
func extractPOSIXTargetNames(b []byte) ([]string, error) {
func extractTargetNames(b []byte) ([]string, error) {
var targetNames []string
s := bufio.NewScanner(bytes.NewBuffer(b))
for s.Scan() {
targetNames = append(targetNames, posixify(s.Text()))
targetNames = append(targetNames, filepath.ToSlash(s.Text()))
}
if err := s.Err(); err != nil {
return nil, err
}
return targetNames, nil
}

// posixify returns a POSIX-like path based on path, stripping any volume name
// and converting backward slashes.
func posixify(path string) string {
return filepath.ToSlash(strings.TrimPrefix(path, filepath.VolumeName(path)))
}

func withManaged(managed managedCmdConfig) configOption {
return func(c *Config) {
c.managed = managed
Expand Down
2 changes: 1 addition & 1 deletion cmd/sourcepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Config) runSourcePathCmd(cmd *cobra.Command, args []string) error {
return err
}
for _, entry := range entries {
if _, err := fmt.Println(filepath.Join(ts.SourceDir, entry.SourceName())); err != nil {
if _, err := fmt.Println(filepath.FromSlash(filepath.Join(ts.SourceDir, entry.SourceName()))); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/chezmoi/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (d *Dir) archive(w *tar.Writer, ignore func(string) bool, headerTemplate *t
}
header := *headerTemplate
header.Typeflag = tar.TypeDir
header.Name = d.targetName
header.Name = d.targetName + "/"
header.Mode = int64(d.Perm &^ umask)
if err := w.WriteHeader(&header); err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestChezmoi(t *testing.T) {
return false, fmt.Errorf("unknown condition: %s", cond)
}
},
Setup: setup,
Setup: setup,
UpdateScripts: os.Getenv("CHEZMOIUPDATESCRIPTS") != "",
})
}

Expand Down Expand Up @@ -69,6 +70,7 @@ func chHome(ts *testscript.TestScript, neg bool, args []string) {
}
ts.Check(os.MkdirAll(homeDir, 0o777))
ts.Setenv("HOME", homeDir)
ts.Setenv("HOMESLASH", filepath.ToSlash(homeDir))
if runtime.GOOS == "windows" {
ts.Setenv("USERPROFILE", homeDir)
}
Expand Down Expand Up @@ -102,18 +104,21 @@ func setup(env *testscript.Env) error {
)

env.Setenv("HOME", homeDir)
env.Setenv("HOMESLASH", filepath.ToSlash(homeDir))
env.Setenv("PATH", prependDirToPath(binDir, env.Getenv("PATH")))
env.Setenv("CHEZMOICONFIGDIR", chezmoiConfigDir)
env.Setenv("CHEZMOISOURCEDIR", chezmoiSourceDir)
switch runtime.GOOS {
case "windows":
env.Setenv("EDITOR", filepath.Join(binDir, "editor.cmd"))
env.Setenv("USERPROFILE", homeDir)
env.Setenv("WORKSLASH", filepath.ToSlash(env.WorkDir))
// There is not currently a convenient way to override the shell on
// Windows.
default:
env.Setenv("EDITOR", filepath.Join(binDir, "editor"))
env.Setenv("SHELL", filepath.Join(binDir, "shell"))
env.Setenv("WORKSLASH", env.WorkDir)
}

// Fix permissions on the source directory, if it exists.
Expand Down
8 changes: 4 additions & 4 deletions testdata/scripts/applyremove.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exists $HOME${/}.bashrc
exists $HOME${/}.inputrc
exists $HOME/.bashrc
exists $HOME/.inputrc
chezmoi apply --remove
! exists $HOME${/}.bashrc
exists $HOME${/}.inputrc
! exists $HOME/.bashrc
exists $HOME/.inputrc

-- home/user/.bashrc --
# contents of .bashrc
Expand Down
8 changes: 4 additions & 4 deletions testdata/scripts/archive.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[windows] stop 'https://github.com/twpayne/chezmoi/issues/745'
[!exec:tar] stop

chezmoi archive --output=user.tar
exec tar -tf user.tar
chezmoi archive --output=archive.tar
exec tar -tf archive.tar
cmp stdout golden/archive

-- golden/archive --
.bashrc
.ssh
.ssh/
.ssh/config
-- home/user/.local/share/chezmoi/dot_bashrc --
# contents of .bashrc
-- home/user/.local/share/chezmoi/private_dot_ssh/config --
# contents .ssh/config
# contents of .ssh/config
26 changes: 25 additions & 1 deletion testdata/scripts/cat.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
chezmoi cat $HOME${/}.bashrc
stdout '# contents of .bashrc'
cmp stdout ${CHEZMOISOURCEDIR}/dot_bashrc

chezmoi cat $HOME${/}.gitconfig
cmp stdout golden/.gitconfig

chezmoi cat $HOME${/}.symlink
stdout 'target'

! chezmoi cat $HOME${/}.ssh
stdout 'not a file or symlink'

-- golden/.gitconfig --
[user]
email = user@home.org
-- home/user/.bashrc --
# contents of .bashrc
-- home/user/.config/chezmoi/chezmoi.toml --
[data]
email = "user@home.org"
-- home/user/.local/share/chezmoi/dot_bashrc --
# contents of .bashrc
-- home/user/.local/share/chezmoi/dot_gitconfig.tmpl --
[user]
email = {{ .email }}
-- home/user/.local/share/chezmoi/private_dot_ssh/config --
# contents of .ssh/config
-- home/user/.local/share/chezmoi/symlink_dot_symlink --
target
2 changes: 1 addition & 1 deletion testdata/scripts/cd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ grep -count=1 ${CHEZMOISOURCEDIR@R} shell.log

# test chezmoi cd with command with args
[!exec:bash] stop
chhome home2${/}user
chhome home2/user
chezmoi cd
stdout version

Expand Down
12 changes: 6 additions & 6 deletions testdata/scripts/chattr.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
chezmoi chattr +empty $HOME${/}.bashrc
exists ${CHEZMOISOURCEDIR}${/}empty_dot_bashrc
exists ${CHEZMOISOURCEDIR}/empty_dot_bashrc

chezmoi chattr +template $HOME${/}.bashrc
exists ${CHEZMOISOURCEDIR}${/}empty_dot_bashrc.tmpl
exists ${CHEZMOISOURCEDIR}/empty_dot_bashrc.tmpl

chezmoi chattr +private $HOME${/}.bashrc
exists ${CHEZMOISOURCEDIR}${/}private_empty_dot_bashrc.tmpl
exists ${CHEZMOISOURCEDIR}/private_empty_dot_bashrc.tmpl

chezmoi chattr noempty $HOME${/}.bashrc
exists ${CHEZMOISOURCEDIR}${/}private_dot_bashrc.tmpl
exists ${CHEZMOISOURCEDIR}/private_dot_bashrc.tmpl

chezmoi chattr -- -template $HOME${/}.bashrc
exists ${CHEZMOISOURCEDIR}${/}private_dot_bashrc
exists ${CHEZMOISOURCEDIR}/private_dot_bashrc

chezmoi chattr nop $HOME${/}.bashrc
exists ${CHEZMOISOURCEDIR}${/}dot_bashrc
exists ${CHEZMOISOURCEDIR}/dot_bashrc

-- home/user/.local/share/chezmoi/dot_bashrc --
# contents of .bashrc
11 changes: 11 additions & 0 deletions testdata/scripts/completion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
chezmoi completion bash
stdout '# bash completion for chezmoi'

chezmoi completion fish
stdout '# fish completion for chezmoi'

chezmoi completion zsh
stdout '#compdef _chezmoi chezmoi'

! chezmoi completion ksh
stdout 'unsupported shell'
11 changes: 11 additions & 0 deletions testdata/scripts/data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
chezmoi data
stdout '"chezmoi":'

chezmoi data --format=json
stdout '"chezmoi":'

chezmoi data --format=toml
stdout '[chezmoi]'

chezmoi data --format=yaml
stdout 'chezmoi:'
85 changes: 85 additions & 0 deletions testdata/scripts/diff.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
chmod 700 $HOME/.ssh

# test that chezmoi diff generates no output when the source and destination states are equal
chezmoi diff
! stdout .

# test that chezmoi diff generates a diff when a file is added to the source state
cp golden/dot_inputrc ${CHEZMOISOURCEDIR}/dot_inputrc
chezmoi diff
cmpenv stdout golden/add-file-diff
rm ${CHEZMOISOURCEDIR}/dot_inputrc

# test that chezmoi diff generates a diff when a file is edited
edit $HOME/.bashrc
chezmoi diff
cmpenv stdout golden/modify-file-diff
chezmoi apply

# test that chezmoi diff generates a diff when a file is removed from the destination directory
rm $HOME/.bashrc
chezmoi diff
cmpenv stdout golden/remove-file-diff
chezmoi apply

# test that chezmoi diff generates a diff when a directory is removed from the destination directory
rm $HOME/.ssh
chezmoi diff
cmpenv stdout golden/remove-dir-diff
chezmoi apply

[windows] stop 'remaining tests use POSIX file modes'

# test that chezmoi diff generates a diff when a file's permissions are changed
chmod 755 $HOME/.bashrc
chezmoi diff
cmpenv stdout golden/chmod-file-diff
chezmoi apply

# test that chezmoi diff generates a diff when a dir's permissions are changed
# FIXME show changes to permissions in diff
chmod 755 $HOME/.ssh
chezmoi diff
cmpenv stdout golden/chmod-dir-diff
chezmoi apply

-- golden/add-file-diff --
install -m 644 /dev/null $WORK/home/user/.inputrc
--- a$WORK/home/user/.inputrc
+++ b$WORK/home/user/.inputrc
@@ -0,0 +1,1 @@
+# contents of .inputrc
-- golden/modify-file-diff --
install -m 644 /dev/null $WORK/home/user/.bashrc
--- a$WORK/home/user/.bashrc
+++ b$WORK/home/user/.bashrc
@@ -1,2 +1,1 @@
# contents of .bashrc
-# edited
-- golden/remove-file-diff --
install -m 644 /dev/null $WORK/home/user/.bashrc
--- a$WORK/home/user/.bashrc
+++ b$WORK/home/user/.bashrc
@@ -0,0 +1,1 @@
+# contents of .bashrc
-- golden/remove-dir-diff --
mkdir -m 700 $WORK/home/user/.ssh
install -m 644 /dev/null $WORK/home/user/.ssh/config
--- a$WORK/home/user/.ssh/config
+++ b$WORK/home/user/.ssh/config
@@ -0,0 +1,1 @@
+# contents of .ssh/config
-- golden/chmod-file-diff --
chmod 644 $WORK/home/user/.bashrc
-- golden/chmod-dir-diff --
chmod 700 $WORK/home/user/.ssh
-- golden/dot_inputrc --
# contents of .inputrc
-- home/user/.bashrc --
# contents of .bashrc
-- home/user/.ssh/config --
# contents of .ssh/config
-- home/user/.local/share/chezmoi/dot_bashrc --
# contents of .bashrc
-- home/user/.local/share/chezmoi/private_dot_ssh/config --
# contents of .ssh/config
Loading