From eee3b7188622bca3587db7d88f6d46c9b3e4b359 Mon Sep 17 00:00:00 2001 From: develar Date: Mon, 30 Jan 2017 08:48:44 +0100 Subject: [PATCH] refactor(electron-updater): get rid of global __test_resourcesPath --- package.json | 2 +- packages/electron-updater/src/AppUpdater.ts | 30 +++++---- test/src/nsisUpdaterTest.ts | 69 +++++++++------------ yarn.lock | 6 +- 4 files changed, 51 insertions(+), 56 deletions(-) diff --git a/package.json b/package.json index 9901c47a224..42f23cb4c96 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "devDependencies": { "@types/electron": "^1.4.31", "@types/ini": "^1.3.29", - "@types/jest": "^16.0.4", + "@types/jest": "^16.0.5", "@types/js-yaml": "^3.5.29", "@types/source-map-support": "^0.2.28", "babel-plugin-array-includes": "^2.0.3", diff --git a/packages/electron-updater/src/AppUpdater.ts b/packages/electron-updater/src/AppUpdater.ts index cbd26f3fb7d..7ae16b56b87 100644 --- a/packages/electron-updater/src/AppUpdater.ts +++ b/packages/electron-updater/src/AppUpdater.ts @@ -37,9 +37,16 @@ export abstract class AppUpdater extends EventEmitter { public readonly signals = new UpdaterSignal(this) + private appUpdateConfigPath: string | null + + setUpdateConfigPath(value: string | null) { + this.clientPromise = null + this.appUpdateConfigPath = value + } + protected updateAvailable = false - private clientPromise: Promise> + private clientPromise: Promise> | null private readonly untilAppReady: Promise private checkForUpdatesPromise: Promise | null @@ -127,21 +134,19 @@ export abstract class AppUpdater extends EventEmitter { this.emit("checking-for-update") try { - if (this.clientPromise == null) { - this.clientPromise = loadUpdateConfig().then(it => createClient(it)) - } return await this.doCheckForUpdates() } catch (e) { - if (this.logger != null) { - this.logger.info("Cannot check for updates:") - } - this.emit("error", e, (e.stack || e).toString()) + this.emit("error", e, `Cannot check for updates: ${(e.stack || e).toString()}`) throw e } } private async doCheckForUpdates(): Promise { + if (this.clientPromise == null) { + this.clientPromise = this.loadUpdateConfig().then(it => createClient(it)) + } + const client = await this.clientPromise client.setRequestHeaders(this.requestHeaders) const versionInfo = await client.getLatestVersion() @@ -225,10 +230,13 @@ export abstract class AppUpdater extends EventEmitter { protected async abstract doDownloadUpdate(versionInfo: VersionInfo, fileInfo: FileInfo): Promise abstract quitAndInstall(): void -} -async function loadUpdateConfig() { - return safeLoad(await readFile(path.join((global).__test_resourcesPath || (process).resourcesPath, "app-update.yml"), "utf-8")) + async loadUpdateConfig() { + if (this.appUpdateConfigPath == null) { + this.appUpdateConfigPath = path.join(process.resourcesPath, "app-update.yml") + } + return safeLoad(await readFile(this.appUpdateConfigPath, "utf-8")) + } } function createClient(data: string | PublishConfiguration | BintrayOptions | GithubOptions) { diff --git a/test/src/nsisUpdaterTest.ts b/test/src/nsisUpdaterTest.ts index 2b90ae0a0ee..2d0f30ab860 100644 --- a/test/src/nsisUpdaterTest.ts +++ b/test/src/nsisUpdaterTest.ts @@ -4,7 +4,7 @@ import * as path from "path" import { TmpDir } from "electron-builder-util/out/tmp" import { outputFile } from "fs-extra-p" import { safeDump } from "js-yaml" -import { GenericServerOptions, GithubOptions } from "electron-builder-http/out/publishOptions" +import { GenericServerOptions, GithubOptions, BintrayOptions } from "electron-builder-http/out/publishOptions" if (process.env.ELECTRON_BUILDER_OFFLINE === "true") { fit("Skip ArtifactPublisherTest suite — ELECTRON_BUILDER_OFFLINE is defined", () => { @@ -12,6 +12,8 @@ if (process.env.ELECTRON_BUILDER_OFFLINE === "true") { }) } +const tmpDir = new TmpDir() + const g = (global) g.__test_app = { getVersion: function () { @@ -46,15 +48,12 @@ test("cannot find suitable file for version", async () => { }) test("file url", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ provider: "bintray", owner: "actperepo", package: "TestApp", })) - g.__test_resourcesPath = testResourcesPath - const updater = new NsisUpdater() const actualEvents: Array = [] const expectedEvents = ["checking-for-update", "update-available", "update-downloaded"] @@ -72,14 +71,11 @@ test("file url", async () => { }) test("file url generic", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ provider: "generic", url: "https://develar.s3.amazonaws.com/test", })) - g.__test_resourcesPath = testResourcesPath - const updater = new NsisUpdater() const actualEvents = trackEvents(updater) @@ -91,15 +87,12 @@ test("file url generic", async () => { }) test("sha2 mismatch error event", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ provider: "generic", url: "https://develar.s3.amazonaws.com/test", channel: "beta", })) - g.__test_resourcesPath = testResourcesPath - const updater = new NsisUpdater() updater.logger = console const actualEvents = trackEvents(updater) @@ -112,14 +105,11 @@ test("sha2 mismatch error event", async () => { }) test("file url generic - manual download", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ provider: "generic", url: "https://develar.s3.amazonaws.com/test", })) - g.__test_resourcesPath = testResourcesPath - const updater = new NsisUpdater() updater.autoDownload = false const actualEvents = trackEvents(updater) @@ -134,14 +124,11 @@ test("file url generic - manual download", async () => { // https://github.com/electron-userland/electron-builder/issues/1045 test("checkForUpdates several times", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ provider: "generic", url: "https://develar.s3.amazonaws.com/test", })) - g.__test_resourcesPath = testResourcesPath - const updater: NsisUpdater = new NsisUpdater() const actualEvents = trackEvents(updater) @@ -157,15 +144,12 @@ test("checkForUpdates several times", async () => { }) test("file url github", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ - provider: "github", - owner: "develar", - repo: "__test_nsis_release", - })) - g.__test_resourcesPath = testResourcesPath - const updater: NsisUpdater = new NsisUpdater() + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ + provider: "github", + owner: "develar", + repo: "__test_nsis_release", + })) const actualEvents: Array = [] const expectedEvents = ["checking-for-update", "update-available", "update-downloaded"] @@ -183,7 +167,6 @@ test("file url github", async () => { }) test("test error", async () => { - g.__test_resourcesPath = null const updater: NsisUpdater = new NsisUpdater() const actualEvents = trackEvents(updater) @@ -193,14 +176,11 @@ test("test error", async () => { }) test("test download progress", async () => { - const tmpDir = new TmpDir() - const testResourcesPath = await tmpDir.getTempFile("update-config") - await outputFile(path.join(testResourcesPath, "app-update.yml"), safeDump({ + const updater = new NsisUpdater() + updater.setUpdateConfigPath(await writeUpdateConfig({ provider: "generic", url: "https://develar.s3.amazonaws.com/test", })) - g.__test_resourcesPath = testResourcesPath - const updater = new NsisUpdater() updater.autoDownload = false const progressEvents: Array = [] @@ -219,6 +199,13 @@ test("test download progress", async () => { expect(lastEvent.transferred).toBe(lastEvent.total) }) + +async function writeUpdateConfig(data: GenericServerOptions | GithubOptions | BintrayOptions): Promise { + const updateConfigPath = path.join(await tmpDir.getTempFile("update-config"), "app-update.yml") + await outputFile(updateConfigPath, safeDump(data)) + return updateConfigPath +} + function trackEvents(updater: NsisUpdater) { const actualEvents: Array = [] for (const eventName of ["checking-for-update", "update-available", "update-downloaded", "error"]) { diff --git a/yarn.lock b/yarn.lock index 28422333b17..f03e4e4517f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -32,9 +32,9 @@ version "1.3.29" resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.29.tgz#1325e981e047d40d13ce0359b821475b97741d2f" -"@types/jest@^16.0.4": - version "16.0.4" - resolved "https://registry.yarnpkg.com/@types/jest/-/jest-16.0.4.tgz#31bdd13e2bdfa71498b022494ba04d407f9f8230" +"@types/jest@^16.0.5": + version "16.0.5" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-16.0.5.tgz#bad01154df405180ba6e195eb80454c70666b1a7" "@types/js-yaml@^3.5.29": version "3.5.29"