Skip to content

Commit

Permalink
Merge pull request #72 from ubports/path-fix
Browse files Browse the repository at this point in the history
Introduce a setPath option for tools
  • Loading branch information
NeoTheThird authored Sep 12, 2022
2 parents 7fae5bf + be0b124 commit 7d96288
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/adb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2020 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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
6 changes: 4 additions & 2 deletions src/adb.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2021 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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 @@ -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" }) }
);
});
});
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/cancelable-promise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2020 UBports Foundation <[email protected]>
* Copyright (C) 2020-2022 UBports Foundation <[email protected]>
* Copyright (C) 2020-2022 Johannah Sprinz <[email protected]>
*
* 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
37 changes: 37 additions & 0 deletions src/cancelable-promise.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

/*
* Copyright (C) 2022 UBports Foundation <[email protected]>
* Copyright (C) 2022 Johannah Sprinz <[email protected]>
*
* 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/>.
*/

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()
);
});
});
3 changes: 2 additions & 1 deletion src/common.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2020 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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
3 changes: 2 additions & 1 deletion src/common.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2021 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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
3 changes: 2 additions & 1 deletion src/fastboot.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2020 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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
10 changes: 6 additions & 4 deletions src/fastboot.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2021 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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 @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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();
});
Expand Down
3 changes: 2 additions & 1 deletion src/heimdall.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2019-2020 UBports Foundation <[email protected]>
* Copyright (C) 2019-2022 UBports Foundation <[email protected]>
* Copyright (C) 2019-2022 Johannah Sprinz <[email protected]>
*
* 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
3 changes: 2 additions & 1 deletion src/heimdall.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2019-2021 UBports Foundation <[email protected]>
* Copyright (C) 2019-2022 UBports Foundation <[email protected]>
* Copyright (C) 2019-2022 Johannah Sprinz <[email protected]>
*
* 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
3 changes: 2 additions & 1 deletion src/module.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2020 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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
3 changes: 2 additions & 1 deletion src/module.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2021 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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
12 changes: 10 additions & 2 deletions src/tool.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2017-2020 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 UBports Foundation <[email protected]>
* Copyright (C) 2017-2022 Johannah Sprinz <[email protected]>
*
* 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 All @@ -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";
Expand All @@ -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}`;
}

/**
Expand Down Expand Up @@ -116,6 +123,7 @@ export class Tool extends EventEmitter {
[...this.extra, ...args].flat(),
{
env: {
...process.env,
ADB_TRACE: "rwx"
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/tool.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use strict";

/*
* Copyright (C) 2019-2021 UBports Foundation <[email protected]>
* Copyright (C) 2019-2022 UBports Foundation <[email protected]>
* Copyright (C) 2019-2022 Johannah Sprinz <[email protected]>
*
* 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 @@ -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);
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 7d96288

Please sign in to comment.