feat: bru cli prints test results

This commit is contained in:
Anoop M D 2023-02-06 14:52:22 +05:30
parent 1c869013c6
commit 827c480689
6 changed files with 42 additions and 23 deletions

View File

@ -7,9 +7,6 @@ const {
const { const {
runSingleRequest runSingleRequest
} = require('../runner/run-single-request'); } = require('../runner/run-single-request');
const {
CLI_EPILOGUE,
} = require('../constants');
const command = 'run <filename>'; const command = 'run <filename>';
const desc = 'Run a request'; const desc = 'Run a request';
@ -27,9 +24,15 @@ const handler = async function (argv) {
console.error(chalk.red(`File or directory ${filename} does not exist`)); console.error(chalk.red(`File or directory ${filename} does not exist`));
} }
// todo
// right now, bru must be run from the root of the collection
// will add support in the future to run it from anywhere inside the collection
const collectionPath = process.cwd();
const collectionVariables = {};
const _isFile = await isFile(filename); const _isFile = await isFile(filename);
if(_isFile) { if(_isFile) {
runSingleRequest(filename); runSingleRequest(filename, collectionPath, collectionVariables);
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);

View File

@ -37,9 +37,9 @@ const bruToJson = (bru) => {
"params": _.get(json, "query", []), "params": _.get(json, "query", []),
"headers": _.get(json, "headers", []), "headers": _.get(json, "headers", []),
"body": _.get(json, "body", {}), "body": _.get(json, "body", {}),
},
"script": _.get(json, "script", ""), "script": _.get(json, "script", ""),
"test": _.get(json, "test", "") "tests": _.get(json, "tests", "")
}
}; };
transformedJson.request.body.mode = _.get(json, "http.mode", "none"); transformedJson.request.body.mode = _.get(json, "http.mode", "none");

View File

@ -2,12 +2,17 @@ const Mustache = require('mustache');
const fs = require('fs'); const fs = require('fs');
const { forOwn, each, extend, get } = require('lodash'); const { forOwn, each, extend, get } = require('lodash');
const FormData = require('form-data'); const FormData = require('form-data');
const path = require('path');
const axios = require('axios'); const axios = require('axios');
const prepareRequest = require('./prepare-request'); const prepareRequest = require('./prepare-request');
const { ScriptRuntime, TestRuntime } = require('@usebruno/js'); const { ScriptRuntime, TestRuntime } = require('@usebruno/js');
const { const {
bruToJson bruToJson
} = require('./bru'); } = require('./bru');
const {
stripExtension
} = require('../utils/filesystem');
const chalk = require('chalk');
// override the default escape function to prevent escaping // override the default escape function to prevent escaping
Mustache.escape = function (value) { Mustache.escape = function (value) {
@ -30,9 +35,9 @@ const getEnvVars = (environment = {}) => {
return envVars; return envVars;
}; };
const runSingleRequest = async function (filepath) { const runSingleRequest = async function (filename, collectionPath, collectionVariables) {
try { try {
const bruContent = fs.readFileSync(filepath, 'utf8'); const bruContent = fs.readFileSync(filename, 'utf8');
const bruJson = bruToJson(bruContent); const bruJson = bruToJson(bruContent);
const request = prepareRequest(bruJson.request); const request = prepareRequest(bruJson.request);
@ -50,10 +55,6 @@ const runSingleRequest = async function (filepath) {
const envVars = getEnvVars({}); const envVars = getEnvVars({});
//todo:
const collectionVariables = {};
const collectionPath = '/Users/anoop/Github/github-rest-api-collection';
if(request.script && request.script.length) { if(request.script && request.script.length) {
let script = request.script + '\n if (typeof onRequest === "function") {onRequest(__brunoRequest);}'; let script = request.script + '\n if (typeof onRequest === "function") {onRequest(__brunoRequest);}';
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
@ -62,19 +63,31 @@ const runSingleRequest = async function (filepath) {
const response = await axios(request); const response = await axios(request);
if(request.script && request.script.length) { const scriptFile = get(bruJson, 'request.script');
let script = request.script + '\n if (typeof onResponse === "function") {onResponse(__brunoResponse);}'; if(scriptFile && scriptFile.length) {
let script = scriptFile + '\n if (typeof onResponse === "function") {onResponse(__brunoResponse);}';
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = scriptRuntime.runResponseScript(script, response, envVars, collectionVariables, collectionPath); const result = scriptRuntime.runResponseScript(script, response, envVars, collectionVariables, collectionPath);
} }
let testResults = [];
const testFile = get(bruJson, 'request.tests'); const testFile = get(bruJson, 'request.tests');
if(testFile && testFile.length) { if(testFile && testFile.length) {
const testRuntime = new TestRuntime(); const testRuntime = new TestRuntime();
const result = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath); const result = testRuntime.runTests(testFile, request, response, envVars, collectionVariables, collectionPath);
testResults = get(result, 'results', []);
} }
console.log(response.status); console.log(chalk.blue(stripExtension(filename)) + chalk.dim(` (${response.status} ${response.statusText})`));
if(testResults && testResults.length) {
each(testResults, (testResult) => {
if(testResult.status === 'pass') {
console.log(chalk.green(` ✔️ `) + chalk.dim(testResult.description));
} else {
console.log(chalk.red(``) + chalk.red(testResult.description));
}
});
}
} catch (err) { } catch (err) {
Promise.reject(err); Promise.reject(err);
} }

View File

@ -99,6 +99,10 @@ const searchForBruFiles = (dir) => {
return searchForFiles(dir, '.bru'); return searchForFiles(dir, '.bru');
}; };
const stripExtension = (filename = '') => {
return filename.replace(/\.[^/.]+$/, "");
}
module.exports = { module.exports = {
exists, exists,
isSymbolicLink, isSymbolicLink,
@ -110,5 +114,6 @@ module.exports = {
hasBruExtension, hasBruExtension,
createDirectory, createDirectory,
searchForFiles, searchForFiles,
searchForBruFiles searchForBruFiles,
stripExtension
}; };

View File

@ -39,9 +39,9 @@ const bruToJson = (bru) => {
"params": _.get(json, "query", []), "params": _.get(json, "query", []),
"headers": _.get(json, "headers", []), "headers": _.get(json, "headers", []),
"body": _.get(json, "body", {}), "body": _.get(json, "body", {}),
},
"script": _.get(json, "script", ""), "script": _.get(json, "script", ""),
"test": _.get(json, "test", "") "tests": _.get(json, "tests", "")
}
}; };
transformedJson.request.body.mode = _.get(json, "http.mode", "none"); transformedJson.request.body.mode = _.get(json, "http.mode", "none");
@ -85,7 +85,7 @@ const jsonToBru = (json) => {
headers: _.get(json, 'request.headers', []), headers: _.get(json, 'request.headers', []),
body: _.get(json, 'request.body', {}), body: _.get(json, 'request.body', {}),
script: _.get(json, 'script', ''), script: _.get(json, 'script', ''),
test: _.get(json, 'test', ''), tests: _.get(json, 'tests', ''),
}; };
return jsonToBruV2(bruJson); return jsonToBruV2(bruJson);

View File

@ -37,13 +37,11 @@ class TestRuntime {
root: [collectionPath] root: [collectionPath]
} }
}); });
console.log(__brunoTestResults);
vm.run(testsFile, path.join(collectionPath, 'vm.js')); vm.run(testsFile, path.join(collectionPath, 'vm.js'));
return { return {
request, request,
response,
environment, environment,
collectionVariables, collectionVariables,
results: __brunoTestResults.getResults() results: __brunoTestResults.getResults()