Search in sources :

Example 1 with YarnLockDependency

use of com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency in project synopsys-detect by blackducksoftware.

the class YarnLockParserTest method testSkipIrrelevantStuff.

@Test
void testSkipIrrelevantStuff() {
    List<String> yarnLockText = Arrays.asList("#", "", "any-root-dep@1:", "  ignoredelement1", // must have a version to create an entry
    "  version: 1.0.0", "  ignoredelement2", "  dependencies:", "    some-peer: ^10.0.0", "  ignoredelement3", "#", "");
    YarnLockParser yarnLockParser = createYarnLockParser();
    YarnLock yarnLock = yarnLockParser.parseYarnLock(yarnLockText);
    Assertions.assertEquals(1, yarnLock.getEntries().size());
    YarnLockEntry first = yarnLock.getEntries().get(0);
    Assertions.assertEquals("1.0.0", first.getVersion());
    Assertions.assertEquals(1, first.getDependencies().size());
    YarnLockDependency dep = first.getDependencies().get(0);
    Assertions.assertEquals("some-peer", dep.getName());
    Assertions.assertEquals("^10.0.0", dep.getVersion());
    Assertions.assertFalse(dep.isOptional());
}
Also used : YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) YarnLockParser(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockParser) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Example 2 with YarnLockDependency

use of com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency in project synopsys-detect by blackducksoftware.

the class YarnLockParserTest method assertEntry.

void assertEntry(YarnLock yarnLock, String idName, String idVersion, String resolvedVersion, YarnLockDependency... dependencies) {
    boolean found = false;
    for (YarnLockEntry entry : yarnLock.getEntries()) {
        for (YarnLockEntryId entryId : entry.getIds()) {
            if (entryId.getName().equals(idName) && entryId.getVersion().equals(idVersion)) {
                found = true;
                assertEquals(resolvedVersion, entry.getVersion(), "Yarn entry should have found correct resolved version.");
                assertEquals(dependencies.length, entry.getDependencies().size(), "Yarn entry should have found correct number of dependencies.");
                for (YarnLockDependency dependency : dependencies) {
                    boolean dFound = false;
                    for (YarnLockDependency entryDependency : entry.getDependencies()) {
                        if (entryDependency.getName().equals(dependency.getName()) && entryDependency.getVersion().equals(dependency.getVersion()) && entryDependency.isOptional() == dependency.isOptional()) {
                            dFound = true;
                            break;
                        }
                    }
                    assertTrue(dFound, "Could not find yarn dependency for entry " + idName + " with name " + dependency.getName() + " and version " + dependency.getVersion() + " and optional " + dependency.isOptional() + ".");
                }
            }
        }
    }
    assertTrue(found, "Could not find yarn lock entry with name " + idName + " and version " + idVersion + ".");
}
Also used : YarnLockEntryId(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency)

Example 3 with YarnLockDependency

use of com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency in project synopsys-detect by blackducksoftware.

the class YarnLockParserTest method testParsingNamesWithAtSymbolSlash.

@Test
void testParsingNamesWithAtSymbolSlash() {
    List<String> yarnLockText = new ArrayList<>();
    yarnLockText.add("# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.");
    yarnLockText.add("# yarn lockfile v1");
    yarnLockText.add("");
    yarnLockText.add("");
    yarnLockText.add("\"@apollo/client@^3.1.3\", \"@apollo/client@^3.1.4\", \"@apollo/client@^3.3.6\":\n");
    yarnLockText.add("  version \"3.3.6\"\n");
    yarnLockText.add("  resolved \"https://registry.yarnpkg.com/@apollo/client/-/client-3.3.6.tgz#f359646308167f38d5bc498dfc2344c888400093\"\n");
    yarnLockText.add("  integrity sha512-XSm/STyNS8aHdDigLLACKNMHwI0qaQmEHWHtTP+jHe/E1wZRnn66VZMMgwKLy2V4uHISHfmiZ4KpUKDPeJAKqg==\n");
    yarnLockText.add("  dependencies:\n");
    yarnLockText.add("    \"@graphql-typed-document-node/core\" \"^3.0.0\"\n");
    YarnLockParser yarnLockParser = createYarnLockParser();
    YarnLock yarnLock = yarnLockParser.parseYarnLock(yarnLockText);
    YarnLockDependency dep = new YarnLockDependency("@graphql-typed-document-node/core", "^3.0.0", false);
    assertEntry(yarnLock, "@apollo/client", "^3.1.3", "3.3.6", dep);
}
Also used : YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) YarnLockParser(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockParser) ArrayList(java.util.ArrayList) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Example 4 with YarnLockDependency

use of com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency in project synopsys-detect by blackducksoftware.

the class YarnTransformerTest method buildTestYarnLockResult.

