From e192dd7696f7d63748475287431836349e58f464 Mon Sep 17 00:00:00 2001 From: Or Geva Date: Thu, 21 Sep 2023 18:34:57 +0300 Subject: [PATCH] Add tests --- .../utils/yarnImpactGraph.ts | 7 +- src/test/tests/yarnImpactGraph.test.ts | 144 ++++++++++++++++++ 2 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/test/tests/yarnImpactGraph.test.ts diff --git a/src/main/treeDataProviders/utils/yarnImpactGraph.ts b/src/main/treeDataProviders/utils/yarnImpactGraph.ts index 63d6c45a..b8375574 100644 --- a/src/main/treeDataProviders/utils/yarnImpactGraph.ts +++ b/src/main/treeDataProviders/utils/yarnImpactGraph.ts @@ -2,7 +2,7 @@ import { IImpactGraph, IImpactGraphNode } from 'jfrog-ide-webview'; import { RootNode } from '../dependenciesTree/dependenciesRoot/rootTree'; import { ScanUtils } from '../../utils/scanUtils'; -type YarnWhyItem = StepItem | InfoItem; +export type YarnWhyItem = StepItem | InfoItem; /** * Represents a step item in the "yarn why" output. @@ -68,7 +68,8 @@ export class YarnImpactGraphUtil { */ private findDependencyChain(output: YarnWhyItem[]): string[] { const startIndex: number | undefined = this.findDependencyPosition(this.dependencyVersion, output); - if (!startIndex) { + // Zero could be a valid index + if (startIndex === undefined) { return []; } for (let i: number = startIndex + 1; i < output.length; i++) { @@ -254,7 +255,7 @@ export class YarnImpactGraphUtil { /** * Executes the "yarn why" command and parses its JSON output. */ - private runYarnWhy(): YarnWhyItem[] { + protected runYarnWhy(): YarnWhyItem[] { const output: string = ScanUtils.executeCmd('yarn why --json --no-progress ' + this.dependencyName, this.workspaceFolder).toString(); return output .split('\n') diff --git a/src/test/tests/yarnImpactGraph.test.ts b/src/test/tests/yarnImpactGraph.test.ts new file mode 100644 index 00000000..56ebfbd3 --- /dev/null +++ b/src/test/tests/yarnImpactGraph.test.ts @@ -0,0 +1,144 @@ +import { IImpactGraph } from 'jfrog-ide-webview'; +import { YarnImpactGraphUtil, YarnWhyItem } from '../../main/treeDataProviders/utils/yarnImpactGraph'; +import { assert } from 'chai'; +import { RootNode } from '../../main/treeDataProviders/dependenciesTree/dependenciesRoot/rootTree'; + +describe('Yarn impact graph util', async () => { + it('Build single impact graph', async () => { + const results: IImpactGraph = new YarnImpactGraphUtilMock('minimist', '0.0.8', 'Mock-Project', '').create(); + assert.deepEqual(results, generateExpectedSingleImpactGraph()); + }); + + it.only('Build multiple impact graphs', async () => { + const results: IImpactGraph = new YarnImpactGraphUtilMock('minimist', '1.2.0', 'Mock-Project', '').create(); + assert.deepEqual(results, generateExpectedMultipleImpactGraphs()); + }); +}); + +class YarnImpactGraphUtilMock extends YarnImpactGraphUtil { + protected runYarnWhy(): YarnWhyItem[] { + const yarnWhyOutput: YarnWhyItem[] = [ + { + type: 'info', + data: '\r=> Found "minimist@1.2.0"' + }, + { + type: 'info', + data: 'Has been hoisted to "minimist"' + }, + { + type: 'info', + data: 'Reasons this module exists' + }, + { + type: 'list', + data: { + type: 'reasons', + items: [ + 'Specified in "dependencies"', + 'Hoisted from "jest-cli#node-notifier#minimist"', + 'Hoisted from "jest-cli#sane#minimist"', + 'Hoisted from "jest-cli#istanbul-lib-instrument#babel-generator#detect-indent#minimist"' + ] + } + }, + { + type: 'info', + data: 'Disk size without dependencies: "96KB"' + }, + { + type: 'info', + data: '\r=> Found "mkdirp#minimist@0.0.8"' + }, + { + type: 'info', + data: 'This module exists because "jest-cli#istanbul-api#mkdirp" depends on it.' + } + ]; + return yarnWhyOutput; + } +} + +function generateExpectedSingleImpactGraph(): IImpactGraph { + return { + root: { + name: 'Mock-Project', + children: [ + { + name: 'jest-cli', + children: [ + { + name: 'istanbul-api', + children: [ + { + name: 'mkdirp', + children: [ + { + name: 'minimist:0.0.8' + } + ] + } + ] + } + ] + } + ] + }, + pathsCount: 1, + pathsLimit: RootNode.IMPACT_PATHS_LIMIT + }; +} + +function generateExpectedMultipleImpactGraphs(): IImpactGraph { + return { + root: { + name: 'Mock-Project', + children: [ + { + name: 'minimist:1.2.0' + }, + { + name: 'jest-cli', + children: [ + { + name: 'node-notifier', + children: [ + { + name: 'minimist:1.2.0' + } + ] + }, + { + name: 'sane', + children: [ + { + name: 'minimist:1.2.0' + } + ] + }, + { + name: 'istanbul-lib-instrument', + children: [ + { + name: 'babel-generator', + children: [ + { + name: 'detect-indent', + children: [ + { + name: 'minimist:1.2.0' + } + ] + } + ] + } + ] + } + ] + } + ] + }, + pathsCount: 4, + pathsLimit: RootNode.IMPACT_PATHS_LIMIT + }; +}