Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/planner/pipeline-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,21 @@ function splitParts(body: string): string[] | undefined {
parts.push(current.trim());
const unquoted = parts.map((part) => part.replace(/"[^"]*"|'[^']*'/g, "\"\""));
// A double-quoted `$` or backtick still expands in the shell.
if (parts.some((part) => !part || /"[^"]*[$`][^"]*"/.test(part)) || unquoted.some((part) => UNSPLITTABLE.test(part))) return undefined;
if (parts.some((part) => !part || expandsBetweenDoubleQuotes(part)) || unquoted.some((part) => UNSPLITTABLE.test(part))) return undefined;
return parts;
}

/**
* True when a `$` or backtick sits between two double quotes, the answer `/"[^"]*[$`][^"]*"/` gives.
* That regex is quadratic on a `"` followed by a long run of backticks with no closing quote (#300);
* this is one linear pass: the text strictly between two consecutive `"` holds a `$` or backtick.
*/
export function expandsBetweenDoubleQuotes(part: string): boolean {
const segments = part.split("\"");
for (let i = 1; i < segments.length - 1; i++) if (/[$`]/.test(segments[i]!)) return true;
return false;
}

function globToRegExp(pattern: string): RegExp {
return new RegExp(`^${pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, ".*").replace(/\*/g, "[^:]*")}$`);
}
Expand Down
8 changes: 7 additions & 1 deletion tests/fuzz/repository-text.fuzz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import fc from "fast-check";
import { evaluateCondition, renderCommand, resolveExpression } from "../../src/ci-inference/expression.js";
import { firstCommandTokens } from "../../src/ci-inference/purpose.js";
import { expressionReferences } from "../../src/ci-inference/resolve.js";
import { discoverGateChecks } from "../../src/planner/pipeline-steps.js";
import { discoverGateChecks, expandsBetweenDoubleQuotes } from "../../src/planner/pipeline-steps.js";
import { nodeTestGlobForPath } from "../../src/planner/test-command.js";
import { contentNamesPath, referencePattern, referenceTokens, widenCharacterClasses } from "../../src/repo/file-references.js";
import { workflowMayRunTests } from "../../src/repo/workflow-test-steps.js";
Expand Down Expand Up @@ -113,6 +113,12 @@ describe("fuzz: linear rewrites agree with the regexes they replaced", () => {
}), runs(1000));
});

it("double-quoted expansion in a script part (#300)", () => {
fc.assert(fc.property(pieces("\"", "$", "`", "a", " ", "'"), (part) => {
assert.equal(expandsBetweenDoubleQuotes(part), /"[^"]*[$`][^"]*"/.test(part));
}), runs(1000));
});

it("first command of a step", () => {
fc.assert(fc.property(pieces(" ", "\t", "|", "&", ";", "a", "b"), (line) => {
const [first = ""] = line.trim().split(/\s*(?:\|\||&&|\||;)\s*/);
Expand Down
7 changes: 7 additions & 0 deletions tests/planner/pipeline-steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ describe("discoverGateChecks", () => {
}
});

it("reads a quote followed by a long run of backticks in linear time (#300)", () => {
const body = `tsc && ""${"`".repeat(100_000)}`;
const started = performance.now();
assert.deepEqual(discoverGateChecks({ check: body }).checks.map((c) => [c.id, c.runsScript]), [["check", true]]);
assert.ok(performance.now() - started < 1000, `took ${Math.round(performance.now() - started)} ms`);
});

it("leaves test runs to test selection but keeps mixed bodies", () => {
const gate = discoverGateChecks({ check: "npm run lint && npm test", lint: "eslint .", test: "vitest run" });
assert.deepEqual(gate.checks.map((c) => c.id), ["lint"]);
Expand Down
Loading