Skip to content

Commit

Permalink
Tidy up integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 9, 2020
1 parent 024a86d commit 76aae4a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 66 deletions.
99 changes: 45 additions & 54 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ func TestChezmoi(t *testing.T) {
return false, fmt.Errorf("unknown condition: %s", cond)
}
},
Setup: func(env *testscript.Env) error {
switch runtime.GOOS {
case "windows":
return setupWindowsEnv(env)
default:
return setupPOSIXEnv(env)
}
},
Setup: setup,
})
}

Expand Down Expand Up @@ -79,15 +72,49 @@ func chHome(ts *testscript.TestScript, neg bool, args []string) {
}
}

func setupPOSIXEnv(env *testscript.Env) error {
binDir := filepath.Join(env.WorkDir, "bin")
env.Setenv("EDITOR", filepath.Join(binDir, "editor"))
env.Setenv("HOME", filepath.Join(env.WorkDir, "home", "user"))
func setup(env *testscript.Env) error {
var (
binDir = filepath.Join(env.WorkDir, "bin")
homeDir = filepath.Join(env.WorkDir, "home", "user")
)

env.Setenv("HOME", homeDir)
env.Setenv("PATH", prependDirToPath(binDir, env.Getenv("PATH")))
env.Setenv("SHELL", filepath.Join(binDir, "shell"))
switch runtime.GOOS {
case "windows":
env.Setenv("EDITOR", filepath.Join(binDir, "editor.cmd"))
env.Setenv("USERPROFILE", homeDir)
default:
env.Setenv("EDITOR", filepath.Join(binDir, "editor"))
env.Setenv("SHELL", filepath.Join(binDir, "shell"))
}

root := map[string]interface{}{
"/home/user": map[string]interface{}{
// .gitconfig is populated with a user and email to avoid warnings
// from git.
".gitconfig": strings.Join([]string{
`[user]`,
` name = Username`,
` email = user@home.org`,
}, "\n"),
},
}

return vfst.NewBuilder().Build(vfs.NewPathFS(vfs.HostOSFS, env.WorkDir), map[string]interface{}{
"/bin": map[string]interface{}{
switch runtime.GOOS {
case "windows":
root["/bin"] = map[string]interface{}{
// editor a non-interactive script that appends "# edited\n" to the
// end of each file.
"editor.cmd": &vfst.File{
Perm: 0o755,
Contents: []byte(`@for %%x in (%*) do echo # edited>>%%x`),
},
// The is not currently a convenient way to override the shell on
// Windows.
}
default:
root["/bin"] = map[string]interface{}{
// editor a non-interactive script that appends "# edited\n" to the
// end of each file.
"editor": &vfst.File{
Expand All @@ -110,46 +137,10 @@ func setupPOSIXEnv(env *testscript.Env) error {
`echo $PWD >> ` + filepath.Join(env.WorkDir, "shell.log"),
}, "\n")),
},
},
"/home/user": map[string]interface{}{
// .gitconfig is populated with a user and email to avoid warnings
// from git.
".gitconfig": strings.Join([]string{
`[user]`,
` name = Username`,
` email = user@home.org`,
}, "\n"),
},
})
}

// setupWindowsEnv sets up the testing environment for Windows. Works the same
// as on POSIX with the exception that there isn't currently a convenient way to
// override the shell, so that feature is skipped.
func setupWindowsEnv(env *testscript.Env) error {
binDir := filepath.Join(env.WorkDir, "bin")
env.Setenv("EDITOR", filepath.Join(binDir, "editor.cmd"))
env.Setenv("HOME", filepath.Join(env.WorkDir, "home", "user"))
env.Setenv("USERPROFILE", env.Getenv("HOME"))
env.Setenv("PATH", prependDirToPath(binDir, env.Getenv("PATH")))
}
}

