Skip to content

Commit 462e1de

Browse files
committed
fix: output module
1 parent 2b404b0 commit 462e1de

25 files changed

+303
-309
lines changed

bin/codecept.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
import { Command } from 'commander';
33
import { version } from '../lib/codecept.js';
4-
import { print, error } from '../lib/output.js';
4+
import * as outputLib from '../lib/output.js';
55
import { printError } from '../lib/command/utils.js';
66
import * as init from '../lib/command/init.js';
77
import * as configMigrate from '../lib/command/configMigrate.js';
@@ -31,10 +31,10 @@ const errorHandler = (fn) => async (...args) => {
3131
};
3232

3333
if (process.versions.node && process.versions.node.split('.') && process.versions.node.split('.')[0] < 12) {
34-
error('NodeJS >= 12 is required to run.');
35-
print();
36-
print('Please upgrade your NodeJS engine');
37-
print(`Current NodeJS version: ${process.version}`);
34+
outputLib.output.error('NodeJS >= 12 is required to run.');
35+
outputLib.print();
36+
outputLib.print('Please upgrade your NodeJS engine');
37+
outputLib.print(`Current NodeJS version: ${process.version}`);
3838
process.exit(1);
3939
}
4040

lib/assert/include.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class InclusionAssertion extends Assertion {
2929
}
3030
err.cliMessage = function () {
3131
const msg = this.template
32-
.replace('{{jar}}', output.colors.bold('{{jar}}'))
33-
.replace('{{needle}}', output.colors.bold('{{needle}}'));
32+
.replace('{{jar}}', output.output.colors.bold('{{jar}}'))
33+
.replace('{{needle}}', output.output.colors.bold('{{needle}}'));
3434
return template(msg, this.params);
3535
};
3636
return err;
@@ -52,7 +52,7 @@ class InclusionAssertion extends Assertion {
5252
const pattern = new RegExp(`^.*?\n?^.*?\n?^.*?${escapeRegExp(this.params.needle)}.*?$\n?.*$\n?.*$`, 'm');
5353
const matched = this.params.haystack.match(pattern);
5454
if (!matched) return err;
55-
err.actual = matched[0].replace(this.params.needle, output.colors.bold(this.params.needle));
55+
err.actual = matched[0].replace(this.params.needle, output.output.colors.bold(this.params.needle));
5656
err.actual = `------\n${err.actual}\n------`;
5757
return err;
5858
}

lib/cli.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as output from './output.js';
55
import AssertionFailedError from './assert/error.js';
66
import * as Codecept from './codecept.js';
77
import container from './container.js';
8-
import {stepShift} from "./output.js";
98

109
const { reporters: { Base } } = mocha;
1110

