From 9f3e75a54a5ae52aa18f5f084e2d5aa199383772 Mon Sep 17 00:00:00 2001 From: dev-ig Date: Tue, 28 Nov 2023 16:04:59 +0100 Subject: [PATCH] fix tests --- src/build-webon/build-webon.ts | 13 +---- src/deploy-webon/deploy-webon.ts | 1 + src/util/util.ts | 2 +- .../invalid-build-options.test.ts | 6 +-- .../invalid-deploy-options.test.ts | 8 +-- test/test-util/test-util.ts | 2 +- test/unitTest/build-tar-gz/build-tar.test.ts | 54 +++---------------- 7 files changed, 18 insertions(+), 68 deletions(-) diff --git a/src/build-webon/build-webon.ts b/src/build-webon/build-webon.ts index 26d9ef5..a3a7120 100644 --- a/src/build-webon/build-webon.ts +++ b/src/build-webon/build-webon.ts @@ -1,7 +1,8 @@ -import { logFatal } from "../util/util"; +import { logFatal, checkDir } from "../util/util"; import { existsSync, mkdirSync, unlinkSync, renameSync } from "fs"; import * as path from "path"; import tar from "tar"; +import * as fs from "fs"; export function renameAssetDir(assetDir: string): void { try { @@ -89,13 +90,3 @@ async function createTarFile( [path.basename(outDirPath)] ); } - -function checkDir(dir: string): void { - if (!existsSync(dir)) { - logFatal(`${getDebugPath(dir)} does not exist.`); - } -} - -function getDebugPath(paths: string): string { - return `\'${path.resolve(paths)}\'`; -} diff --git a/src/deploy-webon/deploy-webon.ts b/src/deploy-webon/deploy-webon.ts index 6bd4980..b9ad831 100644 --- a/src/deploy-webon/deploy-webon.ts +++ b/src/deploy-webon/deploy-webon.ts @@ -12,6 +12,7 @@ export async function deployWebOn(args: { }) { const { deployTarget, archive } = args; checkNotDir(archive); + checkIfTarGz(archive); const nomoCliConfig = readCliConfig(); diff --git a/src/util/util.ts b/src/util/util.ts index 8924a78..bd499f6 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -66,7 +66,7 @@ export function logFatal(msg: string): never { if (isUnitTest()) { throw new Error(`ERROR: ${msg}`); } else { - console.error("\x1b[31m", `ERROR: ${msg}`); + console.error(`ERROR: ${msg}`); process.exit(1); } diff --git a/test/e2e/build-webon-test/invalid-build-options.test.ts b/test/e2e/build-webon-test/invalid-build-options.test.ts index d743fcd..ab3644e 100644 --- a/test/e2e/build-webon-test/invalid-build-options.test.ts +++ b/test/e2e/build-webon-test/invalid-build-options.test.ts @@ -1,18 +1,16 @@ import { runE2ETestExpectFailure } from "../../test-util/test-util"; -//import { buildWebOn } from "../../src/build-webon/build-webon"; -//import { runCliTestExpectFailure } from "../test-util/test-util"; import { getDebugPath } from "../../../src/util/util"; test("assetDir not existing", async () => { const output = await runE2ETestExpectFailure("build some-non-existing-dir"); expect(output).toBe( - `error: ${getDebugPath("some-non-existing-dir")} does not exist.\n` + `ERROR: ${getDebugPath("some-non-existing-dir")} does not exist.\n` ); }); test("assetDir not a dir", async () => { const output = await runE2ETestExpectFailure("build README.md"); expect(output).toBe( - `error: ${getDebugPath("README.md")} is not a directory.\n` + `ERROR: ${getDebugPath("README.md")} is not a directory.\n` ); }); diff --git a/test/e2e/deploy-webon-test/invalid-deploy-options.test.ts b/test/e2e/deploy-webon-test/invalid-deploy-options.test.ts index abd9ec3..bdaadeb 100644 --- a/test/e2e/deploy-webon-test/invalid-deploy-options.test.ts +++ b/test/e2e/deploy-webon-test/invalid-deploy-options.test.ts @@ -3,14 +3,14 @@ import { getDebugPath } from "../../../src/util/util"; test("archive not existing", async () => { const output = await runE2ETestExpectFailure( - "deploy some-non-existing-archive" + "deploy some-non-existing-archive production" ); expect(output).toBe( - `error: ${getDebugPath("some-non-existing-archive")} does not exist.\n` + `ERROR: ${getDebugPath("some-non-existing-archive")} does not exist.\n` ); }); test("archive not a file", async () => { - const output = await runE2ETestExpectFailure("deploy src"); - expect(output).toBe(`error: ${getDebugPath("src")} is a directory.\n`); + const output = await runE2ETestExpectFailure("deploy src production"); + expect(output).toBe(`ERROR: ${getDebugPath("src")} is a directory.\n`); }); diff --git a/test/test-util/test-util.ts b/test/test-util/test-util.ts index 9bd3a68..73e904f 100644 --- a/test/test-util/test-util.ts +++ b/test/test-util/test-util.ts @@ -50,7 +50,7 @@ function runCommandExpectFailure( cmd = buildFinalCommand(cmd, pwd); console.log(`Run expect-fail-command \'${cmd}\'`); return new Promise((resolve, reject) => { - exec(cmd, { env: env }, (error, stdout, stderr) => { + exec(cmd, { env: env, shell: "/bin/bash" }, (error, stdout, stderr) => { console.log(stdout); if (error) { console.log(stderr); diff --git a/test/unitTest/build-tar-gz/build-tar.test.ts b/test/unitTest/build-tar-gz/build-tar.test.ts index 85ae314..c2d8735 100644 --- a/test/unitTest/build-tar-gz/build-tar.test.ts +++ b/test/unitTest/build-tar-gz/build-tar.test.ts @@ -1,49 +1,9 @@ -import { buildWebOn } from "../../../src/build-webon/build-webon"; -import { enableUnitTest } from "../../../src/util/util"; -import tar from "tar"; -import { join, resolve } from "path"; -import { existsSync, mkdirSync, readdirSync, renameSync } from "fs"; +import { runE2ETestExpectFailure } from "../../test-util/test-util"; +import { getDebugPath } from "../../../src/util/util"; -enableUnitTest(); - -jest.mock("fs"); -jest.mock("tar"); - -test("Should create a tar.gz WebOn file", async () => { - const mockExistsSync = jest.spyOn(require("fs"), "existsSync"); - const mockMkdirSync = jest.spyOn(require("fs"), "mkdirSync"); - const mockRenameSync = jest.spyOn(require("fs"), "renameSync"); - const mockTarCreate = jest.spyOn(tar, "create"); - - // Set up mock data and expectations - const assetDir = "test/out"; - const outDir = resolve(assetDir); - const outDirPath = resolve(assetDir, "..", "out"); - - mockExistsSync.mockReturnValueOnce(true); // "out" directory doesn't exist - mockRenameSync.mockImplementationOnce((source, destination) => { - expect(source).toBe(assetDir); - expect(destination).toBe(outDirPath); - }); - mockMkdirSync.mockImplementationOnce((path) => { - expect(path).toBe(outDir); - }); - - console.log('Directory exists:', existsSync(assetDir)); - // Run the function - await buildWebOn({ assetDir }); - - expect(mockExistsSync).toHaveBeenCalledWith(assetDir); - // expect(mockRenameSync).toHaveBeenCalled(); - expect(mockMkdirSync).toHaveBeenCalled(); - expect(mockTarCreate).toHaveBeenCalledWith( - { - file: expect.any(String), - gzip: true, - }, - [outDir] +test("assetDir not a dir", async () => { + const output = await runE2ETestExpectFailure("build README.md"); + expect(output).toBe( + `ERROR: ${getDebugPath("README.md")} is not a directory.\n` ); - - // Clear mocks after the test - jest.clearAllMocks(); -}); \ No newline at end of file +});