test(ci): never execute workflow text in shell harness (#2130)

Co-authored-by: FredApps <FredApps@users.noreply.github.com>
This commit is contained in:
FredAppsandFredApps authored and GitHub committed 2026-11-18 13:05:18 +07:00
1 parent 8bd0f08333
commit af1a6b4261
1 file changed
+74 -6
+74 -6
View File
@@ -5,6 +5,7 @@ import re
import subprocess
import tomllib
import unittest
from unittest.mock import patch
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
@@ -18,19 +19,65 @@ def block(text, name):
return "\n".join(line[8:] for line in match[1].splitlines())
def commands(os_name, fail="", name="command"):
# Functions avoid executing any caller-supplied cargo command or changing the workspace.
# The real bash -euo pipefail semantics still apply, including failures inside a branch.
stub = """
# Only these literal programs may be exercised. Workflow text is compared, never executed.
LINT_SOURCE = """
if [ "$RUNNER_OS" = Linux ]; then
python3 scripts/test-workspace-concurrency.py
python3 scripts/test-workspace-tests.py
fi
cargo fmt --check
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
"""
TEST_SOURCE = """
cargo nextest run --workspace --all-features --locked --profile ci
cargo test --workspace --doc --all-features --locked
cargo nextest run -p craft-image-codecs -p craft-text --locked --profile ci
"""
WINDOWS_SETUP = """powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"'"""
EXPECTED_SOURCE = {
"lint-command": LINT_SOURCE,
"command": TEST_SOURCE,
"windows-command": WINDOWS_SETUP + "\n" + TEST_SOURCE,
}
def source_lines(source):
return [line.strip() for line in source.splitlines()
if line.strip() and not line.lstrip().startswith("#")]
def validate_source(text, name):
if name not in EXPECTED_SOURCE:
raise AssertionError(f"unsupported command block: {name}")
if source_lines(block(text, name)) != source_lines(EXPECTED_SOURCE[name]):
raise AssertionError(f"unexpected command source: {name}")
# The child shell only receives this checked-in literal, never bytes read from the workflow.
HARNESS = """
cargo() {
printf '%s\\n' "$*"
if [ -n "$FAIL" ] && [ "$*" = "$FAIL" ]; then return 17; fi
}
python3() { :; }
case "$BLOCK" in
lint-command)
""" + LINT_SOURCE + """
;;
command)
""" + TEST_SOURCE + """
;;
*) exit 29 ;;
esac
"""
def commands(os_name, fail="", name="command", source=None):
validate_source(WORKFLOW.read_text() if source is None else source, name)
return subprocess.run(
["bash", "-euo", "pipefail", "-c", stub + block(WORKFLOW.read_text(), name)],
env={**os.environ, "RUNNER_OS": os_name, "FAIL": fail},
["bash", "-euo", "pipefail", "-c", HARNESS],
env={**os.environ, "RUNNER_OS": os_name, "FAIL": fail, "BLOCK": name},
text=True, capture_output=True, check=False,
)
@@ -71,10 +118,31 @@ class WorkspaceTests(unittest.TestCase):
self.assertEqual(result.stdout.splitlines(), stages[:index + 1])
def test_windows_runs_the_same_tests(self):
validate_source(WORKFLOW.read_text(), "windows-command")
source = block(WORKFLOW.read_text(), "windows-command")
cargo = [line.strip() for line in source.splitlines() if line.strip().startswith("cargo ")]
self.assertEqual(cargo, ["cargo " + command for command in TESTS])
def test_workflow_source_is_rejected_before_starting_any_process(self):
source = WORKFLOW.read_text()
for payload in ("cargo fmt --check; echo injected", "cargo fmt $(echo injected)",
"cargo fmt --check | cat", "cargo fmt --check > injected",
"echo injected", "cargo fmt --check\n touch injected"):
with self.subTest(payload=payload), patch("subprocess.run") as child:
changed = source.replace("cargo fmt --check", payload, 1)
with self.assertRaisesRegex(AssertionError, "unexpected command source"):
commands("Linux", name="lint-command", source=changed)
child.assert_not_called()
def test_added_or_missing_stage_is_rejected_before_execution(self):
source = WORKFLOW.read_text()
for replacement in ("", "cargo fmt --check\n cargo fmt --check"):
with self.subTest(replacement=replacement), patch("subprocess.run") as child:
with self.assertRaisesRegex(AssertionError, "unexpected command source"):
commands("Linux", name="lint-command",
source=source.replace("cargo fmt --check", replacement, 1))
child.assert_not_called()
def test_ci_profile_keeps_its_failure_policy_without_retries_or_filtering(self):
config = tomllib.loads((ROOT / ".config/nextest.toml").read_text())
self.assertIs(config["profile"]["ci"]["fail-fast"], False)