Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- name: Setup Bun for runtime regression tests
if: matrix.node-version == '24.x'
uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2
with:
bun-version: '1.3.14'

- run: npm ci
- run: npm run lint
- run: npm test
Expand Down
5 changes: 4 additions & 1 deletion lib/pi/command-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ function runCommand({
const entryPoint = path.resolve(requireString(packageRoot, 'packageRoot'), 'bin/index.js');
const cwd = path.resolve(requireString(projectRoot, 'projectRoot'));
const childEnv = buildCliEnvironment(env);
// Compiled Bun hosts must run the CLI, not their embedded application entrypoint.
if (process.versions.bun) childEnv.BUN_BE_BUN = '1';
const argv = Array.isArray(args) ? args.map((arg) => String(arg)) : [];
const runtimeArgs = process.versions.bun ? ['--no-env-file'] : [];
const outputLimit = Number.isFinite(maxOutputBytes) && maxOutputBytes >= 0 ? maxOutputBytes : 0;
const timeoutLimit = Number.isFinite(timeoutMs) && timeoutMs >= 0 ? timeoutMs : 0;

Expand Down Expand Up @@ -251,7 +254,7 @@ function runCommand({
}

try {
child = spawn(process.execPath, [entryPoint, ...argv], {
child = spawn(process.execPath, [...runtimeArgs, entryPoint, ...argv], {
cwd,
env: childEnv,
shell: false,
Expand Down
53 changes: 53 additions & 0 deletions tests/pi-command-runner.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const {
CONFIG_ENV_KEYS,
ConfluencePiError,
Expand Down Expand Up @@ -46,6 +47,58 @@ test('executes package-local argv and parses complete JSON', async () => {
}
});

const bunAvailable = spawnSync('bun', ['--version'], { timeout: 5000 }).status === 0;

(bunAvailable ? test.each : test.skip.each)(['plain', 'compiled'])('executes the CLI from a %s Bun host', async (runtime) => {
const packageRoot = fakePackage(`
process.stdout.write(JSON.stringify({ argv: process.argv.slice(2), cwd: process.cwd(),
domain: process.env.CONFLUENCE_DOMAIN, insecure: process.env.CONFLUENCE_ALLOW_INSECURE_HTTP,
cookie: process.env.CONFLUENCE_COOKIE, injected: process.env.PROJECT_INJECTION }));
`);
const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'pi-untrusted-project-'));
fs.writeFileSync(path.join(projectRoot, '.env'), 'CONFLUENCE_DOMAIN=untrusted.invalid\nCONFLUENCE_ALLOW_INSECURE_HTTP=true\n');
fs.writeFileSync(path.join(projectRoot, '.env.local'), 'CONFLUENCE_COOKIE=untrusted-cookie\nPROJECT_INJECTION=loaded\n');
const hostSource = path.join(packageRoot, 'host.js');
const hostBinary = path.join(packageRoot, process.platform === 'win32' ? 'host.exe' : 'host');
fs.writeFileSync(hostSource, `
if (process.argv.length > 2) throw new Error('Compiled host restarted instead of running the CLI');
const { runCommand } = require(${JSON.stringify(require.resolve('../lib/pi/command-runner'))});
runCommand({
packageRoot: ${JSON.stringify(packageRoot)},
projectRoot: ${JSON.stringify(projectRoot)},
args: ['--json', 'info', '123; echo unsafe'],
env: { PATH: '', CONFLUENCE_DOMAIN: 'trusted.invalid' },
timeoutMs: 3000,
maxOutputBytes: 4096,
expectJson: true,
mutation: false,
}).then(result => process.stdout.write(JSON.stringify(result.json)))
.catch(error => { console.error(error.code); process.exitCode = 1; });
`);
try {
if (runtime === 'compiled') {
const build = spawnSync('bun', ['build', '--compile', hostSource, '--outfile', hostBinary], {
encoding: 'utf8', timeout: 20000,
});
expect(build.status).toBe(0);
}
const env = { ...process.env };
delete env.BUN_BE_BUN;
const executable = runtime === 'compiled' ? hostBinary : 'bun';
const args = runtime === 'compiled' ? [] : ['--no-env-file', hostSource];
const result = spawnSync(executable, args, { env, encoding: 'utf8', timeout: 5000 });
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout)).toEqual({
argv: ['--json', 'info', '123; echo unsafe'],
cwd: fs.realpathSync(projectRoot),
domain: 'trusted.invalid',
});
} finally {
cleanup(packageRoot);
cleanup(projectRoot);
}
}, 30000);

test('rejects malformed JSON with INVALID_JSON', async () => {
const packageRoot = fakePackage('process.stdout.write(\'{not json\');');
const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'pi-project-'));
Expand Down