-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathharness.ts
More file actions
61 lines (56 loc) · 1.75 KB
/
harness.ts
File metadata and controls
61 lines (56 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Test harness for the exec-context-pr-synth bundle.
*
* Provides:
* - `runMain(env)` — invokes the bundle's main() with a captured
* stdout buffer.
* - `makeEnv(overrides)` — returns a minimal env block populated
* with the required vars, easy to override per case.
* - `build_pr_synth_spec(spec)` — base64-encodes a PrSynthSpec JSON
* for the PR_SYNTH_SPEC env var.
*/
import { vi } from "vitest";
import { main } from "../index.js";
import { _resetCompletedForTesting } from "../../shared/vso-logger.js";
export interface RunResult {
code: number;
output: string;
}
export async function runMain(env: NodeJS.ProcessEnv): Promise<RunResult> {
_resetCompletedForTesting();
const chunks: string[] = [];
const writeSpy = vi
.spyOn(process.stdout, "write")
.mockImplementation((c: any) => {
chunks.push(typeof c === "string" ? c : c.toString());
return true;
});
try {
const code = await main(env);
return { code, output: chunks.join("") };
} finally {
writeSpy.mockRestore();
}
}
export function makeEnv(overrides: Record<string, string>): NodeJS.ProcessEnv {
return {
BUILD_REASON: "IndividualCI",
BUILD_REPOSITORY_PROVIDER: "TfsGit",
BUILD_SOURCEBRANCH: "refs/heads/feature/x",
ADO_PROJECT: "MyProject",
ADO_REPO_ID: "00000000-0000-0000-0000-000000000000",
...overrides,
};
}
export function build_pr_synth_spec(
spec: {
branches?: { include: string[]; exclude: string[] };
paths?: { include: string[]; exclude: string[] };
} = {},
): string {
const full = {
branches: spec.branches ?? { include: ["main"], exclude: [] },
paths: spec.paths ?? { include: [], exclude: [] },
};
return Buffer.from(JSON.stringify(full), "utf8").toString("base64");
}