|
| 1 | +/** |
| 2 | + * Tests for npm CLI install helpers. |
| 3 | + */ |
| 4 | + |
| 5 | +const { expect } = require('chai'); |
| 6 | +const fs = require('fs'); |
| 7 | +const os = require('os'); |
| 8 | +const path = require('path'); |
| 9 | +const { spawnSync } = require('child_process'); |
| 10 | + |
| 11 | +const ROOT = path.join(__dirname, '..'); |
| 12 | + |
| 13 | +function runNodeInline(jsCode) { |
| 14 | + return spawnSync(process.execPath, ['-e', jsCode], { |
| 15 | + cwd: ROOT, |
| 16 | + encoding: 'utf8', |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +describe('CLI installer scripts', function () { |
| 21 | + this.timeout(15000); |
| 22 | + |
| 23 | + it('install-cli exits successfully on unsupported platforms', () => { |
| 24 | + const result = runNodeInline( |
| 25 | + "const os=require('os'); os.platform=()=> 'freebsd'; os.arch=()=> 'x64'; require('./scripts/install-cli.js');" |
| 26 | + ); |
| 27 | + |
| 28 | + expect(result.status).to.equal(0); |
| 29 | + expect(result.stdout).to.include('No prebuilt CLI binary for freebsd-x64'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('install-cli exits successfully when download fails', () => { |
| 33 | + const result = runNodeInline( |
| 34 | + "const os=require('os'); os.platform=()=> 'darwin'; os.arch=()=> 'arm64'; const https=require('https'); const {EventEmitter}=require('events'); https.get=()=>{const req=new EventEmitter(); process.nextTick(()=>req.emit('error', new Error('simulated-download-failure'))); return req;}; require('./scripts/install-cli.js');" |
| 35 | + ); |
| 36 | + |
| 37 | + expect(result.status).to.equal(0); |
| 38 | + expect(result.stdout).to.include('Could not install CLI binary: simulated-download-failure'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('bin shim forwards arguments to a local binary when present', () => { |
| 42 | + if (process.platform === 'win32') { |
| 43 | + this.skip(); |
| 44 | + } |
| 45 | + |
| 46 | + const binName = process.platform === 'win32' ? 'jacs-cli.exe' : 'jacs-cli'; |
| 47 | + const binPath = path.join(ROOT, 'bin', binName); |
| 48 | + |
| 49 | + fs.mkdirSync(path.dirname(binPath), { recursive: true }); |
| 50 | + fs.writeFileSync(binPath, '#!/usr/bin/env bash\necho shim-ok \"$@\"\n', { mode: 0o755 }); |
| 51 | + |
| 52 | + try { |
| 53 | + const result = spawnSync(process.execPath, ['bin/jacs-cli.js', 'hello', 'world'], { |
| 54 | + cwd: ROOT, |
| 55 | + encoding: 'utf8', |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result.status).to.equal(0); |
| 59 | + expect(result.stdout).to.include('shim-ok hello world'); |
| 60 | + } finally { |
| 61 | + try { |
| 62 | + fs.unlinkSync(binPath); |
| 63 | + } catch (_) { |
| 64 | + // no-op cleanup |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | +}); |
0 commit comments