@@ -22,35 +21,35 @@ class Cli extends Base {
2221
if (opts.steps) level = 1;
2322
if (opts.debug) level = 2;
2423
if (opts.verbose) level = 3;
25-
output.level(level);
24+
output.output.level(level);
2625
const version = Codecept.version();
27-
output.print(`CodeceptJS v${version} ${output.standWithUkraine()}`);
28-
output.print(`Using test root "${global.codecept_dir}"`);
26+
output.output.print(`CodeceptJS v${version} ${output.output.standWithUkraine()}`);
27+
output.output.print(`Using test root "${global.codecept_dir}"`);
2928

3029
const showSteps = level >= 1;
3130

3231
if (level >= 2) {
33-
output.print(output.styles.debug(`Helpers: ${Object.keys(container.helpers()).join(', ')}`));
34-
output.print(output.styles.debug(`Plugins: ${Object.keys(container.plugins()).join(', ')}`));
32+
output.output.print(output.output.styles.debug(`Helpers: ${Object.keys(container.helpers()).join(', ')}`));
33+
output.output.print(output.output.styles.debug(`Plugins: ${Object.keys(container.plugins()).join(', ')}`));
3534
}
3635

3736
runner.on('start', () => {
3837
console.log();
3938
});
4039

4140
runner.on('suite', (suite) => {
42-
output.suite.started(suite);
41+
output.output.suite.started(suite);
4342
});
4443

4544
runner.on('fail', (test) => {
4645
if (test.ctx.currentTest) {
4746
this.loadedTests.push(test.ctx.currentTest.uid);
4847
}
4948
if (showSteps && test.steps) {
50-
return output.scenario.failed(test);
49+
return output.output.scenario.failed(test);
5150
}
5251
cursor.CR();
53-
output.test.failed(test);
52+
output.output.test.failed(test);
5453
});
5554

5655
runner.on('pending', (test) => {
@@ -63,31 +62,31 @@ class Cli extends Base {
6362
}
6463
this.loadedTests.push(test.uid);
6564
cursor.CR();
66-
output.test.skipped(test);
65+
output.output.test.skipped(test);
6766
});
6867

6968
runner.on('pass', (test) => {
7069
if (showSteps && test.steps) {
71-
return output.scenario.passed(test);
70+
return output.output.scenario.passed(test);
7271
}
7372
cursor.CR();
74-
output.test.passed(test);
73+
output.output.test.passed(test);
7574
});
7675

7776
if (showSteps) {
7877
runner.on('test', (test) => {
7978
currentMetaStep = [];
8079
if (test.steps) {
81-
output.test.started(test);
80+
output.output.test.started(test);
8281
}
8382
});
8483

8584
if (!codeceptjsEventDispatchersRegistered) {
8685
codeceptjsEventDispatchersRegistered = true;
8786

8887
event.dispatcher.on(event.bddStep.started, (step) => {
89-
// output.stepShift = 2;
90-
output.step(step);
88+
// output.output.stepShift = 2;
89+
output.output.step(step);
9190
});
9291

9392
event.dispatcher.on(event.step.started, (step) => {
@@ -101,22 +100,22 @@ class Cli extends Base {
101100

102101
for (let i = 0; i < Math.max(currentMetaStep.length, metaSteps.length); i++) {
103102
if (currentMetaStep[i] !== metaSteps[i]) {
104-
output.stepShift = 3 + 2 * i;
103+
output.output.stepShift = 3 + 2 * i;
105104
if (!metaSteps[i]) continue;
106105
// bdd steps are handled by bddStep.started
107106
if (metaSteps[i].isBDD()) continue;
108-
output.step(metaSteps[i]);
107+
output.output.step(metaSteps[i]);
109108
}
110109
}
111110
currentMetaStep = metaSteps;
112-
output.stepShift = 3 + 2 * shift;
111+
output.output.stepShift = 3 + 2 * shift;
113112
if (step.helper.constructor.name !== 'ExpectHelper') {
114-
output.step(step);
113+
output.output.step(step);
115114
}
116115
});
117116

118117
event.dispatcher.on(event.step.finished, () => {
119-
output.stepShift = 0;
118+
output.output.stepShift = 0;
120119
});
121120
}
122121
}
@@ -134,7 +133,7 @@ class Cli extends Base {
134133
test.opts.skipInfo = {};
135134
}
136135
skipTestConfig(test, 'Skipped due to failure in \'before\' hook');
137-
output.test.skipped(test);
136+
output.output.test.skipped(test);
138137
skippedCount += 1;
139138
}
140139
}
@@ -153,7 +152,7 @@ class Cli extends Base {
153152

154153
// passes
155154
if (stats.failures) {
156-
output.print(output.styles.bold('-- FAILURES:'));
155+
output.output.print(output.output.styles.bold('-- FAILURES:'));
157156
}
158157

159158
const failuresLog = [];
@@ -178,12 +177,12 @@ class Cli extends Base {
178177
// if (step.status === 'failed') line = '' + line;
179178
scenarioTrace += `\n${line}`;
180179
});
181-
log += `${output.styles.bold('Scenario Steps')}:${scenarioTrace}\n`;
180+
log += `${output.output.styles.bold('Scenario Steps')}:${scenarioTrace}\n`;
182181
}
183182

