test(tooling): reuse Periphery workflow code fixtures (#134350)

This commit is contained in:
Peter Steinberger 2026-08-31 11:15:53 -07:00 • committed by GitHub
parent 268833fc63
commit 127ae428ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 44 deletions

View file

@ -2,7 +2,7 @@ import { Buffer } from "node:buffer";
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import { compileFunction } from "node:vm";
import { deflateRawSync } from "node:zlib";
import { crc32, deflateRawSync } from "node:zlib";
import { describe, expect, it } from "vitest";
import { parse } from "yaml";
import { markdownToIR } from "../../packages/markdown-core/src/ir.js";
@ -76,7 +76,7 @@ type WorkflowRun = {
workflow_id: number;
};
function commenterScript(): string {
function readCommenterScript(): string {
const workflow = parse(readFileSync(WORKFLOW_PATH, "utf8")) as Workflow;
const step = workflow.jobs?.comment?.steps?.find(
(candidate) => candidate.name === "Upsert Periphery PR comment",
@ -88,6 +88,14 @@ function commenterScript(): string {
return script;
}
const commenterScript = readCommenterScript();
const executeCommenter = compileFunction(`return (async () => {\n${commenterScript}\n})();`, [
"require",
"context",
"core",
"github",
]) as (require: NodeJS.Require, context: unknown, core: unknown, github: unknown) => Promise<void>;
async function runCommenter(
artifact: Artifact,
archiveData: Buffer,
@ -103,7 +111,6 @@ async function runCommenter(
workflowRuns?: WorkflowRun[];
} = {},
) {
const script = commenterScript();
const core = {
infos: [] as string[],
warnings: [] as string[],
@ -225,19 +232,8 @@ async function runCommenter(
repo: "openclaw",
},
};
const execute = compileFunction(`return (async () => {\n${script}\n})();`, [
"require",
"context",
"core",
"github",
]) as (
require: NodeJS.Require,
context: unknown,
core: unknown,
github: unknown,
) => Promise<void>;
await execute(createRequire(import.meta.url), context, core, github);
await executeCommenter(createRequire(import.meta.url), context, core, github);
return {
artifactListCount,
@ -255,17 +251,6 @@ function expectUnavailableComment(bodies: string[]): void {
expect(bodies[0]).toContain("Periphery did not complete or its report could not be safely read.");
}
function crc32(input: Buffer): number {
let crc = 0xffffffff;
for (const byte of input) {
crc ^= byte;
for (let bit = 0; bit < 8; bit += 1) {
crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1));
}
}
return (crc ^ 0xffffffff) >>> 0;
}
function u16(value: number): Buffer {
const buffer = Buffer.alloc(2);
buffer.writeUInt16LE(value);
@ -375,7 +360,7 @@ function setFirstEntryUncompressedSize(archive: Buffer, size: number): Buffer {
describe("iOS Periphery comment workflow", () => {
it("parses the workflow YAML and embedded github-script JavaScript", () => {
const script = commenterScript();
const script = commenterScript;
expect(script).not.toContain("node:child_process");
expect(script).not.toContain("execFileSync");
expect(() =>

View file

@ -64,16 +64,28 @@ function readWorkflow(workflowPath: string): ScopeWorkflow {
return parse(readFileSync(workflowPath, "utf8")) as ScopeWorkflow;
}
function scopeScript(workflowPath: string): string {
const step = readWorkflow(workflowPath).jobs?.scope?.steps?.find(
(candidate) => candidate.id === "scope",
);
if (!step?.with?.script) {
function compileScopeWorkflow(workflowPath: string) {
const workflow = readWorkflow(workflowPath);
const step = workflow.jobs?.scope?.steps?.find((candidate) => candidate.id === "scope");
const script = step?.with?.script;
if (!script) {
throw new Error(`missing Periphery scope script in ${workflowPath}`);
}
return step.with.script;
const execute = compileFunction(`return (async () => {\n${script}\n})();`, [
"context",
"core",
"exec",
]) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
return { workflow, script, execute };
}
const scopeWorkflows = new Map<string, ReturnType<typeof compileScopeWorkflow>>(
WORKFLOW_CASES.map(({ path: workflowPath }) => [
workflowPath,
compileScopeWorkflow(workflowPath),
]),
);
async function runScope(workflowPath: string, options: ScopeOptions): Promise<string | undefined> {
const outputs = new Map<string, string>();
const context = {
@ -102,11 +114,7 @@ async function runScope(workflowPath: string, options: ScopeOptions): Promise<st
return { exitCode: changed ? 1 : 0 };
},
};
const execute = compileFunction(`return (async () => {\n${scopeScript(workflowPath)}\n})();`, [
"context",
"core",
"exec",
]) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
const { execute } = scopeWorkflows.get(workflowPath)!;
await execute(
context,
@ -134,10 +142,9 @@ describe("Periphery scope workflows", () => {
it.each(WORKFLOW_CASES)(
"uses the synthetic merge parent for $name scope",
({ path: workflowPath }) => {
const workflow = readWorkflow(workflowPath);
const { workflow, script } = scopeWorkflows.get(workflowPath)!;
const steps = workflow.jobs?.scope?.steps ?? [];
const checkout = steps.find((step) => step.name === "Checkout");
const script = scopeScript(workflowPath);
expect(workflow.on?.pull_request?.types).toContain("converted_to_draft");
expect(workflow.on?.pull_request?.paths).toBeUndefined();
@ -210,10 +217,7 @@ describe("Periphery scope workflows", () => {
expect(oldDiff.status).toBe(1);
const outputs = new Map<string, string>();
const execute = compileFunction(
`return (async () => {\n${scopeScript(".github/workflows/shared-openclawkit-periphery.yml")}\n})();`,
["context", "core", "exec"],
) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
const { execute } = scopeWorkflows.get(".github/workflows/shared-openclawkit-periphery.yml")!;
await execute(
{ eventName: "pull_request", payload: { pull_request: { draft: false, number: 123 } } },
{ setOutput: (name: string, value: string) => outputs.set(name, value) },