Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

ci: run-named prints a failing command's failure lines after its group (#10054)

Cindy Zhang9c80ad12f6c9
diff --git a/.github/scripts/run-named.sh b/.github/scripts/run-named.sh
index 24eed1b1eb..f537809fc4 100755
--- a/.github/scripts/run-named.sh
+++ b/.github/scripts/run-named.sh
@@ -3,7 +3,10 @@
#
# Runs one command of a CI step that runs several, inside a named log group.
# When the command fails, an error annotation names it, so a passing summary
-# from an earlier command never reads as the step's result.
+# from an earlier command never reads as the step's result. Its failure lines
+# (from Vitest's failure report: FAIL, AssertionError and Error: lines, at
+# most 30) follow the group, so the step view shows the cause without
+# unfolding the group. Vitest's own annotations name each file and line.
#
# run-named.sh "<label>" [--stdout <file>] -- <command> [args...]
#
@@ -18,15 +21,46 @@ if [ "${1:-}" = --stdout ]; then
shift 2
fi
if [ "${1:-}" = -- ]; then shift; fi
+log=$(mktemp "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/run-named.XXXXXX")
+trap 'rm -f "$log"' EXIT
echo "::group::${label}"
if [ -n "$out" ]; then
- "$@" > "$out"
+ { "$@" > "$out"; } 2>&1 | tee "$log" >&2
else
- "$@"
+ "$@" 2>&1 | tee "$log"
fi
-status=$?
+status=${PIPESTATUS[0]}
echo "::endgroup::"
if [ "$status" -ne 0 ]; then
- echo "::error title=${label} failed::${label} exited with status ${status}. Its output is in the \"${label}\" group of this step's log."
+ # Colour codes would hide the patterns. A line starting with :: is a
+ # workflow command (Vitest's own annotations): echoing it again would
+ # annotate the failure twice.
+ plain=$(sed $'s/\x1b\\[[0-9;]*m//g' "$log" | grep -v '^::' || true)
+ # Vitest's failure report starts at its first banner: Failed Suites (a
+ # file that cannot load, a failing hook), Failed Tests, or Unhandled
+ # Errors. A passing test's console output before it can hold "Error:" too.
+ report=$(awk '/Failed (Suites|Tests) [0-9]+|Unhandled Errors/ { found = 1 } found' <<< "$plain")
+ if [ -n "$report" ]; then
+ all=$(grep -E '^ ?FAIL |AssertionError|Error:' <<< "$report" || true)
+ failures=$(head -n 30 <<< "$all")
+ shown=$(grep -cE '^ ?FAIL ' <<< "$failures" || true)
+ tests=$(grep -cE '^ ?FAIL ' <<< "$report" || true)
+ else
+ failures=$(grep -E '( FAIL |AssertionError|Error:)' <<< "$plain" | head -n 30 || true)
+ tests=0
+ fi
+ message="${label} exited with status ${status}."
+ if [ -n "$failures" ]; then
+ echo "${label} failed. Its failure lines:"
+ echo "$failures"
+ if [ "$tests" -gt 0 ]; then
+ listed="listed after the \"${label}\" group"
+ [ "$shown" -lt "$tests" ] && listed="the first ${shown} listed after the \"${label}\" group"
+ message="${label} exited with status ${status}; ${tests} failing test(s) or file(s), ${listed}. Vitest's annotations name each file and line."
+ else
+ message="${message} Its failure lines follow the \"${label}\" group."
+ fi
+ fi
+ echo "::error title=${label} failed::${message} Its output is in the \"${label}\" group of this step's log."
fi
exit "$status"
diff --git a/.github/scripts/run-named.test.mjs b/.github/scripts/run-named.test.mjs
index 881d079f96..249bb441a7 100644
--- a/.github/scripts/run-named.test.mjs
+++ b/.github/scripts/run-named.test.mjs
@@ -69,6 +69,126 @@ describe('run-named.sh', () => {
});
});
+describe('run-named.sh on a failing test run', () => {
+ // Vitest's shape in CI: a passing test's console output (which can hold
+ // "Error:"), its github-actions annotation, then the failure report after
+ // the "Failed Tests" banner, with colour codes.
+ const vitestFailure = [
+ 'printf "\\033[32m ✓\\033[39m |node| a.test.mjs (3 tests)\\n"',
+ 'printf "stderr | src/frontmatter.test.tsx > rejects bad metadata\\n"',
+ 'printf "TypeError: frontmatter metadata must be finite JSON-like data\\n"',
+ 'printf "::error file=/repo/.github/scripts/ci-test-routing.test.mjs,line=287,column=45::AssertionError: expected 1 to be 2\\n"',
+ 'printf "\\033[31m⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯\\033[39m\\n"',
+ 'printf "\\033[31m FAIL \\033[39m |node| .github/scripts/ci-test-routing.test.mjs > ci.yml test routing > splits each lane\\n"',
+ 'printf "AssertionError: expected 1 to be 2\\n"',
+ 'printf " ❯ .github/scripts/ci-test-routing.test.mjs:287:45\\n"',
+ 'printf " Test Files 1 failed | 1 passed (2)\\n"',
+ 'exit 1',
+ ].join('; ');
+ const afterGroup = result =>
+ result.stdout.slice(result.stdout.indexOf('::endgroup::'));
+
+ it("prints Vitest's failure report lines after the group", () => {
+ const result = run(
+ 'Tests that read source files',
+ '--',
+ 'bash',
+ '-c',
+ vitestFailure,
+ );
+ expect(result.status).toBe(1);
+ const after = afterGroup(result);
+ expect(after).toContain(
+ 'Tests that read source files failed. Its failure lines:\n FAIL |node| .github/scripts/ci-test-routing.test.mjs > ci.yml test routing > splits each lane\nAssertionError: expected 1 to be 2\n',
+ );
+ expect(after).not.toContain('\u001b[');
+ // A passing test's console output is not a failure line.
+ expect(after).not.toContain('frontmatter');
+ });
+
+ it("annotates once: Vitest's own annotation is not echoed, and run-named's names the count", () => {
+ const result = run(
+ 'Tests that read source files',
+ '--',
+ 'bash',
+ '-c',
+ vitestFailure,
+ );
+ const after = afterGroup(result).split('\n');
+ expect(after.filter(line => line.startsWith('::error')).length).toBe(1);
+ expect(after.find(line => line.startsWith('::error'))).toBe(
+ '::error title=Tests that read source files failed::Tests that read source files exited with status 1; 1 failing test(s) or file(s), listed after the "Tests that read source files" group. Vitest\'s annotations name each file and line. Its output is in the "Tests that read source files" group of this step\'s log.',
+ );
+ });
+
+ it("reads a file that cannot load from Vitest's Failed Suites section", () => {
+ const fixture = [
+ 'printf "stderr | src/frontmatter.test.tsx > rejects bad metadata\\n"',
+ 'printf "TypeError: frontmatter metadata must be finite JSON-like data\\n"',
+ 'printf "⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯\\n"',
+ 'printf " FAIL |ui| packages/core/src/List/List.test.tsx [ packages/core/src/List/List.test.tsx ]\\n"',
+ 'printf "Error: Failed to load url ./missing (resolved id: ./missing)\\n"',
+ 'exit 1',
+ ].join('; ');
+ const result = run('T', '--', 'bash', '-c', fixture);
+ const after = afterGroup(result);
+ expect(after).toContain(
+ 'T failed. Its failure lines:\n FAIL |ui| packages/core/src/List/List.test.tsx',
+ );
+ expect(after).toContain('Error: Failed to load url ./missing');
+ expect(after).not.toContain('frontmatter');
+ expect(after).toContain('1 failing test(s) or file(s), listed after');
+ });
+
+ it('caps the failure lines at 30', () => {
+ const result = run(
+ 'T',
+ '--',
+ 'bash',
+ '-c',
+ 'echo "⎯ Failed Tests 50 ⎯"; for i in $(seq 1 50); do echo " FAIL |ui| f$i.test.tsx"; done; exit 1',
+ );
+ const lines = afterGroup(result).split('\n');
+ expect(lines.filter(line => line.startsWith(' FAIL ')).length).toBe(30);
+ expect(lines.find(line => line.startsWith('::error'))).toContain(
+ '50 failing test(s) or file(s), the first 30 listed after',
+ );
+ });
+
+ it('reads a non-Vitest failure from standard error with --stdout', () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'run-named-'));
+ try {
+ const file = path.join(dir, 'list.txt');
+ const result = run(
+ 'List',
+ '--stdout',
+ file,
+ '--',
+ 'bash',
+ '-c',
+ 'echo listed; echo "Error: cannot read the list" >&2; exit 1',
+ );
+ expect(fs.readFileSync(file, 'utf8')).toBe('listed\n');
+ expect(result.stderr).toContain('Error: cannot read the list');
+ expect(result.stdout).toContain(
+ 'List failed. Its failure lines:\nError: cannot read the list\n',
+ );
+ expect(result.stdout).toContain(
+ '::error title=List failed::List exited with status 1. Its failure lines follow the "List" group.',
+ );
+ } finally {
+ fs.rmSync(dir, {recursive: true, force: true});
+ }
+ });
+
+ it('adds nothing but the old error when a failing command prints no failure line', () => {
+ const result = run('T', '--', 'bash', '-c', 'echo quiet; exit 4');
+ expect(result.stdout).toBe(
+ '::group::T\nquiet\n::endgroup::\n::error title=T failed::T exited with status 4. Its output is in the "T" group of this step\'s log.\n',
+ );
+ });
+});
+
describe('the UI and Node test steps', () => {
const workflow = YAML.parse(
fs.readFileSync(path.join(root, '.github/workflows/ci.yml'), 'utf8'),