-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusb_fixtures.go
More file actions
202 lines (191 loc) · 4.89 KB
/
usb_fixtures.go
File metadata and controls
202 lines (191 loc) · 4.89 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package testutil
import (
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
// validateFixtureLayoutDir reports whether layoutDir may be joined under a fixture root.
// Empty layoutDir is allowed (caller may default it).
func validateFixtureLayoutDir(layoutDir string) error {
if layoutDir == "" {
return nil
}
clean := filepath.Clean(layoutDir)
if filepath.IsAbs(clean) {
return fmt.Errorf("must be relative to fixture root: %q", layoutDir)
}
if clean == ".." || strings.HasPrefix(clean, ".."+string(filepath.Separator)) {
return fmt.Errorf("must be relative to fixture root: %q", layoutDir)
}
return nil
}
type USBVolumeOptions struct {
LayoutDir string
Strategy string
CreateReposDir bool
}
type USBVolumeConfig struct {
SchemaVersion int
LayoutDir string
Strategy string
CreatedAt time.Time
}
func mustRelativeLayoutDir(t *testing.T, layoutDir string) string {
t.Helper()
if layoutDir == "" {
return "repos"
}
if err := validateFixtureLayoutDir(layoutDir); err != nil {
t.Fatalf("layout_dir %v", err)
}
return filepath.Clean(layoutDir)
}
func MustUSBVolumeRoot(t *testing.T, opts USBVolumeOptions) string {
t.Helper()
root := t.TempDir()
cfg := USBVolumeConfig{
SchemaVersion: 1,
LayoutDir: mustRelativeLayoutDir(t, opts.LayoutDir),
Strategy: opts.Strategy,
CreatedAt: time.Now().UTC(),
}
if cfg.Strategy == "" {
cfg.Strategy = "git-mirror"
}
WriteUSBVolumeConfig(t, root, cfg)
if opts.CreateReposDir {
if err := os.MkdirAll(filepath.Join(root, cfg.LayoutDir), 0o755); err != nil {
t.Fatalf("failed creating repos dir: %v", err)
}
}
return root
}
func WriteUSBVolumeConfig(t *testing.T, root string, cfg USBVolumeConfig) {
t.Helper()
if cfg.SchemaVersion <= 0 {
cfg.SchemaVersion = 1
}
cfg.LayoutDir = mustRelativeLayoutDir(t, cfg.LayoutDir)
if cfg.Strategy == "" {
cfg.Strategy = "git-mirror"
}
if cfg.CreatedAt.IsZero() {
cfg.CreatedAt = time.Now().UTC()
}
content := fmt.Sprintf(
"schema_version = %d\nlayout_dir = %q\nstrategy = %q\ncreated_at = %q\n",
cfg.SchemaVersion,
cfg.LayoutDir,
cfg.Strategy,
cfg.CreatedAt.Format(time.RFC3339),
)
if err := os.WriteFile(filepath.Join(root, ".git-fire"), []byte(content), 0o644); err != nil {
t.Fatalf("failed writing .git-fire: %v", err)
}
}
func readUSBVolumeConfigBytes(data []byte) (USBVolumeConfig, error) {
cfg := USBVolumeConfig{}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, val, ok := strings.Cut(line, "=")
if !ok {
continue
}
key = strings.TrimSpace(key)
val = strings.TrimSpace(val)
switch key {
case "schema_version":
n, err := strconv.Atoi(val)
if err != nil {
return cfg, fmt.Errorf("invalid schema_version %q: %w", val, err)
}
cfg.SchemaVersion = n
case "layout_dir":
if unquoted, err := strconv.Unquote(val); err == nil {
val = unquoted
}
if err := validateFixtureLayoutDir(val); err != nil {
return cfg, fmt.Errorf("layout_dir: %w", err)
}
if val == "" {
cfg.LayoutDir = ""
} else {
cfg.LayoutDir = filepath.Clean(val)
}
case "strategy":
if unquoted, err := strconv.Unquote(val); err == nil {
val = unquoted
}
cfg.Strategy = val
case "created_at":
if unquoted, err := strconv.Unquote(val); err == nil {
val = unquoted
}
if val == "" {
return cfg, fmt.Errorf("created_at: empty value")
}
ts, err := time.Parse(time.RFC3339, val)
if err != nil {
return cfg, fmt.Errorf("invalid created_at %q: %w", val, err)
}
cfg.CreatedAt = ts
}
}
return cfg, nil
}
func ReadUSBVolumeConfig(t *testing.T, root string) USBVolumeConfig {
t.Helper()
data, err := os.ReadFile(filepath.Join(root, ".git-fire"))
if err != nil {
t.Fatalf("failed reading .git-fire: %v", err)
}
cfg, err := readUSBVolumeConfigBytes(data)
if err != nil {
t.Fatalf("parse .git-fire: %v", err)
}
return cfg
}
func AssertGitDirAt(t *testing.T, path string, wantBare bool) {
t.Helper()
if wantBare {
headPath := filepath.Join(path, "HEAD")
info, err := os.Stat(headPath)
if err != nil {
t.Fatalf("expected bare repo at %s: %v", path, err)
}
if info.IsDir() {
t.Fatalf("expected bare repo HEAD file at %s", path)
}
return
}
gitPath := filepath.Join(path, ".git")
info, err := os.Stat(gitPath)
if err != nil {
t.Fatalf("expected non-bare repo at %s: %v", path, err)
}
if !info.IsDir() {
t.Fatalf("expected non-bare repo .git directory at %s", path)
}
}
func FileURLForPath(t *testing.T, path string) string {
t.Helper()
abs, err := filepath.Abs(path)
if err != nil {
t.Fatalf("failed to make abs path: %v", err)
}
uPath := filepath.ToSlash(abs)
if filepath.VolumeName(abs) != "" && !strings.HasPrefix(uPath, "/") {
uPath = "/" + uPath
}
u := &url.URL{Scheme: "file", Path: uPath}
return u.String()
}