Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use reported file #30

Merged
merged 1 commit into from
Feb 2, 2023
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
15 changes: 5 additions & 10 deletions packages/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stack = new StackUtils({ cwd: WORKSPACE, internals: StackUtils.nodeInterna

const isFile = (name) => name?.startsWith(WORKSPACE);

const getCurrentFile = (name) => (isFile(name) ? path.relative(WORKSPACE, name) : null);
const getFilePath = (name) => (isFile(name) ? path.relative(WORKSPACE, require.resolve(name) ?? '') : null);

const parseStack = (error, file) => {
const stackLines = (error?.stack ?? '').split(/\r?\n/);
Expand All @@ -35,37 +35,32 @@ const DIAGNOSTIC_VALUES = {
module.exports = async function githubReporter(source) {
const counter = { pass: 0, fail: 0 };
const diagnostics = [];
let currentFile = null;
for await (const event of source) {
switch (event.type) {
case 'test:start':
currentFile = getCurrentFile(event.data.name) || currentFile;
core.debug(`starting to run ${event.data.name}`);
break;
case 'test:pass':
counter.pass += 1;
core.debug(`completed running ${event.data.name}`);
currentFile = isFile(event.data.name) ? null : currentFile;
break;
case 'test:fail': {
const error = util.inspect(
event.data.details?.error,
{ colors: false, breakLength: Infinity },
);
const location = parseStack(event.data.details?.error, currentFile);
const location = parseStack(event.data.details?.error, getFilePath(event.data.file));
core.error(error, {
file: location?.file ?? currentFile,
file: location?.file ?? getFilePath(event.data.file),
startLine: location?.line,
startColumn: location?.column,
title: event.data.name,
});
counter.fail += 1;
currentFile = isFile(event.data.name) ? null : currentFile;
break;
} case 'test:diagnostic':
if (currentFile) {
core.notice(event.data.message, { file: currentFile });
} else {
core.notice(event.data.message, { file: getFilePath(event.data.file) });
if (event.data.nesting === 0) {
diagnostics.push(event.data.message);
}
break;
Expand Down
3 changes: 2 additions & 1 deletion packages/github/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { spawnSync } = require('child_process');
const { tmpdir } = require('os');
const { join } = require('path');
const assert = require('assert');
const path = require('path');
const { readFileSync, writeFileSync } = require('fs');
const { compareLines } = require('../../../tests/utils');
const output = require('./output');
Expand All @@ -10,7 +11,7 @@ const GITHUB_STEP_SUMMARY = join(tmpdir(), 'github-actions-test-reporter');
writeFileSync(GITHUB_STEP_SUMMARY, '');

const child = spawnSync(process.execPath, ['--test-reporter', './index.js', '../../tests/example'], {
env: { GITHUB_STEP_SUMMARY },
env: { GITHUB_STEP_SUMMARY, GITHUB_WORKSPACE: path.resolve(__dirname, '../../../') },
});

assert.strictEqual(child.stderr?.toString(), '');
Expand Down
11 changes: 9 additions & 2 deletions packages/github/tests/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ module.exports = {
::debug::starting to run is ok
::debug::completed running is ok
::debug::starting to run fails
::error title=fails,file=fails::\\[Error \\[ERR_TEST_FAILURE\\]: this is an error\\] {%0A failureType: 'testCodeFailure',%0A cause: Error: this is an error%0A at Object.<anonymous> (.*/example.js:6:11).*
::error title=tests::\\[Error \\[ERR_TEST_FAILURE\\]: 1 subtest failed\\] { failureType: 'subtestsFailed', cause: '1 subtest failed', code: 'ERR_TEST_FAILURE' }
::error title=fails,file=tests/example.js::\\[Error \\[ERR_TEST_FAILURE\\]: this is an error\\] {%0A failureType: 'testCodeFailure',%0A cause: Error: this is an error%0A at Object.<anonymous> (.*/example.js:6:11).*
::error title=tests,file=tests/example.js::\\[Error \\[ERR_TEST_FAILURE\\]: 1 subtest failed\\] { failureType: 'subtestsFailed', cause: '1 subtest failed', code: 'ERR_TEST_FAILURE' }
::notice file=tests/example.js::tests 1
::notice file=tests/example.js::pass 0
::notice file=tests/example.js::fail 1
::notice file=tests/example.js::cancelled 0
::notice file=tests/example.js::skipped 0
::notice file=tests/example.js::todo 0
::notice file=tests/example.js::duration_ms .*
::group::Test results \\(1 passed, 2 failed\\)
::notice::Total Tests: 1%0APassed ✅: 0%0AFailed ❌: 1%0ACanceled 🚫: 0%0ASkipped ⏭️: 0%0ATodo 📝: 0%0ADuration: .*ms
::endgroup::
Expand Down