Search in sources :

Example 6 with ExecutableRunnerException

use of com.synopsys.integration.executable.ExecutableRunnerException in project synopsys-detect by blackducksoftware.

the class FilePathGenerator method generateDepsMkFile.

private Optional<File> generateDepsMkFile(File workingDir, CompileCommand compileCommand) {
    String depsMkFilename = deriveDependenciesListFilename(compileCommand);
    File depsMkFile = new File(workingDir, depsMkFilename);
    Map<String, String> optionOverrides = new HashMap<>(1);
    optionOverrides.put(COMPILER_OUTPUT_FILE_OPTION, REPLACEMENT_OUTPUT_FILENAME);
    try {
        List<String> command = commandParser.parseCommand(compileCommand, optionOverrides);
        command.addAll(Arrays.asList("-M", "-MF", depsMkFile.getAbsolutePath()));
        Executable executable = Executable.create(new File(compileCommand.getDirectory()), Collections.emptyMap(), command);
        executableRunner.execute(executable);
    } catch (ExecutableRunnerException e) {
        logger.debug(String.format("Error generating dependencies file for command '%s': %s", compileCommand.getCommand(), e.getMessage()));
        return Optional.empty();
    }
    return Optional.of(depsMkFile);
}
Also used : HashMap(java.util.HashMap) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException)

Example 7 with ExecutableRunnerException

use of com.synopsys.integration.executable.ExecutableRunnerException in project synopsys-detect by blackducksoftware.

the class DpkgVersionResolver method resolvePackageVersion.

public Optional<String> resolvePackageVersion(ClangPackageManagerInfo currentPackageManager, DetectableExecutableRunner executableRunner, File workingDirectory, String packageName) {
    try {
        List<String> args = new ArrayList<>(currentPackageManager.getPkgInfoArgs().get());
        args.add(packageName);
        ExecutableOutput packageStatusOutput = executableRunner.execute(workingDirectory, currentPackageManager.getPkgMgrCmdString(), args);
        logger.debug(String.format("packageStatusOutput: %s", packageStatusOutput));
        return parsePackageVersionFromStatusOutput(packageName, packageStatusOutput.getStandardOutput());
    } catch (ExecutableRunnerException e) {
        logger.error(String.format("Error executing %s to get package info: %s", currentPackageManager.getPkgMgrName(), e.getMessage()));
    }
    return Optional.empty();
}
Also used : ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException)

Example 8 with ExecutableRunnerException

use of com.synopsys.integration.executable.ExecutableRunnerException 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 9 with ExecutableRunnerException

use of com.synopsys.integration.executable.ExecutableRunnerException 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 10 with ExecutableRunnerException

use of com.synopsys.integration.executable.ExecutableRunnerException in project synopsys-detect by blackducksoftware.

the class ExtractionEvaluatorTest method createEvaluationMocks.

private DetectorEvaluation createEvaluationMocks(DetectorEvaluationOptions evaluationOptions, DetectorEvaluationTree detectorEvaluationTree, boolean extractionExists, boolean throwException) throws DetectableException, ExecutableFailedException, IOException, CycleDetectedException, MissingExternalIdException, ExecutableRunnerException, ParserConfigurationException, SAXException {
    ExtractionEnvironment extractionEnvironment = Mockito.mock(ExtractionEnvironment.class);
    DetectorEvaluation detectorEvaluation = Mockito.mock(DetectorEvaluation.class);
    Detectable detectable = Mockito.mock(Detectable.class);
    DetectableResult detectableExtractableResult = Mockito.mock(DetectableResult.class);
    Mockito.when(detectableExtractableResult.getPassed()).thenReturn(true);
    Mockito.when(detectableExtractableResult.toDescription()).thenReturn("test detectable");
    Mockito.when(detectable.extractable()).thenReturn(detectableExtractableResult);
    Mockito.when(detectorEvaluation.getDetectable()).thenReturn(detectable);
    Mockito.when(detectorEvaluation.getExtractionEnvironment()).thenReturn(extractionEnvironment);
    Mockito.when(detectorEvaluation.isSearchable()).thenReturn(true);
    Mockito.when(detectorEvaluation.isApplicable()).thenReturn(true);
    Mockito.when(detectorEvaluation.isExtractable()).thenReturn(true);
    List<DetectorEvaluation> detectorEvaluations = Collections.singletonList(detectorEvaluation);
    Mockito.when(detectorEvaluationTree.getOrderedEvaluations()).thenReturn(detectorEvaluations);
    DetectorRuleSet detectorRuleSet = Mockito.mock(DetectorRuleSet.class);
    Mockito.when(detectorEvaluationTree.getDetectorRuleSet()).thenReturn(detectorRuleSet);
    DetectorRule detectorRule = Mockito.mock(DetectorRule.class);
    Mockito.when(detectorRule.getDescriptiveName()).thenReturn("test rule");
    Mockito.when(detectorEvaluation.getDetectorRule()).thenReturn(detectorRule);
    Mockito.when(detectorEvaluationTree.getDepthFromRoot()).thenReturn(0);
    Mockito.when(evaluationOptions.isForceNested()).thenReturn(true);
    Predicate<DetectorRule> rulePredicate = it -> true;
    Mockito.when(evaluationOptions.getDetectorFilter()).thenReturn(rulePredicate);
    Mockito.when(detectorRule.createDetectable(Mockito.any(DetectableEnvironment.class))).thenReturn(detectable);
    Mockito.when(detectable.applicable()).thenReturn(new PassedDetectableResult());
    if (throwException) {
        Mockito.when(detectable.extract(Mockito.eq(extractionEnvironment))).thenThrow(new RuntimeException("JUnit expected exception"));
    } else {
        Mockito.when(detectable.extract(Mockito.eq(extractionEnvironment))).thenReturn(new Extraction.Builder().success().build());
    }
    return detectorEvaluation;
}
Also used : DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) Detectable(com.synopsys.integration.detectable.Detectable) CycleDetectedException(com.synopsys.integration.detectable.util.CycleDetectedException) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) Predicate(java.util.function.Predicate) IOException(java.io.IOException) MissingExternalIdException(com.synopsys.integration.bdio.graph.builder.MissingExternalIdException) 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) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Collections(java.util.Collections) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Detectable(com.synopsys.integration.detectable.Detectable) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) ExtractionEnvironment(com.synopsys.integration.detectable.extraction.ExtractionEnvironment) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) PassedDetectableResult(com.synopsys.integration.detectable.detectable.result.PassedDetectableResult) Extraction(com.synopsys.integration.detectable.extraction.Extraction)

Aggregations

ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)14 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)10 File (java.io.File)8 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)7 ArrayList (java.util.ArrayList)7 Extraction (com.synopsys.integration.detectable.extraction.Extraction)6 IOException (java.io.IOException)5 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)4 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)4 HashMap (java.util.HashMap)4 List (java.util.List)4 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)3 Executable (com.synopsys.integration.executable.Executable)3 NameVersion (com.synopsys.integration.util.NameVersion)3 Collectors (java.util.stream.Collectors)3 StringUtils (org.apache.commons.lang3.StringUtils)3 MissingExternalIdException (com.synopsys.integration.bdio.graph.builder.MissingExternalIdException)2 FileFinder (com.synopsys.integration.common.util.finder.FileFinder)2 ExecutableUtils (com.synopsys.integration.detectable.ExecutableUtils)2 NugetInspectorParser (com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser)2