Skip to content

Commit

Permalink
chore: Fix missing tests for file-system
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Mar 19, 2024
1 parent 057d677 commit a7752fe
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 16 deletions.
106 changes: 95 additions & 11 deletions test/utils/file-system.test.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,71 @@
import { afterEach, beforeEach, describe, expect, jest, mock, test, spyOn } from 'bun:test';
import {
afterEach,
beforeEach,
describe,
expect,
jest,
mock,
spyOn,
test,
} from 'bun:test';
import fs from 'node:fs';
import { confirm } from '@inquirer/prompts';
import {
copyFile,
createFile,
createPath,
deletePath,
isDirectory,
isFile,
} from '../../src/utils/file-system';
import log from '../../src/utils/log';

mock.module('node:fs', () => ({
default: {
copyFileSync: mock(),
existsSync: mock(),
lstatSync: mock(),
writeFileSync: mock(),
copyFileSync: mock(),
mkdirSync: mock(),
rmdirSync: mock(),
rmSync: mock(),
writeFileSync: mock(),
},
}));

mock.module('@inquirer/prompts', () => ({
confirm: mock(),
}));

beforeEach(() => {
jest.clearAllMocks();
spyOn(console, 'log').mockImplementation(() => {});
})
});

afterEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

describe('Utils: file-system', () => {
describe('isFile', () => {
test('returns true if file exists', () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({ isFile: () => true });
(fs.existsSync as jest.Mock).mockReturnValueOnce(true);
(fs.lstatSync as jest.Mock).mockReturnValueOnce({ isFile: () => true });

expect(isFile('./cli.ts')).toBeTrue();
expect(fs.existsSync).toHaveBeenCalledWith('./cli.ts');
expect(fs.lstatSync).toHaveBeenCalledWith('./cli.ts');
});

test('returns false if file does not exist', () => {
(fs.existsSync as jest.Mock).mockReturnValue(false);
(fs.existsSync as jest.Mock).mockReturnValueOnce(false);

expect(isFile('./cli.not.exists.ts')).toBeFalse();
expect(fs.existsSync).toHaveBeenCalledWith('./cli.not.exists.ts');
expect(fs.lstatSync).not.toHaveBeenCalled();
});

test('returns false if path exists but is not a file', () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({ isFile: () => false });
(fs.existsSync as jest.Mock).mockReturnValueOnce(true);
(fs.lstatSync as jest.Mock).mockReturnValueOnce({ isFile: () => false });

expect(isFile('test-path')).toBeFalse();
expect(fs.existsSync).toHaveBeenCalledWith('test-path');
Expand Down Expand Up @@ -180,4 +196,72 @@ describe('Utils: file-system', () => {
expect(fs.mkdirSync).not.toHaveBeenCalled();
});
});

describe('deletePath', () => {
test('deletes a directory if it exists and force is true', async () => {
(confirm as jest.Mock).mockResolvedValue(true);
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({
isFile: () => false,
isDirectory: () => true,
});

await deletePath({ path: 'test-dir', force: true });

expect(fs.existsSync).toHaveBeenCalledWith('test-dir');
expect(confirm).not.toHaveBeenCalled();
expect(fs.rmdirSync).toHaveBeenCalledWith('test-dir', {
recursive: true,
});
expect(fs.rmSync).not.toHaveBeenCalled();
});

test('deletes a file if it exists and force is true', async () => {
(confirm as jest.Mock).mockResolvedValue(true);
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({
isFile: () => true,
isDirectory: () => false,
});

await deletePath({ path: 'test-file.txt', force: true });

expect(fs.existsSync).toHaveBeenCalledWith('test-file.txt');
expect(confirm).not.toHaveBeenCalled();
expect(fs.rmdirSync).not.toHaveBeenCalled();
expect(fs.rmSync).toHaveBeenCalledWith('test-file.txt');
});

test('does not delete a directory if force is false and confirmation is not given', async () => {
(confirm as jest.Mock).mockResolvedValue(false);
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({
isFile: () => false,
isDirectory: () => true,
});

await deletePath({ path: 'test-dir' });

expect(fs.existsSync).toHaveBeenCalledWith('test-dir');
expect(confirm).toHaveBeenCalledWith({ message: 'Delete directory test-dir recursively?' });
expect(fs.rmdirSync).not.toHaveBeenCalled();
expect(fs.rmSync).not.toHaveBeenCalled();
});

test('should not delete a file if force is false and confirmation is not given', async () => {
(confirm as jest.Mock).mockResolvedValue(false);
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.lstatSync as jest.Mock).mockReturnValue({
isFile: () => true,
isDirectory: () => false,
});

await deletePath({ path: 'test-file.txt' });

expect(fs.existsSync).toHaveBeenCalledWith('test-file.txt');
expect(confirm).toHaveBeenCalledWith({ message: 'Delete file test-file.txt?' });
expect(fs.rmdirSync).not.toHaveBeenCalled();
expect(fs.rmSync).not.toHaveBeenCalled();
});
});
});
16 changes: 13 additions & 3 deletions test/utils/log.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { beforeEach, afterEach, describe, expect, test, jest, spyOn } from 'bun:test';
import log from '../../src/utils/log';
import {
afterEach,
beforeEach,
describe,
expect,
jest,
spyOn,
test,
} from 'bun:test';
import chalk from 'chalk';
import log from '../../src/utils/log';

describe('log', () => {
beforeEach(() => {
Expand Down Expand Up @@ -28,7 +36,9 @@ describe('log', () => {

test('should log dimmed warning message in dim yellow color', () => {
log.dimmedWarning('dimmed warning message');
expect(console.log).toHaveBeenCalledWith(chalk.dim.yellow('dimmed warning message'));
expect(console.log).toHaveBeenCalledWith(
chalk.dim.yellow('dimmed warning message'),
);
});

test('should log error message in red color', () => {
Expand Down
8 changes: 6 additions & 2 deletions test/utils/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ describe('Utils: object', () => {

replaceKeysInFile(filePath, replaceMap);

expect(log.error).toHaveBeenCalledWith('File test-file-path is not a valid file.');
expect(log.error).toHaveBeenCalledWith(
'File test-file-path is not a valid file.',
);
expect(log.success).not.toHaveBeenCalled();
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
Expand All @@ -142,7 +144,9 @@ describe('Utils: object', () => {
});

expect(() => replaceKeysInFile(filePath, replaceMap)).toThrow(Error);
expect(log.error).toHaveBeenCalledWith('Error updating file test-file-path. See below for more info:');
expect(log.error).toHaveBeenCalledWith(
'Error updating file test-file-path. See below for more info:',
);
expect(log.success).not.toHaveBeenCalled();
expect(fs.writeFileSync).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit a7752fe

Please sign in to comment.