184183
// display artifacts in debug mode
185184
if (test?.artifacts && Object.keys(test.artifacts).length) {
186-
log += `\n${output.styles.bold('Artifacts:')}`;
185+
log += `\n${output.output.styles.bold('Artifacts:')}`;
187186
for (const artifact of Object.keys(test.artifacts)) {
188187
log += `\n- ${artifact}: ${test.artifacts[artifact]}`;
189188
}
@@ -194,11 +193,11 @@ class Cli extends Base {
194193
stack.shift();
195194
}
196195

197-
if (output.level() < 3) {
196+
if (output.output.level() < 3) {
198197
stack = stack.slice(0, 3);
199198
}
200199

201-
err.stack = `${stack.join('\n')}\n\n${output.colors.blue(log)}`;
200+
err.stack = `${stack.join('\n')}\n\n${output.output.colors.blue(log)}`;
202201

203202
// clone err object so stack trace adjustments won't affect test other reports
204203
test.err = err;
@@ -216,10 +215,10 @@ class Cli extends Base {
216215
}
217216

218217
event.emit(event.all.failures, { failuresLog, stats });
219-
output.result(stats.passes, stats.failures, stats.pending, ms(stats.duration));
218+
output.output.result(stats.passes, stats.failures, stats.pending, ms(stats.duration));
220219

221-
if (stats.failures && output.level() < 3) {
222-
output.print(output.styles.debug('Run with --verbose flag to see complete NodeJS stacktrace'));
220+
if (stats.failures && output.output.level() < 3) {
221+
output.output.print(output.output.styles.debug('Run with --verbose flag to see complete NodeJS stacktrace'));
223222
}
224223
}
225224
}

lib/codecept.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ export default class Codecept {
9797
* @param {string} dir
9898
*/
9999
initGlobals(dir) {
100+
// @ts-ignore
100101
global.codecept_dir = dir;
102+
// @ts-ignore
101103
global.output_dir = fsPath.resolve(dir, this.config.output);
102104

103105
if (this.config.emptyOutputFolder) emptyFolder(global.output_dir);
@@ -231,7 +233,7 @@ export default class Codecept {
231233
event.emit(event.all.before, this);
232234
mocha.run(() => done());
233235
} catch (e) {
234-
output.error(e.stack);
236+
output.output.error(e.stack);
235237
reject(e);
236238
}
237239
});

lib/command/configMigrate.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ import inquirer from 'inquirer';
44
import mkdirp from 'mkdirp';
55
import path from 'path';
66
import util from 'util';
7-
import { print, success, error } from '../output.js';
7+
import * as outputLib from '../output.js';
88
import { fileExists } from '../utils.js';
99
import { getTestRoot } from './utils.js';
1010

1111
export default function (initPath) {
1212
const testsPath = getTestRoot(initPath);
1313

14-
print();
15-
print(` Welcome to ${colors.magenta.bold('CodeceptJS')} configuration migration tool`);
16-
print(` It will help you switch from ${colors.cyan.bold('.json')} to ${colors.magenta.bold('.js')} config format at ease`);
17-
print();
14+
outputLib.print();
15+
outputLib.print(` Welcome to ${colors.magenta.bold('CodeceptJS')} configuration migration tool`);
16+
outputLib.print(` It will help you switch from ${colors.cyan.bold('.json')} to ${colors.magenta.bold('.js')} config format at ease`);
17+
outputLib.print();
1818

1919
if (!path) {
20-
print('No config file is specified.');
21-
print(`Test root is assumed to be ${colors.yellow.bold(testsPath)}`);
22-
print('----------------------------------');
20+
outputLib.print('No config file is specified.');
21+
outputLib.print(`Test root is assumed to be ${colors.yellow.bold(testsPath)}`);
22+
outputLib.print('----------------------------------');
2323
} else {
24-
print(`Migrating ${colors.magenta.bold('.js')} config to ${colors.bold(testsPath)}`);
24+
outputLib.print(`Migrating ${colors.magenta.bold('.js')} config to ${colors.bold(testsPath)}`);
2525
}
2626

2727
if (!fileExists(testsPath)) {
28-
print(`Directory ${testsPath} does not exist, creating...`);
28+
outputLib.print(`Directory ${testsPath} does not exist, creating...`);
2929
mkdirp.sync(testsPath);
3030
}
3131

3232
const configFile = path.join(testsPath, 'codecept.conf.js');
3333
if (fileExists(configFile)) {
34-
error(`Config is already created at ${configFile}`);
34+
outputLib.output.error(`Config is already created at ${configFile}`);
3535
return;
3636
}
3737

@@ -55,12 +55,12 @@ export default function (initPath) {
5555

5656
const finish = () => {
5757
fs.writeFileSync(configFile, `exports.config = ${util.inspect(config, false, 4, false)}`, 'utf-8');
58-
success(`Config is successfully migrated at ${configFile}`);
58+
outputLib.output.success(`Config is successfully migrated at ${configFile}`);
5959

6060
if (result.delete) {
6161
if (fileExists(jsonConfigFile)) {
6262
fs.unlinkSync(jsonConfigFile);
63-
success('JSON config file is deleted!');
63+
outputLib.output.success('JSON config file is deleted!');
6464
}
6565
}
6666
};

lib/command/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import mkdirp from 'mkdirp';
55
import path from 'path';
66
import { inspect } from 'util';
77
import spawn from 'cross-spawn';
8-
import { print, success, error } from '../output.js';
8+
import * as output from '../output.js';
99
import { fileExists, beautify, installedLocally } from '../utils.js';
1010
import { getTestRoot } from './utils.js';
1111
import generateDefinitions from './definitions.js';

lib/command/utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const readConfig = function (configFile) {
1919
const data = fs.readFileSync(configFile, 'utf8');
2020
return data;
2121
} catch (err) {
22-
output.error(err);
22+
output.output.error(err);
2323
}
2424
};
2525

@@ -32,7 +32,7 @@ function getTestRoot(currentPath) {
3232
export { getTestRoot };
3333

3434
function fail(msg) {
35-
output.error(msg);
35+
output.output.error(msg);
3636
process.exit(1);
3737
}
3838

@@ -43,22 +43,22 @@ function updateConfig(testsPath, config, key, extension = 'js') {
4343
if (!fileExists(configFile)) {
4444
console.log();
4545
const msg = `codecept.conf.${extension} config can\'t be updated automatically`;
46-
console.log(`${output.colors.bold.red(msg)}`);
46+
console.log(`${output.output.colors.bold.red(msg)}`);
4747
console.log('Please update it manually:');
4848
console.log();
4949
console.log(`${key}: ${config[key]}`);
5050
console.log();
5151
return;
5252
}
53-
console.log(`${output.colors.yellow('Updating configuration file...')}`);
53+
console.log(`${output.output.colors.yellow('Updating configuration file...')}`);
5454
return fs.writeFileSync(configFile, beautify(`exports.config = ${util.inspect(config, false, 4, false)}`), 'utf-8');
5555
}
5656

5757
export { updateConfig };
5858

5959
function safeFileWrite(file, contents) {
6060
if (fileExists(file)) {
61-
output.error(`File ${file} already exist, skipping...`);
61+
output.output.error(`File ${file} already exist, skipping...`);
6262
return false;
6363
}
6464
fs.writeFileSync(file, contents);
@@ -86,9 +86,9 @@ export const captureStream = (stream) => {
8686

8787
export const printError = (err) => {
8888
output.print('');
89-
output.error(err.message);
89+
output.output.error(err.message);
9090
output.print('');
91-
output.print(output.colors.grey(err.stack.replace(err.message, '')));
91+
output.print(output.output.colors.grey(err.stack.replace(err.message, '')));
9292
};
9393

9494
export const createOutputDir = (config, testRoot) => {

lib/event.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import debug from 'debug';
22

33
import events from 'events';
4-
import { error } from './output.js';
4+
import * as output from './output.js';
55

66
debug('codeceptjs:event');
77

@@ -170,7 +170,7 @@ export function emit(event, param) {
170170
try {
171171
dispatcher.emit.apply(dispatcher, arguments);
172172
} catch (err) {
173-
error(`Error processing ${event} event:`);
174-
error(err.stack);
173+
output.output.error(`Error processing ${event} event:`);
174+
output.output.error(err.stack);
175175
}
176176
}

0 commit comments

Comments
 (0)