Skip to content

Commit

Permalink
Warn when given unsupported product name. (#5845)
Browse files Browse the repository at this point in the history
* Warn when given unsupported product name.

Fixes #5844.

This change means when a user launches Puppeteer with a product name
that is not supported (which at the time of this commit means it's not
`firefox` or `chrome) we will warn them about it.

Decided on just a warning vs an error because the current behaviour is
that we fallback to launching Chrome and I don't think this warrants a
breaking change.
  • Loading branch information
jackfranklin committed May 12, 2020
1 parent 6099272 commit b38bb43
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -82,6 +82,7 @@
"pixelmatch": "^4.0.2",
"pngjs": "^5.0.0",
"prettier": "^2.0.5",
"sinon": "^9.0.2",
"text-diff": "^1.0.1",
"typescript": "3.8.3"
},
Expand Down
9 changes: 9 additions & 0 deletions src/Launcher.ts
Expand Up @@ -1027,6 +1027,15 @@ function Launcher(
);
case 'chrome':
default:
if (typeof product !== 'undefined' && product !== 'chrome') {
/* The user gave us an incorrect product name
* we'll default to launching Chrome, but log to the console
* to let the user know (they've probably typoed).
*/
console.warn(
`Warning: unknown product name ${product}. Falling back to chrome.`
);
}
return new ChromeLauncher(
projectRoot,
preferredRevision,
Expand Down
14 changes: 14 additions & 0 deletions test/launcher.spec.js
Expand Up @@ -16,6 +16,7 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const sinon = require('sinon');
const { helper } = require('../lib/helper');
const rmAsync = helper.promisify(require('rimraf'));
const mkdtempAsync = helper.promisify(fs.mkdtemp);
Expand Down Expand Up @@ -444,6 +445,19 @@ describe('Launcher specs', function () {
expect(userAgent).toContain('Chrome');
});

it('falls back to launching chrome if there is an unknown product but logs a warning', async () => {
const { puppeteer } = getTestState();
const consoleStub = sinon.stub(console, 'warn');
const browser = await puppeteer.launch({ product: 'SO_NOT_A_PRODUCT' });
const userAgent = await browser.userAgent();
await browser.close();
expect(userAgent).toContain('Chrome');
expect(consoleStub.callCount).toEqual(1);
expect(consoleStub.firstCall.args).toEqual([
'Warning: unknown product name SO_NOT_A_PRODUCT. Falling back to chrome.',
]);
});

/* We think there's a bug in the FF Windows launcher, or some
* combo of that plus it running on CI, but we're deferring fixing
* this so we can get Windows CI stable and then dig into this
Expand Down
5 changes: 5 additions & 0 deletions test/mocha-utils.js
Expand Up @@ -18,6 +18,7 @@ const { TestServer } = require('../utils/testserver/index');
const path = require('path');
const fs = require('fs');
const os = require('os');
const sinon = require('sinon');
const puppeteer = require('../');
const utils = require('./utils');
const { trackCoverage } = require('./coverage-utils');
Expand Down Expand Up @@ -169,3 +170,7 @@ after(async () => {
await state.httpsServer.stop();
state.httpsServer = null;
});

afterEach(() => {
sinon.restore();
});

0 comments on commit b38bb43

Please sign in to comment.