@NotNull
private YarnLockResult buildTestYarnLockResult(List<NameVersion> workspacesThatAreDependencies, List<NameVersion> workspacesThatAreNotDependencies, boolean yarn1project) {
    PackageJson rawPackageJson = new PackageJson();
    rawPackageJson.dependencies = new HashMap<>();
    rawPackageJson.dependencies.put("foo", "fooFuzzyVersion-1.0");
    for (NameVersion workspace : workspacesThatAreDependencies) {
        rawPackageJson.dependencies.put(workspace.getName(), workspace.getVersion());
    }
    rawPackageJson.devDependencies.put("bar", "barFuzzyVersion-1.0");
    NullSafePackageJson packageJson = new NullSafePackageJson(rawPackageJson);
    // yarn.lock: foo and bar both depend on yarn
    List<YarnLockEntryId> yarnLockEntryIdsFoo = Collections.singletonList(new YarnLockEntryId("foo", "fooFuzzyVersion-1.0"));
    List<YarnLockEntryId> yarnLockEntryIdsBar = Collections.singletonList(new YarnLockEntryId("bar", "barFuzzyVersion-1.0"));
    List<YarnLockEntryId> yarnLockEntryIdsYarn = Collections.singletonList(new YarnLockEntryId("yarn", "^1.22.4"));
    List<YarnLockDependency> dependencyRefToYarn = Collections.singletonList(new YarnLockDependency("yarn", "^1.22.4", false));
    List<YarnLockEntry> yarnLockEntries = new LinkedList<>();
    if (!yarn1project) {
        List<YarnLockEntryId> projectEntryIds = Collections.singletonList(new YarnLockEntryId("project", "1.0.0"));
        List<YarnLockDependency> projectDependencies = new LinkedList<>();
        projectDependencies.add(new YarnLockDependency("foo", "fooFuzzyVersion-1.0", false));
        projectDependencies.add(new YarnLockDependency("bar", "barFuzzyVersion-1.0", false));
        for (NameVersion workspaceThatIsDependency : workspacesThatAreDependencies) {
            projectDependencies.add(new YarnLockDependency(workspaceThatIsDependency.getName(), workspaceThatIsDependency.getVersion(), false));
        }
        yarnLockEntries.add(new YarnLockEntry(false, projectEntryIds, "1.0.0", projectDependencies));
    }
    Collection<YarnWorkspace> workspacesByName = new LinkedList<>();
    List<NameVersion> allWorkspaces = new LinkedList<>(workspacesThatAreDependencies);
    allWorkspaces.addAll(workspacesThatAreNotDependencies);
    for (NameVersion workspace : allWorkspaces) {
        String workspaceDepName = workspace.getName() + WORKSPACE_DEP_SUFFIX;
        String workspaceDevDepName = workspace.getName() + "-dev" + WORKSPACE_DEP_SUFFIX;
        addWorkspacePackageJson(workspacesByName, workspace, workspaceDepName, workspaceDevDepName);
        if (!yarn1project) {
            addWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDepName);
        }
        addDependencyOfWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDepName);
        addDependencyOfWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDevDepName);
    }
    yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsFoo, "1.0", dependencyRefToYarn));
    yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsBar, "1.0", dependencyRefToYarn));
    yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsYarn, "1.22.5", new LinkedList<>()));
    String yarnLockVersion = null;
    if (!yarn1project) {
        yarnLockVersion = "4";
    }
    YarnLock yarnLock = new YarnLock(yarnLockVersion, yarn1project, yarnLockEntries);
    YarnWorkspaces workspaceData = new YarnWorkspaces(workspacesByName);
    return new YarnLockResult(packageJson, workspaceData, yarnLock);
}
Also used : NameVersion(com.synopsys.integration.util.NameVersion) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) LinkedList(java.util.LinkedList) YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) YarnLockResult(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult) YarnLockEntryId(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency) YarnWorkspaces(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with YarnLockDependency

use of com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency in project synopsys-detect by blackducksoftware.

the class YarnLockDependencyMetaListSectionParser method makeDependencyOptional.

private void makeDependencyOptional(YarnLockEntryBuilder entryBuilder, String curDependencyName) {
    YarnLockDependency origDependency = entryBuilder.getDependencies().get(curDependencyName);
    if (origDependency == null) {
        logger.warn("Found metadata indicating dependency {} is optional, but it's not in the dependency list", curDependencyName);
        return;
    }
    logger.trace("Marking dependency {}:{} optional", origDependency.getName(), origDependency.getVersion());
    entryBuilder.getDependencies().remove(curDependencyName);
    YarnLockDependency replacementDependency = new YarnLockDependency(origDependency.getName(), origDependency.getVersion(), true);
    entryBuilder.getDependencies().put(curDependencyName, replacementDependency);
}
Also used : YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency)

Aggregations

YarnLockDependency (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency)15 YarnLock (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock)10 UnitTest (com.synopsys.integration.detectable.annotations.UnitTest)9 Test (org.junit.jupiter.api.Test)9 YarnLockParser (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockParser)8 YarnLockEntry (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry)7 ArrayList (java.util.ArrayList)5 YarnLockEntryId (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId)4 PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)2 NullSafePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson)2 WorkspacePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson)2 YarnLockResult (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)1 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)1 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)1 YarnWorkspace (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace)1 YarnWorkspaces (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces)1 NameVersion (com.synopsys.integration.util.NameVersion)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1