forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
148 lines (136 loc) · 3.65 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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"),
Cmds: map[string]func(*testscript.TestScript, bool, []string){
"chhome": chHome,
},
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: setup,
})
}
func testRun() int {
if err := run(); err != nil {
if s := err.Error(); s != "" {
fmt.Printf("chezmoi: %s\n", s)
}
return 1
}
return 0
}
// chHome changes the home directory to its argument, creating the directory if
// it does not already exists. It updates the HOME environment variable, and, if
// running on Windows, USERPROFILE too.
func chHome(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported ! chhome")
}
if len(args) != 1 {
ts.Fatalf("usage: chhome dir")
}
homeDir := args[0]
if !filepath.IsAbs(homeDir) {
homeDir = filepath.Join(ts.Getenv("WORK"), homeDir)
}
ts.Check(os.MkdirAll(homeDir, 0o777))
ts.Setenv("HOME", homeDir)
if runtime.GOOS == "windows" {
ts.Setenv("USERPROFILE", homeDir)
}
}
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")))
switch runtime.GOOS {
case "windows":
env.Setenv("EDITOR", filepath.Join(binDir, "editor.cmd"))
env.Setenv("USERPROFILE", homeDir)
// 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"))
}
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"),
},
}
switch runtime.GOOS {
case "windows":
root["/bin"] = map[string]interface{}{
// editor.cmd 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`),
},
}
default:
root["/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")),
},
}
}
return vfst.NewBuilder().Build(vfs.NewPathFS(vfs.HostOSFS, env.WorkDir), root)
}
func prependDirToPath(dir, path string) string {
return strings.Join(append([]string{dir}, filepath.SplitList(path)...), string(os.PathListSeparator))
}