Skip to content

Commit

Permalink
Add test for new browser finder
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Sep 22, 2024
1 parent d22b562 commit 4108fe9
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/browser/finders/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import fs from 'node:fs'
import path from 'node:path'
import { promisify } from 'node:util'
import { parse as parsePlist } from 'fast-plist'
import { debugBrowserFinder } from '../../utils/debug'
import { isWSL } from '../../utils/wsl'
Expand Down
6 changes: 6 additions & 0 deletions test/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module.exports = {
plugins: ['jest'],
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
rules: {
'jest/no-standalone-expect': [
'error',
{ additionalTestBlockFunctions: ['itOnlyWin', 'itExceptWin'] },
],
},
}
115 changes: 115 additions & 0 deletions test/browser/finder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import path from 'node:path'
import { ChromeBrowser } from '../../src/browser/browsers/chrome'
import { ChromeCdpBrowser } from '../../src/browser/browsers/chrome-cdp'
import { FirefoxBrowser } from '../../src/browser/browsers/firefox'
import { autoFinders, findBrowser } from '../../src/browser/finder'
import { CLIError } from '../../src/error'

afterEach(() => {
jest.resetAllMocks()
jest.restoreAllMocks()
})

const itOnlyWin = process.platform === 'win32' ? it : it.skip
const itExceptWin = process.platform === 'win32' ? it.skip : it

const executableMock = (name: string) =>
path.join(__dirname, `../utils/_executable_mocks`, name)

const macBundle = (name: string) =>
path.join(__dirname, `../utils/_mac_bundles`, name)

