Skip to content

Commit eec627b

Browse files
committed
better assertions
1 parent 0510a72 commit eec627b

File tree

3 files changed

+54
-42
lines changed

3 files changed

+54
-42
lines changed

packages/compass-smoke-tests/src/installers/mac-dmg.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import path from 'node:path';
22
import fs from 'node:fs';
3+
import {
4+
assertFileNotQuarantined,
5+
removeApplicationSupportForApp,
6+
} from './mac-utils';
37

48
import type { InstalledAppInfo, InstallablePackage } from './types';
59
import { execute } from '../execute';
@@ -24,27 +28,9 @@ export function installMacDMG({
2428
execute('hdiutil', ['detach', volumePath]);
2529
}
2630

27-
// TODO: Consider instrumenting the app to use a settings directory in the sandbox
28-
// TODO: Move this somewhere shared between mac installers
29-
if (process.env.HOME) {
30-
const settingsDir = path.resolve(
31-
process.env.HOME,
32-
'Library',
33-
'Application Support',
34-
appName
35-
);
31+
removeApplicationSupportForApp(appName);
3632

37-
if (fs.existsSync(settingsDir)) {
38-
console.log(`${settingsDir} already exists. Removing.`);
39-
fs.rmSync(settingsDir, { recursive: true });
40-
}
41-
}
42-
43-
execute('xattr', ['-dr', 'com.apple.quarantine', appPath]);
44-
45-
// see if the executable will run without being quarantined or similar
46-
// TODO: Move this somewhere shared between mac installers
47-
execute(path.resolve(appPath, 'Contents/MacOS', appName), ['--version']);
33+
assertFileNotQuarantined(appPath);
4834

4935
return {
5036
appPath: appPath,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import assert from 'node:assert/strict';
2+
import { execSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
import path from 'node:path';
5+
6+
// TODO: Consider instrumenting the app to use a settings directory in the sandbox
7+
export function removeApplicationSupportForApp(appName: string) {
8+
assert(typeof process.env.HOME === 'string', 'HOME env var not set');
9+
10+
const settingsDir = path.resolve(
11+
process.env.HOME,
12+
'Library',
13+
'Application Support',
14+
appName
15+
);
16+
17+
if (fs.existsSync(settingsDir)) {
18+
console.log(`${settingsDir} already exists. Removing.`);
19+
fs.rmSync(settingsDir, { recursive: true });
20+
}
21+
}
22+
23+
export function assertFileNotQuarantined(appPath: string) {
24+
try {
25+
execSync(`xattr -p com.apple.quarantine "${appPath}"`, {
26+
encoding: 'utf8',
27+
});
28+
assert.fail(`Expected no com.apple.quarantine attr on ${appPath}`);
29+
} catch (err: unknown) {
30+
assert(
31+
typeof err === 'object' &&
32+
err !== null &&
33+
'message' in err &&
34+
typeof err.message === 'string',
35+
'Expected err to be an error'
36+
);
37+
assert(
38+
/No such xattr/.test(err.message),
39+
`Expected no com.apple.quarantine attr on ${appPath}`
40+
);
41+
}
42+
}

packages/compass-smoke-tests/src/installers/mac-zip.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from 'node:path';
2-
import fs from 'node:fs';
3-
2+
import {
3+
assertFileNotQuarantined,
4+
removeApplicationSupportForApp,
5+
} from './mac-utils';
46
import type { InstalledAppInfo, InstallablePackage } from './types';
57
import { execute } from '../execute';
68

@@ -14,27 +16,9 @@ export function installMacZIP({
1416

1517
execute('ditto', ['-xk', filepath, destinationPath]);
1618

17-
// TODO: Consider instrumenting the app to use a settings directory in the sandbox
18-
// TODO: Move this somewhere shared between mac installers
19-
if (process.env.HOME) {
20-
const settingsDir = path.resolve(
21-
process.env.HOME,
22-
'Library',
23-
'Application Support',
24-
appName
25-
);
26-
27-
if (fs.existsSync(settingsDir)) {
28-
console.log(`${settingsDir} already exists. Removing.`);
29-
fs.rmSync(settingsDir, { recursive: true });
30-
}
31-
}
32-
33-
execute('xattr', ['-dr', 'com.apple.quarantine', appPath]);
19+
removeApplicationSupportForApp(appName);
3420

35-
// see if the executable will run without being quarantined or similar
36-
// TODO: Move this somewhere shared between mac installers
37-
execute(path.resolve(appPath, 'Contents/MacOS', appName), ['--version']);
21+
assertFileNotQuarantined(appPath);
3822

3923
return {
4024
appPath: appPath,

0 commit comments

Comments
 (0)