From 79ecb569c4ec99265f5312ee2c9b3c72bbb662e7 Mon Sep 17 00:00:00 2001 From: Artem-Babich <51639399+Artem-Babich@users.noreply.github.com> Date: Mon, 25 Apr 2022 19:19:06 +0400 Subject: [PATCH] Fix: Invalid browser specified in configuration throws an error when valid browser is specified in CLI(closes #6618) (#6993) * Fix: Invalid browser specified in configuration throws and error when valid browser is specified in CLI(closes #6618) --- src/cli/cli.js | 20 ++++----- src/configuration/testcafe-configuration.ts | 6 +-- src/runner/index.js | 2 +- test/server/configuration-test.js | 49 ++++++++++++++++++--- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/cli/cli.js b/src/cli/cli.js index 7d7d4db195..16a44ae897 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -66,12 +66,12 @@ function error (err) { } async function runTests (argParser) { - const opts = argParser.opts; - const port1 = opts.ports && opts.ports[0]; - const port2 = opts.ports && opts.ports[1]; - const proxy = opts.proxy; - const proxyBypass = opts.proxyBypass; - const configFile = opts.configFile; + const opts = argParser.opts; + const port1 = opts.ports && opts.ports[0]; + const port2 = opts.ports && opts.ports[1]; + const proxy = opts.proxy; + const proxyBypass = opts.proxyBypass; + const configFile = opts.configFile; log.showSpinner(); @@ -79,6 +79,7 @@ async function runTests (argParser) { const testCafe = await createTestCafe({ developmentMode: dev, + isCli: true, hostname, port1, @@ -102,8 +103,6 @@ async function runTests (argParser) { let failed = 0; - runner.isCli = true; - runner .useProxy(proxy, proxyBypass) .src(sources) @@ -126,7 +125,6 @@ async function runTests (argParser) { failed = await runner.run(runOpts); } - finally { showMessageOnExit = false; await testCafe.close(); @@ -149,10 +147,10 @@ async function listBrowsers (providerName) { if (providerName === 'locally-installed') console.log(browserNames.join('\n')); else - console.log(browserNames.map(browserName => `"${providerName}:${browserName}"`).join('\n')); + console.log(browserNames.map(browserName => `"${ providerName }:${ browserName }"`).join('\n')); } else - console.log(`"${providerName}"`); + console.log(`"${ providerName }"`); exit(0); } diff --git a/src/configuration/testcafe-configuration.ts b/src/configuration/testcafe-configuration.ts index 58f9ab702a..00e89e5785 100644 --- a/src/configuration/testcafe-configuration.ts +++ b/src/configuration/testcafe-configuration.ts @@ -94,7 +94,7 @@ export default class TestCafeConfiguration extends Configuration { this._isExplicitConfig = !!configFile; } - public async init (options?: object): Promise { + public async init (options?: Dictionary): Promise { await super.init(); const opts = await this._load(); @@ -108,12 +108,12 @@ export default class TestCafeConfiguration extends Configuration { await this.asyncMergeOptions(options); } - public async asyncMergeOptions (options?: object): Promise { + public async asyncMergeOptions (options?: Dictionary): Promise { options = options || {}; super.mergeOptions(options); - if (this._options.browsers) + if (!options.isCli && this._options.browsers) this._options.browsers.value = await this._getBrowserInfo(); } diff --git a/src/runner/index.js b/src/runner/index.js index 77641ac5b1..74507641dd 100644 --- a/src/runner/index.js +++ b/src/runner/index.js @@ -61,7 +61,7 @@ export default class Runner extends EventEmitter { this.bootstrapper = this._createBootstrapper(browserConnectionGateway, compilerService, this._messageBus); this.pendingTaskPromises = []; this.configuration = configuration; - this.isCli = false; + this.isCli = configuration._options && configuration._options.isCli; this.warningLog = new WarningLog(null, WarningLog.createAddWarningCallback(this._messageBus)); this.compilerService = compilerService; this._options = {}; diff --git a/test/server/configuration-test.js b/test/server/configuration-test.js index d4842e7209..03b24831e8 100644 --- a/test/server/configuration-test.js +++ b/test/server/configuration-test.js @@ -1,12 +1,12 @@ /*eslint-disable no-console */ const { cloneDeep, noop } = require('lodash'); -const { expect } = require('chai'); -const fs = require('fs'); -const tmp = require('tmp'); -const { nanoid } = require('nanoid'); -const del = require('del'); -const pathUtil = require('path'); +const { expect } = require('chai'); +const fs = require('fs'); +const tmp = require('tmp'); +const { nanoid } = require('nanoid'); +const del = require('del'); +const pathUtil = require('path'); const TestCafeConfiguration = require('../../lib/configuration/testcafe-configuration'); const TypeScriptConfiguration = require('../../lib/configuration/typescript-configuration'); @@ -540,6 +540,43 @@ describe('TestCafeConfiguration', function () { expect(testCafeConfiguration._overriddenOptions).eql([]); }); }); + + describe('[RG-6618] Incorrect browser is specified in config file when running tests from CLI', () => { + let configuration; + const customConfigFile = 'custom2.testcaferc.json'; + + const options = { + 'browsers': ['incorrectBrowser'], + }; + + before(async () => { + createJSONConfig(customConfigFile, options); + }); + + after(async () => { + await del(configuration.defaultPaths); + }); + + it('Should success create configuration with incorrect browser value', () => { + configuration = new TestCafeConfiguration(customConfigFile); + + return configuration.init({ isCli: true }) + .then(() => { + expect(pathUtil.basename(configuration.filePath)).eql(customConfigFile); + expect(configuration.getOption('browsers')).eql(options.browsers); + }); + }); + + it('Should throw an error in case of incorrect browser was passed not from CLI', () => { + configuration = new TestCafeConfiguration(customConfigFile); + + return configuration.init().then(() => { + throw new Error('Promise should be rejected'); + }).catch(err => { + expect(err.message).eql('Cannot find the browser. "incorrectBrowser" is neither a known browser alias, nor a path to an executable file.'); + }); + }); + }); }); describe('TypeScriptConfiguration', function () {