Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session store to track executed actions #2832

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

push(action, args, error = null) {
NeoTheThird marked this conversation as resolved.
Show resolved Hide resolved
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.push("fastboot:boot", { partition: "recovery" });
session.push("adb:shell");
session.push("adb:shell", null, "some error");
session.push("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) {
NeoTheThird marked this conversation as resolved.
Show resolved Hide resolved
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.push(...Object.entries(action)[0]) || r)
.catch(error => {
this.session.push(...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
8 changes: 6 additions & 2 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 @@ -43,7 +43,11 @@ class Window {
*/
send(channel, ...args) {
const main = this.getMain();
if (main) main.send(channel, ...args);
try {
if (main) main.send(channel, ...args);
NeoTheThird marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
throw new Error(`Failed to send ${channel}: ${error}`);
}
}
}

Expand Down