forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
186 lines (171 loc) · 4.75 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
"io/ioutil"
"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,
"edit": edit,
},
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)
}
}
// edit edits all of its arguments by appending "# edited\n" to them.
func edit(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported ! edit")
}
for _, arg := range args {
filename := ts.MkAbs(arg)
data, err := ioutil.ReadFile(filename)
if err != nil {
ts.Fatalf("edit: %v", err)
}
data = append(data, []byte("# edited\n")...)
//nolint:gosec
if err := ioutil.WriteFile(filename, data, 0o666); err != nil {
ts.Fatalf("edit: %v", err)
}
}
}
func setup(env *testscript.Env) error {
var (
binDir = filepath.Join(env.WorkDir, "bin")
homeDir = filepath.Join(env.WorkDir, "home", "user")
chezmoiConfigDir = filepath.Join(homeDir, ".config", "chezmoi")
chezmoiSourceDir = filepath.Join(homeDir, ".local", "share", "chezmoi")
)
env.Setenv("HOME", 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)
// 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"))
}
// Fix permissions on the source directory, if it exists.
_ = os.Chmod(chezmoiSourceDir, 0o700)
// Fix permissions on any files in the bin directory.
infos, err := ioutil.ReadDir(binDir)
if err == nil {
for _, info := range infos {
if err := os.Chmod(filepath.Join(binDir, info.Name()), 0o755); err != nil {
return err
}
}
}
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.OSFS, env.WorkDir), root)
}
func prependDirToPath(dir, path string) string {
return strings.Join(append([]string{dir}, filepath.SplitList(path)...), string(os.PathListSeparator))
}