Skip to content

Commit

Permalink
fix: 修复 cli 测试在 Windows 上无法执行的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 12, 2022
1 parent 5eb3644 commit 717e936
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import consola from 'consola'
import fs from 'fs-extra'
import path from 'path'
import tempy from 'tempy'
import { CatId } from './consts'
import { run } from '../src/cli'
import { wait } from 'vtils'
import { uniqueId, wait } from 'vtils'

const tempDir = path.join(__dirname, '../.tmp-ytt-test')

beforeAll(() => {
fs.ensureDirSync(tempDir)
})

afterAll(() => {
fs.removeSync(tempDir)
})

function getTempPaths() {
const targetDir = tempy.directory()
fs.symlinkSync(
path.join(__dirname, '../node_modules'),
path.join(targetDir, 'node_modules'),
'dir',
)
const targetDir = path.join(tempDir, uniqueId('case'))
fs.ensureDirSync(targetDir)
const generatedConfigFile = path.join(targetDir, 'ytt.config.ts')
const generatedApiFile = path.join(targetDir, 'src/api/index.ts')
const generatedRequestFile = path.join(targetDir, 'src/api/request.ts')
Expand Down Expand Up @@ -76,7 +81,10 @@ describe('cli', () => {
fs
.readFileSync(tempPaths.generatedConfigFile)
.toString()
.replace('yapi-to-typescript', path.join(__dirname, '../src'))
.replace(
"'yapi-to-typescript'",
JSON.stringify(path.join(__dirname, '../src')),
)
.replace(`dataKey: 'data',`, '')
.replace(`id: 50,`, `id: ${CatId.test},`),
)
Expand Down Expand Up @@ -151,15 +159,24 @@ describe('cli', () => {
fs
.readFileSync(tempPaths.generatedConfigFile)
.toString()
.replace('yapi-to-typescript', path.join(__dirname, '../src'))
.replace(
"'yapi-to-typescript'",
JSON.stringify(path.join(__dirname, '../src')),
)
.replace(`dataKey: 'data',`, '')
.replace(`id: 50,`, `id: ${CatId.test},`)
.replace(
/(?=\)\s*$)/s,
`, {
success: () => require('fs').writeFileSync('${successFile}', 'success'),
fail: () => require('fs').writeFileSync('${failFile}', 'fail'),
complete: () => require('fs').writeFileSync('${completeFile}', 'complete'),
success: () => require('fs').writeFileSync(${JSON.stringify(
successFile,
)}, 'success'),
fail: () => require('fs').writeFileSync(${JSON.stringify(
failFile,
)}, 'fail'),
complete: () => require('fs').writeFileSync(${JSON.stringify(
completeFile,
)}, 'complete'),
}`,
),
)
Expand Down

0 comments on commit 717e936

Please sign in to comment.