|
| 1 | +import { execFileSync } from 'node:child_process' |
| 2 | +import fs from 'node:fs' |
| 3 | +import os from 'node:os' |
| 4 | +import path from 'node:path' |
| 5 | + |
| 6 | +import { afterEach, beforeEach, describe, expect, it, jest, test } from '@jest/globals' |
| 7 | + |
| 8 | +const globalWarn = jest.fn() |
| 9 | +jest.unstable_mockModule('@pnpm/logger', () => ({ globalWarn })) |
| 10 | + |
| 11 | +const { isNativeBinary, removeQuarantine } = await import('../src/removeQuarantine.js') |
| 12 | + |
| 13 | +// Quarantine xattrs only exist on macOS, so these tests are scoped to it. |
| 14 | +const describeOnMacOS = process.platform === 'darwin' ? describe : describe.skip |
| 15 | + |
| 16 | +const QUARANTINE_ATTR = 'com.apple.quarantine' |
| 17 | + |
| 18 | +function setQuarantine (filePath: string): void { |
| 19 | + execFileSync('/usr/bin/xattr', ['-w', QUARANTINE_ATTR, '0083;00000000;TestApp;', filePath]) |
| 20 | +} |
| 21 | + |
| 22 | +function listXattrs (filePath: string): string { |
| 23 | + return execFileSync('/usr/bin/xattr', ['-l', filePath], { encoding: 'utf8' }) |
| 24 | +} |
| 25 | + |
| 26 | +function hasQuarantine (filePath: string): boolean { |
| 27 | + return listXattrs(filePath).includes(QUARANTINE_ATTR) |
| 28 | +} |
| 29 | + |
| 30 | +test('isNativeBinary matches only native binary extensions handled on macOS', () => { |
| 31 | + expect(isNativeBinary('rollup.darwin-arm64.node')).toBe(true) |
| 32 | + expect(isNativeBinary('addon.DYLIB')).toBe(true) |
| 33 | + expect(isNativeBinary('addon.so')).toBe(true) |
| 34 | + expect(isNativeBinary('index.js')).toBe(false) |
| 35 | + expect(isNativeBinary('package.json')).toBe(false) |
| 36 | + expect(isNativeBinary('README')).toBe(false) |
| 37 | + // .dll is Windows-only and never relevant on macOS. |
| 38 | + expect(isNativeBinary('addon.dll')).toBe(false) |
| 39 | +}) |
| 40 | + |
| 41 | +describeOnMacOS('removeQuarantine', () => { |
| 42 | + let testDir: string |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + globalWarn.mockClear() |
| 46 | + testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'quarantine-test-')) |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + fs.rmSync(testDir, { recursive: true, force: true }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('removes the quarantine xattr from a file', () => { |
| 54 | + const file = path.join(testDir, 'addon.node') |
| 55 | + fs.writeFileSync(file, 'test content') |
| 56 | + setQuarantine(file) |
| 57 | + expect(hasQuarantine(file)).toBe(true) |
| 58 | + |
| 59 | + removeQuarantine([file]) |
| 60 | + |
| 61 | + expect(hasQuarantine(file)).toBe(false) |
| 62 | + expect(globalWarn).not.toHaveBeenCalled() |
| 63 | + }) |
| 64 | + |
| 65 | + it('does nothing when the quarantine xattr is absent', () => { |
| 66 | + const file = path.join(testDir, 'addon.node') |
| 67 | + fs.writeFileSync(file, 'test content') |
| 68 | + expect(hasQuarantine(file)).toBe(false) |
| 69 | + |
| 70 | + expect(() => removeQuarantine([file])).not.toThrow() |
| 71 | + expect(globalWarn).not.toHaveBeenCalled() |
| 72 | + }) |
| 73 | + |
| 74 | + it('removes quarantine from a batch of files while preserving other xattrs', () => { |
| 75 | + const quarantined = path.join(testDir, 'a.node') |
| 76 | + const clean = path.join(testDir, 'b.node') |
| 77 | + fs.writeFileSync(quarantined, 'a') |
| 78 | + fs.writeFileSync(clean, 'b') |
| 79 | + setQuarantine(quarantined) |
| 80 | + execFileSync('/usr/bin/xattr', ['-w', 'com.example.custom', 'keep', quarantined]) |
| 81 | + |
| 82 | + removeQuarantine([quarantined, clean]) |
| 83 | + |
| 84 | + expect(hasQuarantine(quarantined)).toBe(false) |
| 85 | + expect(listXattrs(quarantined)).toContain('com.example.custom') |
| 86 | + expect(globalWarn).not.toHaveBeenCalled() |
| 87 | + }) |
| 88 | + |
| 89 | + it('does not warn for missing files (dropped/renamed by the importer)', () => { |
| 90 | + const quarantined = path.join(testDir, 'real.node') |
| 91 | + const missing = path.join(testDir, 'dropped.node') |
| 92 | + fs.writeFileSync(quarantined, 'a') |
| 93 | + setQuarantine(quarantined) |
| 94 | + |
| 95 | + removeQuarantine([missing, quarantined]) |
| 96 | + |
| 97 | + expect(hasQuarantine(quarantined)).toBe(false) |
| 98 | + expect(globalWarn).not.toHaveBeenCalled() |
| 99 | + }) |
| 100 | + |
| 101 | + it('does not throw when given an empty list', () => { |
| 102 | + expect(() => removeQuarantine([])).not.toThrow() |
| 103 | + }) |
| 104 | +}) |
0 commit comments