return vfst.NewBuilder().Build(vfs.NewPathFS(vfs.HostOSFS, env.WorkDir), map[string]interface{}{
"/bin": map[string]interface{}{
"editor.cmd": &vfst.File{
Perm: 0o755,
Contents: []byte(`@for %%x in (%*) do echo # edited>>%%x`),
},
},
"/home/user": map[string]interface{}{
// .gitconfig is populated with a user and email to avoid warnings
// from git.
".gitconfig": strings.Join([]string{
`[user]`,
` name = Username`,
` email = user@home.org`,
}, "\n"),
},
})
return vfst.NewBuilder().Build(vfs.NewPathFS(vfs.HostOSFS, env.WorkDir), root)
}

func prependDirToPath(dir, path string) string {
Expand Down
4 changes: 2 additions & 2 deletions testdata/scripts/cd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ grep -count=1 ${CHEZMOI_HOME@R} shell.log

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

-- home2/user/.config/chezmoi/chezmoi.toml --
[cd]
command = "bash"
args = ["--version"]
args = ["--version"]
4 changes: 2 additions & 2 deletions testdata/scripts/editconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ chezmoi edit-config
grep -count=2 '# edited' $HOME${/}.config${/}chezmoi${/}chezmoi.toml

# test that edit-config edits an existing YAML config file
chhome $WORK${/}home2${/}user
chhome home2${/}user
chezmoi edit-config
grep -count=1 '# edited' $HOME${/}.config${/}chezmoi${/}chezmoi.yaml

# test that edit-config reports a warning if the config is no longer valid
chhome $WORK${/}home3${/}user
chhome home3${/}user
chezmoi verify
! stderr warning
chezmoi edit-config
Expand Down
6 changes: 3 additions & 3 deletions testdata/scripts/hg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ chezmoi hg -- commit -m 'Add dot_bashrc'
[windows] stop 'Backslash characters in file:// URLs confuse hg on Windows'

# test that chezmoi init clones a mercurial repo
chhome $WORK${/}home2${/}user
chhome home2${/}user
chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
exists $HOME${/}.local${/}share${/}chezmoi${/}.hg
cmp $HOME${/}.bashrc $WORK${/}home${/}user${/}.bashrc

# create another commit
chhome $WORK${/}home${/}user
chhome home${/}user
chezmoi edit $HOME${/}.bashrc
chezmoi hg -- add dot_bashrc
chezmoi hg -- commit -m 'Update dot_bashrc'

# test that chezmoi update pulls from a mercurial repo
chhome $WORK${/}home2${/}user
chhome home2${/}user
chezmoi update
grep '# edited' $HOME${/}.bashrc

Expand Down
4 changes: 2 additions & 2 deletions testdata/scripts/init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ chezmoi git add dot_bashrc
chezmoi git commit -- --message 'Add dot_bashrc'

# test that chezmoi init fetches git repo but does not apply
chhome $WORK${/}home2${/}user
chhome home2${/}user
chezmoi init file://$WORK/home/user/.local/share/chezmoi
exists $HOME${/}.local${/}share${/}chezmoi${/}.git
! exists $HOME${/}.bashrc

# test that chezmoi init --apply fetches a git repo and runs chezmoi apply
chhome $WORK${/}home3${/}user
chhome home3${/}user
chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
exists $HOME${/}.local${/}share${/}chezmoi${/}.git
grep '# contents of .bashrc' $HOME${/}.bashrc
Expand Down
6 changes: 3 additions & 3 deletions testdata/scripts/updategit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ chezmoi git -- add dot_bashrc
chezmoi git -- commit -m 'Add dot_bashrc'

# test chezmoi init --apply
chhome $WORK${/}home2${/}user
chhome home2${/}user
chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
cmp $HOME${/}.bashrc $WORK${/}home${/}user${/}.bashrc

# create a new commit
chhome $WORK${/}home${/}user
chhome home${/}user
chezmoi edit $HOME${/}.bashrc
chezmoi git -- add dot_bashrc
chezmoi git -- commit -m 'Update dot_bashrc'

# test chezmoi update
chhome $WORK${/}home2${/}user
chhome home2${/}user
chezmoi update
grep '# edited' $HOME${/}.bashrc

Expand Down

0 comments on commit 76aae4a

Please sign in to comment.