@reporters/github 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 (2022-12-19)
4
+
5
+ Initial release
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Github Actions Reporter
2
+ A Github actions reporter for `node:test`
3
+
4
+ ## Installation
5
+
6
+ ```bash
7
+ npm install --save-dev @reporters/github
8
+ ```
9
+ or
10
+ ```bash
11
+ yarn add --dev @reporters/github
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```bash
17
+ node --test \
18
+ --test-reporter=@reporters/github --test-reporter-destination=stdout \
19
+ --test-reporter=spec --test-reporter-destination=stdout
20
+ ```
21
+
22
+ ## Features
23
+
24
+ ### annotations
25
+
26
+ ### summary
27
+
package/index.js ADDED
@@ -0,0 +1,88 @@
1
+ const path = require('node:path');
2
+ const util = require('node:util');
3
+ const { EOL } = require('node:os');
4
+ const StackUtils = require('stack-utils');
5
+
6
+ const WORKSPACE = process.env.GITHUB_WORKSPACE ?? '';
7
+
8
+ const stack = new StackUtils({ cwd: WORKSPACE, internals: StackUtils.nodeInternals() });
9
+
10
+ const isFile = (name) => name?.startsWith(WORKSPACE);
11
+
12
+ const getCurrentFile = (name) => (isFile(name) ? path.relative(WORKSPACE, name) : null);
13
+
14
+ const parseStack = (error, file) => {
15
+ const stackLines = (error?.stack ?? '').split(/\r?\n/);
16
+ const line = stackLines.find((l) => l.includes(file)) ?? stackLines[0];
17
+ return line ? stack.parseLine(line) : null;
18
+ };
19
+
20
+ const escapeData = (s = '') => s
21
+ .replace(/%/g, '%25')
22
+ .replace(/\r/g, '%0D')
23
+ .replace(/\n/g, '%0A');
24
+
25
+ const escapeProperty = (s = '') => escapeData(s)
26
+ .replace(/:/g, '%3A')
27
+ .replace(/,/g, '%2C');
28
+
29
+ const propsToString = (props = {}) => {
30
+ const entries = Object.entries(props);
31
+ if (entries.length === 0) {
32
+ return '';
33
+ }
34
+
35
+ const result = entries
36
+ .filter(([, value]) => Boolean(value))
37
+ .map(([key, value]) => `${key}=${escapeProperty(String(value))}`).join(',');
38
+
39
+ return ` ${result}`;
40
+ };
41
+
42
+ const report = (command, message, pros) => process.stdout.write(`::${command}${propsToString(pros)}::${escapeData(message)}${EOL}`);
43
+
44
+ module.exports = async function githubReporter(source) {
45
+ const counter = { pass: 0, fail: 0 };
46
+ const diagnostics = [];
47
+ let currentFile = null;
48
+ for await (const event of source) {
49
+ switch (event.type) {
50
+ case 'test:start':
51
+ currentFile = getCurrentFile(event.data.name) || currentFile;
52
+ report('debug', `starting to run ${event.data.name}`);
53
+ break;
54
+ case 'test:pass':
55
+ counter.pass += 1;
56
+ report('debug', `completed running ${event.data.name}`);
57
+ currentFile = isFile(event.data.name) ? null : currentFile;
58
+ break;
59
+ case 'test:fail': {
60
+ const error = util.inspect(
61
+ event.data.details?.error,
62
+ { colors: false, breakLength: Infinity },
63
+ );
64
+ const location = parseStack(event.data.details?.error, currentFile);
65
+ report('error', error, {
66
+ file: location?.file ?? currentFile,
67
+ line: location?.line,
68
+ col: location?.column,
69
+ title: event.data.name,
70
+ });
71
+ counter.fail += 1;
72
+ currentFile = isFile(event.data.name) ? null : currentFile;
73
+ break;
74
+ } case 'test:diagnostic':
75
+ if (currentFile) {
76
+ report('notice', event.data.message, { file: currentFile });
77
+ } else {
78
+ diagnostics.push(event.data.message);
79
+ }
80
+ break;
81
+ default:
82
+ break;
83
+ }
84
+ }
85
+ report('group', `Test results (${counter.pass} passed, ${counter.fail} failed)`);
86
+ report('notice', diagnostics.join(EOL));
87
+ report('endgroup');
88
+ };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@reporters/github",
3
+ "version": "1.0.0",
4
+ "description": "A junit reporter for `node:test`",
5
+ "keywords": [
6
+ "github actions",
7
+ "node:test",
8
+ "test",
9
+ "reporter",
10
+ "reporters"
11
+ ],
12
+ "dependencies": {
13
+ "stack-utils": "^2.0.6"
14
+ },
15
+ "main": "index.js",
16
+ "repository": "https://github.com/MoLow/reporters.git",
17
+ "author": "Moshe Atlow",
18
+ "license": "MIT"
19
+ }