Skip to content

Commit 127ae42

Browse files
authored
test(tooling): reuse Periphery workflow code fixtures (#134350)
1 parent 268833f commit 127ae42

2 files changed

Lines changed: 34 additions & 45 deletions

File tree

‎test/scripts/ios-periphery-comment-workflow.test.ts‎

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Buffer } from "node:buffer";
22
import { readFileSync } from "node:fs";
33
import { createRequire } from "node:module";
44
import { compileFunction } from "node:vm";
5-
import { deflateRawSync } from "node:zlib";
5+
import { crc32, deflateRawSync } from "node:zlib";
66
import { describe, expect, it } from "vitest";
77
import { parse } from "yaml";
88
import { markdownToIR } from "../../packages/markdown-core/src/ir.js";
@@ -76,7 +76,7 @@ type WorkflowRun = {
7676
workflow_id: number;
7777
};
7878

79-
function commenterScript(): string {
79+
function readCommenterScript(): string {
8080
const workflow = parse(readFileSync(WORKFLOW_PATH, "utf8")) as Workflow;
8181
const step = workflow.jobs?.comment?.steps?.find(
8282
(candidate) => candidate.name === "Upsert Periphery PR comment",
@@ -88,6 +88,14 @@ function commenterScript(): string {
8888
return script;
8989
}
9090

91+
const commenterScript = readCommenterScript();
92+
const executeCommenter = compileFunction(`return (async () => {\n${commenterScript}\n})();`, [
93+
"require",
94+
"context",
95+
"core",
96+
"github",
97+
]) as (require: NodeJS.Require, context: unknown, core: unknown, github: unknown) => Promise<void>;
98+
9199
async function runCommenter(
92100
artifact: Artifact,
93101
archiveData: Buffer,
@@ -103,7 +111,6 @@ async function runCommenter(
103111
workflowRuns?: WorkflowRun[];
104112
} = {},
105113
) {
106-
const script = commenterScript();
107114
const core = {
108115
infos: [] as string[],
109116
warnings: [] as string[],
@@ -225,19 +232,8 @@ async function runCommenter(
225232
repo: "openclaw",
226233
},
227234
};
228-
const execute = compileFunction(`return (async () => {\n${script}\n})();`, [
229-
"require",
230-
"context",
231-
"core",
232-
"github",
233-
]) as (
234-
require: NodeJS.Require,
235-
context: unknown,
236-
core: unknown,
237-
github: unknown,
238-
) => Promise<void>;
239-
240-
await execute(createRequire(import.meta.url), context, core, github);
235+
236+
await executeCommenter(createRequire(import.meta.url), context, core, github);
241237

242238
return {
243239
artifactListCount,
@@ -255,17 +251,6 @@ function expectUnavailableComment(bodies: string[]): void {
255251
expect(bodies[0]).toContain("Periphery did not complete or its report could not be safely read.");
256252
}
257253

258-
function crc32(input: Buffer): number {
259-
let crc = 0xffffffff;
260-
for (const byte of input) {
261-
crc ^= byte;
262-
for (let bit = 0; bit < 8; bit += 1) {
263-
crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
264-
}
265-
}
266-
return (crc ^ 0xffffffff) >>> 0;
267-
}
268-
269254
function u16(value: number): Buffer {
270255
const buffer = Buffer.alloc(2);
271256
buffer.writeUInt16LE(value);
@@ -375,7 +360,7 @@ function setFirstEntryUncompressedSize(archive: Buffer, size: number): Buffer {
375360

376361
describe("iOS Periphery comment workflow", () => {
377362
it("parses the workflow YAML and embedded github-script JavaScript", () => {
378-
const script = commenterScript();
363+
const script = commenterScript;
379364
expect(script).not.toContain("node:child_process");
380365
expect(script).not.toContain("execFileSync");
381366
expect(() =>

‎test/scripts/periphery-scope-workflows.test.ts‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,28 @@ function readWorkflow(workflowPath: string): ScopeWorkflow {
6464
return parse(readFileSync(workflowPath, "utf8")) as ScopeWorkflow;
6565
}
6666

67-
function scopeScript(workflowPath: string): string {
68-
const step = readWorkflow(workflowPath).jobs?.scope?.steps?.find(
69-
(candidate) => candidate.id === "scope",
70-
);
71-
if (!step?.with?.script) {
67+
function compileScopeWorkflow(workflowPath: string) {
68+
const workflow = readWorkflow(workflowPath);
69+
const step = workflow.jobs?.scope?.steps?.find((candidate) => candidate.id === "scope");
70+
const script = step?.with?.script;
71+
if (!script) {
7272
throw new Error(`missing Periphery scope script in ${workflowPath}`);
7373
}
74-
return step.with.script;
74+
const execute = compileFunction(`return (async () => {\n${script}\n})();`, [
75+
"context",
76+
"core",
77+
"exec",
78+
]) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
79+
return { workflow, script, execute };
7580
}
7681

82+
const scopeWorkflows = new Map<string, ReturnType<typeof compileScopeWorkflow>>(
83+
WORKFLOW_CASES.map(({ path: workflowPath }) => [
84+
workflowPath,
85+
compileScopeWorkflow(workflowPath),
86+
]),
87+
);
88+
7789
async function runScope(workflowPath: string, options: ScopeOptions): Promise<string | undefined> {
7890
const outputs = new Map<string, string>();
7991
const context = {
@@ -102,11 +114,7 @@ async function runScope(workflowPath: string, options: ScopeOptions): Promise<st
102114
return { exitCode: changed ? 1 : 0 };
103115
},
104116
};
105-
const execute = compileFunction(`return (async () => {\n${scopeScript(workflowPath)}\n})();`, [
106-
"context",
107-
"core",
108-
"exec",
109-
]) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
117+
const { execute } = scopeWorkflows.get(workflowPath)!;
110118

111119
await execute(
112120
context,
@@ -134,10 +142,9 @@ describe("Periphery scope workflows", () => {
134142
it.each(WORKFLOW_CASES)(
135143
"uses the synthetic merge parent for $name scope",
136144
({ path: workflowPath }) => {
137-
const workflow = readWorkflow(workflowPath);
145+
const { workflow, script } = scopeWorkflows.get(workflowPath)!;
138146
const steps = workflow.jobs?.scope?.steps ?? [];
139147
const checkout = steps.find((step) => step.name === "Checkout");
140-
const script = scopeScript(workflowPath);
141148

142149
expect(workflow.on?.pull_request?.types).toContain("converted_to_draft");
143150
expect(workflow.on?.pull_request?.paths).toBeUndefined();
@@ -210,10 +217,7 @@ describe("Periphery scope workflows", () => {
210217
expect(oldDiff.status).toBe(1);
211218

212219
const outputs = new Map<string, string>();
213-
const execute = compileFunction(
214-
`return (async () => {\n${scopeScript(".github/workflows/shared-openclawkit-periphery.yml")}\n})();`,
215-
["context", "core", "exec"],
216-
) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
220+
const { execute } = scopeWorkflows.get(".github/workflows/shared-openclawkit-periphery.yml")!;
217221
await execute(
218222
{ eventName: "pull_request", payload: { pull_request: { draft: false, number: 123 } } },
219223
{ setOutput: (name: string, value: string) => outputs.set(name, value) },

0 commit comments

Comments
 (0)