diff --git a/package.json b/package.json index a982177..34c3f7f 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,9 @@ "tmp" ], "scripts": { - "test": "npm run test:bash && npm run test:node", - "test:bash": "sudo test/index.sh", - "test:node": "mocha test/index.js", + "test": "mocha test/index.js", "eslint": "eslint src/*.js", - "prettier": "prettier --write *.js", + "prettier": "prettier --write **/*.js", "prepare": "husky install" }, "repository": { diff --git a/src/browser-driver-manager.js b/src/browser-driver-manager.js index a873d6d..213c14d 100644 --- a/src/browser-driver-manager.js +++ b/src/browser-driver-manager.js @@ -174,7 +174,7 @@ async function version() { * @returns */ async function install(browserId, options) { - const [browser, version = 'stable'] = browserId.split('@'); + const [browser, version = 'stable'] = browserId.split(/@|=/); // Should support for other browsers be added, commander should handle this check. // With only one supported browser, this error message is more meaningful than commander's. diff --git a/test/index.js b/test/index.js index c4742ea..93e25fc 100644 --- a/test/index.js +++ b/test/index.js @@ -104,6 +104,7 @@ describe('browser-driver-manager', () => { sinon.assert.calledWith(consoleLogStub, envContents); }); }); + it('errors if no environment file exists', async () => { await wrapConsoleLogStub(async () => { await expect(which()).to.be.rejectedWith( @@ -112,6 +113,7 @@ describe('browser-driver-manager', () => { }); }); }); + describe('version', () => { it('logs the version when a valid one exists', async () => { await makeEnvFile(); @@ -120,12 +122,14 @@ describe('browser-driver-manager', () => { sinon.assert.calledWith(consoleLogStub, mockVersion); }); }); + describe('errors', () => { it('if no environment file exists', async () => { await expect(version()).to.be.rejectedWith( 'No environment file exists. Please install first' ); }); + it('if the environment file does not contain a version', async () => { await makeEnvFile(noVersionEnvContents); await expect(version()).to.be.rejectedWith( @@ -134,6 +138,7 @@ describe('browser-driver-manager', () => { }); }); }); + describe('install', () => { const chromeArgs = sinon.match({ cacheDir: sinon.match.string, @@ -145,6 +150,7 @@ describe('browser-driver-manager', () => { browser: 'chromedriver', buildId: sinon.match.string }); + it('calls the Puppeteer/browser installer when given a valid browser', async () => { await wrapConsoleLogStub(async () => { await install(browser); @@ -152,6 +158,55 @@ describe('browser-driver-manager', () => { sinon.assert.calledWith(mockInstall, chromeArgs); sinon.assert.calledWith(mockInstall, chromedriverArgs); }); + + it('uses "stable" when no version is passed', async () => { + await wrapConsoleLogStub(async () => { + await install('chrome'); + }); + sinon.assert.calledWith( + mockResolveBuildId, + sinon.match.string, + sinon.match.string, + 'stable' + ); + }); + + it('passes the version when using "@"', async () => { + await wrapConsoleLogStub(async () => { + await install('chrome@123'); + }); + sinon.assert.calledWith( + mockResolveBuildId, + sinon.match.string, + sinon.match.string, + '123' + ); + }); + + it('passes the version when using "="', async () => { + await wrapConsoleLogStub(async () => { + await install('chrome=123'); + }); + sinon.assert.calledWith( + mockResolveBuildId, + sinon.match.string, + sinon.match.string, + '123' + ); + }); + + it('uses "stable" when version is not formatted correctly', async () => { + await wrapConsoleLogStub(async () => { + await install('chrome^123'); + }); + sinon.assert.calledWith( + mockResolveBuildId, + sinon.match.string, + sinon.match.string, + 'stable' + ); + }); + describe('creates', () => { it("the cache directory if it doesn't already exist", async () => { await wrapConsoleLogStub(async () => { @@ -184,6 +239,7 @@ describe('browser-driver-manager', () => { expect(env.match(mockVersion)).to.not.be.null; }); }); + describe('errors when', () => { it('an unsupported browser is given', async () => { await expect(install('firefox')).to.be.rejectedWith( @@ -273,6 +329,7 @@ describe('browser-driver-manager', () => { sinon.assert.calledWith(consoleLogStub, sinon.match(mockVersion)); }); }); + describe('when the given version differs from the previous version', () => { describe('and both are valid', async () => { it(`logs the currently installed version and that we're overwriting`, async () => { @@ -288,6 +345,7 @@ describe('browser-driver-manager', () => { ); }); }); + describe('uninstalls the previous version of', () => { ['chrome', 'chromedriver'].forEach(browser => { it(browser, async () => { @@ -306,6 +364,7 @@ describe('browser-driver-manager', () => { }); }); }); + describe('installs the new version of', () => { ['chrome', 'chromedriver'].forEach(browser => { it(browser, async () => { @@ -324,6 +383,7 @@ describe('browser-driver-manager', () => { }); }); }); + it('logs the correct current version after installation', async () => { await wrapConsoleLogStub(async () => { await install(browser); @@ -334,6 +394,7 @@ describe('browser-driver-manager', () => { }); }); }); + describe('with an invalid first version, and a valid second version', async () => { it('does not have an env file until the second installation', async () => { mockResolveBuildId.throws(new Error('invalid version')); @@ -357,6 +418,7 @@ describe('browser-driver-manager', () => { }); }); }); + describe('with a valid first version, and an invalid second version', async () => { it('still has the first version after the second attempt', async () => { mockResolveBuildId.returns(mockVersion); @@ -377,6 +439,7 @@ describe('browser-driver-manager', () => { }); }); }); + describe('with two invalid versions', async () => { it('has no environment file after either attempt', async () => { for (let i = 0; i < 2; i++) { @@ -398,12 +461,14 @@ describe('browser-driver-manager', () => { }); }); }); + describe('does not show download progress when', () => { it('there are no options passed', () => { const downloadProgressCaller = ({ downloadProgressCallback }) => { downloadProgressCallback(1, 1); return { executablePath: chromeTestPath }; }; + let { install } = proxyquire('../src/browser-driver-manager', { '@puppeteer/browsers': { ...puppeteerBrowserMocks, @@ -423,6 +488,7 @@ describe('browser-driver-manager', () => { 'Downloading Chrome' ); }); + it('the verbose option is false', () => { const downloadProgressCaller = ({ downloadProgressCallback }) => { downloadProgressCallback(1, 1); @@ -448,6 +514,7 @@ describe('browser-driver-manager', () => { ); }); }); + describe('when the verbose option is true', () => { it('writes the browser and download progress', async () => { const downloadProgressCaller = ({ downloadProgressCallback }) => { @@ -471,6 +538,7 @@ describe('browser-driver-manager', () => { sinon.match(/Downloading Chrome.../) ); }); + it('writes when the download is done', async () => { const downloadProgressCaller = ({ downloadProgressCallback }) => { downloadProgressCallback(100, 100); diff --git a/tmp/README.md b/tmp/README.md deleted file mode 100644 index d429c7e..0000000 --- a/tmp/README.md +++ /dev/null @@ -1 +0,0 @@ -Intentionally left empty to allow downloading browser and driver files to a temp directory. Git cannot commit empty directories so a file must exist in the directory in order for the directory to be committed. \ No newline at end of file