You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
vitepress/__tests__/unit/node/utils/fs.test.ts

35 lines
1.0 KiB

import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { readFile, readTextFile, readTextFileSync } from 'node/utils/fs'
describe('node/utils/fs', () => {
let root: string
beforeEach(async () => {
root = await mkdtemp(path.join(tmpdir(), 'vitepress-fs-'))
})
afterEach(async () => {
await rm(root, { recursive: true, force: true })
})
test('readFile keeps line endings as is', async () => {
const file = path.join(root, 'crlf.txt')
await writeFile(file, 'a\r\nb\rc\nd')
expect(await readFile(file)).toBe('a\r\nb\rc\nd')
})
test('readTextFile normalizes CRLF and CR to LF', async () => {
const file = path.join(root, 'crlf.txt')
await writeFile(file, 'a\r\nb\rc\nd')
expect(await readTextFile(file)).toBe('a\nb\nc\nd')
})
test('readTextFileSync normalizes CRLF and CR to LF', async () => {
const file = path.join(root, 'crlf.txt')
await writeFile(file, 'a\r\nb\rc\nd')
expect(readTextFileSync(file)).toBe('a\nb\nc\nd')
})
})