mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-24 22:11:38 +02:00
feat: bru cli can now run all requests inside a directory
This commit is contained in:
parent
1b9ec05a58
commit
d0f2eb27bc
@ -1,7 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { exists, isFile } = require('../utils/filesystem');
|
const { exists, isFile, isDirectory } = require('../utils/filesystem');
|
||||||
const { runSingleRequest } = require('../runner/run-single-request');
|
const { runSingleRequest } = require('../runner/run-single-request');
|
||||||
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
||||||
const { rpad } = require('../utils/common');
|
const { rpad } = require('../utils/common');
|
||||||
@ -9,6 +9,33 @@ const { rpad } = require('../utils/common');
|
|||||||
const command = 'run <filename>';
|
const command = 'run <filename>';
|
||||||
const desc = 'Run a request';
|
const desc = 'Run a request';
|
||||||
|
|
||||||
|
const printRunSummary = (assertionResults, testResults) => {
|
||||||
|
// display assertion results and test results summary
|
||||||
|
const totalAssertions = assertionResults.length;
|
||||||
|
const passedAssertions = assertionResults.filter((result) => result.status === 'pass').length;
|
||||||
|
const failedAssertions = totalAssertions - passedAssertions;
|
||||||
|
|
||||||
|
const totalTests = testResults.length;
|
||||||
|
const passedTests = testResults.filter((result) => result.status === 'pass').length;
|
||||||
|
const failedTests = totalTests - passedTests;
|
||||||
|
const maxLength = 12;
|
||||||
|
|
||||||
|
let assertSummary = `${rpad('Tests:', maxLength)} ${chalk.green(`${passedTests} passed`)}`;
|
||||||
|
if (failedTests > 0) {
|
||||||
|
assertSummary += `, ${chalk.red(`${failedTests} failed`)}`;
|
||||||
|
}
|
||||||
|
assertSummary += `, ${totalTests} total`;
|
||||||
|
|
||||||
|
let testSummary = `${rpad('Assertions:', maxLength)} ${chalk.green(`${passedAssertions} passed`)}`;
|
||||||
|
if (failedAssertions > 0) {
|
||||||
|
testSummary += `, ${chalk.red(`${failedAssertions} failed`)}`;
|
||||||
|
}
|
||||||
|
testSummary += `, ${totalAssertions} total`;
|
||||||
|
|
||||||
|
console.log("\n" + chalk.bold(assertSummary));
|
||||||
|
console.log(chalk.bold(testSummary));
|
||||||
|
};
|
||||||
|
|
||||||
const builder = async (yargs) => {
|
const builder = async (yargs) => {
|
||||||
yargs
|
yargs
|
||||||
.option('env', {
|
.option('env', {
|
||||||
@ -61,31 +88,31 @@ const handler = async function (argv) {
|
|||||||
testResults
|
testResults
|
||||||
} = await runSingleRequest(filename, collectionPath, collectionVariables, envVars);
|
} = await runSingleRequest(filename, collectionPath, collectionVariables, envVars);
|
||||||
|
|
||||||
// display assertion results and test results summary
|
printRunSummary(assertionResults, testResults);
|
||||||
const totalAssertions = assertionResults.length;
|
console.log(chalk.dim(chalk.grey('Done.')));
|
||||||
const passedAssertions = assertionResults.filter((result) => result.status === 'pass').length;
|
}
|
||||||
const failedAssertions = totalAssertions - passedAssertions;
|
|
||||||
|
|
||||||
const totalTests = testResults.length;
|
const _isDirectory = await isDirectory(filename);
|
||||||
const passedTests = testResults.filter((result) => result.status === 'pass').length;
|
if(_isDirectory) {
|
||||||
const failedTests = totalTests - passedTests;
|
console.log(chalk.yellow('Running Collection \n'));
|
||||||
const maxLength = 12;
|
|
||||||
|
|
||||||
let assertSummary = `${rpad('Tests:', maxLength)} ${chalk.green(`${passedTests} passed`)}`;
|
const files = fs.readdirSync(filename);
|
||||||
if (failedTests > 0) {
|
const bruFiles = files.filter((file) => file.endsWith('.bru'));
|
||||||
assertSummary += `, ${chalk.red(`${failedTests} failed`)}`;
|
|
||||||
|
let assertionResults = [];
|
||||||
|
let testResults = [];
|
||||||
|
|
||||||
|
for (const bruFile of bruFiles) {
|
||||||
|
const {
|
||||||
|
assertionResults: _assertionResults,
|
||||||
|
testResults: _testResults
|
||||||
|
} = await runSingleRequest(path.join(filename, bruFile), collectionPath, collectionVariables, envVars);
|
||||||
|
|
||||||
|
assertionResults = assertionResults.concat(_assertionResults);
|
||||||
|
testResults = testResults.concat(_testResults);
|
||||||
}
|
}
|
||||||
assertSummary += `, ${totalTests} total`;
|
|
||||||
|
|
||||||
let testSummary = `${rpad('Assertions:', maxLength)} ${chalk.green(`${passedAssertions} passed`)}`;
|
|
||||||
if (failedAssertions > 0) {
|
|
||||||
testSummary += `, ${chalk.red(`${failedAssertions} failed`)}`;
|
|
||||||
}
|
|
||||||
testSummary += `, ${totalAssertions} total`;
|
|
||||||
|
|
||||||
console.log("\n" + chalk.bold(assertSummary));
|
|
||||||
console.log(chalk.bold(testSummary));
|
|
||||||
|
|
||||||
|
printRunSummary(assertionResults, testResults);
|
||||||
console.log(chalk.dim(chalk.grey('Ran all requests.')));
|
console.log(chalk.dim(chalk.grey('Ran all requests.')));
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user