Files
superwork/vscode/src/git/fileAt.test.ts
T

40 lines
1.4 KiB
TypeScript

import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import * as os from 'node:os'
import * as path from 'node:path'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { fileAtRef } from './fileAt'
import { runGit } from './worktree'
describe('fileAtRef', () => {
let repo: string
let head: string
beforeAll(async () => {
repo = mkdtempSync(path.join(os.tmpdir(), 'file-at-'))
await runGit(repo, ['init', '-q'])
mkdirSync(path.join(repo, 'src'))
writeFileSync(path.join(repo, 'src', 'a.py'), 'x = 1\n')
await runGit(repo, ['add', '.'])
await runGit(repo, ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-q', '-m', 'init'])
head = (await runGit(repo, ['rev-parse', 'HEAD'])).trim()
writeFileSync(path.join(repo, 'src', 'a.py'), 'x = 2\n')
writeFileSync(path.join(repo, 'src', 'b.py'), 'y = 1\n')
})
afterAll(() => {
rmSync(repo, { recursive: true, force: true })
})
it('取提交里的内容,不是工作区里改过的', async () => {
expect(await fileAtRef(repo, head, 'src/a.py')).toBe('x = 1\n')
})
it('提交里没有这个文件是 null', async () => {
expect(await fileAtRef(repo, head, 'src/b.py')).toBeNull()
})
it('提交不存在时抛错', async () => {
await expect(fileAtRef(repo, '0123456789abcdef0123456789abcdef01234567', 'src/a.py')).rejects.toThrow()
})
})