commit
This commit is contained in:
@@ -3,7 +3,7 @@ import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { simpleGit } from 'simple-git'
|
||||
import { buildTreeFromPaths, createProjectFile, readAll, readDirChildren, readProjectFile, readTree, writeProjectFile } from '../src/main/fs-service'
|
||||
import { buildTreeFromPaths, createProjectDir, createProjectFile, readAll, readDirChildren, readProjectFile, readTree, renameProjectEntry, writeProjectFile } from '../src/main/fs-service'
|
||||
|
||||
let dir = ''
|
||||
afterEach(async () => { if (dir) await rm(dir, { recursive: true, force: true }) })
|
||||
@@ -150,3 +150,48 @@ describe('createProjectFile', () => {
|
||||
await expect(createProjectFile(dir, '../escape.txt')).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('renameProjectEntry', () => {
|
||||
it('renames a file in place and returns the new relative path', async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'helder-fs-'))
|
||||
await createProjectFile(dir, 'src/old.ts')
|
||||
await writeProjectFile(dir, 'src/old.ts', 'x\n')
|
||||
const next = await renameProjectEntry(dir, 'src/old.ts', 'new.ts')
|
||||
expect(next).toBe('src/new.ts')
|
||||
expect(await readProjectFile(dir, 'src/new.ts')).toBe('x\n')
|
||||
})
|
||||
|
||||
it('renames a folder with everything inside it', async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'helder-fs-'))
|
||||
await createProjectFile(dir, 'old/a.ts')
|
||||
await writeProjectFile(dir, 'old/a.ts', 'a\n')
|
||||
const next = await renameProjectEntry(dir, 'old', 'new')
|
||||
expect(next).toBe('new')
|
||||
expect(await readProjectFile(dir, 'new/a.ts')).toBe('a\n')
|
||||
})
|
||||
|
||||
it('changes only the case of a name', async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'helder-fs-'))
|
||||
await writeProjectFile(dir, 'notes.md', 'hi\n')
|
||||
expect(await renameProjectEntry(dir, 'notes.md', 'Notes.md')).toBe('Notes.md')
|
||||
expect(await readProjectFile(dir, 'Notes.md')).toBe('hi\n')
|
||||
})
|
||||
|
||||
it('refuses a name that is already taken', async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'helder-fs-'))
|
||||
await writeProjectFile(dir, 'a.txt', 'a\n')
|
||||
await writeProjectFile(dir, 'b.txt', 'b\n')
|
||||
await expect(renameProjectEntry(dir, 'a.txt', 'b.txt')).rejects.toThrow()
|
||||
expect(await readProjectFile(dir, 'b.txt')).toBe('b\n')
|
||||
})
|
||||
|
||||
it('refuses a path-shaped or empty name', async () => {
|
||||
dir = await mkdtemp(join(tmpdir(), 'helder-fs-'))
|
||||
await createProjectDir(dir, 'sub')
|
||||
await writeProjectFile(dir, 'a.txt', 'a\n')
|
||||
await expect(renameProjectEntry(dir, 'a.txt', 'sub/a.txt')).rejects.toThrow()
|
||||
await expect(renameProjectEntry(dir, 'a.txt', '../a.txt')).rejects.toThrow()
|
||||
await expect(renameProjectEntry(dir, 'a.txt', ' ')).rejects.toThrow()
|
||||
expect(await readProjectFile(dir, 'a.txt')).toBe('a\n')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user