Skip to content

Commit

Permalink
plugins: core: catch and rethrow error when unpacking
Browse files Browse the repository at this point in the history
Unpacking actually expects the callback to always be provided,
either as second or third argument.

Change-Id: If81661d806ea58304c91d38a168535575ece8afd
Co-developed-by: Maciej Sopyło <[email protected]>
Signed-off-by: Alexander Martinz <[email protected]>
  • Loading branch information
amartinz committed Sep 7, 2022
1 parent 45ff4e1 commit 5463ab0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
23 changes: 16 additions & 7 deletions src/core/plugins/core/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,23 @@ class CorePlugin extends Plugin {
})
.then(basepath =>
Promise.all(
files.map(file =>
unpack(
path.join(basepath, file.archive),
path.join(basepath, file.dir)
)
)
files.map(file => {
const archive = path.join(basepath, file.archive);
const directory = path.join(basepath, file.dir || ".");
this.log.debug("Unpacking " + archive + " to: " + directory);
return new Promise(function (resolve, reject) {
unpack(archive, directory, err => {
if (err) {
reject(Error("Failed to unpack: " + err));
} else {
resolve();
}
});
});
})
)
);
)
.then(() => Promise.resolve());
}

/**
Expand Down
64 changes: 59 additions & 5 deletions src/core/plugins/core/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
process.argv = [null, null, "-vv"];
const mainEvent = { emit: jest.fn() };
const log = { error: jest.fn(), debug: jest.fn(), info: jest.fn() };
const asarLibs = require("../../helpers/asarLibs.js");
jest.mock("../../helpers/asarLibs.js");
beforeEach(() => {
mainEvent.emit.mockReset();
log.error.mockReset();
log.debug.mockReset();
log.info.mockReset();
asarLibs.unpack.mockClear();
});

const path = require("path");

const { download, checkFile } = require("progressive-downloader");
const { writeFile } = require("fs-extra");
const core = new (require("./plugin.js"))(
Expand Down Expand Up @@ -209,11 +214,60 @@ describe("core plugin", () => {
});

describe("action__unpack()", () => {
it("should unpack", () =>
core.action__unpack({
group: "firmware",
files: [{ archive: "a.zip", dir: "a" }]
})); // TODO add assertions
it("should unpack to a child directory called 'unpacked'", () => {
jest
.spyOn(mainEvent, "emit")
.mockImplementation((e, f, g, cb) => (cb ? cb() : null));
return core
.action__unpack({
group: "firmware",
files: [{ archive: "a.zip", dir: "unpacked" }]
})
.then(() => {
expect(asarLibs.unpack).toHaveBeenCalledTimes(1);
expect(asarLibs.unpack).toHaveBeenCalledWith(
path.join("a/yggdrasil/firmware", "a.zip"),
path.join("a/yggdrasil/firmware", "unpacked"),
expect.any(Function)
);
expect(mainEvent.emit).toHaveBeenCalledTimes(3);
});
});
it("should unpack to directory where archive is located", () => {
jest
.spyOn(mainEvent, "emit")
.mockImplementation((e, f, g, cb) => (cb ? cb() : null));
return core
.action__unpack({
group: "firmware",
files: [{ archive: "a.zip" }]
})
.then(() => {
expect(asarLibs.unpack).toHaveBeenCalledTimes(1);
expect(asarLibs.unpack).toHaveBeenCalledWith(
path.join("a/yggdrasil/firmware", "a.zip"),
path.join("a/yggdrasil/firmware", "."),
expect.any(Function)
);
expect(mainEvent.emit).toHaveBeenCalledTimes(3);
});
});
it("should reject on unpack errors", () => {
jest
.spyOn(mainEvent, "emit")
.mockImplementation((e, f, g, cb) => (cb ? cb() : null));
asarLibs.unpack.mockImplementation((e, f, cb) =>
cb(new Error("test error"))
);
return core
.action__unpack({
group: "firmware",
files: [{ archive: "a.zip" }]
})
.catch(e => {
expect(e.message).toEqual("Failed to unpack: Error: test error");
});
});
});

describe("action__manual_download()", () => {
Expand Down

0 comments on commit 5463ab0

Please sign in to comment.