Search in sources :

Example 16 with DetectableException

use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.

the class PnpmYamlTransformer method buildGraph.

private void buildGraph(DependencyGraph graphBuilder, List<String> rootPackageIds, @Nullable Map<String, PnpmPackageInfo> packageMap, PnpmLinkedPackageResolver linkedPackageResolver, @Nullable String reportingProjectPackagePath) throws IntegrationException {
    if (packageMap == null) {
        throw new DetectableException("Could not parse 'packages' section of the pnpm-lock.yaml file.");
    }
    for (Map.Entry<String, PnpmPackageInfo> packageEntry : packageMap.entrySet()) {
        String packageId = packageEntry.getKey();
        Optional<Dependency> pnpmPackage = buildDependencyFromPackageEntry(packageEntry);
        if (!pnpmPackage.isPresent()) {
            logger.debug(String.format("Could not add package %s to the graph.", packageId));
            continue;
        }
        if (isRootPackage(packageId, rootPackageIds)) {
            graphBuilder.addChildToRoot(pnpmPackage.get());
        }
        PnpmPackageInfo packageInfo = packageEntry.getValue();
        if (dependencyTypeFilter.shouldInclude(packageInfo.getDependencyType())) {
            for (Map.Entry<String, String> packageDependency : packageInfo.getDependencies().entrySet()) {
                String dependencyPackageId = convertRawEntryToPackageId(packageDependency, linkedPackageResolver, reportingProjectPackagePath);
                Optional<Dependency> child = buildDependencyFromPackageId(dependencyPackageId);
                child.ifPresent(c -> graphBuilder.addChildWithParent(child.get(), pnpmPackage.get()));
            }
        }
    }
}
Also used : Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) HashMap(java.util.HashMap) Map(java.util.Map) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) PnpmPackageInfo(com.synopsys.integration.detectable.detectables.pnpm.lockfile.model.PnpmPackageInfo)

Example 17 with DetectableException

use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.

the class GoModCliExtractorTest method handleGoModWhyExceptionTest.

@Test
public void handleGoModWhyExceptionTest() throws ExecutableRunnerException, ExecutableFailedException, DetectableException {
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    File directory = new File("");
    ExecutableTarget goExe = ExecutableTarget.forFile(new File(""));
    Answer<ExecutableOutput> executableAnswer = new Answer<ExecutableOutput>() {

        String[] goListArgs = { "list", "-m", "-json" };

        String[] goListJsonArgs = { "list", "-m", "-json", "all" };

        String[] goModGraphArgs = { "mod", "graph" };

        String[] goModWhyArgs = { "mod", "why", "-m", "all" };

        @Override
        public ExecutableOutput answer(InvocationOnMock invocation) throws Throwable {
            Executable executable = invocation.getArgument(0, Executable.class);
            List<String> commandLine = executable.getCommandWithArguments();
            ExecutableOutput result = null;
            if (commandLine.containsAll(Arrays.asList(goListJsonArgs))) {
                result = goListJsonOutput();
            } else if (commandLine.containsAll(Arrays.asList(goListArgs))) {
                result = goListOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModGraphArgs))) {
                result = goModGraphOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModWhyArgs))) {
                throw new ExecutableRunnerException(new DetectableException("Unit Test Go Mod Why error"));
            } else {
                result = new ExecutableOutput(0, "", "");
            }
            return result;
        }
    };
    GoModCliExtractor goModCliExtractor = buildGoModCliExtractor(executableRunner, executableAnswer);
    boolean wasSuccessful = true;
    Extraction extraction = goModCliExtractor.extract(directory, goExe);
    if (extraction.getError() instanceof ArrayIndexOutOfBoundsException) {
        wasSuccessful = false;
    }
    Assertions.assertTrue(wasSuccessful);
}
Also used : ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Answer(org.mockito.stubbing.Answer) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Test(org.junit.jupiter.api.Test)

Example 18 with DetectableException

use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.

the class ExtractableEvaluatorTest method testEvaluationExtractableAlreadyPerformed.

@Test
public void testEvaluationExtractableAlreadyPerformed() throws DetectableException {
    DetectorEvaluationOptions evaluationOptions = Mockito.mock(DetectorEvaluationOptions.class);
    ExtractionEnvironment extractionEnvironment = Mockito.mock(ExtractionEnvironment.class);
    Function<DetectorEvaluation, ExtractionEnvironment> extractionEnvironmentProvider = (detectorEvaluation) -> extractionEnvironment;
    ExtractableEvaluator evaluator = new ExtractableEvaluator(evaluationOptions, extractionEnvironmentProvider);
    DetectorEvaluationTree detectorEvaluationTree = Mockito.mock(DetectorEvaluationTree.class);
    Mockito.when(detectorEvaluationTree.getDirectory()).thenReturn(new File("."));
    DetectorEvaluatorListener detectorEvaluatorListener = Mockito.mock(DetectorEvaluatorListener.class);
    evaluator.setDetectorEvaluatorListener(detectorEvaluatorListener);
    DetectorEvaluation detectorEvaluation = createEvaluationMocks(evaluationOptions, detectorEvaluationTree, true, false);
    DetectorAggregateEvaluationResult result = evaluator.evaluate(detectorEvaluationTree);
    assertEquals(detectorEvaluationTree, result.getEvaluationTree());
    Mockito.verify(detectorEvaluatorListener).extractableStarted(detectorEvaluation);
    Mockito.verify(detectorEvaluation).setExtractable(Mockito.any(DetectorResult.class));
    Mockito.verify(detectorEvaluatorListener).extractableEnded(detectorEvaluation);
}
Also used : DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) Predicate(java.util.function.Predicate) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) Function(java.util.function.Function) File(java.io.File) Test(org.junit.jupiter.api.Test) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) Mockito(org.mockito.Mockito) List(java.util.List) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Detectable(com.synopsys.integration.detectable.Detectable) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) Collections(java.util.Collections) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 19 with DetectableException

