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 {
runSingleRequest
} = require('../runner/run-single-request');
const {
CLI_EPILOGUE,
} = require('../constants');
const command = 'run <filename>';
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`));
}
// 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);
if(_isFile) {
runSingleRequest(filename);
runSingleRequest(filename, collectionPath, collectionVariables);
}
} catch (err) {
console.error(err);

View File

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

View File

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

View File

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

View File

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

View File

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