mirror of
https://github.com/hanzoai/vite.git
synced 2026-10-30 19:36:16 +00:00
118 lines
3.8 KiB
TypeScript
118 lines
3.8 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import type { SyncOptions, SyncResult } from 'execa'
|
|
import { execaCommandSync } from 'execa'
|
|
import { afterEach, beforeAll, expect, test } from 'vitest'
|
|
|
|
const CLI_PATH = path.join(__dirname, '..')
|
|
|
|
const projectName = 'test-app'
|
|
const genPath = path.join(__dirname, projectName)
|
|
|
|
const run = <SO extends SyncOptions>(
|
|
args: string[],
|
|
options?: SO,
|
|
): SyncResult<SO> => {
|
|
return execaCommandSync(`node ${CLI_PATH} ${args.join(' ')}`, options)
|
|
}
|
|
|
|
// Helper to create a non-empty directory
|
|
const createNonEmptyDir = () => {
|
|
// Create the temporary directory
|
|
fs.mkdirSync(genPath, { recursive: true })
|
|
|
|
// Create a package.json file
|
|
const pkgJson = path.join(genPath, 'package.json')
|
|
fs.writeFileSync(pkgJson, '{ "foo": "bar" }')
|
|
}
|
|
|
|
// Vue 3 starter template
|
|
const templateFiles = fs
|
|
.readdirSync(path.join(CLI_PATH, 'template-vue'))
|
|
// _gitignore is renamed to .gitignore
|
|
.map((filePath) => (filePath === '_gitignore' ? '.gitignore' : filePath))
|
|
.sort()
|
|
|
|
beforeAll(() => fs.rmSync(genPath, { recursive: true, force: true }))
|
|
afterEach(() => fs.rmSync(genPath, { recursive: true, force: true }))
|
|
|
|
test('prompts for the project name if none supplied', () => {
|
|
const { stdout } = run([])
|
|
expect(stdout).toContain('Project name:')
|
|
})
|
|
|
|
test('prompts for the framework if none supplied when target dir is current directory', () => {
|
|
fs.mkdirSync(genPath, { recursive: true })
|
|
const { stdout } = run(['.'], { cwd: genPath })
|
|
expect(stdout).toContain('Select a framework:')
|
|
})
|
|
|
|
test('prompts for the framework if none supplied', () => {
|
|
const { stdout } = run([projectName])
|
|
expect(stdout).toContain('Select a framework:')
|
|
})
|
|
|
|
test('prompts for the framework on not supplying a value for --template', () => {
|
|
const { stdout } = run([projectName, '--template'])
|
|
expect(stdout).toContain('Select a framework:')
|
|
})
|
|
|
|
test('prompts for the framework on supplying an invalid template', () => {
|
|
const { stdout } = run([projectName, '--template', 'unknown'])
|
|
expect(stdout).toContain(
|
|
`"unknown" isn't a valid template. Please choose from below:`,
|
|
)
|
|
})
|
|
|
|
test('asks to overwrite non-empty target directory', () => {
|
|
createNonEmptyDir()
|
|
const { stdout } = run([projectName], { cwd: __dirname })
|
|
expect(stdout).toContain(`Target directory "${projectName}" is not empty.`)
|
|
})
|
|
|
|
test('asks to overwrite non-empty current directory', () => {
|
|
createNonEmptyDir()
|
|
const { stdout } = run(['.'], { cwd: genPath })
|
|
expect(stdout).toContain(`Current directory is not empty.`)
|
|
})
|
|
|
|
test('successfully scaffolds a project based on vue starter template', () => {
|
|
const { stdout } = run([projectName, '--template', 'vue'], {
|
|
cwd: __dirname,
|
|
})
|
|
const generatedFiles = fs.readdirSync(genPath).sort()
|
|
|
|
// Assertions
|
|
expect(stdout).toContain(`Scaffolding project in ${genPath}`)
|
|
expect(templateFiles).toEqual(generatedFiles)
|
|
})
|
|
|
|
test('works with the -t alias', () => {
|
|
const { stdout } = run([projectName, '-t', 'vue'], {
|
|
cwd: __dirname,
|
|
})
|
|
const generatedFiles = fs.readdirSync(genPath).sort()
|
|
|
|
// Assertions
|
|
expect(stdout).toContain(`Scaffolding project in ${genPath}`)
|
|
expect(templateFiles).toEqual(generatedFiles)
|
|
})
|
|
|
|
test('accepts command line override for --overwrite', () => {
|
|
createNonEmptyDir()
|
|
const { stdout } = run(['.', '--overwrite', 'ignore'], { cwd: genPath })
|
|
expect(stdout).not.toContain(`Current directory is not empty.`)
|
|
})
|
|
|
|
test('return help usage how to use create-vite', () => {
|
|
const { stdout } = run(['--help'], { cwd: __dirname })
|
|
const message = 'Usage: create-vite [OPTION]... [DIRECTORY]'
|
|
expect(stdout).toContain(message)
|
|
})
|
|
|
|
test('return help usage how to use create-vite with -h alias', () => {
|
|
const { stdout } = run(['--h'], { cwd: __dirname })
|
|
const message = 'Usage: create-vite [OPTION]... [DIRECTORY]'
|
|
expect(stdout).toContain(message)
|
|
})
|