Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dev2-nomo committed Nov 28, 2023
1 parent 9c1cb45 commit 9f3e75a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 68 deletions.
13 changes: 2 additions & 11 deletions src/build-webon/build-webon.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)}\'`;
}
1 change: 1 addition & 0 deletions src/deploy-webon/deploy-webon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function deployWebOn(args: {
}) {
const { deployTarget, archive } = args;
checkNotDir(archive);

checkIfTarGz(archive);

const nomoCliConfig = readCliConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 2 additions & 4 deletions test/e2e/build-webon-test/invalid-build-options.test.ts
Original file line number Diff line number Diff line change
@@ -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`
);
});
8 changes: 4 additions & 4 deletions test/e2e/deploy-webon-test/invalid-deploy-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
2 changes: 1 addition & 1 deletion test/test-util/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 7 additions & 47 deletions test/unitTest/build-tar-gz/build-tar.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 9f3e75a

Please sign in to comment.