Repository navigation
Expand file tree
/
Copy pathconfig_test.go
More file actions
112 lines (88 loc) · 2.77 KB
/
Copy pathconfig_test.go
File metadata and controls
112 lines (88 loc) · 2.77 KB
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
package main
import (
"io"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/BurntSushi/toml"
"github.com/go-quicktest/qt"
)
func TestParseConfigFromTOML(t *testing.T) {
tmp := t.TempDir()
want := &config{"A", "B", "C", "", "", nil, nil}
have := *want
qt.Assert(t, qt.IsNil(parseConfigFromTOML(tmp, &have)))
qt.Assert(t, qt.DeepEquals(&have, want),
qt.Commentf("config shouldn't change if file doesn't exist"))
toml := []byte(`
kernel = "foo"
memory = "bar"
`)
err := os.WriteFile(filepath.Join(tmp, configFileName), toml, 0644)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.IsNil(parseConfigFromTOML(tmp, &have)))
qt.Assert(t, qt.DeepEquals(&have, &config{
Kernel: "foo", Memory: "bar", SMP: want.SMP,
}))
}
func TestRefuseExtraneousConfig(t *testing.T) {
tmp := t.TempDir()
mustWriteConfig(t, tmp, `bazbar = 1`)
qt.Assert(t, qt.ErrorIs(parseConfigFromTOML(tmp, &config{}), errUnrecognisedKeys))
}
func TestFindGitRoot(t *testing.T) {
root, err := findGitRoot(t.TempDir())
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(root, ""))
wd, err := os.Getwd()
qt.Assert(t, qt.IsNil(err))
root, err = findGitRoot(".")
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(root, wd))
}
func TestFindConfigFile(t *testing.T) {
root := t.TempDir()
output, err := exec.Command("git", "-C", root, "init").CombinedOutput()
qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %s", string(output)))
_, err = findConfigFile(root)
qt.Assert(t, qt.ErrorIs(err, os.ErrNotExist))
subdir := filepath.Join(root, "subdir")
qt.Assert(t, qt.IsNil(os.Mkdir(subdir, 0755)))
rootCfg := mustWriteConfig(t, root, `foo=1`)
f, err := findConfigFile(subdir)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(f.Name(), rootCfg))
subdirCfg := mustWriteConfig(t, subdir, `foo=2`)
f, err = findConfigFile(subdir)
qt.Assert(t, qt.IsNil(err))
f.Close()
qt.Assert(t, qt.Equals(f.Name(), subdirCfg))
f, err = findConfigFile(root)
qt.Assert(t, qt.IsNil(err))
f.Close()
qt.Assert(t, qt.Equals(f.Name(), rootCfg))
}
func TestWriteConfig(t *testing.T) {
f, err := os.Create("testdata/default.toml")
qt.Assert(t, qt.IsNil(err))
defer f.Close()
qt.Assert(t, qt.IsNil(toml.NewEncoder(f).Encode(defaultConfig)))
}
func TestConfigFlags(t *testing.T) {
var cfg config
fs := configFlags("test", &cfg)
fs.SetOutput(io.Discard)
err := fs.Parse([]string{"-kernel", ":foo"})
qt.Assert(t, qt.IsNotNil(err))
cfg.Kernel = "example.org/image:bar"
err = fs.Parse([]string{"-kernel", ":foo"})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(cfg.Kernel, "example.org/image:foo"))
}
func mustWriteConfig(tb testing.TB, dir, contents string) string {
tb.Helper()
filename := filepath.Join(dir, configFileName)
qt.Assert(tb, qt.IsNil(os.WriteFile(filename, []byte(contents), 0644)))
return filename
}