-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
differentialUpdateTest.ts
244 lines (205 loc) · 9.38 KB
/
differentialUpdateTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { Arch, Configuration, Platform } from "app-builder-lib"
import { getBinFromUrl } from "app-builder-lib/out/binDownload"
import { doSpawn, getArchSuffix } from "builder-util"
import { GenericServerOptions, S3Options } from "builder-util-runtime"
import { AppImageUpdater, BaseUpdater, MacUpdater, NsisUpdater } from "electron-updater"
import { EventEmitter } from "events"
import { move } from "fs-extra"
import * as path from "path"
import { TmpDir } from "temp-file"
import { TestAppAdapter } from "../helpers/TestAppAdapter"
import { PackedContext, assertPack, removeUnstableProperties } from "../helpers/packTester"
import { tuneTestUpdater, writeUpdateConfig } from "../helpers/updaterTestUtil"
/*
rm -rf ~/Documents/onshape-desktop-shell/node_modules/electron-updater && cp -R ~/Documents/electron-builder/packages/electron-updater ~/Documents/onshape-desktop-shell/node_modules/electron-updater && rm -rf ~/Documents/onshape-desktop-shell/node_modules/electron-updater/src && rm -rf ~/Documents/onshape-desktop-shell/node_modules/builder-util-runtime && cp -R ~/Documents/electron-builder/packages/builder-util-runtime ~/Documents/onshape-desktop-shell/node_modules/builder-util-runtime && rm -rf ~/Documents/onshape-desktop-shell/node_modules/builder-util-runtime/src
*/
// %USERPROFILE%\AppData\Roaming\Onshape
// mkdir -p ~/minio-data/onshape
// minio server ~/minio-data
const OLD_VERSION_NUMBER = "1.0.0"
const testAppCacheDirName = "testapp-updater"
async function doBuild(outDirs: Array<string>, targets: Map<Platform, Map<Arch, Array<string>>>, tmpDir: TmpDir, isWindows: boolean, extraConfig?: Configuration | null) {
async function buildApp(
version: string,
targets: Map<Platform, Map<Arch, Array<string>>>,
extraConfig: Configuration | null | undefined,
packed: (context: PackedContext) => Promise<any>
) {
await assertPack(
"test-app-one",
{
targets,
config: {
extraMetadata: {
version,
},
...extraConfig,
compression: "normal",
publish: {
provider: "s3",
bucket: "develar",
path: "test",
},
},
},
{
signedWin: isWindows,
packed,
}
)
}
const build = (version: string) =>
buildApp(version, targets, extraConfig, async context => {
// move dist temporarily out of project dir so each downloader can reference it
const newDir = await tmpDir.getTempDir({ prefix: version })
await move(context.outDir, newDir)
outDirs.push(newDir)
})
try {
await build(OLD_VERSION_NUMBER)
await build("1.0.1")
} catch (e: any) {
await tmpDir.cleanup()
throw e
}
}
test.ifWindows("web installer", async () => {
const outDirs: Array<string> = []
const tmpDir = new TmpDir("differential-updater-test")
await doBuild(outDirs, Platform.WINDOWS.createTarget(["nsis-web"], Arch.x64), tmpDir, true)
const oldDir = outDirs[0]
await move(path.join(oldDir, "nsis-web", `TestApp-${OLD_VERSION_NUMBER}-x64.nsis.7z`), path.join(getTestUpdaterCacheDir(oldDir), testAppCacheDirName, "package.7z"))
await testBlockMap(outDirs[0], path.join(outDirs[1], "nsis-web"), NsisUpdater, Platform.WINDOWS, Arch.x64)
})
test.ifWindows("nsis", async () => {
const outDirs: Array<string> = []
const tmpDir = new TmpDir("differential-updater-test")
await doBuild(outDirs, Platform.WINDOWS.createTarget(["nsis"], Arch.x64), tmpDir, true)
const oldDir = outDirs[0]
// move to new dir so that localhost server can read both blockmaps
await move(path.join(oldDir, `Test App ßW Setup ${OLD_VERSION_NUMBER}.exe`), path.join(getTestUpdaterCacheDir(oldDir), testAppCacheDirName, "installer.exe"))
await move(path.join(oldDir, `Test App ßW Setup ${OLD_VERSION_NUMBER}.exe.blockmap`), path.join(outDirs[1], "Test App ßW Setup 1.0.0.exe.blockmap"))
await testBlockMap(outDirs[0], outDirs[1], NsisUpdater, Platform.WINDOWS, Arch.x64)
})
async function testLinux(arch: Arch) {
process.env.TEST_UPDATER_ARCH = Arch[arch]
const outDirs: Array<string> = []
const tmpDir = new TmpDir("differential-updater-test")
try {
await doBuild(outDirs, Platform.LINUX.createTarget(["appimage"], arch), tmpDir, false)
process.env.APPIMAGE = path.join(outDirs[0], `Test App ßW-${OLD_VERSION_NUMBER}${arch === Arch.ia32 ? "-i386" : ""}.AppImage`)
await testBlockMap(outDirs[0], outDirs[1], AppImageUpdater, Platform.LINUX, arch)
} finally {
await tmpDir.cleanup()
}
}
test.ifDevOrLinuxCi("AppImage", () => testLinux(Arch.x64))
test.ifDevOrLinuxCi("AppImage ia32", () => testLinux(Arch.ia32))
async function testMac(arch: Arch) {
process.env.TEST_UPDATER_ARCH = Arch[arch]
const outDirs: Array<string> = []
const tmpDir = new TmpDir("differential-updater-test")
try {
await doBuild(outDirs, Platform.MAC.createTarget(["zip"], arch), tmpDir, false, {
mac: {
electronUpdaterCompatibility: ">=2.17.0",
},
})
// move to new dir so that localhost server can read both blockmaps
const oldDir = outDirs[0]
const blockmap = `Test App ßW-${OLD_VERSION_NUMBER}${getArchSuffix(arch)}-mac.zip.blockmap`
await move(path.join(oldDir, blockmap), path.join(outDirs[1], blockmap))
await move(path.join(oldDir, `Test App ßW-${OLD_VERSION_NUMBER}${getArchSuffix(arch)}-mac.zip`), path.join(getTestUpdaterCacheDir(oldDir), testAppCacheDirName, "update.zip"))
await testBlockMap(outDirs[0], outDirs[1], MacUpdater, Platform.MAC, arch, "Test App ßW")
} finally {
await tmpDir.cleanup()
}
}
test.ifMac("Mac Intel", () => testMac(Arch.x64))
test.ifMac("Mac universal", () => testMac(Arch.universal))
// only run on arm64 macs, otherwise of course no files can be found to be updated to (due to arch mismatch)
test.ifMac.ifEnv(process.arch === "arm64")("Mac arm64", () => testMac(Arch.arm64))
async function checkResult(updater: BaseUpdater) {
// disable automatic install otherwise mac updater will permanently wait on mocked electron's native updater to receive update (mocked server can't install)
updater.autoInstallOnAppQuit = false
const updateCheckResult = await updater.checkForUpdates()
const downloadPromise = updateCheckResult?.downloadPromise
// noinspection JSIgnoredPromiseFromCall
expect(downloadPromise).not.toBeNull()
const files = await downloadPromise
const fileInfo: any = updateCheckResult?.updateInfo.files[0]
// delete url because port is random
expect(fileInfo.url).toBeDefined()
delete fileInfo.url
expect(removeUnstableProperties(updateCheckResult?.updateInfo)).toMatchSnapshot()
expect(files!.map(it => path.basename(it))).toMatchSnapshot()
}
class TestNativeUpdater extends EventEmitter {
checkForUpdates() {
console.log("TestNativeUpdater.checkForUpdates")
// MacUpdater expects this to emit corresponding update-downloaded event
this.emit("update-downloaded")
}
setFeedURL(updateConfig: any) {
console.log("TestNativeUpdater.setFeedURL " + updateConfig.url)
}
}
function getTestUpdaterCacheDir(oldDir: string) {
return path.join(oldDir, "updater-cache")
}
async function testBlockMap(oldDir: string, newDir: string, updaterClass: any, platform: Platform, arch: Arch, productFilename?: string) {
const appUpdateConfigPath = path.join(
`${platform.buildConfigurationKey}${getArchSuffix(arch)}${platform === Platform.MAC ? "" : "-unpacked"}`,
platform === Platform.MAC ? `${productFilename}.app` : ""
)
const port = 8000 + (updaterClass.name.charCodeAt(0) as number) + Math.floor(Math.random() * 10000)
const serverBin = await getBinFromUrl("ran", "0.1.3", "imfA3LtT6umMM0BuQ29MgO3CJ9uleN5zRBi3sXzcTbMOeYZ6SQeN7eKr3kXZikKnVOIwbH+DDO43wkiR/qTdkg==")
const httpServerProcess = doSpawn(path.join(serverBin, process.platform, "ran"), [`-root=${newDir}`, `-port=${port}`, "-gzip=false", "-listdir=true"])
// Mac uses electron's native autoUpdater to serve updates to, we mock here since electron API isn't available within jest runtime
const mockNativeUpdater = new TestNativeUpdater()
jest.mock(
"electron",
() => {
return {
autoUpdater: mockNativeUpdater,
}
},
{ virtual: true }
)
return await new Promise<void>((resolve, reject) => {
httpServerProcess.on("error", reject)
const updater = new updaterClass(null, new TestAppAdapter(OLD_VERSION_NUMBER, getTestUpdaterCacheDir(oldDir)))
updater._appUpdateConfigPath = path.join(
oldDir,
updaterClass === MacUpdater ? `${appUpdateConfigPath}/Contents/Resources` : `${appUpdateConfigPath}/resources`,
"app-update.yml"
)
const doTest = async () => {
tuneTestUpdater(updater, {
platform: platform.nodeName as any,
isUseDifferentialDownload: true,
})
updater.logger = console
const currentUpdaterCacheDirName = (await updater.configOnDisk.value).updaterCacheDirName
if (currentUpdaterCacheDirName == null) {
throw new Error(`currentUpdaterCacheDirName must be not null, appUpdateConfigPath: ${updater._appUpdateConfigPath}`)
}
updater.updateConfigPath = await writeUpdateConfig<GenericServerOptions | S3Options>({
provider: "generic",
updaterCacheDirName: currentUpdaterCacheDirName,
url: `http://127.0.0.1:${port}`,
})
await checkResult(updater)
}
doTest().then(resolve).catch(reject)
}).then(
v => {
httpServerProcess.kill()
return v
},
e => {
httpServerProcess.kill()
throw e
}
)
}