Skip to content

Commit

Permalink
fix: use resolve for incoming paths
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi committed Jan 24, 2024
1 parent 3c30270 commit 06d998e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
15 changes: 9 additions & 6 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,32 @@ export async function po2mo({
if (input.endsWith('.po')) {
if (output) {
if (output.endsWith('.mo')) {
await convertPoToMo(join(cwd, input), join(cwd, output))
await convertPoToMo(resolve(cwd, input), resolve(cwd, output))
return
}
if ((await stat(output)).isDirectory()) {
const filename = input.split('/').pop()?.replace('.po', '.mo')
await convertPoToMo(join(cwd, input), join(cwd, output, filename!))
await convertPoToMo(
resolve(cwd, input),
resolve(cwd, output, filename!)
)
return
}
}
await convertPoToMo(
join(cwd, input),
join(cwd, input.replace('.po', '.mo'))
resolve(cwd, input),
resolve(cwd, input.replace('.po', '.mo'))
)
return
}

const poEntries = await getPoEntries(join(cwd, input), recursive)
const poEntries = await getPoEntries(resolve(cwd, input), recursive)
const convertJobs: Promise<void>[] = poEntries.map((poEntry) => {
if (output) {
if (output.endsWith('.mo')) {
throw new Error('Output path is not a directory')
}
return convertPoToMo(poEntry, join(cwd, output))
return convertPoToMo(poEntry, resolve(cwd, output))
}
return convertPoToMo(poEntry, poEntry.replace('.po', '.mo'))
})
Expand Down
35 changes: 12 additions & 23 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
import { spawn } from 'child_process'
import { existsSync } from 'fs'
import { rm, mkdtemp } from 'fs/promises'
import { tmpdir } from 'os'
import { unlink } from 'fs/promises'
import { resolve, join } from 'path'

const fixturesDir = resolve(__dirname, 'fixtures')

async function createTempDir(): Promise<string> {
const tempDir = tmpdir()
const tempDirPrefix = 'po2mo-test'
const tempDirPath = join(tempDir, tempDirPrefix)

return await mkdtemp(tempDirPath)
}

async function removeTempDir(tempDirPath: string) {
await rm(tempDirPath, { recursive: true, force: true })
}

const tests = [
{
name: 'basic',
args: [`./${fixturesDir}/basic/basic.po`],
args: [`${fixturesDir}/basic`],
},
]

describe('cli', () => {
for (const test of tests) {
const { name, args } = test

it(`${name} should convert po to mo properly`, async () => {
const tempDir = await createTempDir()
const { stdout, stderr } = await spawn(
require.resolve('tsx/cli'),
[resolve('../src/bin/index.ts')].concat(args)
const cp = spawn(
`${require.resolve('tsx/cli')}`,
[resolve('./src/bin/index.ts')].concat(args)
)
const code = await new Promise((resolve) => {
cp.on('close', resolve)
})

if (stdout) console.log(stdout)
if (stderr) console.error(stderr)
expect(code).toBe(0)
expect(existsSync(join(fixturesDir, name, `${name}.mo`))).toBe(true)

expect(existsSync(join(tempDir, `${name}.mo`))).toBe(true)
await removeTempDir(tempDir)
await unlink(join(fixturesDir, name, `${name}.mo`))
})
}
})

0 comments on commit 06d998e

Please sign in to comment.