Skip to content

Commit

Permalink
replace callbacks with async await
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Oct 25, 2024
1 parent ae959ae commit 50eccd2
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 264 deletions.
94 changes: 47 additions & 47 deletions src/plugin/packer/__tests__/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import pkg from "../../../../package.json";
const execa = {} as ExecaMethod<{}>;

describe.skip("bin", () => {
it("should output version with --version", () =>
exec("--version").then((result) => {
expect(result.stdout).toBe(pkg.version);
}));
it("should output version with --version", async () => {
const result = await exec("--version");
expect(result.stdout).toBe(pkg.version);
});

it("should fail without args", () =>
exec().then(
Expand All @@ -23,56 +23,56 @@ describe.skip("bin", () => {
},
));

it("should recieve 1st arg as PLUGIN_DIR", () =>
exec("foo").then((result) => {
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false },
});
}));
it("should recieve 1st arg as PLUGIN_DIR", async () => {
const result = await exec("foo");
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false },
});
});

it("should recieve --ppk", () =>
exec("foo", "--ppk", "bar").then((result) => {
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false, ppk: "bar" },
});
}));
it("should recieve --ppk", async () => {
const result = await exec("foo", "--ppk", "bar");
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false, ppk: "bar" },
});
});

it("should recieve --out", () =>
exec("foo", "--out", "bar").then((result) => {
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false, out: "bar" },
});
}));
it("should recieve --out", async () => {
const result = await exec("foo", "--out", "bar");
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false, out: "bar" },
});
});

it("should recieve --watch", () =>
exec("foo", "--watch").then((result) => {
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: true },
});
}));
it("should recieve --watch", async () => {
const result = await exec("foo", "--watch");
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: true },
});
});

it("should recieve -w as an alias of --watch", () =>
exec("foo", "-w").then((result) => {
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: true },
});
}));
it("should recieve -w as an alias of --watch", async () => {
const result = await exec("foo", "-w");
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: true },
});
});

it("should filter unexpected option", () =>
exec("foo", "--bar").then((result) => {
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false },
});
}));
it("should filter unexpected option", async () => {
const result = await exec("foo", "--bar");
expect(JSON.parse(result.stdout)).toStrictEqual({
pluginDir: "foo",
flags: { watch: false },
});
});
});

