|
| 1 | +import { expect } from 'chai' |
| 2 | +import fs from 'fs' |
| 3 | +import os from 'os' |
| 4 | +import path from 'path' |
| 5 | +import { spawnSync } from 'child_process' |
| 6 | + |
| 7 | +const projectRoot = process.cwd() |
| 8 | +const installScriptPath = path.join(projectRoot, '.husky', 'install.mjs') |
| 9 | + |
| 10 | +const runInstall = (cwd: string, env: NodeJS.ProcessEnv = {}) => { |
| 11 | + return spawnSync('node', [installScriptPath], { |
| 12 | + cwd, |
| 13 | + env: { |
| 14 | + ...process.env, |
| 15 | + ...env, |
| 16 | + }, |
| 17 | + encoding: 'utf-8', |
| 18 | + timeout: 10_000, |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +describe('husky install script', () => { |
| 23 | + it('exits successfully when .git is missing', () => { |
| 24 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-no-git-')) |
| 25 | + |
| 26 | + try { |
| 27 | + const result = runInstall(tmpDir) |
| 28 | + expect(result.status).to.equal(0) |
| 29 | + } finally { |
| 30 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + it('exits successfully when HUSKY is disabled', () => { |
| 35 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-disabled-')) |
| 36 | + |
| 37 | + try { |
| 38 | + const result = runInstall(tmpDir, { HUSKY: '0' }) |
| 39 | + expect(result.status).to.equal(0) |
| 40 | + } finally { |
| 41 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + it('exits successfully when husky package is unavailable even if .git exists', function () { |
| 46 | + this.timeout(15_000) |
| 47 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-husky-missing-package-')) |
| 48 | + |
| 49 | + try { |
| 50 | + fs.mkdirSync(path.join(tmpDir, '.git')) |
| 51 | + const result = runInstall(tmpDir) |
| 52 | + expect(result.status).to.equal(0) |
| 53 | + } finally { |
| 54 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 55 | + } |
| 56 | + }) |
| 57 | +}) |
0 commit comments