-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign-win-artifacts.test.ts
More file actions
136 lines (119 loc) · 4.82 KB
/
Copy pathsign-win-artifacts.test.ts
File metadata and controls
136 lines (119 loc) · 4.82 KB
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
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createRequire } from 'node:module';
import { afterEach, describe, expect, it } from 'vitest';
const require = createRequire(import.meta.url);
const {
createSha512Base64,
updateLatestYmlForReferencedInstaller,
updateLatestYmlForSignedInstaller
} = require('../../scripts/sign-win-artifacts.cjs') as {
createSha512Base64(filePath: string): string;
updateLatestYmlForReferencedInstaller(latestPath: string): void;
updateLatestYmlForSignedInstaller(installerPath: string): void;
};
const tempDirs: string[] = [];
afterEach(() => {
for (const tempDir of tempDirs.splice(0)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
describe('sign-win-artifacts updater metadata', () => {
it('updates latest.yml checksums after installer bytes change', () => {
const distDir = createTempDir();
const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe');
const latestPath = path.join(distDir, 'latest.yml');
fs.writeFileSync(installerPath, 'unsigned installer bytes');
fs.writeFileSync(
latestPath,
[
'version: 0.1.1',
'files:',
' - url: Switchify-PC-Setup-0.1.1-x64.exe',
' sha512: old-file-checksum',
'path: Switchify-PC-Setup-0.1.1-x64.exe',
'sha512: old-path-checksum'
].join('\n')
);
fs.writeFileSync(installerPath, 'signed installer bytes');
updateLatestYmlForSignedInstaller(installerPath);
const sha512 = createSha512Base64(installerPath);
const latest = fs.readFileSync(latestPath, 'utf8');
expect(latest.match(/^sha512: .+$/gm)).toEqual([`sha512: ${sha512}`]);
expect(latest.match(/^\s+sha512: .+$/gm)).toEqual([` sha512: ${sha512}`]);
});
it('ignores latest.yml when it does not reference the signed installer', () => {
const distDir = createTempDir();
const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe');
fs.writeFileSync(installerPath, 'signed installer bytes');
fs.writeFileSync(
path.join(distDir, 'latest.yml'),
[
'version: 0.1.1',
'files:',
' - url: Other-Setup.exe',
' sha512: old-file-checksum',
'path: Other-Setup.exe',
'sha512: old-path-checksum'
].join('\n')
);
expect(() => updateLatestYmlForSignedInstaller(installerPath)).not.toThrow();
expect(fs.readFileSync(path.join(distDir, 'latest.yml'), 'utf8')).toContain('old-file-checksum');
});
it('updates latest.yml when the installer name is URL-encoded', () => {
const distDir = createTempDir();
const installerPath = path.join(distDir, 'Switchify PC Setup 0.1.1 x64.exe');
const latestPath = path.join(distDir, 'latest.yml');
fs.writeFileSync(installerPath, 'signed installer bytes');
fs.writeFileSync(
latestPath,
[
'version: 0.1.1',
'files:',
' - url: Switchify%20PC%20Setup%200.1.1%20x64.exe',
' sha512: old-file-checksum',
'path: Switchify%20PC%20Setup%200.1.1%20x64.exe',
'sha512: old-path-checksum'
].join('\n')
);
updateLatestYmlForSignedInstaller(installerPath);
const sha512 = createSha512Base64(installerPath);
const latest = fs.readFileSync(latestPath, 'utf8');
expect(latest.match(/^sha512: .+$/gm)).toEqual([`sha512: ${sha512}`]);
expect(latest.match(/^\s+sha512: .+$/gm)).toEqual([` sha512: ${sha512}`]);
});
it('does nothing when latest.yml is missing', () => {
const distDir = createTempDir();
const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe');
fs.writeFileSync(installerPath, 'signed installer bytes');
expect(() => updateLatestYmlForSignedInstaller(installerPath)).not.toThrow();
});
it('updates the installer referenced by latest.yml', () => {
const distDir = createTempDir();
const installerPath = path.join(distDir, 'Switchify-PC-Setup-0.1.1-x64.exe');
const latestPath = path.join(distDir, 'latest.yml');
fs.writeFileSync(installerPath, 'signed installer bytes');
fs.writeFileSync(
latestPath,
[
'version: 0.1.1',
'files:',
' - url: Switchify-PC-Setup-0.1.1-x64.exe',
' sha512: old-file-checksum',
'path: Switchify-PC-Setup-0.1.1-x64.exe',
'sha512: old-path-checksum'
].join('\n')
);
updateLatestYmlForReferencedInstaller(latestPath);
const sha512 = createSha512Base64(installerPath);
const latest = fs.readFileSync(latestPath, 'utf8');
expect(latest.match(/^sha512: .+$/gm)).toEqual([`sha512: ${sha512}`]);
expect(latest.match(/^\s+sha512: .+$/gm)).toEqual([` sha512: ${sha512}`]);
});
});
function createTempDir(): string {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'switchify-sign-test-'));
tempDirs.push(tempDir);
return tempDir;
}