Skip to content

Commit

Permalink
chore: Fix test for clean command (parseAsync)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 26, 2024
1 parent 30e4dc1 commit 04c74a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
4 changes: 1 addition & 3 deletions src/commands/clean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const cleanCommand = () => {
'by DEMS. Resets your local development environment.',
)
.addCommand(cleanDepsCommand())
.addOption(sharedOptions.verbose().default(true))
.addOption(sharedOptions.force().default(false))
.addOption(sharedOptions.reposRoot().default(config.paths.repos_root))
.option('-e, --env-file <path>', '.env file', config.paths.repos_root)
Expand All @@ -30,11 +29,10 @@ export const cleanCommand = () => {
await deletePath({
path: options.reposRoot,
force: options.force,
verbose: options.verbose,
});
await deletePath({
path: options.envFile,
force: options.verbose,
force: options.force,
});

log.success('Clean completed for current project.');
Expand Down
19 changes: 9 additions & 10 deletions test/commands/clean.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { describe, expect, test } from 'bun:test';
import { $ } from 'bun';
import { projectConfig } from '../../src/config/project';
import { describe, expect, test, beforeEach } from 'bun:test';
import {
createFile,
createPath,
isDirectory,
isFile,
} from '../../src/utils/file-system';
import { omitConsoleLogs } from '../helpers';
import { cleanCommand } from '../../src/commands/clean';

const REPOS_ROOT = './test/repos';
const ENV_FILE = './test/.env';
const REPOS_ROOT = './test/repos';

const prepare = () => {
beforeEach(() => {
omitConsoleLogs();
createPath({ path: REPOS_ROOT });
createFile({ file: ENV_FILE, content: 'KEY=VALUE' });
};
});

describe("Command: 'clean'", () => {
const config = projectConfig();
prepare();

test('Cleans directories using --force', async () => {
expect(isDirectory(REPOS_ROOT)).toBeTrue();
expect(isFile(ENV_FILE)).toBeTrue();
await $`./cli.ts clean -o ${REPOS_ROOT} -e ${ENV_FILE} --force --verbose false`;
const args = [...process.argv, '-o', REPOS_ROOT, '-e', ENV_FILE, '--force'];
await cleanCommand().parseAsync(args);
expect(isDirectory(REPOS_ROOT)).toBeFalse();
expect(isFile(ENV_FILE)).toBeFalse();
});
Expand Down
10 changes: 10 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { jest } from 'bun:test';
import log from '../src/utils/log';

export const omitConsoleLogs = () => {
console.log = jest.fn();
log.info = jest.fn();
log.success = jest.fn();
log.warning = jest.fn();
log.error = jest.fn();
};
9 changes: 6 additions & 3 deletions test/utils/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ import {
replaceKeyValue,
replaceKeysInFile,
} from '../../src/utils/object';
import { omitConsoleLogs } from '../helpers';

omitConsoleLogs();

describe('Utils: object', () => {
const testFile = '.env.test';
const testFileContent = 'KEY1=VALUE1\nKEY2=VALUE2';

test('Replaces a key value in a file', () => {
createFile({ file: testFile, content: testFileContent, verbose: false });
createFile({ file: testFile, content: testFileContent });
expect(isFile(testFile)).toBeTrue();

replaceKeyValue(testFile, 'KEY1', 'VALUE10', false);
replaceKeyValue(testFile, 'KEY1', 'VALUE10');
const replacedContent = fs.readFileSync(testFile, 'utf8');
expect(replacedContent).toContain('VALUE10');
});

test("Replaces various keys' values in a file", () => {
createFile({ file: testFile, content: testFileContent, verbose: false });
createFile({ file: testFile, content: testFileContent });
expect(isFile(testFile)).toBeTrue();

replaceKeysInFile(
Expand Down

0 comments on commit 04c74a4

Please sign in to comment.