use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.

the class GradleAirGapCreator method installGradleDependencies.

public void installGradleDependencies(File gradleTemp, File gradleTarget, String inspectorVersion) throws DetectUserFriendlyException {
    logger.info("Checking for gradle on the path.");
    ExecutableTarget gradle;
    try {
        gradle = gradleResolver.resolveGradle(new DetectableEnvironment(gradleTemp));
        if (gradle == null) {
            throw new DetectUserFriendlyException("Gradle must be on the path to make an Air Gap zip.", ExitCodeType.FAILURE_CONFIGURATION);
        }
    } catch (DetectableException e) {
        throw new DetectUserFriendlyException("An error occurred while finding Gradle which is needed to make an Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
    }
    File gradleOutput = new File(gradleTemp, "dependencies");
    logger.info("Using temporary gradle dependency directory: " + gradleOutput);
    File buildGradle = new File(gradleTemp, "build.gradle");
    File settingsGradle = new File(gradleTemp, "settings.gradle");
    logger.info("Using temporary gradle build file: " + buildGradle);
    logger.info("Using temporary gradle settings file: " + settingsGradle);
    FileUtil.createMissingParentDirectories(buildGradle);
    FileUtil.createMissingParentDirectories(settingsGradle);
    logger.info("Writing to temporary gradle build file.");
    try {
        Map<String, String> gradleScriptData = new HashMap<>();
        gradleScriptData.put("gradleOutput", StringEscapeUtils.escapeJava(gradleOutput.getCanonicalPath()));
        Template gradleScriptTemplate = configuration.getTemplate("create-gradle-airgap-script.ftl");
        try (Writer fileWriter = new FileWriter(buildGradle)) {
            gradleScriptTemplate.process(gradleScriptData, fileWriter);
        }
        FileUtils.writeStringToFile(settingsGradle, "", StandardCharsets.UTF_8);
    } catch (IOException | TemplateException e) {
        throw new DetectUserFriendlyException("An error occurred creating the temporary build.gradle while creating the Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
    }
    logger.info("Invoking gradle install on temporary directory.");
    try {
        ExecutableOutput executableOutput = executableRunner.execute(ExecutableUtils.createFromTarget(gradleTemp, gradle, "installDependencies"));
        if (executableOutput.getReturnCode() != 0) {
            throw new DetectUserFriendlyException("Gradle returned a non-zero exit code while installing Air Gap dependencies.", ExitCodeType.FAILURE_CONFIGURATION);
        }
    } catch (ExecutableRunnerException e) {
        throw new DetectUserFriendlyException("An error occurred using Gradle to make an Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
    }
    try {
        logger.info("Moving generated dependencies to final gradle folder: " + gradleTarget.getCanonicalPath());
        FileUtils.moveDirectory(gradleOutput, gradleTarget);
        FileUtils.deleteDirectory(gradleTemp);
    } catch (IOException e) {
        throw new DetectUserFriendlyException("An error occurred moving gradle dependencies to Air Gap folder.", ExitCodeType.FAILURE_CONFIGURATION);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Template(freemarker.template.Template) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 20 with DetectableException

use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.

the class NugetInspectorAirGapCreator method install.

private void install(File container, String targetDirectory, String propertyName) throws DetectUserFriendlyException {
    try {
        File destination = new File(container, targetDirectory);
        // actually downloads it to 'destination/zipName'
        File downloaded = inspectorInstaller.downloadZip(propertyName, destination);
        // move 'destination/zipName' to 'destination'
        FileUtils.copyDirectory(downloaded, destination);
        // delete 'destination/zipName'
        FileUtils.deleteDirectory(downloaded);
    } catch (DetectableException | IOException e) {
        throw new DetectUserFriendlyException("An error occurred installing project-inspector to the " + targetDirectory + " folder.", e, ExitCodeType.FAILURE_GENERAL_ERROR);
    }
}
Also used : DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) IOException(java.io.IOException) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Aggregations

DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)39 File (java.io.File)22 IOException (java.io.IOException)13 Extraction (com.synopsys.integration.detectable.extraction.Extraction)8 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)8 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)8 List (java.util.List)8 DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)6 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)6 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)6 PassedDetectableResult (com.synopsys.integration.detectable.detectable.result.PassedDetectableResult)6 ExtractionEnvironment (com.synopsys.integration.detectable.extraction.ExtractionEnvironment)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Test (org.junit.jupiter.api.Test)6 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)5 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)5 NameVersion (com.synopsys.integration.util.NameVersion)5 ArrayList (java.util.ArrayList)5 Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)4