diff --git a/lib/index.js b/lib/index.js index edd8988..aa7b55d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -140,6 +140,10 @@ const open = (_args, opts = {}, extra = {}) => { // RC file, e.g. .bashrc or .zshrc. if (platform === 'linux' && os.release().toLowerCase().includes('microsoft')) { platform = 'wsl' + if (!process.env.BROWSER) { + return Promise.reject( + new Error('Set the BROWSER environment variable to your desired browser.')) + } } let command = options.command diff --git a/test/open.js b/test/open.js index 3918749..a5fa2ae 100644 --- a/test/open.js +++ b/test/open.js @@ -159,12 +159,14 @@ t.test('process.platform === linux', (t) => { t.ok(proc.called) }) - t.test('when os.release() includes Microsoft treats as win32', async (t) => { + t.test('when os.release() includes Microsoft treats as WSL', async (t) => { const promiseSpawnMock = t.mock('../lib/index.js', { os: { release: () => 'Microsoft', }, }) + const browser = process.env.BROWSER + process.env.BROWSER = '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' const proc = spawk.spawn('sh', ['-c', 'sensible-browser https://google.com'], { shell: false }) @@ -174,15 +176,21 @@ t.test('process.platform === linux', (t) => { signal: undefined, }) + t.teardown(() => { + process.env.BROWSER = browser + }) + t.ok(proc.called) }) - t.test('when os.release() includes microsoft treats as win32', async (t) => { + t.test('when os.release() includes microsoft treats as WSL', async (t) => { const promiseSpawnMock = t.mock('../lib/index.js', { os: { release: () => 'microsoft', }, }) + const browser = process.env.BROWSER + process.env.BROWSER = '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' const proc = spawk.spawn('sh', ['-c', 'sensible-browser https://google.com'], { shell: false }) @@ -192,9 +200,35 @@ t.test('process.platform === linux', (t) => { signal: undefined, }) + t.teardown(() => { + process.env.BROWSER = browser + }) + t.ok(proc.called) }) + t.test('fails on WSL if BROWSER is not set', async (t) => { + const promiseSpawnMock = t.mock('../lib/index.js', { + os: { + release: () => 'microsoft', + }, + }) + const browser = process.env.BROWSER + delete process.env.BROWSER + + const proc = spawk.spawn('sh', ['-c', 'sensible-browser https://google.com'], { shell: false }) + + await t.rejects(promiseSpawnMock.open('https://google.com'), { + message: 'Set the BROWSER environment variable to your desired browser.', + }) + + t.teardown(() => { + process.env.BROWSER = browser + }) + + t.notOk(proc.called) + }) + t.end() })