From 3e2dbca10243de0c40e10fb168ae7dfe5cca0658 Mon Sep 17 00:00:00 2001 From: noys Date: Thu, 16 Nov 2023 16:41:16 +0200 Subject: [PATCH] :white_check_mark: YarnTreeBuilder tests --- .../ide/common/yarn/YarnTreeBuilderTest.java | 106 +++++++++--------- 1 file changed, 50 insertions(+), 56 deletions(-) diff --git a/src/test/java/com/jfrog/ide/common/yarn/YarnTreeBuilderTest.java b/src/test/java/com/jfrog/ide/common/yarn/YarnTreeBuilderTest.java index c490e08..be7c9b2 100644 --- a/src/test/java/com/jfrog/ide/common/yarn/YarnTreeBuilderTest.java +++ b/src/test/java/com/jfrog/ide/common/yarn/YarnTreeBuilderTest.java @@ -67,14 +67,25 @@ private Object[][] yarnTreeBuilderProvider() { }; } - @Test(dataProvider = "yarnTreeBuilderProvider") - public void yarnTreeBuilderTest(Project project, int expectedChildren) throws IOException { + @DataProvider + private Object[][] findDependencyImpactPathsProvider() { + return new Object[][]{ + {Project.DEPENDENCY, "@types/node", Set.of("14.14.10"), List.of(List.of("package-name2", "@types/node:14.14.10"))}, + }; + } + + private YarnTreeBuilder getYarnTreeBuilder(Project project) throws IOException { tempProject = Files.createTempDirectory("ide-plugins-common-yarn").toFile(); tempProject.deleteOnExit(); FileUtils.copyDirectory((project).path.toFile(), tempProject); Path projectDir = tempProject.toPath(); descriptorFilePath = projectDir.resolve("package.json").toString(); - YarnTreeBuilder yarnTreeBuilder = new YarnTreeBuilder(projectDir, descriptorFilePath, null); + return new YarnTreeBuilder(projectDir, descriptorFilePath, null); + } + + @Test(dataProvider = "yarnTreeBuilderProvider") + public void yarnTreeBuilderTest(Project project, int expectedChildren) throws IOException { + YarnTreeBuilder yarnTreeBuilder = getYarnTreeBuilder(project); depTree = yarnTreeBuilder.buildTree(new NullLog()); assertNotNull(depTree); String expectedProjectName = project.name; @@ -117,72 +128,55 @@ private void fourChildrenScenario() { assertEquals(count, 4); } - @Test - public void extractSinglePathTest() { - String projectRootId = "root"; - String packageFullName = "pkg:1.0.0"; - String rawDependency = "{\"type\":\"info\",\"data\":\"This module exists because \\\"pkg#subpkg#dep\\\" depends on it.\"}"; - - YarnTreeBuilder yarnTreeBuilder = new YarnTreeBuilder(Paths.get(""), "", null); - List pathResult = yarnTreeBuilder.extractSinglePath(projectRootId, packageFullName, rawDependency); - - assertNotNull(pathResult); - assertEquals(pathResult.size(), 2); - assertEquals(pathResult.get(0), projectRootId); - assertEquals(pathResult.get(1), packageFullName); - } - @Test public void extractMultiplePathsTest() { String projectRootId = "root"; String packageFullName = "pkg:1.0.0"; List rawDependencyPaths = List.of( - "{\"type\":\"reasons\",\"items\":[\"Specified in \\\"dependencies\\\"\",\"Hoisted from \\\"pkg#dep1\\\"\",\"Hoisted from \\\"pkg#dep2\\\"\"]}", - "{\"type\":\"reasons\",\"items\":[\"Specified in \\\"devDependencies\\\"\",\"Hoisted from \\\"pkg#dep3\\\"\"]}" + "Specified in \"dependencies\"", + "Hoisted from \"jest-cli#node-notifier#minimist\"", + "Hoisted from \"jest-cli#sane#minimist\"", + "Hoisted from \"@test0#\\test1#test2#pkg\"", + "This module exists because \"jest-cli#istanbul-api#mkdirp\" depends on it.", + "This module exists because it's specified in \"devDependencies\"." ); + List> expectedPaths = List.of( + List.of(projectRootId, packageFullName), + List.of(projectRootId, "jest-cli", "node-notifier", "minimist", packageFullName), + List.of(projectRootId, "jest-cli", "sane", "minimist", packageFullName), + List.of(projectRootId, "@test0", "\\test1", "test2", packageFullName), + List.of(projectRootId, "jest-cli", "istanbul-api", "mkdirp", packageFullName), + List.of(projectRootId, packageFullName) + ); YarnTreeBuilder yarnTreeBuilder = new YarnTreeBuilder(Paths.get(""), "", null); List> paths = yarnTreeBuilder.extractMultiplePaths(projectRootId, packageFullName, rawDependencyPaths); assertNotNull(paths); - assertEquals(paths.size(), 2); - assertEquals(paths.get(0).size(), 4); - assertEquals(paths.get(0).get(0), projectRootId); - assertEquals(paths.get(0).get(1), packageFullName); - assertEquals(paths.get(0).get(2), "pkg#dep1"); - assertEquals(paths.get(0).get(3), "pkg#dep2"); - - assertEquals(paths.get(1).size(), 3); - assertEquals(paths.get(1).get(0), projectRootId); - assertEquals(paths.get(1).get(1), packageFullName); - assertEquals(paths.get(1).get(2), "pkg#dep3"); - } + assertEquals(paths.size(), expectedPaths.size()); + for (List path : paths) { + assertTrue(expectedPaths.contains(path)); + } - @Test - public void findDependencyImpactPathsTest() throws IOException { - String projectRootId = "root"; - String packageName = "pkg"; - Set packageVersions = Set.of("1.0.0", "2.0.0"); - List yarnWhyOutput = List.of( - "{\"type\":\"info\",\"data\":\"Found \\\"pkg@1.0.0\\\"\"}", - "{\"type\":\"list\",\"data\":{\"type\":\"reasons\",\"items\":[\"Specified in \\\"dependencies\\\"\",\"Hoisted from \\\"pkg#dep1\\\"\",\"Hoisted from \\\"pkg#dep2\\\"\"]}}" - ); - YarnTreeBuilder yarnTreeBuilder = new YarnTreeBuilder(Paths.get(""), "", null); - Map>> paths = yarnTreeBuilder.findDependencyImpactPaths(new NullLog(), projectRootId, packageName, packageVersions); + } - assertNotNull(paths); - assertEquals(paths.size(), 1); - - List> pkgPaths = paths.get("pkg:1.0.0"); - assertNotNull(pkgPaths); - assertEquals(pkgPaths.size(), 1); - - List singlePath = pkgPaths.get(0); - assertEquals(singlePath.size(), 4); - assertEquals(singlePath.get(0), projectRootId); - assertEquals(singlePath.get(1), "pkg:1.0.0"); - assertEquals(singlePath.get(2), "pkg#dep1"); - assertEquals(singlePath.get(3), "pkg#dep2"); + @Test(dataProvider = "findDependencyImpactPathsProvider") + public void findDependencyImpactPathsTest(Project project, String packageName, Set packageVersions, List> expectedPaths) throws IOException { + String projectRootId = project.name; + + YarnTreeBuilder yarnTreeBuilder = getYarnTreeBuilder(project); + Map>> pathsMap = yarnTreeBuilder.findDependencyImpactPaths(new NullLog(), projectRootId, packageName, packageVersions); + + assertNotNull(pathsMap); + for (String packageVersion : packageVersions) { + String packageFullName = packageName + ":" + packageVersion; + assertTrue(pathsMap.containsKey(packageFullName)); + List> paths = pathsMap.get(packageFullName); + assertEquals(paths.size(), expectedPaths.size()); + for (List path : paths) { + assertTrue(expectedPaths.contains(path)); + } + } } } \ No newline at end of file