Feature: Add an optional param to cli to only run requests that have tests (#1314)

* Add an optional param to cli to only run requests that have tests

* Added to recrusive run

* added asserts to test only flag

---------

Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
This commit is contained in:
Jeff Edmondson 2024-01-29 13:06:24 -05:00 committed by GitHub
parent 0dd8154d8b
commit 555387598a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,6 @@ const makeJUnitOutput = require('../reporters/junit');
const { rpad } = require('../utils/common'); const { rpad } = require('../utils/common');
const { bruToJson, getOptions, collectionBruToJson } = require('../utils/bru'); const { bruToJson, getOptions, collectionBruToJson } = require('../utils/bru');
const { dotenvToJson } = require('@usebruno/lang'); const { dotenvToJson } = require('@usebruno/lang');
const command = 'run [filename]'; const command = 'run [filename]';
const desc = 'Run a request'; const desc = 'Run a request';
@ -92,7 +91,7 @@ const printRunSummary = (results) => {
}; };
}; };
const getBruFilesRecursively = (dir) => { const getBruFilesRecursively = (dir, testsOnly) => {
const environmentsPath = 'environments'; const environmentsPath = 'environments';
const getFilesInOrder = (dir) => { const getFilesInOrder = (dir) => {
@ -131,10 +130,22 @@ const getBruFilesRecursively = (dir) => {
if (!stats.isDirectory() && path.extname(filePath) === '.bru') { if (!stats.isDirectory() && path.extname(filePath) === '.bru') {
const bruContent = fs.readFileSync(filePath, 'utf8'); const bruContent = fs.readFileSync(filePath, 'utf8');
const bruJson = bruToJson(bruContent); const bruJson = bruToJson(bruContent);
currentDirBruJsons.push({ const requestHasTests = bruJson.request?.tests;
bruFilepath: filePath, const requestHasActiveAsserts = bruJson.request?.assertions.some((x) => x.enabled) || false;
bruJson
}); if (testsOnly) {
if (requestHasTests || requestHasActiveAsserts) {
currentDirBruJsons.push({
bruFilepath: filePath,
bruJson
});
}
} else {
currentDirBruJsons.push({
bruFilepath: filePath,
bruJson
});
}
} }
} }
@ -200,6 +211,9 @@ const builder = async (yargs) => {
type: 'boolean', type: 'boolean',
description: 'Allow insecure server connections' description: 'Allow insecure server connections'
}) })
.option('tests-only', {
type: 'boolean',
description: 'Only run requests that have a test'
.option('bail', { .option('bail', {
type: 'boolean', type: 'boolean',
description: 'Stop execution after a failure of a request, test, or assertion' description: 'Stop execution after a failure of a request, test, or assertion'
@ -219,12 +233,13 @@ const builder = async (yargs) => {
.example( .example(
'$0 run request.bru --output results.xml --format junit', '$0 run request.bru --output results.xml --format junit',
'Run a request and write the results to results.xml in junit format in the current directory' 'Run a request and write the results to results.xml in junit format in the current directory'
); )
.example('$0 run request.bru --test-only', 'Run all requests that have a test');
}; };
const handler = async function (argv) { const handler = async function (argv) {
try { try {
let { filename, cacert, env, envVar, insecure, r: recursive, output: outputPath, format, bail } = argv; let { filename, cacert, env, envVar, insecure, r: recursive, output: outputPath, format, testsOnly, bail } = argv;
const collectionPath = process.cwd(); const collectionPath = process.cwd();
// todo // todo
@ -335,7 +350,7 @@ const handler = async function (argv) {
}); });
} }
const _isFile = await isFile(filename); const _isFile = isFile(filename);
let results = []; let results = [];
let bruJsons = []; let bruJsons = [];
@ -350,7 +365,7 @@ const handler = async function (argv) {
}); });
} }
const _isDirectory = await isDirectory(filename); const _isDirectory = isDirectory(filename);
if (_isDirectory) { if (_isDirectory) {
if (!recursive) { if (!recursive) {
console.log(chalk.yellow('Running Folder \n')); console.log(chalk.yellow('Running Folder \n'));
@ -361,10 +376,21 @@ const handler = async function (argv) {
const bruFilepath = path.join(filename, bruFile); const bruFilepath = path.join(filename, bruFile);
const bruContent = fs.readFileSync(bruFilepath, 'utf8'); const bruContent = fs.readFileSync(bruFilepath, 'utf8');
const bruJson = bruToJson(bruContent); const bruJson = bruToJson(bruContent);
bruJsons.push({ const requestHasTests = bruJson.request?.tests;
bruFilepath, const requestHasActiveAsserts = bruJson.request?.assertions.some((x) => x.enabled) || false;
bruJson if (testsOnly) {
}); if (requestHasTests || requestHasActiveAsserts) {
bruJsons.push({
bruFilepath,
bruJson
});
}
} else {
bruJsons.push({
bruFilepath,
bruJson
});
}
} }
bruJsons.sort((a, b) => { bruJsons.sort((a, b) => {
const aSequence = a.bruJson.seq || 0; const aSequence = a.bruJson.seq || 0;
@ -374,7 +400,7 @@ const handler = async function (argv) {
} else { } else {
console.log(chalk.yellow('Running Folder Recursively \n')); console.log(chalk.yellow('Running Folder Recursively \n'));
bruJsons = getBruFilesRecursively(filename); bruJsons = getBruFilesRecursively(filename, testsOnly);
} }
} }