-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathtest.js
More file actions
180 lines (134 loc) · 5.67 KB
/
test.js
File metadata and controls
180 lines (134 loc) · 5.67 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
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
import path from 'node:path';
import fs from 'node:fs';
import process from 'node:process';
import {spawnSync} from 'node:child_process';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {execa} from 'execa';
import {temporaryDirectory} from 'tempy';
import jestImageSnapshot from 'jest-image-snapshot';
const {configureToMatchImageSnapshot} = jestImageSnapshot;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const runCreateDmg = async (args, cwd) => {
try {
await execa(path.join(__dirname, 'cli.js'), args, {cwd});
} catch (error) {
// Silence code signing failure
if (!error.message.includes('Code signing failed')) {
throw error;
}
}
};
const getDmgPath = (cwd, name, version) => path.join(cwd, version ? `${name} ${version}.dmg` : `${name}.dmg`);
/**
Validate that the icon for the DMG matches the snapshot (with a small tolerance to avoid flakiness).
Set `UPDATE_SNAPSHOT=true` environment variable to update snapshots.
*/
const assertVolumeIconMatchesSnapshot = async (t, dmgPath) => {
const existingVolumes = new Set(fs.readdirSync('/Volumes'));
const mountResult = spawnSync('hdiutil', ['mount', dmgPath], {timeout: 10_000});
if (mountResult.status !== 0) {
throw new Error(`Failed to mount DMG: ${mountResult.stderr?.toString()}`);
}
const volumes = new Set(fs.readdirSync('/Volumes'));
const mountLocation = [...volumes].find(volume => !existingVolumes.has(volume));
if (!mountLocation) {
throw new Error('Failed to determine DMG mount location.');
}
const mountPath = path.join('/Volumes', mountLocation);
const dirPath = path.dirname(dmgPath);
const iconPath = path.join(dirPath, 'VolumeIcon.icns');
const pngPath = path.join(dirPath, 'VolumeIcon.png');
try {
const dmgIconPath = path.join(mountPath, '.VolumeIcon.icns');
if (!fs.existsSync(dmgIconPath)) {
throw new Error(`Volume icon not found at ${dmgIconPath}`);
}
fs.copyFileSync(dmgIconPath, iconPath);
const sipsResult = spawnSync('sips', ['-s', 'format', 'png', iconPath, '--out', pngPath], {timeout: 10_000});
if (sipsResult.status !== 0) {
throw new Error(`Failed to convert icon to PNG: ${sipsResult.stderr?.toString()}`);
}
const image = fs.readFileSync(pngPath);
// Jest-image-snapshot requires a Jest-like context object.
// We mock the minimum required interface to make it work with AVA.
const jestContext = {
testPath: fileURLToPath(import.meta.url),
currentTestName: t.title,
snapshotState: {
_counters: new Map(),
_updateSnapshot: process.env.UPDATE_SNAPSHOT === 'true' ? 'all' : 'new',
updated: 0,
added: 0,
},
};
const result = configureToMatchImageSnapshot({
failureThreshold: 0.01,
failureThresholdType: 'percent',
}).call(jestContext, image);
if (result.pass) {
t.pass();
} else {
t.fail(result.message());
}
} finally {
// Clean up temp files
for (const filePath of [iconPath, pngPath]) {
try {
fs.unlinkSync(filePath);
} catch {}
}
const unmountResult = spawnSync('hdiutil', ['unmount', '-force', mountPath], {timeout: 10_000});
if (unmountResult.status !== 0) {
console.error(`Failed to unmount ${mountLocation}: ${unmountResult.stderr?.toString()}`);
}
}
};
test('main', async t => {
const cwd = temporaryDirectory();
await runCreateDmg(['--identity=0', path.join(__dirname, 'fixtures/Fixture.app')], cwd);
const dmgPath = getDmgPath(cwd, 'Fixture', '0.0.1');
t.true(fs.existsSync(dmgPath));
await assertVolumeIconMatchesSnapshot(t, dmgPath);
});
test('binary plist', async t => {
const cwd = temporaryDirectory();
await runCreateDmg(['--identity=0', path.join(__dirname, 'fixtures/Fixture-with-binary-plist.app')], cwd);
const dmgPath = getDmgPath(cwd, 'Fixture', '0.0.1');
t.true(fs.existsSync(dmgPath));
await assertVolumeIconMatchesSnapshot(t, dmgPath);
});
test('app without icon', async t => {
const cwd = temporaryDirectory();
await runCreateDmg(['--identity=0', path.join(__dirname, 'fixtures/Fixture-no-icon.app')], cwd);
t.true(fs.existsSync(getDmgPath(cwd, 'Fixture', '0.0.1')));
});
test('--no-version-in-filename flag', async t => {
const cwd = temporaryDirectory();
await runCreateDmg(['--identity=0', '--no-version-in-filename', path.join(__dirname, 'fixtures/Fixture.app')], cwd);
t.true(fs.existsSync(getDmgPath(cwd, 'Fixture')));
t.false(fs.existsSync(getDmgPath(cwd, 'Fixture', '0.0.1')));
});
test('--no-code-sign flag', async t => {
const cwd = temporaryDirectory();
// This should succeed without any code signing errors
await execa(path.join(__dirname, 'cli.js'), ['--no-code-sign', path.join(__dirname, 'fixtures/Fixture.app')], {cwd});
t.true(fs.existsSync(getDmgPath(cwd, 'Fixture', '0.0.1')));
});
test('app with missing icon file', async t => {
const cwd = temporaryDirectory();
await runCreateDmg(['--identity=0', path.join(__dirname, 'fixtures/Fixture-missing-icon.app')], cwd);
t.true(fs.existsSync(getDmgPath(cwd, 'Fixture', '0.0.1')));
});
test('license agreement with txt', async t => {
const cwd = temporaryDirectory();
fs.writeFileSync(path.join(cwd, 'license.txt'), 'This is a test license agreement.\n\nYou must agree to these terms.');
await runCreateDmg(['--identity=0', path.join(__dirname, 'fixtures/Fixture.app')], cwd);
t.true(fs.existsSync(getDmgPath(cwd, 'Fixture', '0.0.1')));
});
test('license agreement with rtf', async t => {
const cwd = temporaryDirectory();
fs.writeFileSync(path.join(cwd, 'license.rtf'), '{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Times New Roman;}}\\f0\\fs24 This is a test license agreement.\\par\\par You must agree to these terms.}');
await runCreateDmg(['--identity=0', path.join(__dirname, 'fixtures/Fixture.app')], cwd);
t.true(fs.existsSync(getDmgPath(cwd, 'Fixture', '0.0.1')));
});