Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cloneUrl.test.ts

62 lines2.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { cloneUrlForProtocol, cloneUrlProtocol, cloneUrlRepositoryPath } from "./cloneUrl.ts";
4​
5describe("cloneUrlForProtocol", () => {
6 it("switches between scp-style SSH and HTTPS", () => {
7 expect(cloneUrlForProtocol("git@github.com:pingdotgg/t3code.git", "https")).toBe(
8 "https://github.com/pingdotgg/t3code.git",
9 );
10 expect(cloneUrlForProtocol("https://github.com/pingdotgg/t3code.git", "ssh")).toBe(
11 "git@github.com:pingdotgg/t3code.git",
12 );
13 expect(cloneUrlForProtocol("ssh://git@gitlab.com/group/sub/repo.git", "https")).toBe(
14 "https://gitlab.com/group/sub/repo.git",
15 );
16 });
17​
18 it("keeps the URL as is for its own protocol and drops HTTPS credentials on switch", () => {
19 expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "https")).toBe(
20 "https://token@github.com/a/b.git",
21 );
22 expect(cloneUrlForProtocol("https://token@github.com/a/b.git", "ssh")).toBe(
23 "git@github.com:a/b.git",
24 );
25 });
26​
27 it("refuses rewrites that would guess wrong", () => {
28 expect(cloneUrlForProtocol("ssh://git@host.example:2222/a/b.git", "https")).toBeNull();
29 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "ssh")).toBeNull();
30 expect(cloneUrlForProtocol("https://host.example:8443/a/b.git", "https")).toBe(
31 "https://host.example:8443/a/b.git",
32 );
33 expect(cloneUrlForProtocol("/srv/git/repo.git", "https")).toBeNull();
34 expect(cloneUrlProtocol("file:///srv/git/repo.git")).toBeNull();
35 });
36});
37​
38describe("cloneUrlRepositoryPath", () => {
39 it("rejects Windows device names and trailing dots or spaces in every folder", () => {
40 for (const segment of [
41 "CON",
42 "nul.txt",
43 "COM1",
44 "LPT0",
45 "com¹",
46 "CONIN$",
47 "conout$",
48 "aux .txt",
49 "repo.",
50 "repo ",
51 ]) {
52 expect(cloneUrlRepositoryPath(`https://github.com/${segment}/repo.git`)).toBeNull();
53 expect(cloneUrlRepositoryPath(`https://${segment}/owner/repo.git`)).toBeNull();
54 expect(cloneUrlRepositoryPath(`https://github.com/owner/${segment}.git`)).toBeNull();
55 }
56 expect(cloneUrlRepositoryPath("https://github.com/acme/repo.git")).toBe("github.com/acme/repo");
57 expect(cloneUrlRepositoryPath("ssh://git@gitlab.com:2222/group/sub/repo.git")).toBe(
58 "gitlab.com/group/sub/repo",
59 );
60 expect(cloneUrlRepositoryPath("C:/repos/project.git")).toBeNull();
61 });
62});