Skip to content

Commit

Permalink
fix: gracefully fails if trying to detect but edge not installed
Browse files Browse the repository at this point in the history
Closes #25.
  • Loading branch information
Kelly Selden committed Oct 5, 2022
1 parent 2c86376 commit ae3f976
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,41 @@ async function getDriverVersion() {
if (process.env.EDGEDRIVER_VERSION) {
version = process.env.EDGEDRIVER_VERSION;
} else if (yn(process.env.DETECT_EDGEDRIVER_VERSION)) {
const browserCmd = (() => {
let browserCmd = (() => {
let edgePathOverride = process.env.EDGE_PATH;

if (edgePathOverride) {
return edgePathOverride;
}

switch (platform) {
case 'linux': return 'microsoft-edge';
case 'darwin': return '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge';
default: throw new Error(`Platform "${platform}" not supported`);
}
})();

let ps = await execa(browserCmd, ['--version']);
let ps;

// "Microsoft Edge 105.0.1343.53 "
version = ps.stdout.match(/(?:\d|\.)+/)[0];
try {
ps = await execa(browserCmd, ['--version']);
} catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}

console.log(`DETECT_EDGEDRIVER_VERSION=${process.env.DETECT_EDGEDRIVER_VERSION}, detected version ${version}`);
} else {
if (ps) {
// "Microsoft Edge 105.0.1343.53 "
version = ps.stdout.match(/(?:\d|\.)+/)[0];

console.log(`DETECT_EDGEDRIVER_VERSION=${process.env.DETECT_EDGEDRIVER_VERSION}, detected version ${version}`);
} else {
console.log(`DETECT_EDGEDRIVER_VERSION=${process.env.DETECT_EDGEDRIVER_VERSION}, but ${browserCmd} not found`);
}
}

if (!version) {
version = await getLatestDriverVersion();
}

Expand Down
19 changes: 18 additions & 1 deletion test/acceptance/install-msedgedriver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const execa = require('execa');
const fs = require('fs').promises;
const { getDriverPath } = require('../../src');
const path = require('path');
const { oldVersion } = require('../helpers/edge');
const { oldVersion, missingPath } = require('../helpers/edge');

const installerPath = require.resolve('../../bin/install-msedgedriver');
const driverPath = getDriverPath();
Expand Down Expand Up @@ -87,4 +87,21 @@ describe(path.basename(installerPath), function() {

expect(ps.stdout).to.include('DETECT_EDGEDRIVER_VERSION=true, detected version ');
});

it('gracefully fails if trying to detect but edge not installed', async function() {
if (process.platform === 'win32') {
return this.skip();
}

let ps = await execa.node(installerPath, [], {
env: {
DETECT_EDGEDRIVER_VERSION: 'true',
EDGE_PATH: missingPath,
},
});

expect(driverPath).to.be.a.file();

expect(ps.stdout).to.include(`DETECT_EDGEDRIVER_VERSION=true, but ${missingPath} not found`);
});
});
2 changes: 2 additions & 0 deletions test/helpers/edge.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

const oldVersion = '102.0.1245.33';
const missingPath = 'missing-edge';

module.exports = {
oldVersion,
missingPath,
};

0 comments on commit ae3f976

Please sign in to comment.