|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { tmpdir } from 'os'; |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | +import { ensureExecutable } from './permissions.js'; |
| 6 | + |
| 7 | +function makeTempBinary(mode: number): { dir: string; path: string } { |
| 8 | + const dir = fs.mkdtempSync(join(tmpdir(), 'occ-permissions-')); |
| 9 | + const path = join(dir, 'occ'); |
| 10 | + fs.writeFileSync(path, '#!/usr/bin/env sh\necho ok\n', 'utf8'); |
| 11 | + fs.chmodSync(path, mode); |
| 12 | + return { dir, path }; |
| 13 | +} |
| 14 | + |
| 15 | +describe('ensureExecutable', () => { |
| 16 | + it('sets execute permissions when the file is not executable', () => { |
| 17 | + const tmp = makeTempBinary(0o644); |
| 18 | + |
| 19 | + ensureExecutable(tmp.path); |
| 20 | + |
| 21 | + const mode = fs.statSync(tmp.path).mode & 0o777; |
| 22 | + expect(mode & 0o111).not.toBe(0); |
| 23 | + |
| 24 | + fs.rmSync(tmp.dir, { recursive: true, force: true }); |
| 25 | + }); |
| 26 | + |
| 27 | + it('keeps executable files executable', () => { |
| 28 | + const tmp = makeTempBinary(0o755); |
| 29 | + |
| 30 | + ensureExecutable(tmp.path); |
| 31 | + |
| 32 | + const mode = fs.statSync(tmp.path).mode & 0o777; |
| 33 | + expect(mode & 0o111).not.toBe(0); |
| 34 | + |
| 35 | + fs.rmSync(tmp.dir, { recursive: true, force: true }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('does not throw when chmod fails', () => { |
| 39 | + const tmp = makeTempBinary(0o644); |
| 40 | + const throwingFs = { |
| 41 | + statSync: (filePath: string) => fs.statSync(filePath), |
| 42 | + chmodSync: () => { |
| 43 | + throw new Error('chmod failed'); |
| 44 | + }, |
| 45 | + }; |
| 46 | + |
| 47 | + expect(() => ensureExecutable(tmp.path, throwingFs)).not.toThrow(); |
| 48 | + const mode = fs.statSync(tmp.path).mode & 0o777; |
| 49 | + expect(mode & 0o111).toBe(0); |
| 50 | + |
| 51 | + fs.rmSync(tmp.dir, { recursive: true, force: true }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments