Search in sources :

Example 6 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmPeerExclusionTest method testPeerDependencyExists.

@Test
public void testPeerDependencyExists() {
    NpmPackagerResult result = createPackager(NpmDependencyType.DEV).parseAndTransform(packageJsonText, packageLockText);
    GraphAssert graphAssert = new GraphAssert(Forge.NPMJS, result.getCodeLocation().getDependencyGraph());
    graphAssert.hasDependency(childPeer);
    graphAssert.hasDependency(parentPeer);
    graphAssert.hasRootSize(1);
}
Also used : GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Test(org.junit.jupiter.api.Test) FunctionalTest(com.synopsys.integration.detectable.annotations.FunctionalTest)

Example 7 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmCliExtractor method extract.

public Extraction extract(File directory, ExecutableTarget npmExe, @Nullable String npmArguments, File packageJsonFile) {
    toolVersionLogger.log(directory, npmExe);
    PackageJson packageJson;
    try {
        packageJson = parsePackageJson(packageJsonFile);
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
    List<String> exeArgs = new ArrayList<>();
    exeArgs.add("ls");
    exeArgs.add("-json");
    Optional.ofNullable(npmArguments).map(arg -> arg.split(" ")).ifPresent(additionalArguments -> exeArgs.addAll(Arrays.asList(additionalArguments)));
    ExecutableOutput npmLsOutput;
    try {
        npmLsOutput = executableRunner.execute(ExecutableUtils.createFromTarget(directory, npmExe, exeArgs));
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
    String standardOutput = npmLsOutput.getStandardOutput();
    String errorOutput = npmLsOutput.getErrorOutput();
    if (StringUtils.isNotBlank(errorOutput)) {
        logger.error("Error when running npm ls -json command");
        logger.error(errorOutput);
        return new Extraction.Builder().failure("Npm wrote to stderr while running npm ls.").build();
    } else if (StringUtils.isNotBlank(standardOutput)) {
        logger.debug("Parsing npm ls file.");
        logger.debug(standardOutput);
        NpmPackagerResult result = npmCliParser.generateCodeLocation(standardOutput, packageJson);
        String projectName = result.getProjectName() != null ? result.getProjectName() : packageJson.name;
        String projectVersion = result.getProjectVersion() != null ? result.getProjectVersion() : packageJson.version;
        return new Extraction.Builder().success(result.getCodeLocation()).projectName(projectName).projectVersion(projectVersion).build();
    } else {
        logger.error("Nothing returned from npm ls -json command");
        return new Extraction.Builder().failure("Npm returned error after running npm ls.").build();
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmCliParser(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ToolVersionLogger(com.synopsys.integration.detectable.util.ToolVersionLogger) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Gson(com.google.gson.Gson) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) IOException(java.io.IOException)

Example 8 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmDevExclusionTest method testDevDependencyNotExists.

@Test
public void testDevDependencyNotExists() {
    NpmPackagerResult result = createPackager(NpmDependencyType.DEV, NpmDependencyType.PEER).parseAndTransform(packageJsonText, packageLockText);
    GraphAssert graphAssert = new GraphAssert(Forge.NPMJS, result.getCodeLocation().getDependencyGraph());
    graphAssert.hasNoDependency(childDev);
    graphAssert.hasNoDependency(parentDev);
    graphAssert.hasRootSize(0);
}
Also used : GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Test(org.junit.jupiter.api.Test) FunctionalTest(com.synopsys.integration.detectable.annotations.FunctionalTest)

Example 9 with NpmPackagerResult

use of com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult in project synopsys-detect by blackducksoftware.

the class NpmDevExclusionTest method testDevDependencyExists.

@Test
public void testDevDependencyExists() {
    NpmPackagerResult result = createPackager(NpmDependencyType.PEER).parseAndTransform(packageJsonText, packageLockText);
    GraphAssert graphAssert = new GraphAssert(Forge.NPMJS, result.getCodeLocation().getDependencyGraph());
    graphAssert.hasDependency(childDev);
    graphAssert.hasDependency(parentDev);
    graphAssert.hasRootSize(1);
}
Also used : GraphAssert(com.synopsys.integration.detectable.util.graph.GraphAssert) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Test(org.junit.jupiter.api.Test) FunctionalTest(com.synopsys.integration.detectable.annotations.FunctionalTest)

Aggregations

NpmPackagerResult (com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult)9 Test (org.junit.jupiter.api.Test)5 FunctionalTest (com.synopsys.integration.detectable.annotations.FunctionalTest)4 GraphAssert (com.synopsys.integration.detectable.util.graph.GraphAssert)4 PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)3 Gson (com.google.gson.Gson)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)2 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)2 NpmCliParser (com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser)2 Extraction (com.synopsys.integration.detectable.extraction.Extraction)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Nullable (org.jetbrains.annotations.Nullable)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 BasicDependencyGraph (com.synopsys.integration.bdio.graph.BasicDependencyGraph)1