Skip to content

Commit

Permalink
Reject if BROWSER not set
Browse files Browse the repository at this point in the history
  • Loading branch information
mbtools committed Oct 17, 2024
1 parent 73d7116 commit 2f4aa14
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 36 additions & 2 deletions test/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand All @@ -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 })

Expand All @@ -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()
})

Expand Down

0 comments on commit 2f4aa14

Please sign in to comment.