Files
git-clone/test/git-clone.test.ts

150 lines
4.5 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
destinationPreview,
normalizeRemoteUrl,
parseGitUrl,
validateBranchName,
validateDirectoryName,
} from "../shared/git-url.ts";
test("parseGitUrl parses standard HTTPS, HTTP, SSH, and git URLs", () => {
const httpsRes = parseGitUrl("https://github.com/octocat/Hello-World.git");
assert.deepEqual(httpsRes, {
ok: true,
url: "https://github.com/octocat/Hello-World.git",
host: "github.com",
repoPath: "octocat/Hello-World",
repoName: "Hello-World",
});
const subgroupRes = parseGitUrl("https://gitlab.example.com:8443/org/team/sub/repo/");
assert.deepEqual(subgroupRes, {
ok: true,
url: "https://gitlab.example.com:8443/org/team/sub/repo/",
host: "gitlab.example.com",
repoPath: "org/team/sub/repo",
repoName: "repo",
});
const httpRes = parseGitUrl("http://git.site.latha.org/experiments/git-clone");
assert.deepEqual(httpRes, {
ok: true,
url: "http://git.site.latha.org/experiments/git-clone",
host: "git.site.latha.org",
repoPath: "experiments/git-clone",
repoName: "git-clone",
});
const sshRes = parseGitUrl("ssh://git@git.site.latha.org:2222/nandi/cluster.git");
assert.deepEqual(sshRes, {
ok: true,
url: "ssh://git@git.site.latha.org:2222/nandi/cluster.git",
host: "git.site.latha.org",
repoPath: "nandi/cluster",
repoName: "cluster",
});
const gitRes = parseGitUrl("git://example.com/project.git");
assert.deepEqual(gitRes, {
ok: true,
url: "git://example.com/project.git",
host: "example.com",
repoPath: "project",
repoName: "project",
});
});
test("parseGitUrl parses SCP-like SSH syntax", () => {
const res = parseGitUrl("git@github.com:org/my-repo.git");
assert.deepEqual(res, {
ok: true,
url: "git@github.com:org/my-repo.git",
host: "github.com",
repoPath: "org/my-repo",
repoName: "my-repo",
});
const customUser = parseGitUrl("deploy@git.site.latha.org:experiments/k3s");
assert.deepEqual(customUser, {
ok: true,
url: "deploy@git.site.latha.org:experiments/k3s",
host: "git.site.latha.org",
repoPath: "experiments/k3s",
repoName: "k3s",
});
});
test("parseGitUrl rejects unsafe schemes, leading dashes, whitespace, and missing paths", () => {
for (const bad of [
"",
" ",
"file:///etc/passwd",
"ext::sh -c id",
"EXT::sh -c id",
"--upload-pack=evil",
"https://github.com/org/repo extra",
"https://github.com/",
"https://github.com/.",
"https://github.com/org/..",
"not-a-url",
"git@github.com://weird",
"ftp://example.com/repo.git",
]) {
const res = parseGitUrl(bad);
assert.equal(res.ok, false, `Expected failure for: ${bad}`);
}
});
test("validateDirectoryName enforces safe single folder names", () => {
assert.deepEqual(validateDirectoryName(" my-repo "), { ok: true, value: "my-repo" });
for (const bad of ["", ".", "..", "-bad", "a/b", "a\\b", "bad\0name"]) {
assert.equal(validateDirectoryName(bad).ok, false, `Expected failure for: ${bad}`);
}
});
test("validateBranchName accepts valid refs and rejects injection or invalid refs", () => {
assert.deepEqual(validateBranchName("main"), { ok: true, value: "main" });
assert.deepEqual(validateBranchName("feat/git-url-v1.0"), {
ok: true,
value: "feat/git-url-v1.0",
});
for (const bad of [
"",
"-bad",
"--upload-pack",
"a b",
"a..b",
"a@{0}",
"a//b",
"/main",
"main/",
"main.",
"main.lock",
"a~1",
"a^1",
"a:b",
"a?b",
"a*b",
"a[b",
]) {
assert.equal(validateBranchName(bad).ok, false, `Expected failure for: ${bad}`);
}
});
test("normalizeRemoteUrl equates HTTPS, ssh://, and SCP forms for the same repo", () => {
const https = normalizeRemoteUrl("https://user@git.site.latha.org/experiments/git-clone.git");
const sshUrl = normalizeRemoteUrl("ssh://git@git.site.latha.org:2222/experiments/git-clone.git");
const scp = normalizeRemoteUrl("git@git.site.latha.org:experiments/git-clone");
assert.equal(https, "git.site.latha.org/experiments/git-clone");
assert.equal(sshUrl, https);
assert.equal(scp, https);
});
test("destinationPreview joins parent directory and folder name cleanly", () => {
assert.equal(destinationPreview("/workspace/", "git-clone"), "/workspace/git-clone");
assert.equal(destinationPreview("/home/nandi/Projects", "repo"), "/home/nandi/Projects/repo");
assert.equal(destinationPreview("C:\\code\\", "repo"), "C:\\code\\repo");
assert.equal(destinationPreview("", "repo"), "repo");
});