From 0bf6318a2250a9d631f8f9d8842ed83ed92e2b35 Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Sun, 19 Jan 2025 23:18:43 +0900 Subject: [PATCH] Update invalid tests against SOffice --- src/browser/manager.ts | 2 +- test/soffice/finder.ts | 103 ++++++++-------------------------------- test/soffice/soffice.ts | 64 +++---------------------- 3 files changed, 28 insertions(+), 141 deletions(-) diff --git a/src/browser/manager.ts b/src/browser/manager.ts index aa94d0de..081c64e9 100644 --- a/src/browser/manager.ts +++ b/src/browser/manager.ts @@ -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' ) diff --git a/test/soffice/finder.ts b/test/soffice/finder.ts index 13322856..4f37ec14 100644 --- a/test/soffice/finder.ts +++ b/test/soffice/finder.ts @@ -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') @@ -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 @@ -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', () => { @@ -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 } @@ -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), } }) @@ -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), ]) }) @@ -215,6 +204,8 @@ describe('SOffice finder', () => { expect(findExecutableSpy).toHaveBeenCalledWith([ path.join(...winProgramFiles, ...sofficePathRest), + path.join(...winUserProfile, ...sofficeScoopPathRest), + path.join(...winAllUsersProfile, ...sofficeScoopPathRest), ]) }) @@ -231,12 +222,16 @@ 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) @@ -244,60 +239,4 @@ describe('SOffice finder', () => { 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) - }) - }) }) diff --git a/test/soffice/soffice.ts b/test/soffice/soffice.ts index 070db0c2..eba96d73 100644 --- a/test/soffice/soffice.ts +++ b/test/soffice/soffice.ts @@ -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 } @@ -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() @@ -94,7 +93,7 @@ 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) @@ -102,61 +101,10 @@ describe('SOffice class', () => { 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 }) }) }) })