const exec = (...args: string[]) => {
const exec = async (...args: string[]) => {
const binPath = path.resolve(__dirname, "../bin/cli.js");
const env = Object.assign({}, process.env, { NODE_ENV: "test" });
return execa(binPath, args, { env });
Expand Down
97 changes: 44 additions & 53 deletions src/plugin/packer/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,26 @@ describe("cli", () => {
const pluginDir = path.join(sampleDir, "plugin-dir");
let packer: MockedPacker;
let resultPluginPath: string;
beforeEach(() => {

beforeEach(async () => {
packer = jest.fn().mockReturnValue({
id: ID,
privateKey: PRIVATE_KEY,
plugin: PLUGIN_BUFFER,
});

// TODO: use os tempdir
return rimraf(`${sampleDir}/*.*(ppk|zip)`, { glob: true })
.then(() => cli(pluginDir, { packerMock_: packer }))
.then((filePath) => {
resultPluginPath = filePath;
});
await rimraf(`${sampleDir}/*.*(ppk|zip)`, { glob: true });
resultPluginPath = await cli(pluginDir, { packerMock_: packer });
});

it("calles `packer` with contents.zip as the 1st argument", (done) => {
it("calles `packer` with contents.zip as the 1st argument", async () => {
expect(packer.mock.calls.length).toBe(1);
expect(packer.mock.calls[0][0]).toBeTruthy();
readZipContentsNames(packer.mock.calls[0][0]).then((files) => {
expect(files.sort()).toStrictEqual(
["image/icon.png", "manifest.json"].sort(),
);
done();
});
const files = await readZipContentsNames(packer.mock.calls[0][0]);
expect(files.sort()).toStrictEqual(
["image/icon.png", "manifest.json"].sort(),
);
});

it("calles `packer` with privateKey as the 2nd argument", () => {
Expand All @@ -141,16 +137,15 @@ describe("cli", () => {
describe("with ppk", () => {
const pluginDir = path.join(sampleDir, "plugin-dir");
let packer: MockedPacker;
beforeEach(() => {
beforeEach(async () => {
packer = jest.fn().mockReturnValue({
id: ID,
privateKey: PRIVATE_KEY,
plugin: PLUGIN_BUFFER,
});

return rimraf(`${sampleDir}/*.*(ppk|zip)`, { glob: true }).then(() =>
cli(pluginDir, { ppk: ppkPath, packerMock_: packer }),
);
await rimraf(`${sampleDir}/*.*(ppk|zip)`, { glob: true });
return cli(pluginDir, { ppk: ppkPath, packerMock_: packer });
});

it("calles `packer` with privateKey as the 2nd argument", () => {
Expand All @@ -165,36 +160,33 @@ describe("cli", () => {
});
});

it("includes files listed in manifest.json only", () => {
it("includes files listed in manifest.json only", async () => {
const pluginDir = path.join(fixturesDir, "plugin-full-manifest");
const packer = jest.fn().mockReturnValue({
id: ID,
privateKey: PRIVATE_KEY,
plugin: PLUGIN_BUFFER,
});

return rimraf(`${sampleDir}/*.*(ppk|zip)`, { glob: true })
.then(() => cli(pluginDir, { packerMock_: packer }))
.then(() => {
return readZipContentsNames(packer.mock.calls[0][0]).then((files) => {
expect(files.sort()).toStrictEqual(
[
"css/config.css",
"css/desktop.css",
"css/mobile.css",
"html/config.html",
"image/icon.png",
"js/config.js",
"js/desktop.js",
"js/mobile.js",
"manifest.json",
].sort(),
);
});
});
await rimraf(`${sampleDir}/*.*(ppk|zip)`, { glob: true });
await cli(pluginDir, { packerMock_: packer });
const files = await readZipContentsNames(packer.mock.calls[0][0]);
expect(files.sort()).toStrictEqual(
[
"css/config.css",
"css/desktop.css",
"css/mobile.css",
"html/config.html",
"image/icon.png",
"js/config.js",
"js/desktop.js",
"js/mobile.js",
"manifest.json",
].sort(),
);
});

it("includes files listed in manifest.json only", () => {
it("includes files listed in manifest.json only", async () => {
const pluginDir = path.join(sampleDir, "plugin-dir");
const outputDir = path.join("test", ".output");
const outputPluginPath = path.join(outputDir, "foo.zip");
Expand All @@ -204,20 +196,19 @@ describe("cli", () => {
plugin: PLUGIN_BUFFER,
});

return rimraf(outputDir)
.then(() =>
cli(pluginDir, { packerMock_: packer, out: outputPluginPath }),
)
.then((resultPluginPath) => {
expect(resultPluginPath).toBe(outputPluginPath);
const pluginBuffer = fs.readFileSync(outputPluginPath);
expect(PLUGIN_BUFFER.equals(pluginBuffer)).toBe(true);
const ppk = fs.readFileSync(path.join(outputDir, `${ID}.ppk`));
expect(PRIVATE_KEY).toBe(ppk.toString());
})
.then(() => {
// TODO: use os tempdir for more safe testing
rimraf(outputDir);
});
await rimraf(outputDir);
const resultPluginPath = await cli(pluginDir, {
packerMock_: packer,
out: outputPluginPath,
});

expect(resultPluginPath).toBe(outputPluginPath);
const pluginBuffer = fs.readFileSync(outputPluginPath);
expect(PLUGIN_BUFFER.equals(pluginBuffer)).toBe(true);
const ppk = fs.readFileSync(path.join(outputDir, `${ID}.ppk`));
expect(PRIVATE_KEY).toBe(ppk.toString());

// TODO: use os tempdir for more safe testing
await rimraf(outputDir);
});
});
14 changes: 6 additions & 8 deletions src/plugin/packer/__tests__/create-contents-zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ const fixturesDir = path.join(__dirname, "fixtures");
const pluginDir = path.join(fixturesDir, "sample-plugin", "plugin-dir");

describe("create-contents-zip", () => {
it("should be able to create buffer from a plugin directory", (done) => {
it("should be able to create buffer from a plugin directory", async () => {
const manifestJSONPath = path.join(pluginDir, "manifest.json");
const manifest = JSON.parse(fs.readFileSync(manifestJSONPath, "utf-8"));
createContentsZip(pluginDir, manifest).then((buffer) => {
readZipContentsNames(buffer as Buffer).then((files) => {
expect(files).toStrictEqual(["manifest.json", "image/icon.png"]);
expect(buffer).toBeInstanceOf(Buffer);
done();
});
});

const buffer = await createContentsZip(pluginDir, manifest);
const files = await readZipContentsNames(buffer as Buffer);
expect(files).toStrictEqual(["manifest.json", "image/icon.png"]);
expect(buffer).toBeInstanceOf(Buffer);
});
});
2 changes: 1 addition & 1 deletion src/plugin/packer/__tests__/helpers/zip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yauzl from "yauzl";

export const readZipContentsNames = (
export const readZipContentsNames = async (
zipFilePath: Buffer,
): Promise<string[]> => {
return new Promise((resolve, reject) => {
Expand Down
27 changes: 12 additions & 15 deletions src/plugin/packer/__tests__/pack-plugin-from-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@ const ppkFilePath = path.join(fixturesDir, "private.ppk");
const pluginDir = path.join(fixturesDir, "sample-plugin", "plugin-dir");

describe("pack-plugin-from-manifest", () => {
it("should be able to create a plugin from the manifest json path", (done) => {
it("should be able to create a plugin from the manifest json path", async () => {
const manifestJSONPath = path.join(pluginDir, "manifest.json");
const privateKey = fs.readFileSync(ppkFilePath, "utf-8");
const manifest = JSON.parse(fs.readFileSync(manifestJSONPath, "utf-8"));
Promise.all([
packPluginFromManifest(manifestJSONPath, privateKey),
createContentsZip(pluginDir, manifest).then((buffer) =>
packer(buffer as any, privateKey),
),
]).then(([result1, result2]) => {
expect(result1.id).toBe(result2.id);
expect(result1.plugin.length).toBe(result2.plugin.length);
expect(result1.privateKey).toBe(result2.privateKey);
readZipContentsNames(result1.plugin).then((files) => {
expect(files).toStrictEqual(["contents.zip", "PUBKEY", "SIGNATURE"]);
done();
});
});

const result1 = await packPluginFromManifest(manifestJSONPath, privateKey);
const buffer = await createContentsZip(pluginDir, manifest);
const result2 = await packer(buffer as any, privateKey);

expect(result1.id).toBe(result2.id);
expect(result1.plugin.length).toBe(result2.plugin.length);
expect(result1.privateKey).toBe(result2.privateKey);

const files = await readZipContentsNames(result1.plugin);
expect(files).toStrictEqual(["contents.zip", "PUBKEY", "SIGNATURE"]);
});
});
Loading

0 comments on commit 50eccd2

Please sign in to comment.