merge: integrate review-14-script-json-output
This commit is contained in:
commit
e872beaab8
4 files changed
+51
-16
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import { expect, spyOn, test } from "bun:test";
|
||||
import { mkdtempSync, readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { parseArgs } from "../../parser.js";
|
||||
import { handleWorkflowLintScript, handleWorkflowEvalScript } from "./script.js";
|
||||
import * as shared from "./shared.js";
|
||||
import * as services from "../../../domains/platform/services.js";
|
||||
|
||||
test("all local and remote script JSON branches return reports and honor output files", async () => {
|
||||
const client = spyOn(shared, "createWorkflowDetailClient").mockResolvedValue({ client: {} } as any);
|
||||
const detail = spyOn(services, "getWorkflowDetail").mockResolvedValue({ id: "wf", tasks: [{ name: "code", type: "code-js2", code: "return 7;" }] } as any);
|
||||
const root = mkdtempSync(join(tmpdir(), "hose-script-json-"));
|
||||
try {
|
||||
for (const action of ["lint-script", "eval-script"]) {
|
||||
for (const remote of [false, true]) {
|
||||
const handler = action === "lint-script" ? handleWorkflowLintScript : handleWorkflowEvalScript;
|
||||
const args = remote ? ["wf", "code", "--input", '{"x":1}'] : ["--code", "return 7;"];
|
||||
const parsed = parseArgs(["workflow", action, ...args, "--json"]);
|
||||
const report = await handler(parsed, { jsonMode: true } as any);
|
||||
expect(report.exitCode).toBe(0);
|
||||
const data = JSON.parse(report.stdout);
|
||||
if (action === "eval-script") expect(data.returnValue).toBe(7);
|
||||
else expect(data.valid ?? data.allValid).toBe(true);
|
||||
const outputFile = join(root, `${action}-${remote}.json`);
|
||||
const output = await handler(parsed, { jsonMode: true, outputOptions: { outputFile } } as any);
|
||||
expect(JSON.parse(output.stdout).outputFile).toBe(outputFile);
|
||||
expect(JSON.parse(readFileSync(outputFile, "utf8"))).toEqual(expect.objectContaining(action === "eval-script" ? { returnValue: 7 } : {}));
|
||||
}
|
||||
}
|
||||
} finally { client.mockRestore(); detail.mockRestore(); }
|
||||
});
|
||||
test("script failures retain JSON output and failure exit code", async () => {
|
||||
const result = await handleWorkflowEvalScript(parseArgs(["workflow", "eval-script", "--code", "throw new Error('failure');", "-j"]), { jsonMode: true } as any);
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(JSON.parse(result.stdout).error.message).toBe("failure");
|
||||
});
|
||||
@@ -20,7 +20,7 @@ export async function handleWorkflowLintScript(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const isJson = booleanOption(parsed.options, ["json"]);
|
||||
const isJson = runtime.jsonMode || booleanOption(parsed.options, ["json", "j"]);
|
||||
const filePath = stringOption(parsed.options, ["file", "path"]);
|
||||
const codeInline = stringOption(parsed.options, ["code"]);
|
||||
const explicitType = (stringOption(parsed.options, ["type"]) || "code-js2").toLowerCase();
|
||||
@@ -68,8 +68,7 @@ export async function handleWorkflowLintScript(
|
||||
};
|
||||
|
||||
if (isJson) {
|
||||
printJson(report);
|
||||
return { stdout: "", stderr: "", exitCode: hasErrors ? 1 : 0 };
|
||||
return { stdout: printJson(report, runtime.outputOptions), stderr: "", exitCode: hasErrors ? 1 : 0 };
|
||||
}
|
||||
|
||||
const lines: string[] = [];
|
||||
@@ -132,8 +131,7 @@ export async function handleWorkflowLintScript(
|
||||
const report = lintWorkflowScripts(workflow, targetNode);
|
||||
|
||||
if (isJson) {
|
||||
printJson(report);
|
||||
return { stdout: "", stderr: "", exitCode: report.allValid ? 0 : 1 };
|
||||
return { stdout: printJson(report, runtime.outputOptions), stderr: "", exitCode: report.allValid ? 0 : 1 };
|
||||
}
|
||||
|
||||
const lines: string[] = [];
|
||||
@@ -193,7 +191,7 @@ export async function handleWorkflowEvalScript(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const isJson = booleanOption(parsed.options, ["json"]);
|
||||
const isJson = runtime.jsonMode || booleanOption(parsed.options, ["json", "j"]);
|
||||
const filePath = stringOption(parsed.options, ["file", "path"]);
|
||||
const codeInline = stringOption(parsed.options, ["code"]);
|
||||
const timeoutStr = stringOption(parsed.options, ["timeout"]);
|
||||
@@ -250,8 +248,7 @@ export async function handleWorkflowEvalScript(
|
||||
const evalResult = await executeJavaScriptSandbox(code, inputs, { timeoutMs });
|
||||
|
||||
if (isJson) {
|
||||
printJson(evalResult);
|
||||
return { stdout: "", stderr: "", exitCode: evalResult.success ? 0 : 1 };
|
||||
return { stdout: printJson(evalResult, runtime.outputOptions), stderr: "", exitCode: evalResult.success ? 0 : 1 };
|
||||
}
|
||||
|
||||
return formatEvalCliResult(evalResult, filePath ? `file: ${filePath}` : "inline-code");
|
||||
@@ -358,8 +355,7 @@ export async function handleWorkflowEvalScript(
|
||||
const evalResult = await executeJavaScriptSandbox(code, inputs, { timeoutMs });
|
||||
|
||||
if (isJson) {
|
||||
printJson(evalResult);
|
||||
return { stdout: "", stderr: "", exitCode: evalResult.success ? 0 : 1 };
|
||||
return { stdout: printJson(evalResult, runtime.outputOptions), stderr: "", exitCode: evalResult.success ? 0 : 1 };
|
||||
}
|
||||
|
||||
const targetDesc = `节点 [${task.name}] ${task.label || task.displayName || ""} (${pipelineId})`;
|
||||
|
||||
Reference in new issue
Block a user