describe('Browser finder', () => {
describe('#findBrowser', () => {
it('rejects when no finder is specified', async () => {
await expect(findBrowser([])).rejects.toThrow(CLIError)
})

it('resolves as Chrome browser when no finder is specified with preferred executable path', async () => {
const browser = await findBrowser([], {
preferredPath: executableMock('empty'),
})

expect(browser).toStrictEqual<typeof browser>({
path: expect.stringMatching(/\bempty$/),
acceptedBrowsers: [ChromeBrowser, ChromeCdpBrowser],
})
})

itExceptWin(
'rejects when no finder is specified with preferred non-executable path in non-Windows platforms',
async () => {
await expect(
findBrowser([], {
preferredPath: executableMock('non-executable'),
})
).rejects.toThrow(CLIError)
}
)

itOnlyWin(
'resolves as Chrome browser when no finder is specified with preferred non-executable path in Windows',
async () => {
const browser = await findBrowser([], {
preferredPath: executableMock('non-executable'),
})

expect(browser).toStrictEqual<typeof browser>({
path: expect.stringMatching(/\bnon-executable$/),
acceptedBrowsers: [ChromeBrowser, ChromeCdpBrowser],
})
}
)

it('resolves as Firefox browser when Firefox finder was preferred with preferred executable path', async () => {
const browser = await findBrowser(['firefox', 'chrome'], {
preferredPath: executableMock('empty'),
})

expect(browser).toStrictEqual<typeof browser>({
path: expect.stringMatching(/\bempty$/),
acceptedBrowsers: [FirefoxBrowser],
})
})

describe('with macOS', () => {
let originalPlatform: string | undefined

beforeEach(() => {
originalPlatform = process.platform
Object.defineProperty(process, 'platform', { value: 'darwin' })
})

afterEach(() => {
if (originalPlatform !== undefined) {
Object.defineProperty(process, 'platform', {
value: originalPlatform,
})
}
originalPlatform = undefined
})

it('normalizes executable path if preferred path was pointed to valid app bundle', async () => {
const browser = await findBrowser(autoFinders, {
preferredPath: macBundle('Valid.app'),
})

expect(browser).toStrictEqual<typeof browser>({
path: macBundle('Valid.app/Contents/MacOS/Valid app'),
acceptedBrowsers: [ChromeBrowser, ChromeCdpBrowser],
})
})

it('does not normalize executable path if preferred path was pointed to invalid app bundle', async () => {
const browser = await findBrowser(autoFinders, {
preferredPath: macBundle('Invalid.app'),
})

expect(browser).toStrictEqual<typeof browser>({
path: macBundle('Invalid.app'),
acceptedBrowsers: [ChromeBrowser, ChromeCdpBrowser],
})
})
})
})
})
Empty file modified test/utils/_executable_mocks/empty
100644 → 100755
Empty file.
Empty file.
Empty file modified test/utils/_executable_mocks/shebang-chromium
100644 → 100755
Empty file.
Empty file modified test/utils/_executable_mocks/shebang-snapd-chromium
100644 → 100755
Empty file.
Empty file modified test/utils/_mac_bundles/Invalid.app/Contents/MacOS/Invalid app
100644 → 100755
Empty file.
Empty file modified test/utils/_mac_bundles/Valid.app/Contents/MacOS/Valid app
100644 → 100755
Empty file.
45 changes: 22 additions & 23 deletions test/utils/wsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,69 +109,68 @@ describe('#isWSL', () => {
it('returns 1 if running on WSL 1', async () => {
jest.doMock('is-wsl', () => true)

const readFileSync = jest
.spyOn(fs, 'readFileSync')
.mockImplementation(
() => 'Linux version 4.5.6-12345-Microsoft (gcc version 5.4.0 (GCC) )'
const readFile = jest
.spyOn(fs.promises, 'readFile')
.mockResolvedValue(
'Linux version 4.5.6-12345-Microsoft (gcc version 5.4.0 (GCC) )'
)

expect(await wsl().isWSL()).toBe(1)
expect(readFileSync).toHaveBeenCalledTimes(1)
expect(readFile).toHaveBeenCalledTimes(1)

// Returns cached result to prevent excessive file I/O
await wsl().isWSL()
expect(readFileSync).toHaveBeenCalledTimes(1)
expect(readFile).toHaveBeenCalledTimes(1)
})

it('returns 2 if running on WSL 2 (Fast check by environment values)', async () => {
jest.doMock('is-wsl', () => true)

const readFileSync = jest.spyOn(fs, 'readFileSync')
const readFile = jest.spyOn(fs.promises, 'readFile')

// WSL 2 has WSL_DISTRO_NAME and WSL_INTEROP
process.env.WSL_DISTRO_NAME = 'Ubuntu'
process.env.WSL_INTEROP = '/run/WSL/11_interop'

expect(await wsl().isWSL()).toBe(2)
expect(readFileSync).not.toHaveBeenCalled()
expect(readFile).not.toHaveBeenCalled()
})

it('returns 2 if running on WSL 2 (Check WSL2 annotation in /proc/version string)', async () => {
jest.doMock('is-wsl', () => true)

const readFileSync = jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'Linux version 4.5.6-Microsoft-Standard-WSL2')
const readFile = jest
.spyOn(fs.promises, 'readFile')
.mockResolvedValue('Linux version 4.5.6-Microsoft-Standard-WSL2')

expect(await wsl().isWSL()).toBe(2)
expect(readFileSync).toHaveBeenCalledTimes(1)
expect(readFile).toHaveBeenCalledTimes(1)
})

it('returns 2 if running on WSL 2 (Check gcc version in /proc/version string)', async () => {
jest.doMock('is-wsl', () => true)

const readFileSync = jest
.spyOn(fs, 'readFileSync')
.mockImplementation(
() => 'Linux version 4.5.6-12345-Microsoft (gcc version 8.4.0 (GCC) )'
const readFile = jest
.spyOn(fs.promises, 'readFile')
.mockResolvedValue(
'Linux version 4.5.6-12345-Microsoft (gcc version 8.4.0 (GCC) )'
)

expect(await wsl().isWSL()).toBe(2)
expect(readFileSync).toHaveBeenCalledTimes(1)
expect(readFile).toHaveBeenCalledTimes(1)
})

it('returns 2 if running on WSL 2 (The latest format of /proc/version string)', async () => {
jest.doMock('is-wsl', () => true)

const readFileSync = jest
.spyOn(fs, 'readFileSync')
.mockImplementation(
() =>
'Linux version 5.10.74.3 (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220)'
const readFile = jest
.spyOn(fs.promises, 'readFile')
.mockResolvedValue(
'Linux version 5.10.74.3 (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220)'
)

expect(await wsl().isWSL()).toBe(2)
expect(readFileSync).toHaveBeenCalledTimes(1)
expect(readFile).toHaveBeenCalledTimes(1)
})
})

Expand Down

0 comments on commit 4108fe9

Please sign in to comment.