|
| 1 | +package githubdownloader |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// DefaultOptions |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | + |
| 11 | +func TestDefaultOptions_concurrency(t *testing.T) { |
| 12 | + opts := DefaultOptions() |
| 13 | + if opts.Concurrency <= 0 { |
| 14 | + t.Errorf("Concurrency should be positive, got %d", opts.Concurrency) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestDefaultOptions_not_dry_run(t *testing.T) { |
| 19 | + opts := DefaultOptions() |
| 20 | + if opts.Concurrency <= 0 { |
| 21 | + t.Errorf("Concurrency should be positive, got %d", opts.Concurrency) |
| 22 | + } |
| 23 | + if !opts.AllowFallback { |
| 24 | + t.Error("AllowFallback should default to true") |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | +// ParseLsRemoteOutput: additional edge cases |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +func TestParseLsRemoteOutput_tabs_only(t *testing.T) { |
| 33 | + refs := ParseLsRemoteOutput("\t\n") |
| 34 | + if len(refs) != 0 { |
| 35 | + t.Errorf("expected 0 refs for tab-only input, got %d", len(refs)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestParseLsRemoteOutput_windows_line_endings(t *testing.T) { |
| 40 | + input := "abc123\trefs/heads/main\r\ndef456\trefs/tags/v1.0.0\r\n" |
| 41 | + refs := ParseLsRemoteOutput(input) |
| 42 | + // Should parse at least the valid lines; SHA/name may or may not have \r |
| 43 | + // depending on implementation -- just ensure no panic |
| 44 | + _ = refs |
| 45 | +} |
| 46 | + |
| 47 | +func TestParseLsRemoteOutput_sha_case_preserved(t *testing.T) { |
| 48 | + input := "ABCDEF1234567890\trefs/heads/feature\n" |
| 49 | + refs := ParseLsRemoteOutput(input) |
| 50 | + if len(refs) != 1 { |
| 51 | + t.Fatalf("expected 1 ref, got %d", len(refs)) |
| 52 | + } |
| 53 | + if refs[0].SHA != "ABCDEF1234567890" { |
| 54 | + t.Errorf("SHA case should be preserved, got %q", refs[0].SHA) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | +// SemverSortKey: additional cases |
| 60 | +// --------------------------------------------------------------------------- |
| 61 | + |
| 62 | +func TestSemverSortKey_no_v_prefix(t *testing.T) { |
| 63 | + key := SemverSortKey("3.0.1") |
| 64 | + if key != [4]int{3, 0, 1, 0} { |
| 65 | + t.Errorf("SemverSortKey(3.0.1) = %v, want [3 0 1 0]", key) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestSemverSortKey_empty(t *testing.T) { |
| 70 | + key := SemverSortKey("") |
| 71 | + if key[0] != -1 { |
| 72 | + t.Errorf("expected -1 for empty string, got %v", key) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestSemverSortKey_pre_release_less_than_release(t *testing.T) { |
| 77 | + pre := SemverSortKey("v1.0.0-alpha") |
| 78 | + rel := SemverSortKey("v1.0.0") |
| 79 | + // pre-release should sort lower |
| 80 | + if !(pre[3] < rel[3]) { |
| 81 | + t.Errorf("pre-release should sort lower: pre=%v rel=%v", pre, rel) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// --------------------------------------------------------------------------- |
| 86 | +// SortRemoteRefs: stability and ties |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | + |
| 89 | +func TestSortRemoteRefs_single(t *testing.T) { |
| 90 | + refs := []RemoteRef{{Name: "v1.0.0", SHA: "abc"}} |
| 91 | + sorted := SortRemoteRefs(refs) |
| 92 | + if len(sorted) != 1 || sorted[0].Name != "v1.0.0" { |
| 93 | + t.Errorf("single ref sort broken: %+v", sorted) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestSortRemoteRefs_empty(t *testing.T) { |
| 98 | + sorted := SortRemoteRefs(nil) |
| 99 | + if len(sorted) != 0 { |
| 100 | + t.Errorf("nil input should return empty slice, got %v", sorted) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestSortRemoteRefs_non_semver_last(t *testing.T) { |
| 105 | + refs := []RemoteRef{ |
| 106 | + {Name: "latest", SHA: "a"}, |
| 107 | + {Name: "v1.0.0", SHA: "b"}, |
| 108 | + } |
| 109 | + sorted := SortRemoteRefs(refs) |
| 110 | + // semver v1.0.0 should come first |
| 111 | + if sorted[0].Name != "v1.0.0" { |
| 112 | + t.Errorf("expected v1.0.0 first, got %s", sorted[0].Name) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestSortRemoteRefs_preserves_all_refs(t *testing.T) { |
| 117 | + refs := []RemoteRef{ |
| 118 | + {Name: "v2.0.0", SHA: "a"}, |
| 119 | + {Name: "v1.0.0", SHA: "b"}, |
| 120 | + {Name: "v3.0.0", SHA: "c"}, |
| 121 | + } |
| 122 | + sorted := SortRemoteRefs(refs) |
| 123 | + if len(sorted) != 3 { |
| 124 | + t.Errorf("expected 3 refs, got %d", len(sorted)) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// --------------------------------------------------------------------------- |
| 129 | +// RemoteRef struct fields |
| 130 | +// --------------------------------------------------------------------------- |
| 131 | + |
| 132 | +func TestRemoteRef_fields(t *testing.T) { |
| 133 | + r := RemoteRef{Name: "refs/tags/v1.0.0", SHA: "deadbeef"} |
| 134 | + if r.Name != "refs/tags/v1.0.0" { |
| 135 | + t.Errorf("unexpected Name: %q", r.Name) |
| 136 | + } |
| 137 | + if r.SHA != "deadbeef" { |
| 138 | + t.Errorf("unexpected SHA: %q", r.SHA) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | +// ProtocolPreference constants |
| 144 | +// --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +func TestProtocolPreference_distinct(t *testing.T) { |
| 147 | + vals := map[ProtocolPreference]bool{} |
| 148 | + for _, p := range []ProtocolPreference{ProtocolHTTPSOnly, ProtocolSSHOnly, ProtocolPreferHTTPS, ProtocolPreferSSH} { |
| 149 | + if vals[p] { |
| 150 | + t.Errorf("duplicate ProtocolPreference value: %v", p) |
| 151 | + } |
| 152 | + vals[p] = true |
| 153 | + } |
| 154 | +} |
0 commit comments