file.test.ts
1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { writeFile, createFiles } from '../../src/utils/file';
describe('file utilities', () => {
let tempDir: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'coderio-file-'));
});
afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});
it('should create folder and write file content', () => {
const folderPath = path.join(tempDir, 'nested', 'dir');
writeFile(folderPath, 'hello.txt', 'hello');
const fullPath = path.join(folderPath, 'hello.txt');
expect(fs.existsSync(fullPath)).toBe(true);
expect(fs.readFileSync(fullPath, 'utf-8')).toBe('hello');
});
it('should no-op when missing required arguments', () => {
writeFile('', 'a.txt', 'content');
writeFile(tempDir, '', 'content');
writeFile(tempDir, 'a.txt', '');
expect(fs.readdirSync(tempDir)).toHaveLength(0);
});
it('createFiles should write multiple files to directory derived from filePath', () => {
const outputFilePath = path.join(tempDir, 'App.tsx');
createFiles({
filePath: outputFilePath,
files: [
{ filename: 'A.ts', content: 'export const A = 1;' },
{ filename: 'B.ts', content: 'export const B = 2;' },
],
});
expect(fs.readFileSync(path.join(tempDir, 'A.ts'), 'utf-8')).toBe('export const A = 1;');
expect(fs.readFileSync(path.join(tempDir, 'B.ts'), 'utf-8')).toBe('export const B = 2;');
});
});