diff --git a/packages/bruno-cli/src/commands/run.js b/packages/bruno-cli/src/commands/run.js index b8a742943..62ae74ad5 100644 --- a/packages/bruno-cli/src/commands/run.js +++ b/packages/bruno-cli/src/commands/run.js @@ -7,9 +7,6 @@ const { const { runSingleRequest } = require('../runner/run-single-request'); -const { - CLI_EPILOGUE, -} = require('../constants'); const command = 'run '; 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); diff --git a/packages/bruno-cli/src/runner/bru/index.js b/packages/bruno-cli/src/runner/bru/index.js index 384a8f382..4ad46ad31 100644 --- a/packages/bruno-cli/src/runner/bru/index.js +++ b/packages/bruno-cli/src/runner/bru/index.js @@ -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"); diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 5e62bf623..f8a00f3d4 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -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); } diff --git a/packages/bruno-cli/src/utils/filesystem.js b/packages/bruno-cli/src/utils/filesystem.js index 10657b397..64aa37498 100644 --- a/packages/bruno-cli/src/utils/filesystem.js +++ b/packages/bruno-cli/src/utils/filesystem.js @@ -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 }; diff --git a/packages/bruno-electron/src/bru/index.js b/packages/bruno-electron/src/bru/index.js index bbe52223e..89f1e42a4 100644 --- a/packages/bruno-electron/src/bru/index.js +++ b/packages/bruno-electron/src/bru/index.js @@ -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); diff --git a/packages/bruno-js/src/scripts/test-runtime.js b/packages/bruno-js/src/scripts/test-runtime.js index db1560d2d..cc2a33567 100644 --- a/packages/bruno-js/src/scripts/test-runtime.js +++ b/packages/bruno-js/src/scripts/test-runtime.js @@ -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()