From 26f1b141c030bcfc3bb478db66ad079383f25d2f Mon Sep 17 00:00:00 2001 From: dev-ig Date: Fri, 1 Dec 2023 11:07:59 +0100 Subject: [PATCH] Build tar.gz tests --- src/build-webon/build-webon.ts | 11 +++------- src/index.ts | 2 +- src/util/util.ts | 6 +++-- .../packaging-completed.test.ts | 22 +++++++++++++++++++ test/unitTest/build-tar-gz/build-tar.test.ts | 11 +++++++--- 5 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 test/e2e/build-webon-test/packaging-completed.test.ts diff --git a/src/build-webon/build-webon.ts b/src/build-webon/build-webon.ts index b5b99dd..75dd437 100644 --- a/src/build-webon/build-webon.ts +++ b/src/build-webon/build-webon.ts @@ -1,21 +1,18 @@ -import { logFatal, checkDir } from "../util/util"; +import { 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 { renameSync(assetDir, path.join(path.dirname(assetDir), "out")); } catch (error) { - console.error(`Error renaming directory: ${error}`); throw error; } } export function createOutDir(outDirPath: string): void { if (!existsSync(outDirPath)) { - console.log("Creating 'out' directory..."); mkdirSync(outDirPath); } } @@ -32,13 +29,11 @@ export function checkRequiredFiles(outDirPath: string): string[] { export function deleteExistingTarFile(tarFilePath: string): void { if (existsSync(tarFilePath)) { - console.log(`Deleting existing nomo.tar.gz...`); unlinkSync(tarFilePath); } } -export async function buildWebOn(args: { assetDir: string }): Promise { - const assetDir = args.assetDir; +export async function buildWebOn(assetDir: string): Promise { const isOutDir = assetDir.endsWith("/out"); const outDirPath = isOutDir ? assetDir : path.resolve(assetDir, "..", "out"); @@ -79,7 +74,7 @@ async function createTarFile( outDirPath: string, tarFilePath: string ): Promise { - console.log(`Creating new ${path.basename(tarFilePath)}: ${tarFilePath}`); + // console.log( `Creating new webon ${path.basename(tarFilePath)}: ${tarFilePath}`); await tar.create( { diff --git a/src/index.ts b/src/index.ts index 8bcc603..c078610 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,7 @@ function commanderBuildWebOn() { .description("Build a WebOn archive") .action((assetDir) => { runAsyncCommand(async () => { - await buildWebOn({ assetDir }); + await buildWebOn(assetDir); }); }); } diff --git a/src/util/util.ts b/src/util/util.ts index bd499f6..25d3640 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -3,6 +3,7 @@ import { join, resolve } from "path"; import semver from "semver"; import { NomoCliConfigs } from "../init/interface"; import { exec } from "child_process"; +import * as fs from "fs"; let _isUnitTest: boolean = false; @@ -13,9 +14,10 @@ export function joinDirWithFileName(dir: string, fileName: string): string { function isDirectory(path: string): boolean { try { - const stat = lstatSync(path); - return stat.isDirectory(); + const stat = fs.lstatSync(path).isDirectory(); + return stat; } catch (e) { + console.log(e); return false; } } diff --git a/test/e2e/build-webon-test/packaging-completed.test.ts b/test/e2e/build-webon-test/packaging-completed.test.ts new file mode 100644 index 0000000..029a4c4 --- /dev/null +++ b/test/e2e/build-webon-test/packaging-completed.test.ts @@ -0,0 +1,22 @@ +import * as fs from "fs"; +import { + runUnitTestExpectFailure, + runE2ETest, +} from "../../test-util/test-util"; + +test("successful tar.gz build", async () => { + const output = await runE2ETest("build test_assets/out/"); + expect(output).toContain("Build and packaging completed!"); + const existsFile = fs.existsSync("test_assets/out/nomo.tar.gz"); + expect(existsFile).toBe(true); + fs.unlinkSync("test_assets/out/nomo.tar.gz"); +}); + +test("missing required file", async () => { + const output = await runUnitTestExpectFailure( + "build test_assets/out_incomplete/out" + ); + expect(output).toMatch( + /Error: The 'out' directory is missing the following required files: .* Use nomo-webon-cli init if you do not have a nomo_manifest.json yet.\n/ + ); +}); diff --git a/test/unitTest/build-tar-gz/build-tar.test.ts b/test/unitTest/build-tar-gz/build-tar.test.ts index 1658f73..ccc5585 100644 --- a/test/unitTest/build-tar-gz/build-tar.test.ts +++ b/test/unitTest/build-tar-gz/build-tar.test.ts @@ -1,7 +1,12 @@ -import { runUnitTestExpectFailure } from "../../test-util/test-util"; +import { + runUnitTestExpectFailure, + runE2ETest, +} from "../../test-util/test-util"; import { enableUnitTest } from "../../../src/util/util"; - -enableUnitTest(); +import * as fs from "fs"; +import { buildWebOn } from "../../../src/build-webon/build-webon"; // Replace with the actual path to your module +import tar from "tar"; +import * as path from "path"; test("missing required file", async () => { const output = await runUnitTestExpectFailure(