-
Notifications
You must be signed in to change notification settings - Fork 0
Fix GetRemotes parsing for URLs with spaces #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bschellenberger2600
merged 5 commits into
chore/repo-quality-hardening
from
fix/getremotes-space-paths
Apr 5, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3d216ef
Fix remote URL parsing for paths with spaces.
bschellenberger2600 58fe7d0
feat: add USB fixture helpers for target-volume testing
bschellenberger2600 929834e
fix(testutil): harden USB fixtures per review feedback
bschellenberger2600 c2b6dc4
fix(testutil): stricter .git-fire parsing and layout validation tests
bschellenberger2600 e3d6168
Merge origin/chore/repo-quality-hardening into fix/getremotes-space-p…
bschellenberger2600 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| 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.Trim(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 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": | ||
| cfg.Strategy = val | ||
| case "created_at": | ||
| 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 { | ||
| if _, err := os.Stat(filepath.Join(path, "HEAD")); err != nil { | ||
| t.Fatalf("expected bare repo at %s: %v", path, err) | ||
| } | ||
| return | ||
| } | ||
| if _, err := os.Stat(filepath.Join(path, ".git")); err != nil { | ||
| t.Fatalf("expected non-bare repo at %s: %v", path, err) | ||
| } | ||
| } | ||
|
|
||
| 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() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| 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 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) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.