Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions site/scripts/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, describe, expect, test } from "bun:test";
import { afterEach, describe, expect, setDefaultTimeout, test } from "bun:test";
import { execFile } from "node:child_process";
import {
mkdir,
Expand Down Expand Up @@ -39,6 +39,16 @@ describe("source fetch command construction", () => {
]);
});

test("joins a base URL without a trailing slash", () => {
const args = buildCloneArgs(
"RigelBuild/meissa",
"/tmp/source",
undefined,
"https://github.com",
);
expect(args).toContain("https://github.com/RigelBuild/meissa.git");
});

test("keeps blob filtering and drops clone depth for a pinned source", () => {
const args = buildCloneArgs(
"RigelBuild/meissa",
Expand Down Expand Up @@ -152,14 +162,19 @@ async function makeRemote(): Promise<Remote> {
await mkdir(join(base, "RigelBuild"), { recursive: true });
await git(["init", "--quiet", "--bare", bare], base);
await git(["symbolic-ref", "HEAD", "refs/heads/main"], bare);
// Without this the server drops --filter and the blob:none path is untested.
// Without this the server drops --filter. The blob:none path is exercised
// here, not verified: nothing checks that blobs were actually skipped.
await git(["config", "uploadpack.allowFilter", "true"], bare);

await git(["init", "--quiet", scratch], base);
await git(["symbolic-ref", "HEAD", "refs/heads/main"], scratch);
await git(["remote", "add", "origin", bare], scratch);
await writeFile(join(scratch, "README.md"), "# one\n");
await writeFile(join(scratch, "docs-bundle.toml"), 'name = "tool"\n');
await mkdir(join(scratch, "docs"), { recursive: true });
await writeFile(join(scratch, "docs", "x.md"), "# docs\n");
await mkdir(join(scratch, "src"), { recursive: true });
await writeFile(join(scratch, "src", "main.ts"), "export {};\n");
await commitAll(scratch, "first");
const first = await git(["rev-parse", "HEAD"], scratch);
await writeFile(join(scratch, "README.md"), "# two\n");
Expand All @@ -170,7 +185,7 @@ async function makeRemote(): Promise<Remote> {
await commitAll(scratch, "side");
const side = await git(["rev-parse", "HEAD"], scratch);
await git(["push", "--quiet", "origin", "main", "side"], scratch);
// Trailing slash: fetch.ts appends `${repo}.git` directly to baseUrl.
// Trailing slash, as in GITHUB_BASE_URL. buildCloneArgs also accepts none.
return { baseUrl: `${pathToFileURL(base).href}/`, first, second, side };
}

Expand All @@ -195,6 +210,9 @@ async function readFetched(workDir: string): Promise<unknown> {
return JSON.parse(await readFile(join(workDir, "fetched.json"), "utf8"));
}

// The bare-repo tests clone for real; a loaded gate host can exceed Bun's 5 s default.
setDefaultTimeout(30_000);

describe("source fetch against a local bare repo", () => {
test("checks out the pinned SHA instead of the branch head", async () => {
const remote = await makeRemote();
Expand Down Expand Up @@ -222,10 +240,10 @@ describe("source fetch against a local bare repo", () => {
expect.objectContaining({ sha: remote.first }),
]);

// runGit labels failures with args[0], which is "-C" for this call.
// runGit labels failures with the git subcommand: merge-base here.
await expect(
fetchTool(remote, new Map([[REPO, remote.side]]), true),
).rejects.toThrow("git -C failed (1)");
).rejects.toThrow("git merge-base failed (1)");

// Control: the same side-branch SHA is accepted when production is off.
const dev = await fetchTool(remote, new Map([[REPO, remote.side]]), false);
Expand All @@ -239,6 +257,10 @@ describe("source fetch against a local bare repo", () => {
const workDir = await fetchTool(remote, new Map<string, string>(), false);
const root = join(workDir, "sources", "tool");
expect(await readFile(join(root, "README.md"), "utf8")).toBe("# two\n");
expect(await readFile(join(root, "docs", "x.md"), "utf8")).toBe("# docs\n");
await expect(stat(join(root, "src", "main.ts"))).rejects.toMatchObject({
code: "ENOENT",
});
await expect(stat(join(root, ".git"))).rejects.toMatchObject({
code: "ENOENT",
});
Expand Down
18 changes: 15 additions & 3 deletions site/scripts/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface FetchOptions {
workDir: string;
pins: ReadonlyMap<string, string>;
production: boolean;
/** Clone URL prefix ending in "/". Defaults to GitHub. */
/** Clone URL prefix. Defaults to GitHub. */
baseUrl?: string;
}

Expand Down Expand Up @@ -85,7 +85,8 @@ export function buildCloneArgs(
"--sparse",
];
if (pin === undefined) args.push("--depth", "1");
args.push(`${baseUrl}${repo}.git`, destination);
const prefix = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
args.push(`${prefix}${repo}.git`, destination);
return args;
}

Expand All @@ -112,6 +113,13 @@ export function buildGitEnvironment(
return safe;
}

// First argument after any leading `-C <dir>` or `-c <kv>` pairs.
function gitSubcommand(args: readonly string[]): string | undefined {
let index = 0;
while (args[index] === "-C" || args[index] === "-c") index += 2;
return args[index];
}

function runGit(
args: readonly string[],
cwd: string,
Expand All @@ -137,7 +145,11 @@ function runGit(
child.on("close", (code) => {
if (code === 0) resolve(stdout.trim());
else
reject(new Error(`git ${args[0]} failed (${code}): ${stderr.trim()}`));
reject(
new Error(
`git ${gitSubcommand(args)} failed (${code}): ${stderr.trim()}`,
),
);
});
});
}
Expand Down
5 changes: 5 additions & 0 deletions site/sources.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"repo": "RigelBuild/meissa",
"mount": "/meissa/",
"config": "default"
},
{
"repo": "RigelBuild/terraform-provider-googleworkspace",
"mount": "/terraform-provider-googleworkspace/",
Expand Down
Loading