|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { createScriptTestHarness } from "./test-helpers.js"; |
| 6 | + |
| 7 | +const { createTempDir } = createScriptTestHarness(); |
| 8 | + |
| 9 | +function git(cwd: string, ...args: string[]): string { |
| 10 | + return execFileSync("git", args, { cwd, encoding: "utf8" }).trim(); |
| 11 | +} |
| 12 | + |
| 13 | +function writeFile(filePath: string, content: string): void { |
| 14 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 15 | + fs.writeFileSync(filePath, content); |
| 16 | +} |
| 17 | + |
| 18 | +describe("run-opengrep.sh", () => { |
| 19 | + it("validates the rulepack when only OpenGrep rulepack files changed", () => { |
| 20 | + const repo = createTempDir("openclaw-run-opengrep-"); |
| 21 | + git(repo, "init", "-q"); |
| 22 | + git(repo, "config", "user.email", "[email protected]"); |
| 23 | + git(repo, "config", "user.name", "Test User"); |
| 24 | + |
| 25 | + const scriptSource = path.resolve("scripts/run-opengrep.sh"); |
| 26 | + writeFile(path.join(repo, "scripts/run-opengrep.sh"), fs.readFileSync(scriptSource, "utf8")); |
| 27 | + fs.chmodSync(path.join(repo, "scripts/run-opengrep.sh"), 0o755); |
| 28 | + writeFile(path.join(repo, "security/opengrep/precise.yml"), "rules: []\n"); |
| 29 | + git(repo, "add", "."); |
| 30 | + git(repo, "commit", "-qm", "initial"); |
| 31 | + |
| 32 | + fs.appendFileSync(path.join(repo, "security/opengrep/precise.yml"), "# changed\n"); |
| 33 | + const argsPath = path.join(repo, "opengrep-args.txt"); |
| 34 | + const binDir = path.join(repo, "bin"); |
| 35 | + fs.mkdirSync(binDir); |
| 36 | + writeFile( |
| 37 | + path.join(binDir, "opengrep"), |
| 38 | + [ |
| 39 | + "#!/usr/bin/env bash", |
| 40 | + `printf '%s\\n' "$@" > ${JSON.stringify(argsPath)}`, |
| 41 | + "exit 0", |
| 42 | + "", |
| 43 | + ].join("\n"), |
| 44 | + ); |
| 45 | + fs.chmodSync(path.join(binDir, "opengrep"), 0o755); |
| 46 | + |
| 47 | + execFileSync("bash", ["scripts/run-opengrep.sh", "--changed"], { |
| 48 | + cwd: repo, |
| 49 | + env: { |
| 50 | + ...process.env, |
| 51 | + PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`, |
| 52 | + OPENCLAW_OPENGREP_BASE_REF: "HEAD", |
| 53 | + }, |
| 54 | + encoding: "utf8", |
| 55 | + }); |
| 56 | + |
| 57 | + const args = fs.readFileSync(path.join(repo, "opengrep-args.txt"), "utf8"); |
| 58 | + expect(args).toContain("security/opengrep/precise.yml"); |
| 59 | + }); |
| 60 | +}); |
0 commit comments