From 76aae4a2b7e7a2b89ac3bdc36f2eb3abafe9d678 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sat, 9 May 2020 21:07:23 +0100 Subject: [PATCH] Tidy up integration tests --- main_test.go | 99 +++++++++++++++------------------ testdata/scripts/cd.txt | 4 +- testdata/scripts/editconfig.txt | 4 +- testdata/scripts/hg.txt | 6 +- testdata/scripts/init.txt | 4 +- testdata/scripts/updategit.txt | 6 +- 6 files changed, 57 insertions(+), 66 deletions(-) diff --git a/main_test.go b/main_test.go index 2ebb4463341..282b8fb491c 100644 --- a/main_test.go +++ b/main_test.go @@ -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, }) } @@ -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{ @@ -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 { diff --git a/testdata/scripts/cd.txt b/testdata/scripts/cd.txt index 58f9dccd244..676845fa594 100644 --- a/testdata/scripts/cd.txt +++ b/testdata/scripts/cd.txt @@ -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"] \ No newline at end of file + args = ["--version"] diff --git a/testdata/scripts/editconfig.txt b/testdata/scripts/editconfig.txt index 5ac69864f14..b28f42d494c 100644 --- a/testdata/scripts/editconfig.txt +++ b/testdata/scripts/editconfig.txt @@ -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 diff --git a/testdata/scripts/hg.txt b/testdata/scripts/hg.txt index 3dbdb7433cf..505a50e64a5 100644 --- a/testdata/scripts/hg.txt +++ b/testdata/scripts/hg.txt @@ -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 diff --git a/testdata/scripts/init.txt b/testdata/scripts/init.txt index 5a274a6772f..c627d824c35 100644 --- a/testdata/scripts/init.txt +++ b/testdata/scripts/init.txt @@ -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 diff --git a/testdata/scripts/updategit.txt b/testdata/scripts/updategit.txt index b6b7cbe44bf..606e6e7dbce 100644 --- a/testdata/scripts/updategit.txt +++ b/testdata/scripts/updategit.txt @@ -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