Skip to content

Commit

Permalink
Merge pull request #2832 from ubports/session
Browse files Browse the repository at this point in the history
session store to track executed actions
  • Loading branch information
NeoTheThird authored Nov 28, 2022
2 parents 919a81e + 5ab18b4 commit 1756231
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const settings = require("../lib/settings.js");
const errors = require("../lib/errors.js");
const window = require("../lib/window.js");
const api = require("./helpers/api.js");
const Session = require("./helpers/session.js");
const PluginIndex = require("./plugins/index.js");
const packageInfo = require("../../package.json");
const semver = require("semver");
Expand All @@ -47,21 +48,24 @@ const semver = require("semver");
*/
class Core {
constructor() {
this.session = new Session();
this.props = {};
this.reset();
this.plugins = new PluginIndex(
this.props,
cachePath,
mainEvent,
log,
settings
settings,
this.session
);
}

/**
* reset run properties
*/
reset() {
this.session.reset();
this.props = {
config: null,
os: null,
Expand Down
48 changes: 48 additions & 0 deletions src/core/helpers/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict";

/*
* Copyright (C) 2022 UBports Foundation <info@ubports.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class Session {
constructor() {
this.reset();
}

reset() {
this.store = new Map();
}

set(action, args, error = null) {
if (!this.store.get(action)?.error) this.store.set(action, { args, error });
}

get() {
return this.store.entries();
}

getActionsDebugInfo() {
return Array.from(this.get())
.map(
([action, { args, error }]) =>
`${action}: ${error || "OK"}\n` +
(args ? ` ${JSON.stringify(args)}\n` : "")
)
.join("");
}
}

module.exports = Session;
23 changes: 23 additions & 0 deletions src/core/helpers/session.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Session = require("./session.js");
const session = new Session();

describe("Session", () => {
it("should have store", () => {
expect(session.store).toBeInstanceOf(Map);
});
it("should store", () => {
session.set("fastboot:boot", { partition: "recovery" });
session.set("adb:shell");
session.set("adb:shell", null, "some error");
session.set("adb:shell");
expect(Object.fromEntries(session.get())).toEqual({
"adb:shell": { error: "some error", args: null },
"fastboot:boot": { args: { partition: "recovery" }, error: null }
});
expect(session.getActionsDebugInfo()).toEqual(
"fastboot:boot: OK\n" +
' {"partition":"recovery"}\n' +
"adb:shell: some error\n"
);
});
});
12 changes: 7 additions & 5 deletions src/core/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ const SystemimagePlugin = require("./systemimage/plugin.js");
* @property {SystemimagePlugin} plugins.systemimage systemimage plugin
*/
class PluginIndex {
constructor(props, cachePath, mainEvent, log, settings) {
constructor(props, cachePath, mainEvent, log, settings, session) {
this.props = props;
this.log = log;
this.settings = settings;
this.session = session;
this.event = mainEvent;
const pluginArgs = [props, cachePath, this.event, log, settings];
this.plugins = {
Expand Down Expand Up @@ -78,11 +79,12 @@ class PluginIndex {
action(action) {
return Promise.resolve(this.parsePluginId(action)).then(([p, f]) => {
this.log.verbose(`running ${p} action ${f}`);
return this.plugins[p][`action__${f}`](action[`${p}:${f}`]).catch(
error => {
return this.plugins[p][`action__${f}`](action[`${p}:${f}`])
.then(r => this.session.set(...Object.entries(action)[0]) || r)
.catch(error => {
this.session.set(...Object.entries(action)[0], error);
throw { error, action: `${p}:${f}` };
}
);
});
});
}

Expand Down
9 changes: 8 additions & 1 deletion src/core/plugins/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const log = {
};

const settings = {};
const session = new (require("../helpers/session.js"))();

const pluginArgs = [{}, "a", {}, log, settings];
const pluginArgs = [{}, "a", {}, log, settings, session];

const pluginIndex = new (require("./index.js"))(...pluginArgs);
const originalPluginList = pluginIndex.plugins;
Expand All @@ -36,6 +37,7 @@ describe("PluginIndex", () => {
});
describe("action", () => {
it("should run action", () => {
pluginIndex.session.reset();
jest
.spyOn(pluginIndex.plugins.adb, "action__format")
.mockResolvedValue(1337);
Expand All @@ -46,9 +48,14 @@ describe("PluginIndex", () => {
});
expect(pluginIndex.plugins.adb.action__format).toHaveBeenCalledTimes(1);
pluginIndex.plugins.adb.action__format.mockRestore();
expect(Array.from(pluginIndex.session.get())).toContainEqual([
"adb:format",
{ error: null, args: { a: "b" } }
]);
});
});
it("should reject on error", done => {
pluginIndex.session.reset();
jest
.spyOn(pluginIndex.plugins.adb, "action__format")
.mockRejectedValue("terrible");
Expand Down
8 changes: 8 additions & 0 deletions src/lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ class Reporter {
}
],
logs: [
...(core.session.getActionsDebugInfo()
? [
{
name: "actions",
content: core.session.getActionsDebugInfo()
}
]
: []),
{
name: "ubports-installer.log",
content: await logfile
Expand Down
7 changes: 5 additions & 2 deletions src/lib/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ describe("sendOpenCutsRun()", () => {
it("should send open-cuts run", () => {
log.get.mockResolvedValue("log content");
errors.errors = ["error one", "error two"];
core.session.getActionsDebugInfo.mockReturnValue("adb:shell: OK");
const smartRun = jest.fn();
OpenCutsReporter.mockImplementation(() => ({
smartRun
Expand All @@ -179,8 +180,9 @@ describe("sendOpenCutsRun()", () => {
],
comment: undefined,
logs: [
{ content: "log content", name: "ubports-installer.log" },
{ content: "error one\n\nerror two", name: "ignored errors" }
{ name: "actions", content: "adb:shell: OK" },
{ name: "ubports-installer.log", content: "log content" },
{ name: "ignored errors", content: "error one\n\nerror two" }
],
result: "FAIL"
}
Expand All @@ -190,6 +192,7 @@ describe("sendOpenCutsRun()", () => {
it("should send open-cuts run", () => {
log.get.mockResolvedValue("log content");
errors.errors = [];
core.session.getActionsDebugInfo.mockReturnValue();
const smartRun = jest.fn();
OpenCutsReporter.mockImplementation(() => ({
smartRun
Expand Down
10 changes: 7 additions & 3 deletions src/lib/window.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

/*
* Copyright (C) 2017-2021 UBports Foundation <info@ubports.com>
* Copyright (C) 2017-2022 UBports Foundation <info@ubports.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -42,8 +42,12 @@ class Window {
* @param {...any} args arguments to send
*/
send(channel, ...args) {
const main = this.getMain();
if (main) main.send(channel, ...args);
try {
const main = this.getMain();
main?.send(channel, ...args);
} catch (error) {
throw new Error(`Failed to send ${channel}: ${error}`);
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/lib/window.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,21 @@ describe("Window class", () => {
expect(window.send("a", "b", { c: "d" })).toEqual(undefined);
expect(mockWebContents.send).toHaveBeenCalledWith("a", "b", { c: "d" });
});
it("should fail silently", () => {
it("should pass silently if window is null", () => {
webContents.fromId.mockReturnValue(null);
expect(window.send("a", "b", { c: "d" })).toEqual(undefined);
});
it("should throw on error", done => {
webContents.fromId.mockReturnValue({
send: () => {
throw "error";
}
});
try {
window.send("a", "b", { c: "d" });
} catch (e) {
done();
}
});
});
});

0 comments on commit 1756231

Please sign in to comment.