-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusb_fixtures_test.go
More file actions
182 lines (171 loc) · 4.66 KB
/
usb_fixtures_test.go
File metadata and controls
182 lines (171 loc) · 4.66 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
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
package testutil
import (
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)
func TestMustUSBVolumeRoot(t *testing.T) {
root := MustUSBVolumeRoot(t, USBVolumeOptions{
LayoutDir: "repos",
Strategy: "git-mirror",
CreateReposDir: true,
})
if _, err := os.Stat(filepath.Join(root, ".git-fire")); err != nil {
t.Fatalf("expected .git-fire marker: %v", err)
}
if _, err := os.Stat(filepath.Join(root, "repos")); err != nil {
t.Fatalf("expected repos dir: %v", err)
}
}
func TestReadWriteUSBVolumeConfig(t *testing.T) {
root := t.TempDir()
WriteUSBVolumeConfig(t, root, USBVolumeConfig{
SchemaVersion: 2,
LayoutDir: "custom",
Strategy: "git-clone",
})
cfg := ReadUSBVolumeConfig(t, root)
if cfg.SchemaVersion != 2 {
t.Fatalf("schema mismatch: %d", cfg.SchemaVersion)
}
if cfg.LayoutDir != "custom" {
t.Fatalf("layout mismatch: %s", cfg.LayoutDir)
}
if cfg.Strategy != "git-clone" {
t.Fatalf("strategy mismatch: %s", cfg.Strategy)
}
}
func TestReadWriteUSBVolumeConfig_preservesEscapedChars(t *testing.T) {
root := t.TempDir()
WriteUSBVolumeConfig(t, root, USBVolumeConfig{
SchemaVersion: 1,
LayoutDir: `repo\"dir\path`,
Strategy: `mirror\"strategy\mode`,
})
cfg := ReadUSBVolumeConfig(t, root)
if cfg.LayoutDir != `repo\"dir\path` {
t.Fatalf("layout mismatch: got %q", cfg.LayoutDir)
}
if cfg.Strategy != `mirror\"strategy\mode` {
t.Fatalf("strategy mismatch: got %q", cfg.Strategy)
}
}
func TestValidateFixtureLayoutDir(t *testing.T) {
t.Parallel()
root := t.TempDir()
absUnderRoot := filepath.Join(root, "abs-layout")
if err := os.MkdirAll(absUnderRoot, 0o755); err != nil {
t.Fatal(err)
}
bad := []string{
"..",
"../escape",
"nested/../../../escape",
absUnderRoot,
}
for _, dir := range bad {
if err := validateFixtureLayoutDir(dir); err == nil {
t.Errorf("validateFixtureLayoutDir(%q): want error, got nil", dir)
}
}
good := []string{"", "repos", "nested", "nested/../repos"}
for _, dir := range good {
if err := validateFixtureLayoutDir(dir); err != nil {
t.Errorf("validateFixtureLayoutDir(%q): %v", dir, err)
}
}
}
func TestReadUSBVolumeConfigBytes_roundTrip(t *testing.T) {
t.Parallel()
input := "schema_version = 2\nlayout_dir = \"custom\"\nstrategy = \"git-clone\"\ncreated_at = \"2020-01-02T15:04:05Z\"\n"
cfg, err := readUSBVolumeConfigBytes([]byte(input))
if err != nil {
t.Fatal(err)
}
if cfg.SchemaVersion != 2 {
t.Fatalf("schema: got %d", cfg.SchemaVersion)
}
if cfg.LayoutDir != "custom" {
t.Fatalf("layout: got %q", cfg.LayoutDir)
}
if cfg.Strategy != "git-clone" {
t.Fatalf("strategy: got %q", cfg.Strategy)
}
if cfg.CreatedAt.IsZero() {
t.Fatal("created_at: zero")
}
}
func TestReadUSBVolumeConfigBytes_errors(t *testing.T) {
t.Parallel()
cases := []struct {
name, content, wantSubstring string
}{
{
name: "invalid_schema_version",
content: "schema_version = notint\n",
wantSubstring: "schema_version",
},
{
name: "layout_dir_escape",
content: "layout_dir = ../x\n",
wantSubstring: "layout_dir",
},
{
name: "invalid_created_at",
content: "created_at = not-a-date\n",
wantSubstring: "created_at",
},
{
name: "empty_created_at",
content: "created_at = \n",
wantSubstring: "created_at",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, err := readUSBVolumeConfigBytes([]byte(tc.content))
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), tc.wantSubstring) {
t.Fatalf("error %q does not contain %q", err.Error(), tc.wantSubstring)
}
})
}
}
func TestFileURLForPath(t *testing.T) {
root := t.TempDir()
got := FileURLForPath(t, root)
parsed, err := url.Parse(got)
if err != nil {
t.Fatalf("parse URL: %v", err)
}
if parsed.Scheme != "file" {
t.Fatalf("scheme %q, want file", parsed.Scheme)
}
if parsed.Path == "" || parsed.Path[0] != '/' {
t.Fatalf("expected absolute path in URL, got path=%q for %q", parsed.Path, got)
}
if !strings.HasPrefix(got, "file:///") {
t.Fatalf("expected canonical file URL with empty authority (file:///...), got %q", got)
}
}
func TestAssertGitDirAt(t *testing.T) {
t.Run("bare_repo", func(t *testing.T) {
repo := t.TempDir()
if err := os.WriteFile(filepath.Join(repo, "HEAD"), []byte("ref: refs/heads/main\n"), 0o644); err != nil {
t.Fatalf("write HEAD: %v", err)
}
AssertGitDirAt(t, repo, true)
})
t.Run("non_bare_repo", func(t *testing.T) {
repo := t.TempDir()
if err := os.Mkdir(filepath.Join(repo, ".git"), 0o755); err != nil {
t.Fatalf("create .git directory: %v", err)
}
AssertGitDirAt(t, repo, false)
})
}