Skip to content

Commit

Permalink
refactor(electron-updater): get rid of global __test_resourcesPath
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Jan 30, 2017
1 parent 1152f2b commit eee3b71
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 19 additions & 11 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Provider<any>>
private clientPromise: Promise<Provider<any>> | null

private readonly untilAppReady: Promise<boolean>
private checkForUpdatesPromise: Promise<UpdateCheckResult> | null
Expand Down Expand Up @@ -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<UpdateCheckResult> {
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()
Expand Down Expand Up @@ -225,10 +230,13 @@ export abstract class AppUpdater extends EventEmitter {
protected async abstract doDownloadUpdate(versionInfo: VersionInfo, fileInfo: FileInfo): Promise<any>

abstract quitAndInstall(): void
}

async function loadUpdateConfig() {
return safeLoad(await readFile(path.join((<any>global).__test_resourcesPath || (<any>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) {
Expand Down
69 changes: 28 additions & 41 deletions test/src/nsisUpdaterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ 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", () => {
console.warn("[SKIP] Skip ArtifactPublisherTest suite — ELECTRON_BUILDER_OFFLINE is defined")
})
}

const tmpDir = new TmpDir()

const g = (<any>global)
g.__test_app = {
getVersion: function () {
Expand Down Expand Up @@ -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(<BintrayOptions>{
provider: "bintray",
owner: "actperepo",
package: "TestApp",
}))
g.__test_resourcesPath = testResourcesPath
const updater = new NsisUpdater()

const actualEvents: Array<string> = []
const expectedEvents = ["checking-for-update", "update-available", "update-downloaded"]
Expand All @@ -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(<GenericServerOptions>{
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
g.__test_resourcesPath = testResourcesPath
const updater = new NsisUpdater()

const actualEvents = trackEvents(updater)

Expand All @@ -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(<GenericServerOptions>{
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
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)
Expand All @@ -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(<GenericServerOptions>{
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
g.__test_resourcesPath = testResourcesPath
const updater = new NsisUpdater()
updater.autoDownload = false

const actualEvents = trackEvents(updater)
Expand All @@ -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(<GenericServerOptions>{
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
g.__test_resourcesPath = testResourcesPath
const updater: NsisUpdater = new NsisUpdater()

const actualEvents = trackEvents(updater)

Expand All @@ -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(<GithubOptions>{
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(<GithubOptions>{
provider: "github",
owner: "develar",
repo: "__test_nsis_release",
}))

const actualEvents: Array<string> = []
const expectedEvents = ["checking-for-update", "update-available", "update-downloaded"]
Expand All @@ -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)
Expand All @@ -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(<GenericServerOptions>{
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<any> = []
Expand All @@ -219,6 +199,13 @@ test("test download progress", async () => {
expect(lastEvent.transferred).toBe(lastEvent.total)
})


async function writeUpdateConfig(data: GenericServerOptions | GithubOptions | BintrayOptions): Promise<string> {
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<string> = []
for (const eventName of ["checking-for-update", "update-available", "update-downloaded", "error"]) {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit eee3b71

Please sign in to comment.