Skip to content

Commit

Permalink
🚧 YarnTreeBuilder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noyshabtay committed Nov 16, 2023
1 parent 927c2b5 commit f974dce
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/jfrog/ide/common/yarn/YarnTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void addDepTreeNodes(Map<String, DepTreeNode> nodes, JsonNode jsonDep, D
* @param rawDependency - The raw dependency Json string returned from 'Yarn why' command.
* @return The extracted dependency path as a list of dependencies starting from projectRootId till packageFullName.
*/
private List<String> extractSinglePath(String projectRootId, String packageFullName, String rawDependency) {
List<String> extractSinglePath(String projectRootId, String packageFullName, String rawDependency) {
List<String> pathResult = new ArrayList<>();
pathResult.add(projectRootId); // The root project is guaranteed to be the first element in the path

Expand Down Expand Up @@ -135,7 +135,7 @@ private List<String> extractSinglePath(String projectRootId, String packageFullN
* @param rawDependencyPaths - The raw dependency Json strings returned from 'Yarn why' command.
* @return The extracted dependency paths as a list of dependencies starting from projectRootId till packageFullName.
*/
private List<List<String>> extractMultiplePaths(String projectRootId, String packageFullName, List<String> rawDependencyPaths) {
List<List<String>> extractMultiplePaths(String projectRootId, String packageFullName, List<String> rawDependencyPaths) {
List<List<String>> paths = new ArrayList<>();
int limit = rawDependencyPaths.size() < ImpactTree.IMPACT_PATHS_LIMIT ? rawDependencyPaths.size() : 50;
for (int i = 0; i < limit; i++) {
Expand Down
98 changes: 83 additions & 15 deletions src/test/java/com/jfrog/ide/common/yarn/YarnTreeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.testng.Assert.*;

Expand Down Expand Up @@ -46,17 +49,8 @@ enum Project {

@BeforeMethod
public void setUp(Object[] testArgs) {
try {
tempProject = Files.createTempDirectory("ide-plugins-common-yarn").toFile();
tempProject.deleteOnExit();
FileUtils.copyDirectory(((Project) testArgs[0]).path.toFile(), tempProject);
Path projectDir = tempProject.toPath();
descriptorFilePath = projectDir.resolve("package.json").toString();
YarnTreeBuilder yarnTreeBuilder = new YarnTreeBuilder(projectDir, descriptorFilePath, null);
depTree = yarnTreeBuilder.buildTree(new NullLog());
assertNotNull(depTree);
} catch (IOException e) {
fail(e.getMessage(), e);
if (!yarnDriver.isYarnInstalled()) {
throw new SkipException("Skip test, yarn is not installed.");
}
}

Expand All @@ -74,10 +68,15 @@ private Object[][] yarnTreeBuilderProvider() {
}

@Test(dataProvider = "yarnTreeBuilderProvider")
public void yarnTreeBuilderTest(Project project, int expectedChildren) {
if (!yarnDriver.isYarnInstalled()) {
throw new SkipException("Skip test, yarn is not installed.");
}
public void yarnTreeBuilderTest(Project project, int expectedChildren) 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);
depTree = yarnTreeBuilder.buildTree(new NullLog());
assertNotNull(depTree);
String expectedProjectName = project.name;
checkDependencyTree(expectedProjectName, expectedChildren);
}
Expand Down Expand Up @@ -117,4 +116,73 @@ 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<String> 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<String> 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\\\"\"]}"
);

YarnTreeBuilder yarnTreeBuilder = new YarnTreeBuilder(Paths.get(""), "", null);
List<List<String>> 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");
}

@Test
public void findDependencyImpactPathsTest() throws IOException {
String projectRootId = "root";
String packageName = "pkg";
Set<String> packageVersions = Set.of("1.0.0", "2.0.0");
List<String> yarnWhyOutput = List.of(
"{\"type\":\"info\",\"data\":\"Found \\\"[email protected]\\\"\"}",
"{\"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<String, List<List<String>>> paths = yarnTreeBuilder.findDependencyImpactPaths(new NullLog(), projectRootId, packageName, packageVersions);

assertNotNull(paths);
assertEquals(paths.size(), 1);

List<List<String>> pkgPaths = paths.get("pkg:1.0.0");
assertNotNull(pkgPaths);
assertEquals(pkgPaths.size(), 1);

List<String> 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");
}
}

0 comments on commit f974dce

Please sign in to comment.