diff --git a/CHANGELOG.md b/CHANGELOG.md index f35edcc..cedb31d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ promise-android-tools versioning started at 1.0.0, but this changelog was not ad ## [Unreleased] +### Added + +- Introduce a setPath option for tools ([#72](https://github.com/ubports/promise-android-tools/pull/72)) + ## [4.0.13] - 2022-09-07 ### Changed diff --git a/package-lock.json b/package-lock.json index def1294..f7aabfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "promise-android-tools", - "version": "4.0.13", + "version": "4.0.14", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "promise-android-tools", - "version": "4.0.13", + "version": "4.0.14", "license": "GPL-3.0", "dependencies": { "android-tools-bin": "^1.0.7", diff --git a/package.json b/package.json index a5803a5..40d5571 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "promise-android-tools", - "version": "4.0.13", + "version": "4.0.14", "description": "A wrapper for adb, fastboot, and heimdall that returns convenient promises.", "main": "./lib/module.cjs", "type": "module", diff --git a/src/adb.js b/src/adb.js index 0712242..5b520b3 100644 --- a/src/adb.js +++ b/src/adb.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2020 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 diff --git a/src/adb.spec.js b/src/adb.spec.js index d619d09..c83bf3b 100644 --- a/src/adb.spec.js +++ b/src/adb.spec.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2021 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 @@ -353,7 +354,7 @@ describe("Adb module", function () { expect(child_process.spawn).toHaveBeenCalledWith( adb.executable, [...adb.extra, "sideload", "tests/test-data/test_file"], - { env: { ADB_TRACE: "rwx" } } + { env: expect.objectContaining({ ADB_TRACE: "rwx" }) } ); }); }); @@ -1017,6 +1018,7 @@ describe("Adb module", function () { it("should restore full backup", function () { stubExec(1, "should not be called"); jest.useFakeTimers(); + jest.setSystemTime(); fs.readJSON = jest.fn().mockReturnValue({ codename: "codename", comment: "Ubuntu Touch backup created on 1970-01-01T00:00:00.000Z", diff --git a/src/cancelable-promise.js b/src/cancelable-promise.js index bcee752..abbc2f8 100644 --- a/src/cancelable-promise.js +++ b/src/cancelable-promise.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2020 UBports Foundation + * Copyright (C) 2020-2022 UBports Foundation + * Copyright (C) 2020-2022 Johannah Sprinz * * 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 diff --git a/src/cancelable-promise.spec.js b/src/cancelable-promise.spec.js new file mode 100644 index 0000000..94a5954 --- /dev/null +++ b/src/cancelable-promise.spec.js @@ -0,0 +1,37 @@ +"use strict"; + +/* + * Copyright (C) 2022 UBports Foundation + * Copyright (C) 2022 Johannah Sprinz + * + * 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 . + */ + +import { jest, expect } from "@jest/globals"; + +import cp from "cancelable-promise"; + +describe("CancelablePromise", function () { + it("should export tool when packaged", function () { + cp.CancelablePromise = null; + return import("./cancelable-promise.js").then(cp => + expect(cp).toBeTruthy() + ); + }); + it("should export tool when native", function () { + return import("./cancelable-promise.js").then(cp => + expect(cp).toBeTruthy() + ); + }); +}); diff --git a/src/common.js b/src/common.js index 4908214..8f81216 100644 --- a/src/common.js +++ b/src/common.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2020 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 diff --git a/src/common.spec.js b/src/common.spec.js index f6b1978..a54c0c8 100644 --- a/src/common.spec.js +++ b/src/common.spec.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2021 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 diff --git a/src/fastboot.js b/src/fastboot.js index 28b9070..5ae7f2a 100644 --- a/src/fastboot.js +++ b/src/fastboot.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2020 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 diff --git a/src/fastboot.spec.js b/src/fastboot.spec.js index 61ab9c5..f62529d 100644 --- a/src/fastboot.spec.js +++ b/src/fastboot.spec.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2021 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 @@ -81,6 +82,7 @@ describe("Fastboot module", function () { on: jest.fn((_, cb) => { if (i++ === 0) { cb("Sending 'boot'"); + cb("OKAY"); setTimeout(() => cb("Writing 'boot'"), 1); setTimeout(() => cb("Finished 'boot'"), 2); } else { @@ -118,7 +120,7 @@ describe("Fastboot module", function () { expect(child_process.spawn).toHaveBeenCalledWith( fastboot.executable, ["flash", "boot", "/path/to/boot.img"], - { env: { ADB_TRACE: "rwx" } } + { env: expect.objectContaining({ ADB_TRACE: "rwx" }) } ); expect(child_process.spawn).toHaveBeenCalledWith( fastboot.executable, @@ -129,7 +131,7 @@ describe("Fastboot module", function () { "--disable-verity", "/path/to/recovery.img" ], - { env: { ADB_TRACE: "rwx" } } + { env: expect.objectContaining({ ADB_TRACE: "rwx" }) } ); expect(progress).toHaveBeenCalledWith(0); expect(progress).toHaveBeenCalledWith(0.15); @@ -179,7 +181,7 @@ describe("Fastboot module", function () { expect(child_process.spawn).toHaveBeenCalledWith( fastboot.executable, ["flash", "boot", "/path/to/image"], - { env: { ADB_TRACE: "rwx" } } + { env: expect.objectContaining({ ADB_TRACE: "rwx" }) } ); done(); }); diff --git a/src/heimdall.js b/src/heimdall.js index 3fa5ee1..fa4f03d 100644 --- a/src/heimdall.js +++ b/src/heimdall.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2019-2020 UBports Foundation + * Copyright (C) 2019-2022 UBports Foundation + * Copyright (C) 2019-2022 Johannah Sprinz * * 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 diff --git a/src/heimdall.spec.js b/src/heimdall.spec.js index 1a611f3..1e2f85a 100644 --- a/src/heimdall.spec.js +++ b/src/heimdall.spec.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2019-2021 UBports Foundation + * Copyright (C) 2019-2022 UBports Foundation + * Copyright (C) 2019-2022 Johannah Sprinz * * 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 diff --git a/src/module.js b/src/module.js index 429060a..66e3549 100644 --- a/src/module.js +++ b/src/module.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2020 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 diff --git a/src/module.spec.js b/src/module.spec.js index 9e03867..9681be4 100644 --- a/src/module.spec.js +++ b/src/module.spec.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2021 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 diff --git a/src/tool.js b/src/tool.js index 0f85ece..47b5305 100644 --- a/src/tool.js +++ b/src/tool.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2017-2020 UBports Foundation + * Copyright (C) 2017-2022 UBports Foundation + * Copyright (C) 2017-2022 Johannah Sprinz * * 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 @@ -18,7 +19,7 @@ */ import child_process from "child_process"; -import { getAndroidToolPath } from "android-tools-bin"; +import { getAndroidToolPath, getAndroidToolBaseDir } from "android-tools-bin"; import EventEmitter from "events"; import { removeFalsy } from "./common.js"; import { CancelablePromise } from "./cancelable-promise.js"; @@ -38,6 +39,12 @@ export class Tool extends EventEmitter { this.extra = options?.extra || []; this.execOptions = options?.execOptions || {}; this.processes = []; + if ( + options.setPath && + process.env.PATH && + !process.env.PATH.includes(getAndroidToolBaseDir()) + ) + process.env.PATH = `${getAndroidToolBaseDir()}:${process.env.PATH}`; } /** @@ -116,6 +123,7 @@ export class Tool extends EventEmitter { [...this.extra, ...args].flat(), { env: { + ...process.env, ADB_TRACE: "rwx" } } diff --git a/src/tool.spec.js b/src/tool.spec.js index f757c93..ddbe86b 100644 --- a/src/tool.spec.js +++ b/src/tool.spec.js @@ -1,7 +1,8 @@ "use strict"; /* - * Copyright (C) 2019-2021 UBports Foundation + * Copyright (C) 2019-2022 UBports Foundation + * Copyright (C) 2019-2022 Johannah Sprinz * * 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 @@ -40,7 +41,7 @@ describe("Tool module", function () { describe("constructor()", function () { ["adb", "fastboot", "heimdall"].forEach(t => { it(`should create generic ${t}`, function () { - const tool = new Tool({ tool: t }); + const tool = new Tool({ tool: t, setPath: true }); expect(tool).toExist; expect(tool.tool).toEqual(t); expect(tool.executable).toMatch(t); @@ -217,7 +218,7 @@ describe("Tool module", function () { expect(child_process.spawn).toHaveBeenCalledWith( tool.executable, [...tool.extra, ...args], - { env: { ADB_TRACE: "rwx" } } + { env: expect.objectContaining({ ADB_TRACE: "rwx" }) } ); expect(spawnStartListenerStub).toHaveBeenCalledWith({ cmd: [tool.tool, ...tool.extra, ...args]