From 4b4bd3bc95a54aa12eed2ff0eb94f4b2ebbf1a19 Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Thu, 5 Dec 2024 15:45:36 +0530 Subject: [PATCH] fix: updates --- .../bruno-cli/src/runner/prepare-request.js | 57 +++++-------------- .../src/runner/run-single-request.js | 3 +- .../bruno-tests/collection/collection.bru | 1 + .../collection/scripting/api/bru/folder.bru | 7 +++ .../scripting/api/bru/getCollectionVar.bru | 18 ++++++ .../scripting/api/bru/getFolderVar.bru | 18 ++++++ .../scripting/api/bru/getRequestVar.bru | 22 +++++++ 7 files changed, 80 insertions(+), 46 deletions(-) create mode 100644 packages/bruno-tests/collection/scripting/api/bru/folder.bru create mode 100644 packages/bruno-tests/collection/scripting/api/bru/getCollectionVar.bru create mode 100644 packages/bruno-tests/collection/scripting/api/bru/getFolderVar.bru create mode 100644 packages/bruno-tests/collection/scripting/api/bru/getRequestVar.bru diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index 6fc297fee..1113ed6f0 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -164,44 +164,22 @@ const mergeScripts = (collection, request, requestTreePath, scriptFlow) => { } }; -const findItemInCollection = (collection, itemId) => { - let item = null; - - if (collection.uid === itemId) { - return collection; - } - - if (collection.items && collection.items.length) { - collection.items.forEach((item) => { - if (item.uid === itemId) { - item = item; - } else if (item.type === 'folder') { - item = findItemInCollection(item, itemId); - } - }); - } - - return item; +const findItem = (items = [], pathname) => { + return find(items, (i) => i.pathname === pathname); }; -const findItemInCollectionByPath = (collection, pathname) => { - let item = null; +const findItemInCollection = (collection, pathname) => { + let flattenedItems = flattenItems(collection.items); - if (collection.pathname === pathname) { - return collection; - } + return findItem(flattenedItems, pathname); +}; - if (collection.items && collection.items.length) { - collection.items.forEach((_item) => { - if (_item.pathname === pathname) { - item = _item; - } else if (_item.type === 'folder') { - item = findItemInCollectionByPath(_item, pathname); - } - }); - } +const findParentItemInCollection = (collection, pathname) => { + let flattenedItems = flattenItems(collection.items); - return item; + return find(flattenedItems, (item) => { + return item.items && find(item.items, (i) => i.pathname === pathname); + }); }; const flattenItems = (items = []) => { @@ -222,22 +200,13 @@ const flattenItems = (items = []) => { return flattenedItems; }; -const findParentItemInCollectionByPath = (collection, pathname) => { - let flattenedItems = flattenItems(collection.items); - - return find(flattenedItems, (item) => { - return item.items && find(item.items, (i) => i.pathname === pathname); - }); -}; - const getTreePathFromCollectionToItem = (collection, _item) => { let path = []; - let item = findItemInCollectionByPath(collection, _item.pathname); + let item = findItemInCollection(collection, _item.pathname); while (item) { path.unshift(item); - item = findParentItemInCollectionByPath(collection, item.pathname); + item = findParentItemInCollection(collection, item.pathname); } - return path; }; diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index ed40bfb58..35cafb590 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -43,10 +43,9 @@ const runSingleRequest = async function ( let request; let nextRequestName; let item = { - pathname: `${collectionPath}/${filename}`, + pathname: path.join(collectionPath, filename), ...bruJson } - request = prepareRequest(item, collection); request.__bruno__executionMode = 'cli'; diff --git a/packages/bruno-tests/collection/collection.bru b/packages/bruno-tests/collection/collection.bru index d4b353eb8..32eb8a7af 100644 --- a/packages/bruno-tests/collection/collection.bru +++ b/packages/bruno-tests/collection/collection.bru @@ -14,6 +14,7 @@ auth:bearer { vars:pre-request { collection_pre_var: collection_pre_var_value collection_pre_var_token: {{request_pre_var_token}} + collection-var: collection-var-value } docs { diff --git a/packages/bruno-tests/collection/scripting/api/bru/folder.bru b/packages/bruno-tests/collection/scripting/api/bru/folder.bru new file mode 100644 index 000000000..7cb15610d --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/folder.bru @@ -0,0 +1,7 @@ +meta { + name: bru +} + +vars:pre-request { + folder-var: folder-var-value +} diff --git a/packages/bruno-tests/collection/scripting/api/bru/getCollectionVar.bru b/packages/bruno-tests/collection/scripting/api/bru/getCollectionVar.bru new file mode 100644 index 000000000..b5a91c600 --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/getCollectionVar.bru @@ -0,0 +1,18 @@ +meta { + name: getCollectionVar + type: http + seq: 9 +} + +get { + url: {{host}}/ping + body: none + auth: none +} + +tests { + test("should get collection var in scripts", function() { + const testVar = bru.getCollectionVar("collection-var"); + expect(testVar).to.equal("collection-var-value"); + }); +} diff --git a/packages/bruno-tests/collection/scripting/api/bru/getFolderVar.bru b/packages/bruno-tests/collection/scripting/api/bru/getFolderVar.bru new file mode 100644 index 000000000..425799451 --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/getFolderVar.bru @@ -0,0 +1,18 @@ +meta { + name: getFolderVar + type: http + seq: 8 +} + +get { + url: {{host}}/ping + body: none + auth: none +} + +tests { + test("should get folder var in scripts", function() { + const testVar = bru.getFolderVar("folder-var"); + expect(testVar).to.equal("folder-var-value"); + }); +} diff --git a/packages/bruno-tests/collection/scripting/api/bru/getRequestVar.bru b/packages/bruno-tests/collection/scripting/api/bru/getRequestVar.bru new file mode 100644 index 000000000..d2deff4c3 --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/getRequestVar.bru @@ -0,0 +1,22 @@ +meta { + name: getRequestVar + type: http + seq: 7 +} + +get { + url: {{host}}/ping + body: none + auth: none +} + +vars:pre-request { + request-var: request-var-value +} + +tests { + test("should get request var in scripts", function() { + const testVar = bru.getRequestVar("request-var"); + expect(testVar).to.equal("request-var-value"); + }); +}