From 86e6d1509f9b9c76c559e9c3a12b7a1595fe3ac4 Mon Sep 17 00:00:00 2001 From: Mike Maietta Date: Sun, 6 Feb 2022 08:19:58 -0800 Subject: [PATCH] fix(updater): Remove checks for app-update.yml when auto-updates are not supported (#6616) * Moving `isUpdaterActive` check to `checkForUpdates` which is utilized by `checkForUpdatesAndNotify`. Fixes: #6322 --- .changeset/sweet-parrots-wait.md | 5 ++++ packages/electron-updater/src/AppUpdater.ts | 15 ++++++----- test/src/helpers/updaterTestUtil.ts | 10 ++++---- test/src/updater/differentialUpdateTest.ts | 8 +++--- test/src/updater/macUpdaterTest.ts | 10 ++++---- test/src/updater/nsisUpdaterTest.ts | 28 ++++++++++----------- 6 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 .changeset/sweet-parrots-wait.md diff --git a/.changeset/sweet-parrots-wait.md b/.changeset/sweet-parrots-wait.md new file mode 100644 index 00000000000..2131cd28be3 --- /dev/null +++ b/.changeset/sweet-parrots-wait.md @@ -0,0 +1,5 @@ +--- +"electron-updater": patch +--- + +fix(updater): Remove checks for app-update.yml when auto-updates are not supported diff --git a/packages/electron-updater/src/AppUpdater.ts b/packages/electron-updater/src/AppUpdater.ts index fa38986b971..b5b66765912 100644 --- a/packages/electron-updater/src/AppUpdater.ts +++ b/packages/electron-updater/src/AppUpdater.ts @@ -224,7 +224,11 @@ export abstract class AppUpdater extends EventEmitter { /** * Asks the server whether there is an update. */ - checkForUpdates(): Promise { + checkForUpdates(): Promise { + if (!this.isUpdaterActive()) { + return Promise.resolve(null) + } + let checkForUpdatesPromise = this.checkForUpdatesPromise if (checkForUpdatesPromise != null) { this._logger.info("Checking for update (already in progress)") @@ -259,20 +263,15 @@ export abstract class AppUpdater extends EventEmitter { // noinspection JSUnusedGlobalSymbols checkForUpdatesAndNotify(downloadNotification?: DownloadNotification): Promise { - if (!this.isUpdaterActive()) { - return Promise.resolve(null) - } - return this.checkForUpdates().then(it => { - const downloadPromise = it.downloadPromise - if (downloadPromise == null) { + if (!it?.downloadPromise) { if (this._logger.debug != null) { this._logger.debug("checkForUpdatesAndNotify called, downloadPromise is null") } return it } - void downloadPromise.then(() => { + void it.downloadPromise.then(() => { const notificationContent = AppUpdater.formatDownloadNotification(it.updateInfo.version, this.app.name, downloadNotification) new (require("electron").Notification)(notificationContent).show() }) diff --git a/test/src/helpers/updaterTestUtil.ts b/test/src/helpers/updaterTestUtil.ts index b0a002ec029..47d26bef9b7 100644 --- a/test/src/helpers/updaterTestUtil.ts +++ b/test/src/helpers/updaterTestUtil.ts @@ -34,18 +34,18 @@ export async function validateDownload(updater: AppUpdater, expectDownloadPromis const actualEvents = trackEvents(updater) const updateCheckResult = await updater.checkForUpdates() - const assets = (updateCheckResult.updateInfo as any).assets + const assets = (updateCheckResult?.updateInfo as any).assets if (assets != null) { for (const asset of assets) { delete asset.download_count } } - expect(updateCheckResult.updateInfo).toMatchSnapshot() + expect(updateCheckResult?.updateInfo).toMatchSnapshot() if (expectDownloadPromise) { // noinspection JSIgnoredPromiseFromCall - expect(updateCheckResult.downloadPromise).toBeDefined() - const downloadResult = await updateCheckResult.downloadPromise + expect(updateCheckResult?.downloadPromise).toBeDefined() + const downloadResult = await updateCheckResult?.downloadPromise if (updater instanceof MacUpdater) { expect(downloadResult).toEqual([]) } else { @@ -53,7 +53,7 @@ export async function validateDownload(updater: AppUpdater, expectDownloadPromis } } else { // noinspection JSIgnoredPromiseFromCall - expect(updateCheckResult.downloadPromise).toBeUndefined() + expect(updateCheckResult?.downloadPromise).toBeUndefined() } expect(actualEvents).toMatchSnapshot() diff --git a/test/src/updater/differentialUpdateTest.ts b/test/src/updater/differentialUpdateTest.ts index d61790aa12e..f5d17db77ba 100644 --- a/test/src/updater/differentialUpdateTest.ts +++ b/test/src/updater/differentialUpdateTest.ts @@ -238,16 +238,16 @@ async function doBuild(outDirs: Array, targets: Map path.basename(it))).toMatchSnapshot() } @@ -309,7 +309,7 @@ async function testBlockMap(oldDir: string, newDir: string, updaterClass: any, a "app-update.yml" ) const doTest = async () => { - await tuneTestUpdater(updater, { + tuneTestUpdater(updater, { platform: platform.nodeName as any, isUseDifferentialDownload: true, }) diff --git a/test/src/updater/macUpdaterTest.ts b/test/src/updater/macUpdaterTest.ts index 5e6253fb34c..a56b57229a9 100644 --- a/test/src/updater/macUpdaterTest.ts +++ b/test/src/updater/macUpdaterTest.ts @@ -51,15 +51,15 @@ test.ifAll.ifNotCi.ifMac("mac updates", async () => { // console.log(JSON.stringify(data)) }) - await tuneTestUpdater(updater) + tuneTestUpdater(updater) ;(updater as any)._testOnlyOptions.platform = process.platform const actualEvents = trackEvents(updater) const updateCheckResult = await updater.checkForUpdates() // todo when will be updated to use files - // expect(removeUnstableProperties(updateCheckResult.updateInfo.files)).toMatchSnapshot() - const files = await updateCheckResult.downloadPromise - expect(files!!.length).toEqual(1) - await assertThat(files!![0]).isFile() + // expect(removeUnstableProperties(updateCheckResult?.updateInfo.files)).toMatchSnapshot() + const files = await updateCheckResult?.downloadPromise + expect(files!.length).toEqual(1) + await assertThat(files![0]).isFile() expect(actualEvents).toMatchSnapshot() }) diff --git a/test/src/updater/nsisUpdaterTest.ts b/test/src/updater/nsisUpdaterTest.ts index 5e61f1bd584..48da3d82105 100644 --- a/test/src/updater/nsisUpdaterTest.ts +++ b/test/src/updater/nsisUpdaterTest.ts @@ -34,9 +34,9 @@ test("downgrade (disallowed, beta)", async () => { } const updateCheckResult = await updater.checkForUpdates() - expect(removeUnstableProperties(updateCheckResult.updateInfo)).toMatchSnapshot() + expect(removeUnstableProperties(updateCheckResult?.updateInfo)).toMatchSnapshot() // noinspection JSIgnoredPromiseFromCall - expect(updateCheckResult.downloadPromise).toBeUndefined() + expect(updateCheckResult?.downloadPromise).toBeUndefined() expect(actualEvents).toEqual(expectedEvents) }) @@ -95,8 +95,8 @@ test.skip.ifNotCiWin("sha512 mismatch error event", async () => { const actualEvents = trackEvents(updater) const updateCheckResult = await updater.checkForUpdates() - expect(removeUnstableProperties(updateCheckResult.updateInfo)).toMatchSnapshot() - await assertThat(updateCheckResult.downloadPromise).throws() + expect(removeUnstableProperties(updateCheckResult?.updateInfo)).toMatchSnapshot() + await assertThat(updateCheckResult?.downloadPromise).throws() expect(actualEvents).toMatchSnapshot() }) @@ -112,9 +112,9 @@ test("file url generic - manual download", async () => { const actualEvents = trackEvents(updater) const updateCheckResult = await updater.checkForUpdates() - expect(removeUnstableProperties(updateCheckResult.updateInfo)).toMatchSnapshot() + expect(removeUnstableProperties(updateCheckResult?.updateInfo)).toMatchSnapshot() // noinspection JSIgnoredPromiseFromCall - expect(updateCheckResult.downloadPromise).toBeNull() + expect(updateCheckResult?.downloadPromise).toBeNull() expect(actualEvents).toMatchSnapshot() await assertThat(path.join((await updater.downloadUpdate())[0])).isFile() @@ -132,12 +132,12 @@ test("checkForUpdates several times", async () => { for (let i = 0; i < 10; i++) { //noinspection JSIgnoredPromiseFromCall - updater.checkForUpdates() + void updater.checkForUpdates() } async function checkForUpdates() { const updateCheckResult = await updater.checkForUpdates() - expect(removeUnstableProperties(updateCheckResult.updateInfo)).toMatchSnapshot() + expect(removeUnstableProperties(updateCheckResult?.updateInfo)).toMatchSnapshot() await checkDownloadPromise(updateCheckResult) } @@ -148,8 +148,8 @@ test("checkForUpdates several times", async () => { expect(actualEvents).toMatchSnapshot() }) -async function checkDownloadPromise(updateCheckResult: UpdateCheckResult) { - return await assertThat(path.join((await updateCheckResult.downloadPromise)!![0])).isFile() +async function checkDownloadPromise(updateCheckResult: UpdateCheckResult | null) { + return await assertThat(path.join((await updateCheckResult?.downloadPromise)![0])).isFile() } test("file url github", async () => { @@ -183,7 +183,7 @@ test("file url github pre-release and fullChangelog", async () => { expect(info).toMatchSnapshot() }) const updateCheckResult = await validateDownload(updater) - expect(updateCheckResult.updateInfo).toMatchSnapshot() + expect(updateCheckResult?.updateInfo).toMatchSnapshot() }) test.ifEnv(process.env.GH_TOKEN || process.env.GITHUB_TOKEN)("file url github private", async () => { @@ -271,7 +271,7 @@ test.skip.ifAll("invalid signature", async () => { publisherName: ["Foo Bar"], }) const actualEvents = trackEvents(updater) - await assertThat(updater.checkForUpdates().then((it): any => it.downloadPromise)).throws() + await assertThat(updater.checkForUpdates().then((it): any => it?.downloadPromise)).throws() expect(actualEvents).toMatchSnapshot() }) @@ -318,7 +318,7 @@ test("cancel download with progress", async () => { updater.signals.updateCancelled(() => (cancelled = true)) const checkResult = await updater.checkForUpdates() - checkResult.cancellationToken!!.cancel() + checkResult?.cancellationToken!.cancel() if (progressEvents.length > 0) { const lastEvent = progressEvents[progressEvents.length - 1] @@ -327,7 +327,7 @@ test("cancel download with progress", async () => { expect(lastEvent.transferred).not.toBe(lastEvent.total) } - const downloadPromise = checkResult.downloadPromise!! + const downloadPromise = checkResult?.downloadPromise await assertThat(downloadPromise).throws() expect(cancelled).toBe(true) })