From 858e4afa6679d0c394ad086b2551ad27c22378b3 Mon Sep 17 00:00:00 2001 From: Vaibhav Singh Date: Tue, 28 Feb 2023 23:11:28 +0530 Subject: [PATCH] Update logging output to use JSON for the list-files cli argument (#3619) --- lib/index.js | 18 ++++++----- lib/reporter/global-reporter.js | 4 +-- test/src/cli/testCliRunner.js | 57 ++++++++++++++++++++++++++++++++- test/src/runner/testReporter.js | 28 ++++++++++++++++ 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7b47a18ea4..869c45be8d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -181,6 +181,16 @@ Nightwatch.cli = function(callback) { const ArgvSetup = require('./runner/cli/argv-setup.js'); const {argv} = ArgvSetup; + if (argv['list-files']) { + argv._source = argv['_'].slice(0); + const runner = Nightwatch.CliRunner(argv); + + return runner.setupAsync() + .then(() => runner.getTestsFiles()) + // eslint-disable-next-line no-console + .then((result) => console.log(JSON.stringify(result))); + } + if (argv.help) { ArgvSetup.showHelp(); } else if (argv.info) { @@ -201,14 +211,6 @@ Nightwatch.cli = function(callback) { ).then(console.log); } else if (argv.version) { Utils.printVersionInfo(); - } else if (argv['list-files']) { - argv._source = argv['_'].slice(0); - const runner = Nightwatch.CliRunner(argv); - - runner.setupAsync() - .then(() => runner.getTestsFiles()) - // eslint-disable-next-line no-console - .then((result) => console.log(Logger.inspectObject(result))); } else { if (!Utils.isFunction(callback)) { throw new Error('Supplied callback argument needs to be a function.'); diff --git a/lib/reporter/global-reporter.js b/lib/reporter/global-reporter.js index 74cf40dd57..a01b8896ba 100644 --- a/lib/reporter/global-reporter.js +++ b/lib/reporter/global-reporter.js @@ -38,8 +38,8 @@ module.exports = class GlobalReporter { this.reporterFile = reporter; this.reportFileName = reportFileName; - if (!Array.isArray(this.reporterFile)) { - this.reporterFile = [reporter]; + if (!Array.isArray(this.reporterFile) && typeof this.reporterFile == 'string') { + this.reporterFile = this.reporterFile.split(','); } this.settings = settings; this.summary = new Summary(settings); diff --git a/test/src/cli/testCliRunner.js b/test/src/cli/testCliRunner.js index 7ff93aa12c..197b0ed077 100644 --- a/test/src/cli/testCliRunner.js +++ b/test/src/cli/testCliRunner.js @@ -647,7 +647,7 @@ describe('Test CLI Runner', function() { mockery.registerMock('fs', { existsSync() { - return false + return false; }, stat(file, cb) { if (file === TEST_SRC_PATH || file === './custom.js') { @@ -1288,4 +1288,59 @@ describe('Test CLI Runner', function() { assert.strictEqual(runner.test_settings.desiredCapabilities['moz:firefoxOptions'].androidPackage, 'org.mozilla.firefox'); assert.strictEqual(runner.test_settings.desiredCapabilities['moz:firefoxOptions'].androidDeviceSerial, 'ZD2222W62Y'); }); + + describe('Test \'list-files\' flag', () => { + it('output list of files - default environment', async () => { + const testsPath = [origPath.join(__dirname, origPath.join('..', '..', 'sampletests', 'simple', 'test', 'sample.js'))]; + const consoleData = []; + const listFileOutput = JSON.stringify({ + default: testsPath + }); + + const origConsoleLog = console.log; + + console.log = function (data) { + consoleData.push(data); + }; + + mockery.registerMock('./runner/cli/argv-setup.js', { + argv: { + _: testsPath, + 'list-files': true + } + }); + const NwClient = common.require('index.js'); + await NwClient.cli(); + assert.deepStrictEqual(listFileOutput, consoleData[0]); + console.log = origConsoleLog; + }); + + it('output list of files - chrome environment', async () => { + const testsPath = [origPath.join(__dirname, origPath.join('..', '..', 'sampletests', 'simple', 'test', 'sample.js'))]; + const consoleData = []; + const listFileOutput = JSON.stringify({ + chrome: testsPath + }); + + const origConsoleLog = console.log; + + console.log = function (data) { + consoleData.push(data); + }; + + mockery.registerMock('./runner/cli/argv-setup.js', { + argv: { + _: testsPath, + env: 'chrome', + 'list-files': true + } + }); + const NwClient = common.require('index.js'); + await NwClient.cli(); + assert.deepStrictEqual(listFileOutput, consoleData[0]); + console.log = origConsoleLog; + }); + }); + + }); diff --git a/test/src/runner/testReporter.js b/test/src/runner/testReporter.js index 96c62b6cba..967e35d60c 100644 --- a/test/src/runner/testReporter.js +++ b/test/src/runner/testReporter.js @@ -364,4 +364,32 @@ describe('testReporter', function() { } assert.strictEqual(possibleError, null); }); + + it('test with multiple reporters', function() { + mockery.registerMock('nightwatch_reporter', { + async write(_results, _options) { + + return 'nightwatch_reporter_output'; + } + }); + mockery.registerMock('html_reporter', { + async write(_results, _options) { + + return 'html_reporter_output'; + } + }); + + const reporter = new Reporter('nightwatch_reporter,html_reporter', { + globals: { + reporter(_results, done) { + done(); + } + }, + output_folder: 'output' + }); + + return reporter.writeReportToFile().then(function(result) { + assert.deepStrictEqual(result, ['nightwatch_reporter_output', 'html_reporter_output']); + }); + }); });