|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +const mode = process.argv[2]; |
| 7 | +const home = path.resolve('shell-guidance/user'); |
| 8 | +const shellBin = path.resolve('shell-guidance/bin'); |
| 9 | +const binary = path.join(process.env.VP_HOME, 'current/bin/vp'); |
| 10 | +const env = { ...process.env, HOME: home, ZDOTDIR: home, PATH: shellBin }; |
| 11 | +delete env.VP_SHELL; |
| 12 | +delete env.SHELL; |
| 13 | +fs.mkdirSync(home, { recursive: true }); |
| 14 | +fs.mkdirSync(shellBin, { recursive: true }); |
| 15 | + |
| 16 | +/** @returns {string} */ |
| 17 | +function capture(args, extra = {}) { |
| 18 | + const result = spawnSync(binary, args, { |
| 19 | + env: { ...env, ...extra }, |
| 20 | + encoding: 'utf8', |
| 21 | + timeout: 30000, |
| 22 | + }); |
| 23 | + assert.equal(result.status, 0, result.error?.message ?? result.stdout + result.stderr); |
| 24 | + return result.stdout.replace(/\u001b\[[0-9;]*m/g, '').replaceAll('\r\n', '\n'); |
| 25 | +} |
| 26 | + |
| 27 | +const dirs = Object.fromEntries( |
| 28 | + capture([], { VP_DUMP_DIRS: '1' }) |
| 29 | + .trim() |
| 30 | + .split('\n') |
| 31 | + .map((line) => line.split('\t')), |
| 32 | +); |
| 33 | + |
| 34 | +/** @returns {string} */ |
| 35 | +function setup() { |
| 36 | + const output = capture(['env', 'setup']); |
| 37 | + const heading = output.indexOf('Next Steps:\n'); |
| 38 | + assert.ok(heading >= 0, output); |
| 39 | + const instructions = output.slice(heading).trimEnd(); |
| 40 | + assert.doesNotMatch(instructions, /Or open a new terminal/); |
| 41 | + console.log(instructions); |
| 42 | + return instructions; |
| 43 | +} |
| 44 | + |
| 45 | +if (mode === 'hints') { |
| 46 | + // Even matching profiles must not turn a login-shell hint into a current-shell guarantee. |
| 47 | + for (const profile of ['.zshrc', '.bashrc']) { |
| 48 | + fs.writeFileSync(path.join(home, profile), `. "${dirs.config}/env"\n`); |
| 49 | + } |
| 50 | + for (const [shell, label, file] of [ |
| 51 | + ['zsh', 'Zsh', 'env'], |
| 52 | + ['bash', 'Bash', 'env'], |
| 53 | + ['sh', 'sh', 'env'], |
| 54 | + ['fish', 'Fish', 'env.fish'], |
| 55 | + ['nu', 'Nushell', 'env.nu'], |
| 56 | + ['pwsh', 'PowerShell', 'env.ps1'], |
| 57 | + ]) { |
| 58 | + env.SHELL = `/usr/bin/${shell}`; |
| 59 | + console.log(`SHELL=${env.SHELL}, VP_SHELL=<unset>:`); |
| 60 | + const output = setup(); |
| 61 | + assert.ok(output.includes(`For ${label}, run:`)); |
| 62 | + assert.ok(output.includes(path.join(dirs.config, file))); |
| 63 | + assert.equal(output.split('\n').filter((line) => line.includes(dirs.config)).length, 1); |
| 64 | + assert.doesNotMatch(output, /Activate Vite\+ in this terminal/); |
| 65 | + } |
| 66 | + env.SHELL = '/bin/zsh'; |
| 67 | + env.VP_SHELL = 'unrecognized'; |
| 68 | + console.log('SHELL=/bin/zsh, VP_SHELL=unrecognized:'); |
| 69 | + assert.match(setup(), /For Zsh, run:/); |
| 70 | + env.VP_SHELL = 'fish'; |
| 71 | + console.log('SHELL=/bin/zsh, VP_SHELL=fish (Fish is not on PATH):'); |
| 72 | + const output = setup(); |
| 73 | + assert.match(output, /Activate Vite\+ in this terminal:/); |
| 74 | + assert.match(output, /env\.fish/); |
| 75 | + assert.doesNotMatch(output, /For Zsh|env\.ps1|env\.nu/); |
| 76 | +} else { |
| 77 | + assert.equal(mode, 'available'); |
| 78 | + env.SHELL = '/bin/unrecognized'; |
| 79 | + for (const shell of ['fish', 'nu', 'pwsh']) { |
| 80 | + fs.writeFileSync(path.join(shellBin, shell), '', { mode: 0o644 }); |
| 81 | + } |
| 82 | + console.log('Unknown shell; optional shell files are not executable:'); |
| 83 | + assert.doesNotMatch(setup(), /Fish:|Nushell:|PowerShell:|\$PROFILE/); |
| 84 | + for (const [shell, label] of [ |
| 85 | + ['fish', 'Fish'], |
| 86 | + ['nu', 'Nushell'], |
| 87 | + ['pwsh', 'PowerShell'], |
| 88 | + ]) { |
| 89 | + fs.chmodSync(path.join(shellBin, shell), 0o755); |
| 90 | + console.log(`Unknown shell; only ${label} is available:`); |
| 91 | + const output = setup(); |
| 92 | + assert.ok(output.includes(`${label}:`)); |
| 93 | + for (const other of ['Fish', 'Nushell', 'PowerShell'].filter((name) => name !== label)) { |
| 94 | + assert.ok(!output.includes(`${other}:`)); |
| 95 | + } |
| 96 | + assert.equal(output.includes('$PROFILE'), shell === 'pwsh'); |
| 97 | + fs.chmodSync(path.join(shellBin, shell), 0o644); |
| 98 | + } |
| 99 | +} |
0 commit comments