Skip to content

Commit

Permalink
Update invalid tests against SOffice
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Jan 19, 2025
1 parent c11e616 commit 0bf6318
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 141 deletions.
2 changes: 1 addition & 1 deletion src/browser/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class BrowserManager implements AsyncDisposable {
this._finderResult.value = undefined // Reset finder result cache
}
if (config.protocol) {
if (this._conversionBrowser)
if (this._conversionBrowser.value)
debugBrowser(
'WARNING: Changing protocol after created browser for conversion is not supported'
)
Expand Down
103 changes: 21 additions & 82 deletions test/soffice/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import which from 'which'
import { CLIError } from '../../src/error'
import { findSOffice } from '../../src/soffice/finder'
import * as utils from '../../src/utils/finder'
import * as wsl from '../../src/utils/wsl'

jest.mock('which')

Expand All @@ -17,6 +16,13 @@ afterEach(() => {
})

const sofficePathRest = ['LibreOffice', 'program', 'soffice.exe']
const sofficeScoopPathRest = [
'scoop',
'apps',
'libreoffice',
'current',
...sofficePathRest,
]

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

Expand Down Expand Up @@ -113,31 +119,6 @@ describe('SOffice finder', () => {
await expect(findSOffice({})).rejects.toThrow(CLIError)
expect(which).toHaveBeenCalled()
})

it('fallbacks to WSL resolution if in WSL 2 with mirrored network mode', async () => {
jest.spyOn(wsl, 'getWSL2NetworkingMode').mockResolvedValue('mirrored')

mockedWhich.mockImplementation(async () => null)

jest
.spyOn(utils, 'isExecutable')
.mockImplementation(
async (p) =>
p === '/mnt/c/Program Files/LibreOffice/program/soffice.exe'
)

expect(await findSOffice({})).toStrictEqual({
path: '/mnt/c/Program Files/LibreOffice/program/soffice.exe',
})
expect(which).toHaveBeenCalled()
})

it('throws error if in WSL 2 with NAT mode', async () => {
jest.spyOn(wsl, 'getWSL2NetworkingMode').mockResolvedValue('nat')
mockedWhich.mockImplementation(async () => null)

await expect(findSOffice({})).rejects.toThrow(CLIError)
})
})

describe('with macOS', () => {
Expand Down Expand Up @@ -172,6 +153,8 @@ describe('SOffice finder', () => {
describe('with Windows', () => {
const winProgramFiles = ['c:', 'Mock', 'Program Files']
const winProgramFilesX86 = ['c:', 'Mock', 'Program Files (x86)']
const winUserProfile = ['c:', 'Mock', 'Users', 'user']
const winAllUsersProfile = ['c:', 'Mock', 'ProgramData']
const sofficePath = path.join(...winProgramFiles, ...sofficePathRest)

const originalEnv = { ...process.env }
Expand All @@ -189,6 +172,8 @@ describe('SOffice finder', () => {
PATH: undefined,
PROGRAMFILES: path.join(...winProgramFiles),
'PROGRAMFILES(X86)': path.join(...winProgramFilesX86),
USERPROFILE: path.join(...winUserProfile),
ALLUSERSPROFILE: path.join(...winAllUsersProfile),
}
})

Expand All @@ -204,6 +189,10 @@ describe('SOffice finder', () => {
expect(findExecutableSpy).toHaveBeenCalledWith([
path.join(...winProgramFiles, ...sofficePathRest),
path.join(...winProgramFilesX86, ...sofficePathRest),

// Scoop
path.join(...winUserProfile, ...sofficeScoopPathRest),
path.join(...winAllUsersProfile, ...sofficeScoopPathRest),
])
})

Expand All @@ -215,6 +204,8 @@ describe('SOffice finder', () => {

expect(findExecutableSpy).toHaveBeenCalledWith([
path.join(...winProgramFiles, ...sofficePathRest),
path.join(...winUserProfile, ...sofficeScoopPathRest),
path.join(...winAllUsersProfile, ...sofficeScoopPathRest),
])
})

Expand All @@ -231,73 +222,21 @@ describe('SOffice finder', () => {
path.join('d:', ...winProgramFilesX86.slice(1), ...sofficePathRest),
path.join('z:', ...winProgramFiles.slice(1), ...sofficePathRest),
path.join('z:', ...winProgramFilesX86.slice(1), ...sofficePathRest),
path.join(...winUserProfile, ...sofficeScoopPathRest),
path.join(...winAllUsersProfile, ...sofficeScoopPathRest),
])
})

it('throws error if no executable path is found', async () => {
process.env.PROGRAMFILES = ''
process.env['PROGRAMFILES(X86)'] = ''
process.env.USERPROFILE = ''
process.env.ALLUSERSPROFILE = ''

const findExecutableSpy = jest.spyOn(utils, 'findExecutable')
await expect(findSOffice({})).rejects.toThrow(CLIError)

expect(findExecutableSpy).toHaveBeenCalledWith([])
})
})

describe('with WSL1', () => {
const originalEnv = { ...process.env }

beforeEach(() => {
jest.resetModules()

jest.spyOn(utils, 'getPlatform').mockResolvedValue('wsl1')
jest
.spyOn(utils, 'isExecutable')
.mockImplementation(
async (p) =>
p === '/mnt/c/Program Files/LibreOffice/program/soffice.exe'
)

process.env = { ...originalEnv, PATH: undefined }
})

afterEach(() => {
process.env = { ...originalEnv }
})

it('finds possible executable path and returns the matched path', async () => {
const findExecutableSpy = jest.spyOn(utils, 'findExecutable')
const soffice = await findSOffice({})

expect(soffice).toStrictEqual({
path: '/mnt/c/Program Files/LibreOffice/program/soffice.exe',
})
expect(findExecutableSpy).toHaveBeenCalledWith([
path.posix.join('/mnt/c/Program Files', ...sofficePathRest),
path.posix.join('/mnt/c/Program Files (x86)', ...sofficePathRest),
])
})

it('finds from detected drives when the PATH environment has paths starting with any drive letters', async () => {
process.env.PATH = '/mnt/z/Mock:/mnt/d/Mock:/mnt/d/Mock/Mock'

const findExecutableSpy = jest.spyOn(utils, 'findExecutable')
await findSOffice({})

expect(findExecutableSpy).toHaveBeenCalledWith([
path.posix.join('/mnt/c/Program Files', ...sofficePathRest),
path.posix.join('/mnt/c/Program Files (x86)', ...sofficePathRest),
path.posix.join('/mnt/d/Program Files', ...sofficePathRest),
path.posix.join('/mnt/d/Program Files (x86)', ...sofficePathRest),
path.posix.join('/mnt/z/Program Files', ...sofficePathRest),
path.posix.join('/mnt/z/Program Files (x86)', ...sofficePathRest),
])
})

it('throws error if no executable path is found', async () => {
jest.spyOn(utils, 'isExecutable').mockResolvedValue(false)
await expect(findSOffice({})).rejects.toThrow(CLIError)
})
})
})
64 changes: 6 additions & 58 deletions test/soffice/soffice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import EventEmitter from 'node:events'
import fs from 'node:fs'
import * as cli from '../../src/cli'
import { SOffice } from '../../src/soffice/soffice'
import * as wsl from '../../src/utils/wsl'

