Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Apr 20, 2024
1 parent 886cb8f commit 1714b89
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions packages/tscx/src/cmd/copyfiles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { copyfiles } from "./copyfiles.js";

const TMP1 = path.resolve(os.tmpdir(), "tscx_tmp_for_copy1");
const scrDirPath1 = path.resolve(TMP1, "src");
const distDirPath1 = path.resolve(TMP1, "dist");

const TMP2 = path.resolve(os.tmpdir(), "tscx_tmp_for_copy2");
const scrDirPath2 = path.resolve(TMP2, "src");
const distDirPath2 = path.resolve(TMP2, "dist");

describe("copyfiles", () => {
beforeEach(async () => {
await fs.mkdir(TMP1);
await fs.mkdir(scrDirPath1);
await fs.mkdir(distDirPath1);
await fs.mkdir(TMP2);
await fs.mkdir(scrDirPath2);
await fs.mkdir(distDirPath2);
});
afterEach(async () => {
await fs.rm(TMP1, { force: true, recursive: true });
await fs.rm(TMP2, { force: true, recursive: true });
});

it("should copy files when src and dist are peers", async () => {
// 1. prepare
await fs.mkdir(path.resolve(distDirPath1, "a", "b"), { recursive: true });

await fs.mkdir(path.resolve(scrDirPath1, "a", "b"), { recursive: true });
await fs.writeFile(path.resolve(scrDirPath1, "a", "c"), "foo");
await fs.mkdir(path.resolve(scrDirPath1, "a", "b", "node_modules"), {
recursive: true,
});
await fs.writeFile(
path.resolve(scrDirPath1, "a", "b", "node_modules", "d"),
"bar",
);
await fs.writeFile(path.resolve(scrDirPath1, "a", "b", "e"), "baz");

// 2. call
await copyfiles(scrDirPath1, distDirPath1);

// 3. expect
expect(await fs.readdir(scrDirPath1)).toStrictEqual(["a"]);
expect(await fs.readdir(path.resolve(scrDirPath1, "a"))).toStrictEqual([
"b",
"c",
]);
expect(await fs.readFile(path.resolve(scrDirPath1, "a", "c"), "utf8")).toBe(
"foo",
);
expect(await fs.readdir(path.resolve(scrDirPath1, "a", "b"))).toStrictEqual(
["e", "node_modules"],
);
expect(
await fs.readFile(
path.resolve(scrDirPath1, "a", "b", "node_modules", "d"),
"utf8",
),
).toBe("bar");
expect(
await fs.readFile(path.resolve(scrDirPath1, "a", "b", "e"), "utf8"),
).toBe("baz");

expect(await fs.readdir(distDirPath1)).toStrictEqual(["a"]);
expect(await fs.readdir(path.resolve(distDirPath1, "a"))).toStrictEqual([
"b",
"c",
]);
expect(
await fs.readFile(path.resolve(distDirPath1, "a", "c"), "utf8"),
).toBe("foo");
expect(
await fs.readdir(path.resolve(distDirPath1, "a", "b")),
).toStrictEqual(["e"]);
expect(
await fs.readFile(path.resolve(distDirPath1, "a", "b", "e"), "utf8"),
).toBe("baz");
});

it("should copy files when src and dist are not peers", async () => {
// 1. prepare
await fs.writeFile(path.resolve(scrDirPath2, "a"), "foo");
await fs.writeFile(path.resolve(TMP2, "b"), "bar");
await fs.mkdir(path.resolve(distDirPath2, "src"));

// 2. call
await copyfiles(TMP2, distDirPath2);

// 3. expect
expect(await fs.readdir(distDirPath2)).toStrictEqual(["b", "src"]);
expect(await fs.readFile(path.resolve(distDirPath2, "b"), "utf8")).toBe(
"bar",
);
expect(await fs.readdir(path.resolve(distDirPath2, "src"))).toStrictEqual([
"a",
]);
expect(
await fs.readFile(path.resolve(distDirPath2, "src", "a"), "utf8"),
).toBe("foo");
});
});

0 comments on commit 1714b89

Please sign in to comment.