@testomatio/reporter 1.6.13 → 2.0.1-beta-esm

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.
Files changed (142) hide show
  1. package/lib/adapter/codecept.d.ts +2 -0
  2. package/lib/adapter/codecept.js +295 -335
  3. package/lib/adapter/cucumber/current.d.ts +14 -0
  4. package/lib/adapter/cucumber/current.js +195 -203
  5. package/lib/adapter/cucumber/legacy.d.ts +0 -0
  6. package/lib/adapter/cucumber/legacy.js +130 -155
  7. package/lib/adapter/cucumber.d.ts +2 -0
  8. package/lib/adapter/cucumber.js +5 -16
  9. package/lib/adapter/cypress-plugin/index.d.ts +2 -0
  10. package/lib/adapter/cypress-plugin/index.js +93 -105
  11. package/lib/adapter/jasmine.d.ts +11 -0
  12. package/lib/adapter/jasmine.js +54 -53
  13. package/lib/adapter/jest.d.ts +13 -0
  14. package/lib/adapter/jest.js +97 -99
  15. package/lib/adapter/mocha.d.ts +2 -0
  16. package/lib/adapter/mocha.js +112 -140
  17. package/lib/adapter/playwright.d.ts +14 -0
  18. package/lib/adapter/playwright.js +195 -231
  19. package/lib/adapter/vitest.d.ts +35 -0
  20. package/lib/adapter/vitest.js +150 -149
  21. package/lib/adapter/webdriver.d.ts +24 -0
  22. package/lib/adapter/webdriver.js +134 -119
  23. package/lib/bin/cli.d.ts +2 -0
  24. package/lib/bin/cli.js +164 -211
  25. package/lib/bin/reportXml.d.ts +2 -0
  26. package/lib/bin/reportXml.js +49 -52
  27. package/lib/bin/startTest.d.ts +2 -0
  28. package/lib/bin/startTest.js +82 -95
  29. package/lib/bin/uploadArtifacts.d.ts +2 -0
  30. package/lib/bin/uploadArtifacts.js +55 -61
  31. package/lib/client.d.ts +76 -0
  32. package/lib/client.js +411 -465
  33. package/lib/config.d.ts +1 -0
  34. package/lib/config.js +16 -21
  35. package/lib/constants.d.ts +25 -0
  36. package/lib/constants.js +50 -44
  37. package/lib/data-storage.d.ts +34 -0
  38. package/lib/data-storage.js +206 -188
  39. package/lib/junit-adapter/adapter.d.ts +9 -0
  40. package/lib/junit-adapter/adapter.js +17 -20
  41. package/lib/junit-adapter/csharp.d.ts +4 -0
  42. package/lib/junit-adapter/csharp.js +18 -14
  43. package/lib/junit-adapter/index.d.ts +3 -0
  44. package/lib/junit-adapter/index.js +27 -25
  45. package/lib/junit-adapter/java.d.ts +5 -0
  46. package/lib/junit-adapter/java.js +41 -53
  47. package/lib/junit-adapter/javascript.d.ts +4 -0
  48. package/lib/junit-adapter/javascript.js +30 -27
  49. package/lib/junit-adapter/python.d.ts +5 -0
  50. package/lib/junit-adapter/python.js +38 -37
  51. package/lib/junit-adapter/ruby.d.ts +4 -0
  52. package/lib/junit-adapter/ruby.js +11 -8
  53. package/lib/output.d.ts +11 -0
  54. package/lib/output.js +44 -52
  55. package/lib/package.json +3 -0
  56. package/lib/pipe/bitbucket.d.ts +23 -0
  57. package/lib/pipe/bitbucket.js +210 -229
  58. package/lib/pipe/csv.d.ts +47 -0
  59. package/lib/pipe/csv.js +113 -126
  60. package/lib/pipe/debug.d.ts +29 -0
  61. package/lib/pipe/debug.js +104 -99
  62. package/lib/pipe/github.d.ts +30 -0
  63. package/lib/pipe/github.js +186 -213
  64. package/lib/pipe/gitlab.d.ts +23 -0
  65. package/lib/pipe/gitlab.js +166 -207
  66. package/lib/pipe/html.d.ts +34 -0
  67. package/lib/pipe/html.js +260 -319
  68. package/lib/pipe/index.d.ts +1 -0
  69. package/lib/pipe/index.js +84 -66
  70. package/lib/pipe/testomatio.d.ts +70 -0
  71. package/lib/pipe/testomatio.js +413 -462
  72. package/lib/reporter-functions.d.ts +34 -0
  73. package/lib/reporter-functions.js +28 -26
  74. package/lib/reporter.d.ts +232 -0
  75. package/lib/reporter.js +34 -29
  76. package/lib/services/artifacts.d.ts +33 -0
  77. package/lib/services/artifacts.js +55 -51
  78. package/lib/services/index.d.ts +9 -0
  79. package/lib/services/index.js +14 -12
  80. package/lib/services/key-values.d.ts +27 -0
  81. package/lib/services/key-values.js +56 -53
  82. package/lib/services/logger.d.ts +64 -0
  83. package/lib/services/logger.js +227 -245
  84. package/lib/template/testomatio.hbs +651 -1366
  85. package/lib/uploader.d.ts +60 -0
  86. package/lib/uploader.js +291 -360
  87. package/lib/utils/pipe_utils.d.ts +41 -0
  88. package/lib/utils/pipe_utils.js +89 -85
  89. package/lib/utils/utils.d.ts +45 -0
  90. package/lib/utils/utils.js +347 -307
  91. package/lib/xmlReader.d.ts +92 -0
  92. package/lib/xmlReader.js +490 -529
  93. package/package.json +57 -15
  94. package/src/adapter/codecept.js +375 -0
  95. package/src/adapter/cucumber/current.js +228 -0
  96. package/src/adapter/cucumber/legacy.js +158 -0
  97. package/src/adapter/cucumber.js +4 -0
  98. package/src/adapter/cypress-plugin/index.js +112 -0
  99. package/src/adapter/jasmine.js +60 -0
  100. package/src/adapter/jest.js +107 -0
  101. package/src/adapter/mocha.cjs +2 -0
  102. package/src/adapter/mocha.js +157 -0
  103. package/src/adapter/playwright.js +250 -0
  104. package/src/adapter/vitest.js +183 -0
  105. package/src/adapter/webdriver.js +142 -0
  106. package/src/bin/cli.js +280 -0
  107. package/src/bin/reportXml.js +74 -0
  108. package/src/bin/startTest.js +123 -0
  109. package/src/bin/uploadArtifacts.js +90 -0
  110. package/src/client.js +504 -0
  111. package/src/config.js +30 -0
  112. package/src/constants.js +53 -0
  113. package/src/data-storage.js +204 -0
  114. package/src/junit-adapter/adapter.js +23 -0
  115. package/src/junit-adapter/csharp.js +16 -0
  116. package/src/junit-adapter/index.js +28 -0
  117. package/src/junit-adapter/java.js +58 -0
  118. package/src/junit-adapter/javascript.js +31 -0
  119. package/src/junit-adapter/python.js +42 -0
  120. package/src/junit-adapter/ruby.js +10 -0
  121. package/src/output.js +57 -0
  122. package/src/pipe/bitbucket.js +254 -0
  123. package/src/pipe/csv.js +140 -0
  124. package/src/pipe/debug.js +104 -0
  125. package/src/pipe/github.js +233 -0
  126. package/src/pipe/gitlab.js +229 -0
  127. package/src/pipe/html.js +374 -0
  128. package/src/pipe/index.js +71 -0
  129. package/src/pipe/testomatio.js +503 -0
  130. package/src/reporter-functions.js +55 -0
  131. package/src/reporter.cjs_decprecated +21 -0
  132. package/src/reporter.js +33 -0
  133. package/src/services/artifacts.js +59 -0
  134. package/src/services/index.js +13 -0
  135. package/src/services/key-values.js +59 -0
  136. package/src/services/logger.js +316 -0
  137. package/src/template/emptyData.svg +23 -0
  138. package/src/template/testomatio.hbs +706 -0
  139. package/src/uploader.js +371 -0
  140. package/src/utils/pipe_utils.js +119 -0
  141. package/src/utils/utils.js +383 -0
  142. package/src/xmlReader.js +562 -0