const defaultSpawnSetting = { code: 0, delay: 50 }
const spawnSetting = { ...defaultSpawnSetting }
Expand Down Expand Up @@ -60,7 +59,7 @@ describe('SOffice class', () => {
it('throws error if soffice exits with non-zero code', async () => {
spawnSetting.code = 123

jest.spyOn(cli, 'error').mockImplementation()
jest.spyOn(cli, 'warn').mockImplementation()

const spawnSpy = jest.spyOn(childProcess, 'spawn')
const soffice = new SOffice()
Expand Down Expand Up @@ -94,69 +93,18 @@ describe('SOffice class', () => {
})

describe('get #profileDir', () => {
it('returns the profile directory', async () => {
it('returns the profile directory object', async () => {
const mkdir = jest
.spyOn(fs.promises, 'mkdir')
.mockResolvedValue(undefined)

const soffice = new SOffice()
const profileDir = await soffice.profileDir

expect(profileDir).toContain('marp-cli-soffice-')
expect(mkdir).toHaveBeenCalledWith(profileDir, { recursive: true })
})

it('returns the profile directory with Windows TMP if the binary is in WSL host', async () => {
const mkdir = jest
.spyOn(fs.promises, 'mkdir')
.mockResolvedValue(undefined)

jest.spyOn(wsl, 'getWindowsEnv').mockResolvedValue('C:\\Windows\\Temp')
jest
.spyOn(wsl, 'translateWindowsPathToWSL')
.mockResolvedValue('/mnt/c/Windows/Temp/test')

const soffice = new SOffice()
jest.spyOn(soffice as any, 'binaryInWSLHost').mockResolvedValue(true)

const profileDir = await soffice.profileDir
expect(profileDir).toContain('C:\\Windows\\Temp')
expect(profileDir).toContain('marp-cli-soffice-')
expect(mkdir).toHaveBeenCalledWith('/mnt/c/Windows/Temp/test', {
recursive: true,
})
})
})

describe('private #binaryInWSLHost', () => {
it('always returns false if the current environment is not WSL', async () => {
jest.spyOn(wsl, 'isWSL').mockResolvedValue(0)

const soffice: any = new SOffice({
path: '/mnt/c/Program Files/LibreOffice/program/soffice.exe',
})

expect(await soffice.binaryInWSLHost()).toBe(false)
})

it('returns true if the current environment is WSL and the browser path is located in the host OS', async () => {
jest.spyOn(wsl, 'isWSL').mockResolvedValue(1)

const soffice: any = new SOffice({
path: '/mnt/c/Program Files/LibreOffice/program/soffice.exe',
})

expect(await soffice.binaryInWSLHost()).toBe(true)
})

it('returns false if the current environment is WSL and the browser path is not located in the host OS', async () => {
jest.spyOn(wsl, 'isWSL').mockResolvedValue(1)

const soffice: any = new SOffice({
path: '/usr/lib/libreoffice/program/libreoffice',
})

expect(await soffice.binaryInWSLHost()).toBe(false)
expect(profileDir.path).toContain('marp-cli-soffice-')
expect(profileDir.fileURL).toContain('marp-cli-soffice-')
expect(profileDir.fileURL.startsWith('file://')).toBe(true)
expect(mkdir).toHaveBeenCalledWith(profileDir.path, { recursive: true })
})
})
})

0 comments on commit 0bf6318

Please sign in to comment.