Skip to content

Commit

Permalink
Fix: Invalid browser specified in configuration throws an error when …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
Artem-Babich committed Apr 25, 2022
1 parent 7569f6a commit 79ecb56
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
20 changes: 9 additions & 11 deletions src/cli/cli.js
Expand Up @@ -66,19 +66,20 @@ 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();

const { hostname, ssl, dev, experimentalDebug, retryTestPages, cache, disableHttp2, v8Flags } = opts;

const testCafe = await createTestCafe({
developmentMode: dev,
isCli: true,

hostname,
port1,
Expand All @@ -102,8 +103,6 @@ async function runTests (argParser) {

let failed = 0;

runner.isCli = true;

runner
.useProxy(proxy, proxyBypass)
.src(sources)
Expand All @@ -126,7 +125,6 @@ async function runTests (argParser) {

failed = await runner.run(runOpts);
}

finally {
showMessageOnExit = false;
await testCafe.close();
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/configuration/testcafe-configuration.ts
Expand Up @@ -94,7 +94,7 @@ export default class TestCafeConfiguration extends Configuration {
this._isExplicitConfig = !!configFile;
}

public async init (options?: object): Promise<void> {
public async init (options?: Dictionary<object>): Promise<void> {
await super.init();

const opts = await this._load();
Expand All @@ -108,12 +108,12 @@ export default class TestCafeConfiguration extends Configuration {
await this.asyncMergeOptions(options);
}

public async asyncMergeOptions (options?: object): Promise<void> {
public async asyncMergeOptions (options?: Dictionary<object>): Promise<void> {
options = options || {};

super.mergeOptions(options);

if (this._options.browsers)
if (!options.isCli && this._options.browsers)
this._options.browsers.value = await this._getBrowserInfo();
}

Expand Down
2 changes: 1 addition & 1 deletion src/runner/index.js
Expand Up @@ -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 = {};
Expand Down
49 changes: 43 additions & 6 deletions 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');
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 79ecb56

Please sign in to comment.