import { tmpdir } from 'node:os' import { describe, expect, it, onTestFinished } from 'vitest' import { fs, path } from 'vuepress/utils' import { assertSafeOutputDir } from '../../src/node/utils/assertSafeOutputDir.js' interface Fixture { cwd: string rootDir: string sourceDir: string } /** * Create a temporary project layout outside of the repository * * The temp root is a symlink on macOS (`/var` -> `/private/var`) and the helper * resolves symlinks, so the root is canonicalized before being used. * * 在仓库之外创建临时的项目结构 * * MacOS 上的临时根目录是软链接(`/var` -> `/private/var`),而工具函数会解析软链接,因此根目录会先被规范化。 * * @returns The paths of the fixture / 测试结构中的路径 */ const createFixture = async (): Promise => { const rootDir = await fs.realpath( await fs.mkdtemp(path.join(tmpdir(), 'vuepress-redirect-')), ) const sourceDir = path.join(rootDir, 'site', 'docs') const cwd = path.join(rootDir, 'work', 'nested') await fs.ensureDir(sourceDir) await fs.ensureDir(cwd) onTestFinished(async () => { await fs.remove(rootDir) }) return { cwd, rootDir, sourceDir } } describe(assertSafeOutputDir, () => { it('rejects the source directory', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ output: sourceDir, sourceDir, cwd }), ).rejects.toThrow(/source directory or an ancestor/u) }) it('rejects the working directory', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ output: cwd, sourceDir, cwd }), ).rejects.toThrow(/working directory or an ancestor/u) }) it('rejects "." as the output directory', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ output: '.', sourceDir, cwd }), ).rejects.toThrow(/working directory or an ancestor/u) }) it('rejects an ancestor of the source directory', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ output: path.dirname(sourceDir), sourceDir, cwd, }), ).rejects.toThrow(/source directory or an ancestor/u) }) it('rejects an ancestor of the working directory', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ output: '..', sourceDir, cwd }), ).rejects.toThrow(/working directory or an ancestor/u) }) it('allows the default output directory', async () => { const { cwd, sourceDir } = await createFixture() await expect(assertSafeOutputDir({ sourceDir, cwd })).resolves.toBe( path.join(sourceDir, '.vuepress', 'redirect'), ) }) it('allows a non-empty default output directory without force', async () => { const { cwd, sourceDir } = await createFixture() const output = path.join(sourceDir, '.vuepress', 'redirect') await fs.ensureDir(output) await fs.writeFile(path.join(output, 'index.html'), 'stale') await expect(assertSafeOutputDir({ sourceDir, cwd })).resolves.toBe(output) }) it('allows an explicit sibling directory outside the source directory', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const output = path.join(rootDir, 'redirect') await expect(assertSafeOutputDir({ output, sourceDir, cwd })).resolves.toBe( output, ) }) it('allows an explicit child directory of the working directory', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ output: 'out', sourceDir, cwd }), ).resolves.toBe(path.join(cwd, 'out')) }) it('allows an explicit child directory of the source directory', async () => { const { cwd, sourceDir } = await createFixture() const output = path.join(sourceDir, 'redirect') await expect(assertSafeOutputDir({ output, sourceDir, cwd })).resolves.toBe( output, ) }) it('rejects a non-empty explicit output directory without force', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const output = path.join(rootDir, 'redirect') await fs.ensureDir(output) await fs.writeFile(path.join(output, 'index.html'), 'stale') await expect( assertSafeOutputDir({ output, sourceDir, cwd }), ).rejects.toThrow(/Pass --force/u) }) it('allows a non-empty explicit output directory with force', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const output = path.join(rootDir, 'redirect') await fs.ensureDir(output) await fs.writeFile(path.join(output, 'index.html'), 'stale') await expect( assertSafeOutputDir({ force: true, output, sourceDir, cwd }), ).resolves.toBe(output) }) it('rejects an unsafe output directory even with force', async () => { const { cwd, sourceDir } = await createFixture() await expect( assertSafeOutputDir({ force: true, output: sourceDir, sourceDir, cwd }), ).rejects.toThrow(/source directory or an ancestor/u) }) it('rejects the directory of the user config file', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const configDir = path.join(rootDir, 'config') await fs.ensureDir(configDir) await expect( assertSafeOutputDir({ configDir, output: configDir, sourceDir, cwd }), ).rejects.toThrow(/user config file/u) }) it('rejects an ancestor of the user config directory', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const configDir = path.join(rootDir, 'shared', 'config') await fs.ensureDir(configDir) await expect( assertSafeOutputDir({ configDir, output: path.join(rootDir, 'shared'), sourceDir, cwd, }), ).rejects.toThrow(/user config file/u) }) it('rejects an output directory resolving to the source directory through a symlink', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const link = path.join(rootDir, 'docs-link') await fs.symlink(sourceDir, link, 'dir') await expect( assertSafeOutputDir({ output: link, sourceDir, cwd }), ).rejects.toThrow(/source directory or an ancestor/u) }) it('rejects an output directory that is not a directory', async () => { const { cwd, rootDir, sourceDir } = await createFixture() const output = path.join(rootDir, 'redirect.html') await fs.writeFile(output, '') await expect( assertSafeOutputDir({ output, sourceDir, cwd }), ).rejects.toThrow(/not a directory/u) }) })