@@ -0,0 +1,142 @@
1
+ // eslint-disable-next-line
2
+ import WDIOReporter, { RunnerStats } from '@wdio/reporter';
3
+ import TestomatClient from '../client.js';
4
+ import { getTestomatIdFromTestTitle, fileSystem } from '../utils/utils.js';
5
+ import { services } from '../services/index.js';
6
+ import { TESTOMAT_TMP_STORAGE_DIR } from '../constants.js';
7
+
8
+ class WebdriverReporter extends WDIOReporter {
9
+ constructor(options) {
10
+ super(options);
11
+
12
+ this.client = new TestomatClient({ apiKey: options?.apiKey });
13
+ options = Object.assign(options, { stdout: true });
14
+
15
+ this._addTestPromises = [];
16
+
17
+ this._isSynchronising = false;
18
+ // NOTE: new functionality; may break everything
19
+ this.client.createRun();
20
+ }
21
+
22
+ get isSynchronised() {
23
+ return this._isSynchronising === false;
24
+ }
25
+
26
+ /**
27
+ *
28
+ * @param {RunnerStats} runData
29
+ */
30
+ async onRunnerEnd(runData) {
31
+ this._isSynchronising = true;
32
+
33
+ await Promise.all(this._addTestPromises);
34
+
35
+ this._isSynchronising = false;
36
+
37
+ // NOTE: new functionality; may break everything
38
+ // also this may require additional status mapping
39
+ await this.client.updateRunStatus(runData.failures ? 'failed' : 'passed');
40
+ }
41
+
42
+ onRunnerStart() {
43
+ // clear dir with artifacts/logs
44
+ //
45
+ fileSystem.clearDir(TESTOMAT_TMP_STORAGE_DIR);
46
+ }
47
+
48
+ onTestStart(test) {
49
+ services.setContext(test.fullTitle);
50
+ }
51
+
52
+ onTestEnd(test) {
53
+ test.suite = test.parent;
54
+ const logs = getTestLogs(test.fullTitle);
55
+ // TODO: FIX: artifacts for some reason leads to empty report on Testomat.io
56
+ // const artifacts = services.artifacts.get(test.fullTitle);
57
+ // const keyValues = services.keyValues.get(test.fullTitle);
58
+ test.logs = logs;
59
+ // test.artifacts = artifacts;
60
+ // test.meta = keyValues;
61
+
62
+ this._addTestPromises.push(this.addTest(test));
63
+ }
64
+
65
+ // wdio-cucumber does not trigger onTestEnd hook, thus, using this one
66
+ onSuiteEnd(scerario) {
67
+ if (scerario.type === 'scenario') {
68
+ this._addTestPromises.push(this.addBddScenario(scerario));
69
+ }
70
+ }
71
+
72
+ async addTest(test) {
73
+ if (!this.client) return;
74
+
75
+ const { title, _duration: duration, state, error, output } = test;
76
+
77
+ const testId = getTestomatIdFromTestTitle(title);
78
+
79
+ const screenshotEndpoint = '/session/:sessionId/screenshot';
80
+ const screenshotsBuffers = output
81
+ .filter(el => el.endpoint === screenshotEndpoint && el.result && el.result.value)
82
+ .map(el => Buffer.from(el.result.value, 'base64'));
83
+
84
+ await this.client.addTestRun(state, {
85
+ manuallyAttachedArtifacts: test.artifacts,
86
+ error,
87
+ logs: test.logs,
88
+ meta: test.meta,
89
+ title,
90
+ test_id: testId,
91
+ time: duration,
92
+ filesBuffers: screenshotsBuffers,
93
+ });
94
+ }
95
+
96
+ /**
97
+ * @param {import('../../types/types.js').WebdriverIOScenario} scenario
98
+ */
99
+ addBddScenario(scenario) {
100
+ if (!this.client) return;
101
+
102
+ const { title, _duration: duration } = scenario;
103
+
104
+ const testId = getTestomatIdFromTestTitle(title || scenario.tags.map(tag => tag.name).join(' '));
105
+
106
+ let scenarioState = scenario.tests.every(test => test.state === 'passed') ? 'passed' : 'failed';
107
+ if (scenario.tests.every(test => test.state === 'skipped')) {
108
+ scenarioState = 'skipped';
109
+ }
110
+ const errors = scenario.tests
111
+ .filter(test => test.state === 'failed')
112
+ .map(test => test.error?.stack)
113
+ .filter(Boolean);
114
+ const error = errors.join('\n');
115
+
116
+ const tags = scenario.tags.map(tag => tag.name);
117
+
118
+ return this.client.addTestRun(scenarioState, {
119
+ error: error ? Error(error) : null,
120
+ title,
121
+ test_id: testId,
122
+ time: duration,
123
+ tags,
124
+ file: scenario.file,
125
+ // filesBuffers: screenshotsBuffers,
126
+ });
127
+ }
128
+ }
129
+
130
+ /**
131
+ *
132
+ * @param {*} fullTestTitle
133
+ * @returns string
134
+ */
135
+ function getTestLogs(fullTestTitle) {
136
+ const logsArr = services.logger.getLogs(fullTestTitle);
137
+ // remove duplicates (for some reason, logs are duplicated several times)
138
+ const logs = logsArr ? Array.from(new Set(logsArr)).join('\n').trim() : '';
139
+ return logs;
140
+ }
141
+
142
+ export default WebdriverReporter;
package/src/bin/cli.js ADDED
@@ -0,0 +1,280 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander';
4
+ import { spawn } from 'cross-spawn';
5
+ import glob from 'glob';
6
+ import createDebugMessages from 'debug';
7
+ import TestomatClient from '../client.js';
8
+ import XmlReader from '../xmlReader.js';
9
+ import { APP_PREFIX, STATUS } from '../constants.js';
10
+ import { version } from '../../package.json';
11
+ import { config } from '../config.js';
12
+ import { readLatestRunId } from '../utils/utils.js';
13
+ import pc from 'picocolors';
14
+ import { filesize as prettyBytes } from 'filesize';
15
+ import dotenv from 'dotenv';
16
+
17
+ const debug = createDebugMessages('@testomatio/reporter:xml-cli');
18
+ console.log(pc.cyan(pc.bold(` 🤩 Testomat.io Reporter v${version}`)));
19
+ const program = new Command();
20
+
21
+ program
22
+ .version(version)
23
+ .option('--env-file <envfile>', 'Load environment variables from env file')
24
+ .hook('preAction', thisCommand => {
25
+ const opts = thisCommand.opts();
26
+ if (opts.envFile) {
27
+ dotenv.config({ path: opts.envFile });
28
+ } else {
29
+ dotenv.config();
30
+ }
31
+ });
32
+
33
+ program
34
+ .command('start')
35
+ .description('Start a new run and return its ID')
36
+ .action(async () => {
37
+ console.log('Starting a new Run on Testomat.io...');
38
+ const apiKey = process.env['INPUT_TESTOMATIO-KEY'] || config.TESTOMATIO;
39
+ const client = new TestomatClient({ apiKey });
40
+
41
+ client.createRun().then(() => {
42
+ console.log(process.env.runId);
43
+ process.exit(0);
44
+ });
45
+ });
46
+
47
+ program
48
+ .command('finish')
49
+ .description('Finish Run by its ID')
50
+ .action(async () => {
51
+ process.env.TESTOMATIO_RUN ||= readLatestRunId();
52
+
53
+ if (!process.env.TESTOMATIO_RUN) {
54
+ console.log('TESTOMATIO_RUN environment variable must be set or restored from a previous run.');
55
+ return process.exit(1);
56
+ }
57
+
58
+ console.log('Finishing Run on Testomat.io...');
59
+ const apiKey = process.env['INPUT_TESTOMATIO-KEY'] || config.TESTOMATIO;
60
+ const client = new TestomatClient({ apiKey });
61
+
62
+ // @ts-ignore
63
+ client.updateRunStatus(STATUS.FINISHED).then(() => {
64
+ console.log(pc.yellow(`Run ${process.env.TESTOMATIO_RUN} was finished`));
65
+ process.exit(0);
66
+ });
67
+ });
68
+
69
+ program
70
+ .command('run')
71
+ .description('Run tests with the specified command')
72
+ .argument('<command>', 'Test runner command')
73
+ .option('--filter <filter>', 'Additional execution filter')
74
+ .action(async (command, opts) => {
75
+ const apiKey = process.env['INPUT_TESTOMATIO-KEY'] || config.TESTOMATIO;
76
+ const title = process.env.TESTOMATIO_TITLE;
77
+
78
+ if (!command || !command.split) {
79
+ console.log(APP_PREFIX, `No command provided. Use -c option to launch a test runner.`);
80
+ return process.exit(255);
81
+ }
82
+
83
+ const client = new TestomatClient({ apiKey, title, parallel: true });
84
+
85
+ if (opts.filter) {
86
+ const [pipe, ...optsArray] = opts.filter.split(':');
87
+ const pipeOptions = optsArray.join(':');
88
+
89
+ try {
90
+ const tests = await client.prepareRun({ pipe, pipeOptions });
91
+ if (tests && tests.length > 0) {
92
+ command += ` --grep (${tests.join('|')})`;
93
+ }
94
+ } catch (err) {
95
+ console.log(APP_PREFIX, err);
96
+ }
97
+ }
98
+
99
+ console.log(APP_PREFIX, `🚀 Running`, pc.green(command));
100
+
101
+ const runTests = () => {
102
+ const testCmds = command.split(' ');
103
+ const cmd = spawn(testCmds[0], testCmds.slice(1), { stdio: 'inherit' });
104
+
105
+ cmd.on('close', code => {
106
+ const emoji = code === 0 ? '🟢' : '🔴';
107
+ console.log(APP_PREFIX, emoji, `Runner exited with ${pc.bold(code)}`);
108
+ if (apiKey) {
109
+ const status = code === 0 ? 'passed' : 'failed';
110
+ client.updateRunStatus(status, true);
111
+ }
112
+ process.exit(code);
113
+ });
114
+ };
115
+
116
+ if (apiKey) {
117
+ client.createRun().then(runTests);
118
+ } else {
119
+ runTests();
120
+ }
121
+ });
122
+
123
+ program
124
+ .command('xml')
125
+ .description('Parse XML reports and upload to Testomat.io')
126
+ .argument('<pattern>', 'XML file pattern')
127
+ .option('-d, --dir <dir>', 'Project directory')
128
+ .option('--java-tests [java-path]', 'Load Java tests from path, by default: src/test/java')
129
+ .option('--lang <lang>', 'Language used (python, ruby, java)')
130
+ .option('--timelimit <time>', 'default time limit in seconds to kill a stuck process')
131
+ .action(async (pattern, opts) => {
132
+ if (!pattern.endsWith('.xml')) {
133
+ pattern += '.xml';
134
+ }
135
+ let { javaTests, lang } = opts;
136
+ if (javaTests === true) javaTests = 'src/test/java';
137
+ lang = lang?.toLowerCase();
138
+ const runReader = new XmlReader({ javaTests, lang });
139
+ const files = glob.sync(pattern, { cwd: opts.dir || process.cwd() });
140
+ if (!files.length) {
141
+ console.log(APP_PREFIX, `Report can't be created. No XML files found 😥`);
142
+ process.exit(1);
143
+ }
144
+
145
+ for (const file of files) {
146
+ console.log(APP_PREFIX, `Parsed ${file}`);
147
+ runReader.parse(file);
148
+ }
149
+
150
+ let timeoutTimer;
151
+ if (opts.timelimit) {
152
+ timeoutTimer = setTimeout(
153
+ () => {
154
+ console.log(
155
+ `⚠️ Reached timeout of ${opts.timelimit}s. Exiting... (Exit code is 0 to not fail the pipeline)`,
156
+ );
157
+ process.exit(0);
158
+ },
159
+ parseInt(opts.timelimit, 10) * 1000,
160
+ );
161
+ }
162
+
163
+ try {
164
+ await runReader.createRun();
165
+ await runReader.uploadData();
166
+ } catch (err) {
167
+ console.log(APP_PREFIX, 'Error updating status, skipping...', err);
168
+ }
169
+
170
+ if (timeoutTimer) clearTimeout(timeoutTimer);
171
+ });
172
+
173
+ program
174
+ .command('upload-artifacts')
175
+ .description('Upload artifacts to Testomat.io')
176
+ .option('--force', 'Re-upload artifacts even if they were uploaded before')
177
+ .action(async opts => {
178
+ const apiKey = config.TESTOMATIO;
179
+
180
+ process.env.TESTOMATIO_DISABLE_ARTIFACTS = '';
181
+ const runId = process.env.TESTOMATIO_RUN || process.env.runId || readLatestRunId();
182
+
183
+ if (!runId) {
184
+ console.log('TESTOMATIO_RUN environment variable must be set or restored from a previous run.');
185
+ return process.exit(1);
186
+ }
187
+
188
+ const client = new TestomatClient({
189
+ apiKey,
190
+ runId,
191
+ isBatchEnabled: false,
192
+ });
193
+
194
+ let testruns = client.uploader.readUploadedFiles(runId);
195
+ const numTotalArtifacts = testruns.length;
196
+
197
+ debug('Found testruns:', testruns);
198
+
199
+ if (!opts.force) testruns = testruns.filter(tr => !tr.uploaded);
200
+
201
+ if (!testruns.length) {
202
+ console.log(APP_PREFIX, '🗄️ Total artifacts:', numTotalArtifacts);
203
+ if (numTotalArtifacts) {
204
+ console.log(APP_PREFIX, 'No new artifacts to upload');
205
+ console.log(APP_PREFIX, 'To re-upload artifacts run this command with --force flag');
206
+ }
207
+ process.exit(0);
208
+ }
209
+
210
+ const testrunsByRid = testruns.reduce((acc, { rid, file }) => {
211
+ if (!acc[rid]) {
212
+ acc[rid] = [];
213
+ }
214
+ if (!acc[rid].includes(file)) acc[rid].push(file);
215
+ return acc;
216
+ }, {});
217
+
218
+ await client.createRun();
219
+ client.uploader.checkEnabled();
220
+ client.uploader.disableLogStorage();
221
+
222
+ for (const rid in testrunsByRid) {
223
+ const files = testrunsByRid[rid];
224
+ await client.addTestRun(undefined, { rid, files });
225
+ }
226
+
227
+ console.log(APP_PREFIX, '🗄️', client.uploader.successfulUploads.length, 'artifacts 🟢uploaded');
228
+
229
+ if (client.uploader.successfulUploads.length) {
230
+ debug('\n', APP_PREFIX, `🗄️ ${client.uploader.successfulUploads.length} artifacts uploaded to S3 bucket`);
231
+ const uploadedArtifacts = client.uploader.successfulUploads.map(file => ({
232
+ relativePath: file.path.replace(process.cwd(), ''),
233
+ link: file.link,
234
+ sizePretty: prettyBytes(file.size, { round: 0 }).toString(),
235
+ }));
236
+
237
+ uploadedArtifacts.forEach(upload => {
238
+ debug(
239
+ `🟢Uploaded artifact`,
240
+ `${upload.relativePath},`,
241
+ 'size:',
242
+ `${upload.sizePretty},`,
243
+ 'link:',
244
+ `${upload.link}`,
245
+ );
246
+ });
247
+ }
248
+
249
+ const filesizeStrMaxLength = 7;
250
+
251
+ if (client.uploader.failedUploads.length) {
252
+ console.log(
253
+ '\n',
254
+ APP_PREFIX,
255
+ '🗄️',
256
+ client.uploader.failedUploads.length,
257
+ `artifacts 🔴${pc.bold('failed')} to upload`,
258
+ );
259
+
260
+ const failedUploads = client.uploader.failedUploads.map(({ path, size }) => ({
261
+ relativePath: path.replace(process.cwd(), ''),
262
+ sizePretty: prettyBytes(size, { round: 0 }).toString(),
263
+ }));
264
+
265
+ const pathPadding = Math.max(...failedUploads.map(upload => upload.relativePath.length)) + 1;
266
+ failedUploads.forEach(upload => {
267
+ console.log(
268
+ ` ${pc.gray('|')} 🔴 ${upload.relativePath.padEnd(pathPadding)} ${pc.gray(
269
+ `| ${upload.sizePretty.padStart(filesizeStrMaxLength)} |`,
270
+ )}`,
271
+ );
272
+ });
273
+ }
274
+ });
275
+
276
+ program.parse(process.argv);
277
+
278
+ if (!process.argv.slice(2).length) {
279
+ program.outputHelp();
280
+ }
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+ import pc from 'picocolors';
4
+ import { glob } from 'glob';
5
+ import createDebugMessages from 'debug';
6
+ import { APP_PREFIX } from '../constants.js';
7
+ import XmlReader from '../xmlReader.js';
8
+ import { version } from '../../package.json';
9
+ import dotenv from 'dotenv';
10
+
11
+ const debug = createDebugMessages('@testomatio/reporter:xml-cli');
12
+ console.log(pc.cyan(pc.bold(` 🤩 Testomat.io XML Reporter v${version}`)));
13
+ const program = new Command();
14
+
15
+ program
16
+ .arguments('<pattern>')
17
+ .option('-d, --dir <dir>', 'Project directory')
18
+ .option('--java-tests [java-path]', 'Load Java tests from path, by default: src/test/java')
19
+ .option('--lang <lang>', 'Language used (python, ruby, java)')
20
+ .option('--timelimit <time>', 'default time limit in seconds to kill a stuck process')
21
+ .option('--env-file <envfile>', 'Load environment variables from env file')
22
+ .action(async (pattern, opts) => {
23
+ if (!pattern.endsWith('.xml')) {
24
+ pattern += '.xml';
25
+ }
26
+ let { javaTests, lang } = opts;
27
+ if (opts.envFile) {
28
+ console.log(APP_PREFIX, 'Loading env file:', opts.envFile);
29
+ debug('Loading env file: %s', opts.envFile);
30
+ dotenv.config({ path: opts.envFile });
31
+ }
32
+ lang = lang?.toLowerCase();
33
+ if (javaTests === true || (lang === 'java' && !javaTests)) javaTests = 'src/test/java';
34
+ const runReader = new XmlReader({ javaTests, lang });
35
+ const files = glob.sync(pattern, { cwd: opts.dir || process.cwd() });
36
+ if (!files.length) {
37
+ console.log(APP_PREFIX, `Report can't be created. No XML files found 😥`);
38
+ process.exitCode = 1;
39
+ return;
40
+ }
41
+
42
+ for (const file of files) {
43
+ console.log(APP_PREFIX, `Parsed ${file}`);
44
+ runReader.parse(file);
45
+ }
46
+
47
+ let timeoutTimer;
48
+ if (opts.timelimit) {
49
+ timeoutTimer = setTimeout(
50
+ () => {
51
+ console.log(
52
+ `⚠️ Reached timeout of ${opts.timelimit}s. Exiting... (Exit code is 0 to not fail the pipeline)`,
53
+ );
54
+ process.exit(0);
55
+ },
56
+ parseInt(opts.timelimit, 10) * 1000,
57
+ );
58
+ }
59
+
60
+ try {
61
+ await runReader.createRun();
62
+ await runReader.uploadData();
63
+ } catch (err) {
64
+ console.log(APP_PREFIX, 'Error updating status, skipping...', err);
65
+ }
66
+
67
+ if (timeoutTimer) clearTimeout(timeoutTimer);
68
+ });
69
+
70
+ if (process.argv.length < 3) {
71
+ program.outputHelp();
72
+ }
73
+
74
+ program.parse(process.argv);
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env node
2
+ import { spawn } from 'cross-spawn';
3
+ import { Command } from 'commander';
4
+ import pc from 'picocolors';
5
+ import TestomatClient from '../client.js';
6
+ import { APP_PREFIX, STATUS } from '../constants.js';
7
+ import { version } from '../../package.json';
8
+ import { config } from '../config.js';
9
+ import dotenv from 'dotenv';
10
+
11
+ console.log(pc.cyan(pc.bold(` 🤩 Testomat.io Reporter v${version}`)));
12
+ const program = new Command();
13
+
14
+ program
15
+ .option('-c, --command <cmd>', 'Test runner command')
16
+ .option('--launch', 'Start a new run and return its ID')
17
+ .option('--finish', 'Finish Run by its ID')
18
+ .option('--env-file <envfile>', 'Load environment variables from env file')
19
+ .option('--filter <filter>', 'Additional execution filter')
20
+ .action(async opts => {
21
+ const { launch, finish, filter } = opts;
22
+ let { command } = opts;
23
+
24
+ if (opts.envFile) dotenv.config({ path: opts.envFile });
25
+
26
+ const apiKey = process.env['INPUT_TESTOMATIO-KEY'] || config.TESTOMATIO;
27
+ const title = process.env.TESTOMATIO_TITLE;
28
+
29
+ if (launch) {
30
+ console.log('Starting a new Run on Testomat.io...');
31
+ const client = new TestomatClient({ apiKey });
32
+
33
+ client.createRun().then(() => {
34
+ console.log(process.env.runId);
35
+ process.exit(0);
36
+ });
37
+ return;
38
+ }
39
+
40
+ if (finish) {
41
+ // TODO: add error in case of TESTOMATIO environment variable is not set
42
+ // because command is fine in console, but actually (on testomat.io) run is not finished
43
+ if (!process.env.TESTOMATIO_RUN) {
44
+ console.log('TESTOMATIO_RUN environment variable must be set.');
45
+ return process.exit(1);
46
+ }
47
+
48
+ console.log('Finishing Run on Testomat.io...');
49
+
50
+ const client = new TestomatClient({ apiKey });
51
+
52
+ // @ts-ignore
53
+ client.updateRunStatus(STATUS.FINISHED).then(() => {
54
+ console.log(pc.yellow(`Run ${process.env.TESTOMATIO_RUN} was finished`));
55
+ process.exit(0);
56
+ });
57
+ return;
58
+ }
59
+
60
+ let exitCode = 0;
61
+
62
+ if (!command.split) {
63
+ process.exitCode = 255;
64
+ console.log(APP_PREFIX, `No command provided. Use -c option to launch a test runner.`);
65
+ return;
66
+ }
67
+
68
+ const client = new TestomatClient({ apiKey, title, parallel: true });
69
+
70
+ if (filter) {
71
+ const [pipe, ...optsArray] = filter.split(':');
72
+ const pipeOptions = optsArray.join(':');
73
+
74
+ try {
75
+ const tests = await client.prepareRun({ pipe, pipeOptions });
76
+
77
+ if (!tests || tests.length === 0) {
78
+ return;
79
+ }
80
+
81
+ const grep = ` --grep (${tests.join('|')})`;
82
+ command += grep;
83
+ } catch (err) {
84
+ console.log(APP_PREFIX, err);
85
+ }
86
+ }
87
+
88
+ const testCmds = command.split(' ');
89
+ console.log(APP_PREFIX, `🚀 Running`, pc.green(command));
90
+
91
+ if (!apiKey) {
92
+ const cmd = spawn(testCmds[0], testCmds.slice(1), { stdio: 'inherit' });
93
+
94
+ cmd.on('close', code => {
95
+ console.log(APP_PREFIX, '⚠️ ', `Runner exited with ${pc.bold(code)}, report is ignored`);
96
+
97
+ if (code > exitCode) exitCode = code;
98
+ process.exitCode = exitCode;
99
+ });
100
+
101
+ return;
102
+ }
103
+
104
+ client.createRun().then(() => {
105
+ const cmd = spawn(testCmds[0], testCmds.slice(1), { stdio: 'inherit' });
106
+
107
+ cmd.on('close', code => {
108
+ const emoji = code === 0 ? '🟢' : '🔴';
109
+ console.log(APP_PREFIX, emoji, `Runner exited with ${pc.bold(code)}`);
110
+ const status = code === 0 ? 'passed' : 'failed';
111
+ client.updateRunStatus(status, true);
112
+
113
+ if (code > exitCode) exitCode = code;
114
+ process.exitCode = exitCode;
115
+ });
116
+ });
117
+ });
118
+
119
+ if (process.argv.length <= 2) {
120
+ program.outputHelp();
121
+ }
122
+
123
+ program.parse(process.argv);
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Command } from 'commander';
4
+ import pc from 'picocolors';
5
+ import createDebugMessages from 'debug';
6
+ import TestomatClient from '../client.js';
7
+ import { APP_PREFIX } from '../constants.js';
8
+ import { version } from '../../package.json';
9
+ import { config } from '../config.js';
10
+ import { readLatestRunId } from '../utils/utils.js';
11
+ import dotenv from 'dotenv';
12
+
13
+ const debug = createDebugMessages('@testomatio/reporter:upload-cli');
14
+ console.log(pc.cyan(pc.bold(` 🤩 Testomat.io Reporter v${version}`)));
15
+ const program = new Command();
16
+
17
+ program
18
+ .option('--env-file <envfile>', 'Load environment variables from env file')
19
+ .option('--force', 'Re-upload artifacts even if they were uploaded before')
20
+ .action(async opts => {
21
+ if (opts.envFile) {
22
+ dotenv.config({ path: opts.envFile });
23
+ } else {
24
+ dotenv.config();
25
+ }
26
+
27
+ const apiKey = config.TESTOMATIO;
28
+ process.env.TESTOMATIO_DISABLE_ARTIFACTS = '';
29
+ const runId = process.env.TESTOMATIO_RUN || process.env.runId || readLatestRunId();
30
+
31
+ if (!runId) {
32
+ console.log('TESTOMATIO_RUN environment variable must be set or restored from a previous run.');
33
+ return process.exit(1);
34
+ }
35
+
36
+ const client = new TestomatClient({
37
+ apiKey,
38
+ runId,
39
+ isBatchEnabled: false,
40
+ });
41
+ let testruns = client.uploader.readUploadedFiles(process.env.TESTOMATIO_RUN);
42
+
43
+ const numTotalArtifacts = testruns.length;
44
+
45
+ debug('Found testruns:', testruns);
46
+
47
+ if (!opts.force) testruns = testruns.filter(tr => !tr.uploaded);
48
+
49
+ if (!testruns.length) {
50
+ console.log(APP_PREFIX, 'Total artifacts:', numTotalArtifacts);
51
+ if (numTotalArtifacts) {
52
+ console.log(APP_PREFIX, 'No new artifacts to upload');
53
+ console.log(APP_PREFIX, 'To re-upload artifacts run this command with --force flag');
54
+ }
55
+ process.exit(0);
56
+ }
57
+
58
+ const testrunsByRid = testruns.reduce((acc, { rid, file }) => {
59
+ if (!acc[rid]) {
60
+ acc[rid] = [];
61
+ }
62
+ if (!acc[rid].includes(file)) acc[rid].push(file);
63
+ return acc;
64
+ }, {});
65
+
66
+ // we need to obtain S3 credentials
67
+ await client.createRun();
68
+
69
+ client.uploader.checkEnabled();
70
+ client.uploader.disableLogStorage();
71
+
72
+ for (const rid in testrunsByRid) {
73
+ const files = testrunsByRid[rid];
74
+ await client.addTestRun(undefined, {
75
+ rid,
76
+ files,
77
+ });
78
+ }
79
+
80
+ console.log(APP_PREFIX, client.uploader.successfulUploads.length, 'artifacts uploaded');
81
+ if (client.uploader.failedUploads.length) {
82
+ console.log(APP_PREFIX, client.uploader.failedUploads.length, 'artifacts failed to upload');
83
+ }
84
+ });
85
+
86
+ if (process.argv.length <= 1) {
87
+ program.outputHelp();
88
+ }
89
+
90
+ program.parse(process.argv);