111 lines
3.6 KiB
TypeScript
111 lines
3.6 KiB
TypeScript
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
||
import { fileExists, readJsonFile, readTextFile, ensureDir, writeFileWithDirs } from '../../../src/utils/file.js';
|
||
import { setupTest } from '../../helpers/setup.js';
|
||
import { join } from 'path';
|
||
import { writeFile, mkdir } from 'fs/promises';
|
||
|
||
describe('file utils', () => {
|
||
let tempDir: string;
|
||
let cleanup: () => Promise<void>;
|
||
|
||
beforeEach(async () => {
|
||
const setup = await setupTest();
|
||
tempDir = setup.tempDir;
|
||
cleanup = setup.cleanup;
|
||
});
|
||
|
||
afterEach(async () => {
|
||
await cleanup();
|
||
});
|
||
|
||
describe('fileExists', () => {
|
||
test('должен вернуть true для существующего файла', async () => {
|
||
const filePath = join(tempDir, 'test.txt');
|
||
await writeFile(filePath, 'test content');
|
||
|
||
const exists = await fileExists(filePath);
|
||
expect(exists).toBe(true);
|
||
});
|
||
|
||
test('должен вернуть false для несуществующего файла', async () => {
|
||
const filePath = join(tempDir, 'nonexistent.txt');
|
||
|
||
const exists = await fileExists(filePath);
|
||
expect(exists).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe('readTextFile', () => {
|
||
test('должен прочитать содержимое текстового файла', async () => {
|
||
const filePath = join(tempDir, 'test.txt');
|
||
const content = 'Hello, World!';
|
||
await writeFile(filePath, content);
|
||
|
||
const result = await readTextFile(filePath);
|
||
expect(result).toBe(content);
|
||
});
|
||
});
|
||
|
||
describe('readJsonFile', () => {
|
||
test('должен прочитать и распарсить JSON файл', async () => {
|
||
const filePath = join(tempDir, 'test.json');
|
||
const data = { name: 'Test', value: 42 };
|
||
await writeFile(filePath, JSON.stringify(data));
|
||
|
||
const result = await readJsonFile(filePath);
|
||
expect(result).toEqual(data);
|
||
});
|
||
|
||
test('должен выбросить ошибку для невалидного JSON', async () => {
|
||
const filePath = join(tempDir, 'invalid.json');
|
||
await writeFile(filePath, 'not a json');
|
||
|
||
await expect(readJsonFile(filePath)).rejects.toThrow();
|
||
});
|
||
});
|
||
|
||
describe('ensureDir', () => {
|
||
test('должен создать директорию', async () => {
|
||
const dirPath = join(tempDir, 'test-dir');
|
||
|
||
await ensureDir(dirPath);
|
||
|
||
const exists = await fileExists(dirPath);
|
||
expect(exists).toBe(true);
|
||
});
|
||
|
||
test('должен создать вложенные директории', async () => {
|
||
const dirPath = join(tempDir, 'a', 'b', 'c');
|
||
|
||
await ensureDir(dirPath);
|
||
|
||
const exists = await fileExists(dirPath);
|
||
expect(exists).toBe(true);
|
||
});
|
||
|
||
test('не должен падать если директория уже существует', async () => {
|
||
const dirPath = join(tempDir, 'existing');
|
||
await mkdir(dirPath);
|
||
|
||
// Не должно выбрасывать ошибку
|
||
await ensureDir(dirPath);
|
||
const exists = await fileExists(dirPath);
|
||
expect(exists).toBe(true);
|
||
});
|
||
});
|
||
|
||
describe('writeFileWithDirs', () => {
|
||
test('должен записать файл и создать директории', async () => {
|
||
const filePath = join(tempDir, 'nested', 'dir', 'file.txt');
|
||
const content = 'test content';
|
||
|
||
await writeFileWithDirs(filePath, content);
|
||
|
||
const exists = await fileExists(filePath);
|
||
expect(exists).toBe(true);
|
||
|
||
const readContent = await readTextFile(filePath);
|
||
expect(readContent).toBe(content);
|
||
});
|
||
});
|
||
}); |