From 0397d0dae342786d2057f0ee1a2ec1ab55aa0c96 Mon Sep 17 00:00:00 2001 From: "Angel M. Adames" <9947992+angelmadames@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:40:56 -0400 Subject: [PATCH] chore: Enable unit tests on CI (#5) * chore: Enable unit tests on CI * chore: Fix Biome lint warnings --- .github/workflows/tests.yml | 4 +- biome.json | 2 +- src/config/env.ts | 4 +- src/utils/compose.ts | 4 +- test/commands/clean.test.ts | 45 ------------------ test/commands/clone.test.ts | 22 --------- test/commands/compose.test.ts | 47 ------------------ test/commands/config.current-project.test.ts | 50 -------------------- test/lifecycle.ts | 29 ------------ 9 files changed, 7 insertions(+), 200 deletions(-) delete mode 100644 test/commands/clean.test.ts delete mode 100644 test/commands/clone.test.ts delete mode 100644 test/commands/compose.test.ts delete mode 100644 test/commands/config.current-project.test.ts delete mode 100644 test/lifecycle.ts diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0a0e703..2c76831 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,5 +36,5 @@ jobs: bun run lint:ci bun run check:ci - # - name: 🧪 Run tests - # run: bun run test + - name: 🧪 Run tests + run: bun run test diff --git a/biome.json b/biome.json index 17ac84b..c3bbf79 100644 --- a/biome.json +++ b/biome.json @@ -7,7 +7,7 @@ "enabled": true, "rules": { "recommended": true, - "nursery": { + "correctness": { "noUnusedImports": "warn" }, "suspicious": { diff --git a/src/config/env.ts b/src/config/env.ts index 6a15795..0ab5df1 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -1,7 +1,7 @@ -import fs from 'fs'; +import * as fs from 'node:fs'; import log from '../utils/log'; import { flattenObject } from '../utils/object'; -import { type DEMSProjectConfig } from './dems'; +import type { DEMSProjectConfig } from './dems'; export const dotEnv = { generate(envFilePath: string, config: DEMSProjectConfig): void { diff --git a/src/utils/compose.ts b/src/utils/compose.ts index d56cc92..1c7828c 100644 --- a/src/utils/compose.ts +++ b/src/utils/compose.ts @@ -1,5 +1,5 @@ -import fs from 'node:fs'; -import path from 'path'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; import { projectConfig } from '../config/project'; import { cmd as $ } from './cmd'; import { isFile } from './file-system'; diff --git a/test/commands/clean.test.ts b/test/commands/clean.test.ts deleted file mode 100644 index e5bd0f1..0000000 --- a/test/commands/clean.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { afterAll, beforeEach, describe, expect, test } from 'bun:test'; -import { cleanCommand } from '../../src/commands/clean'; -import { cleanDepsCommand } from '../../src/commands/clean/deps'; -import { projectConfig } from '../../src/config/project'; -import { - createFile, - createPath, - isDirectory, - isFile, -} from '../../src/utils/file-system'; -import { testSetup, testTeardown } from '../lifecycle'; - -const ENV_FILE = './test/.env'; -const REPOS_ROOT = './test/repos'; - -beforeEach(() => { - testSetup(); - createPath({ path: REPOS_ROOT }); - createFile({ file: ENV_FILE, content: 'KEY=VALUE' }); -}); - -afterAll(() => { - testTeardown(); -}); - -describe("Command: 'clean'", () => { - test('Clean directories using --force', async () => { - expect(isDirectory(REPOS_ROOT)).toBeTrue(); - expect(isFile(ENV_FILE)).toBeTrue(); - 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(); - }); - - test('Clean application dependencies', async () => { - const result = await cleanDepsCommand().parseAsync([ - ...process.argv, - '--force', - ]); - for (const repo of Object.values(projectConfig().paths.repos)) { - expect(isDirectory(`${repo}/node_modules`)).toBeFalse(); - } - }); -}); diff --git a/test/commands/clone.test.ts b/test/commands/clone.test.ts deleted file mode 100644 index 06c579e..0000000 --- a/test/commands/clone.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { afterAll, beforeEach, describe, expect, test } from 'bun:test'; -import { cloneCommand } from '../../src/commands/clone'; -import { projectConfig } from '../../src/config/project'; -import { localRepoExists } from '../../src/utils/git'; -import { testSetup, testTeardown } from '../lifecycle'; - -beforeEach(() => { - testSetup(); -}); - -afterAll(() => { - testTeardown(); -}); - -describe("Command: 'clone'", () => { - test.todo('Clones git repositories correctly', () => { - cloneCommand().parse(); - for (const repo of Object.values(projectConfig().paths.repos)) { - expect(localRepoExists(repo)).toBeTrue(); - } - }); -}); diff --git a/test/commands/compose.test.ts b/test/commands/compose.test.ts deleted file mode 100644 index 0c83577..0000000 --- a/test/commands/compose.test.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { afterAll, beforeEach, describe, expect, test } from 'bun:test'; -import { projectConfig } from '../../src/config/project'; -import { composeExecParams, composeFiles } from '../../src/utils/compose'; -import { testSetup, testTeardown } from '../lifecycle'; - -beforeEach(() => { - testSetup(); -}); - -afterAll(() => { - testTeardown(); -}); - -describe("Command: 'compose'", () => { - const config = projectConfig(); - - test('Returns compose exec params', () => { - const composeSettingsString = composeExecParams(); - expect(composeSettingsString).toContain( - `--env-file ${config.paths.env_file}`, - ); - expect(composeSettingsString).toContain( - `--project-name ${config.compose.project_name}`, - ); - }); - - test.skip('Returns compose files params', () => { - const files = composeFiles({}); - expect(files).toBeArray(); - expect(files.join(' ').split(' ')).toContain('--file'); - }); - - test.skip('Returns error when no arguments', () => { - const command = Bun.spawnSync(['./cli.ts', 'compose']); - expect(command.stdout.toString()).toContain( - 'A Compose command needs to be specified.', - ); - expect(command.exitCode).toEqual(1); - }); - - test.skip('Returns both files and exec params', () => { - const command = Bun.spawnSync(['./cli.ts', 'compose', 'show-args']); - expect(command.stdout.toString()).toContain('Compose command params:'); - expect(command.stdout.toString()).toContain('Compose files params:'); - expect(command.exitCode).toEqual(0); - }); -}); diff --git a/test/commands/config.current-project.test.ts b/test/commands/config.current-project.test.ts deleted file mode 100644 index 745c082..0000000 --- a/test/commands/config.current-project.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; -import { spawnSync } from 'bun'; -import { currentProjectCommand } from '../../src/commands/config/current-project'; -import cliConfig from '../../src/config/cli'; -import { createFile, deletePath } from '../../src/utils/file-system'; -import { testSetup } from '../lifecycle'; - -const PROJECT = 'testProject'; -const CURRENT_PROJECT_FILE = './current-project-test'; - -beforeEach(() => { - testSetup(); - createFile({ file: CURRENT_PROJECT_FILE, content: 'test' }); -}); - -afterEach(() => { - deletePath({ path: CURRENT_PROJECT_FILE, force: true }); -}); - -describe("Command: 'config current-project'", () => { - test('is set by --set flag', () => { - currentProjectCommand().parse([ - ...process.argv, - `--set=${PROJECT}`, - `--current-project-file=${CURRENT_PROJECT_FILE}`, - ]); - currentProjectCommand().parse(); - const currentProject = cliConfig.selectCurrentProject(CURRENT_PROJECT_FILE); - expect(currentProject).toEqual(PROJECT); - }); - - test('is set by environment variable', () => { - const result = spawnSync(['./cli.ts', 'config', 'current-project'], { - env: { ...process.env, DEMS_CURRENT_PROJECT: 'dev' }, - }); - expect(result.stdout.toString()).toContain('dev'); - }); - - test('empty --set flag is missing argument', () => { - const command = currentProjectCommand(); - expect(command.getOptionValue('set')).toBeUndefined(); - const result = spawnSync([ - './cli.ts', - 'config', - 'current-project', - '--set', - ]); - expect(result.stderr.toString()).toContain('argument missing'); - }); -}); diff --git a/test/lifecycle.ts b/test/lifecycle.ts deleted file mode 100644 index e723b69..0000000 --- a/test/lifecycle.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { setupCommand } from '../src/commands/setup'; -import cliConfig from '../src/config/cli'; -import { projectConfig } from '../src/config/project'; -import { deletePath } from '../src/utils/file-system'; - -const PROJECT = 'test'; - -export const testSetup = () => { - // omitConsoleLogs(); - setupCommand().parse([ - ...process.argv, - '--interactive=false', - `--project-name=${PROJECT}`, - `--repos-root=${import.meta.dirname}/repos`, - '--repos=demo-api,demo-webapp', - '--git-org=git@github.com:gbh-tech', - '--git-ref=main', - '--dot-env=dems.Dockerfile', - '--dockerfile=test.Dockerfile', - ]); -}; - -export const testTeardown = () => { - if (cliConfig.currentProject === PROJECT) { - deletePath({ path: `${cliConfig.root}/${PROJECT}`, force: true }); - deletePath({ path: projectConfig().paths.repos_root, force: true }); - deletePath({ path: projectConfig().paths.env_file, force: true }); - } -};