feat: bruno cli can not run a folder recursively

This commit is contained in:
Anoop M D 2023-02-08 02:53:55 +05:30
parent 03fa46d8b3
commit 411c06f4cb
2 changed files with 101 additions and 22 deletions

View File

@ -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, isDirectory } = require('../utils/filesystem'); const { exists, isFile, isDirectory, getSubDirectories } = 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');
@ -37,21 +37,78 @@ const printRunSummary = (assertionResults, testResults) => {
console.log(chalk.bold(testSummary)); console.log(chalk.bold(testSummary));
}; };
const getBruFilesRecursively = (dir) => {
const getFilesInOrder = (dir) => {
let bruJsons = [];
const traverse = (currentPath) => {
const filesInCurrentDir = fs.readdirSync(currentPath);
for (const file of filesInCurrentDir) {
const filePath = path.join(currentPath, file);
const stats = fs.lstatSync(filePath);
if (stats.isDirectory()) {
traverse(filePath);
}
}
const currentDirBruJsons = [];
for (const file of filesInCurrentDir) {
const filePath = path.join(currentPath, file);
const stats = fs.lstatSync(filePath);
if (!stats.isDirectory() && path.extname(filePath) === '.bru') {
const bruContent = fs.readFileSync(filePath, 'utf8');
const bruJson = bruToJson(bruContent);
currentDirBruJsons.push({
bruFilepath: filePath,
bruJson
});
}
}
// order requests by sequence
currentDirBruJsons.sort((a, b) => {
const aSequence = a.bruJson.seq || 0;
const bSequence = b.bruJson.seq || 0;
return aSequence - bSequence;
});
bruJsons = bruJsons.concat(currentDirBruJsons);
};
traverse(dir);
return bruJsons;
};
const bruJsons = getFilesInOrder(dir);
return bruJsons;
};
const builder = async (yargs) => { const builder = async (yargs) => {
yargs yargs
.option('r', {
describe: 'Indicates a recursive run',
type: 'boolean',
default: false
})
.option('env', { .option('env', {
describe: 'Environment variables', describe: 'Environment variables',
type: 'string', type: 'string',
}) })
.example('$0 run request.bru', 'Run a request') .example('$0 run request.bru', 'Run a request')
.example('$0 run request.bru --env local', 'Run a request with the environment set to local'); .example('$0 run request.bru --env local', 'Run a request with the environment set to local')
.example('$0 run folder', 'Run all requests in a folder')
.example('$0 run folder -r', 'Run all requests in a folder recursively')
}; };
const handler = async function (argv) { const handler = async function (argv) {
try { try {
const { const {
filename, filename,
env env,
r: recursive
} = argv; } = argv;
const pathExists = await exists(filename); const pathExists = await exists(filename);
@ -97,11 +154,12 @@ const handler = async function (argv) {
const _isDirectory = await isDirectory(filename); const _isDirectory = await isDirectory(filename);
if(_isDirectory) { if(_isDirectory) {
console.log(chalk.yellow('Running Collection \n')); let bruJsons = [];
if(!recursive) {
console.log(chalk.yellow('Running Folder \n'));
const files = fs.readdirSync(filename); const files = fs.readdirSync(filename);
const bruFiles = files.filter((file) => file.endsWith('.bru')); const bruFiles = files.filter((file) => file.endsWith('.bru'));
const bruJsons = [];
for (const bruFile of bruFiles) { for (const bruFile of bruFiles) {
const bruFilepath = path.join(filename, bruFile) const bruFilepath = path.join(filename, bruFile)
const bruContent = fs.readFileSync(bruFilepath, 'utf8'); const bruContent = fs.readFileSync(bruFilepath, 'utf8');
@ -118,6 +176,11 @@ const handler = async function (argv) {
const bSequence = b.bruJson.seq || 0; const bSequence = b.bruJson.seq || 0;
return aSequence - bSequence; return aSequence - bSequence;
}); });
} else {
console.log(chalk.yellow('Running Folder Recursively \n'));
bruJsons = await getBruFilesRecursively(filename);
}
let assertionResults = []; let assertionResults = [];
let testResults = []; let testResults = [];

View File

@ -103,6 +103,21 @@ const stripExtension = (filename = '') => {
return filename.replace(/\.[^/.]+$/, ""); return filename.replace(/\.[^/.]+$/, "");
} }
const getSubDirectories = (dir) => {
try {
const files = fs.readdirSync(dir);
const subDirectories = files
.filter((file) => {
return fs.lstatSync(path.join(dir, file)).isDirectory();
})
.sort();
return subDirectories;
} catch (err) {
return [];
}
};
module.exports = { module.exports = {
exists, exists,
isSymbolicLink, isSymbolicLink,
@ -115,5 +130,6 @@ module.exports = {
createDirectory, createDirectory,
searchForFiles, searchForFiles,
searchForBruFiles, searchForBruFiles,
stripExtension stripExtension,
getSubDirectories
}; };