Go · 5674 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package repo
4
5 import (
6 "strings"
7 "testing"
8
9 repogit "github.com/tenseleyFlow/shithub/internal/repos/git"
10 )
11
12 func TestTreeEntryURL_EscapesPathSegments(t *testing.T) {
13 t.Parallel()
14 got := treeEntryURL("octo-user", "demo.repo", "feature/x", repogit.EntryBlob, "dir/a file.go")
15 want := "/octo-user/demo.repo/blob/feature/x/dir/a%20file.go"
16 if got != want {
17 t.Fatalf("treeEntryURL = %q, want %q", got, want)
18 }
19 }
20
21 func TestTreeEntryURL_NonNavigableEntries(t *testing.T) {
22 t.Parallel()
23 got := treeEntryURL("octo-user", "demo", "trunk", repogit.EntrySymlink, "vendor/lib")
24 if got != "" {
25 t.Fatalf("symlink URL = %q, want empty", got)
26 }
27 }
28
29 func TestCodeRefDisplayShortensSHAs(t *testing.T) {
30 t.Parallel()
31 sha := "cea2569cfb80705f3eaeceadd8968aa031c22813"
32 if got := codeRefDisplay(sha); got != "cea2569" {
33 t.Fatalf("codeRefDisplay(sha) = %q, want cea2569", got)
34 }
35 if got := codeRefDisplay("release/v1"); got != "release/v1" {
36 t.Fatalf("codeRefDisplay(branch) = %q, want release/v1", got)
37 }
38 }
39
40 func TestSubmoduleRouteURL_GitHubRemotesBecomeLocalTreeLinks(t *testing.T) {
41 t.Parallel()
42 cfg := submoduleRouteConfig{
43 owner: "FortranGoingOnForty",
44 repoName: "armfortas",
45 baseURL: "https://shithub.sh",
46 sshHost: "git@shithub.sh",
47 }
48 oid := "56efe6d8fcc64bdc186d9c1a0308db6cc8e03125"
49
50 tests := []struct {
51 name string
52 remote string
53 want string
54 }{
55 {
56 name: "github scp",
57 remote: "git@github.com:FortranGoingOnForty/afs-as.git",
58 want: "/FortranGoingOnForty/afs-as/tree/" + oid,
59 },
60 {
61 name: "github https cross org",
62 remote: "https://github.com/tenseleyFlow/bencch.git",
63 want: "/tenseleyFlow/bencch/tree/" + oid,
64 },
65 {
66 name: "github ssh url",
67 remote: "ssh://git@github.com/FortranGoingOnForty/afs-ld.git",
68 want: "/FortranGoingOnForty/afs-ld/tree/" + oid,
69 },
70 {
71 name: "configured shithub https",
72 remote: "https://shithub.sh/tenseleyFlow/bencch.git",
73 want: "/tenseleyFlow/bencch/tree/" + oid,
74 },
75 {
76 name: "configured shithub scp",
77 remote: "git@shithub.sh:tenseleyFlow/bencch.git",
78 want: "/tenseleyFlow/bencch/tree/" + oid,
79 },
80 {
81 name: "relative sibling",
82 remote: "../afs-ld.git",
83 want: "/FortranGoingOnForty/afs-ld/tree/" + oid,
84 },
85 }
86
87 for _, tt := range tests {
88 t.Run(tt.name, func(t *testing.T) {
89 t.Parallel()
90 got := submoduleRouteURL(cfg, tt.remote, oid)
91 if got != tt.want {
92 t.Fatalf("submoduleRouteURL(%q) = %q, want %q", tt.remote, got, tt.want)
93 }
94 route, ok := submoduleRouteForRemote(cfg, tt.remote, oid)
95 if !ok {
96 t.Fatalf("submoduleRouteForRemote(%q) ok = false", tt.remote)
97 }
98 wantRepoURL := strings.TrimSuffix(tt.want, "/tree/"+oid)
99 if route.RepoURL != wantRepoURL {
100 t.Fatalf("RepoURL = %q, want %q", route.RepoURL, wantRepoURL)
101 }
102 })
103 }
104 }
105
106 func TestSubmoduleRouteURL_UnsupportedRemotesStayPlain(t *testing.T) {
107 t.Parallel()
108 cfg := submoduleRouteConfig{owner: "octo", repoName: "super", baseURL: "https://shithub.sh", sshHost: "git@shithub.sh"}
109 oid := "56efe6d8fcc64bdc186d9c1a0308db6cc8e03125"
110
111 for _, remote := range []string{
112 "https://example.com/octo/lib.git",
113 "javascript:alert(1)",
114 "https://github.com/octo/nested/lib.git",
115 "https://github.com/%2F/lib.git",
116 "",
117 } {
118 remote := remote
119 t.Run(remote, func(t *testing.T) {
120 t.Parallel()
121 if got := submoduleRouteURL(cfg, remote, oid); got != "" {
122 t.Fatalf("submoduleRouteURL(%q) = %q, want empty", remote, got)
123 }
124 })
125 }
126 }
127
128 func TestGitHubSubmoduleFetchURL_CanonicalizesSupportedRemotes(t *testing.T) {
129 t.Parallel()
130 cfg := submoduleRouteConfig{
131 owner: "FortranGoingOnForty",
132 repoName: "lib-modules",
133 baseURL: "https://shithub.sh",
134 sshHost: "git@shithub.sh",
135 }
136
137 for _, tt := range []struct {
138 name string
139 remote string
140 want string
141 }{
142 {
143 name: "scp",
144 remote: "git@github.com:FortranGoingOnForty/afs-as.git",
145 want: "https://github.com/FortranGoingOnForty/afs-as.git",
146 },
147 {
148 name: "https",
149 remote: "https://github.com/tenseleyFlow/bencch.git",
150 want: "https://github.com/tenseleyFlow/bencch.git",
151 },
152 {
153 name: "ssh url",
154 remote: "ssh://git@github.com/FortranGoingOnForty/afs-ld.git",
155 want: "https://github.com/FortranGoingOnForty/afs-ld.git",
156 },
157 {
158 name: "relative sibling without suffix",
159 remote: "../fgof-process",
160 want: "https://github.com/FortranGoingOnForty/fgof-process.git",
161 },
162 {
163 name: "relative sibling with suffix",
164 remote: "../fgof-fs.git",
165 want: "https://github.com/FortranGoingOnForty/fgof-fs.git",
166 },
167 } {
168 tt := tt
169 t.Run(tt.name, func(t *testing.T) {
170 t.Parallel()
171 got, ok := githubSubmoduleFetchURL(cfg, tt.remote)
172 if !ok {
173 t.Fatalf("githubSubmoduleFetchURL(%q) ok = false", tt.remote)
174 }
175 if got != tt.want {
176 t.Fatalf("githubSubmoduleFetchURL(%q) = %q, want %q", tt.remote, got, tt.want)
177 }
178 })
179 }
180 }
181
182 func TestGitHubSubmoduleFetchURL_RejectsUnsupportedRemotes(t *testing.T) {
183 t.Parallel()
184 cfg := submoduleRouteConfig{owner: "octo", repoName: "super", baseURL: "https://shithub.sh", sshHost: "git@shithub.sh"}
185
186 for _, remote := range []string{
187 "https://shithub.sh/tenseleyFlow/bencch.git",
188 "../../../afs-ld.git",
189 "https://example.com/octo/lib.git",
190 "https://github.com/octo/nested/lib.git",
191 "https://github.com/%2F/lib.git",
192 "javascript:alert(1)",
193 "",
194 } {
195 remote := remote
196 t.Run(remote, func(t *testing.T) {
197 t.Parallel()
198 if got, ok := githubSubmoduleFetchURL(cfg, remote); ok || got != "" {
199 t.Fatalf("githubSubmoduleFetchURL(%q) = %q, %v; want empty, false", remote, got, ok)
200 }
201 })
202 }
203 }
204