forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
133 lines (123 loc) · 3.56 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/rogpeppe/go-internal/testscript"
"github.com/twpayne/go-vfs"
"github.com/twpayne/go-vfs/vfst"
)
//nolint:interfacer
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"chezmoi": testRun,
}))
}
func TestChezmoi(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration tests in short mode")
}
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
Condition: func(cond string) (bool, error) {
switch cond {
case "windows":
return runtime.GOOS == "windows", nil
default:
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)
}
},
})
}
func testRun() int {
if err := run(); err != nil {
if s := err.Error(); s != "" {
fmt.Printf("chezmoi: %s\n", s)
}
return 1
}
return 0
}
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"))
env.Setenv("PATH", prependDirToPath(binDir, env.Getenv("PATH")))
env.Setenv("SHELL", filepath.Join(binDir, "shell"))
return vfst.NewBuilder().Build(vfs.NewPathFS(vfs.HostOSFS, env.WorkDir), map[string]interface{}{
"/bin": map[string]interface{}{
// editor a non-interactive script that appends "# edited\n" to the
// end of each file.
"editor": &vfst.File{
Perm: 0o755,
Contents: []byte(strings.Join([]string{
`#!/bin/sh`,
``,
`for filename in $*; do`,
` echo "# edited" >> $filename`,
`done`,
}, "\n")),
},
// shell is a non-interactive script that appends the directory in
// which it was launched to $WORK/shell.log.
"shell": &vfst.File{
Perm: 0o755,
Contents: []byte(strings.Join([]string{
`#!/bin/sh`,
``,
`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"),
},
})
}
func prependDirToPath(dir, path string) string {
return strings.Join(append([]string{dir}, filepath.SplitList(path)...), string(os.PathListSeparator))
}