Skip to content

Commit

Permalink
Add test for get SOffice.profileDir
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Jan 15, 2025
1 parent 8f82b58 commit 1a70e60
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/soffice/soffice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class SOffice {
let needToTranslateWindowsPathToWSL = false

const dir = await (async () => {
// In WSL environment, Marp CLI may use Chrome on Windows. If Chrome has
// In WSL environment, Marp CLI may use soffice on Windows. If soffice has
// located in host OS (Windows), we have to specify Windows path.
if (await this.binaryInWSLHost()) {
if (wslTmp === undefined) wslTmp = await getWindowsEnv('TMP')
Expand Down
36 changes: 36 additions & 0 deletions test/soffice/soffice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as childProcess from 'node:child_process'
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'
Expand Down Expand Up @@ -92,6 +93,41 @@ describe('SOffice class', () => {
})
})

describe('get #profileDir', () => {
it('returns the profile directory', 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)
Expand Down

0 comments on commit 1a70e60

Please sign in to comment.