feat: enhance json environment file support in bruno-cli (#5660)

* feat: enhance json environment file support in bruno-cli

feat: add parseEnvironmentJson function to normalize environment JSON structure

lint fixes

feat: added tests for invalid JSON environment files in CLI and added missing constant defenition.

feat: improve JSON environment file handling and update tests

Trigger test

fix: update CLI command syntax for non-existent JSON environment file test

fix: correct CLI command syntax in test for non-existent JSON environment file

fix: update CLI command syntax in test for non-existent JSON environment file

fix: update test to use temporary path for non-existent JSON environment file

trying to fix the tests

fix tests

refactor: rename ERROR_INVALID_JSON to ERROR_INVALID_FILE and update related error handling in CLI commands and tests

fix: update parseEnvironmentJson to preserve secret flag

test: improved tests

* refactor: move parseEnvironmentJson function to utils/ environment.js file and update imports

* test: update tests
This commit is contained in:
Sanjai Kumar
2025-10-07 12:49:22 +05:30
committed by GitHub
parent 1cc3a6432a
commit 8d2f087206
8 changed files with 233 additions and 6 deletions

View File

@@ -0,0 +1,61 @@
import { test, expect } from '../../../playwright';
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import constants from '../../../packages/bruno-cli/src/constants.js';
test.describe('CLI JSON Environment File Support', () => {
const collectionPath = path.resolve(__dirname, 'collection');
const BRU = 'node ../../../../packages/bruno-cli/bin/bru.js';
// Helper: emulate `bru run` from a given working directory and
// return the process exit code (0 on success). We use execSync so
// these tests behave like invoking the CLI directly in a shell.
const runFrom = (cwd: string, args: string): number => {
try {
execSync(`cd "${cwd}" && ${BRU} ${args}`, { stdio: 'pipe' });
return 0;
} catch (error: any) {
return error?.status ?? 1;
}
};
test('CLI: Run with invalid JSON environment file should fail', async () => {
// Create a temporary invalid JSON file
const tempDir = '/tmp/bruno-cli-test';
const invalidEnvPath = path.join(tempDir, 'invalid-env.json');
fs.mkdirSync(tempDir, { recursive: true });
fs.writeFileSync(invalidEnvPath,
JSON.stringify({
name: 'Invalid Env'
// missing variables array - invalid JSON
}));
const status = runFrom(collectionPath, `run --env-file "${invalidEnvPath}"`);
expect(status).toBe(constants.EXIT_STATUS.ERROR_INVALID_FILE);
try {
// Cleanup
fs.unlinkSync(invalidEnvPath);
fs.rmdirSync(tempDir);
} catch (e) {}
});
test('CLI: Run with valid JSON env and interpolates variables', async () => {
const envPath = path.join(collectionPath, 'env.json');
const outputPath = path.join(collectionPath, 'out.json');
// Even if exit is non-zero (network warnings), reporter should be written
runFrom(collectionPath, `run request.bru --env-file "${envPath}" --reporter-json "${outputPath}"`);
expect(fs.existsSync(outputPath)).toBe(true);
const report = JSON.parse(fs.readFileSync(outputPath, 'utf8'));
const result = report.results[0];
expect(result.request.url).toBe('https://echo.usebruno.com');
expect(result.response.status).toBe(200);
try {
fs.unlinkSync(outputPath);
} catch (_) {}
});
});

View File

@@ -0,0 +1,6 @@
{
"version": "1",
"name": "cli-json-env-fixture",
"type": "collection"
}

View File

@@ -0,0 +1,4 @@
{
"name": "CLI JSON Env",
"variables": [{ "name": "baseUrl", "value": "https://echo.usebruno.com", "enabled": true }]
}

View File

@@ -0,0 +1,17 @@
meta {
name: cli-json-env-test
type: http
}
http {
method: POST
url: {{baseUrl}}
body: json
auth: none
}
body:json {
{
"ping": "pong"
}
}