Search in sources :

Example 11 with ExecutableOutput

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

the class FunctionalDetectableExecutableRunner method execute.

@NotNull
@Override
public ExecutableOutput execute(@NotNull Executable executable) {
    ExecutableOutput executableOutput = executableExecutableOutputMap.get(new FunctionalExecutable(executable));
    if (executableOutput == null) {
        StringBuilder errorMessage = new StringBuilder("Missing mocked executable output for:").append(System.lineSeparator()).append(executable.getExecutableDescription()).append(System.lineSeparator());
        errorMessage.append("Executable Output Map Contents:").append(System.lineSeparator());
        executableExecutableOutputMap.forEach((key, value) -> {
            errorMessage.append("--------------------------------").append(System.lineSeparator());
            errorMessage.append("Key: ").append(System.lineSeparator()).append(key.getReferencedExecutable().getExecutableDescription()).append(System.lineSeparator());
            errorMessage.append("Standard Output: ").append(System.lineSeparator()).append(value.getStandardOutput()).append(System.lineSeparator());
            errorMessage.append("Error Output: ").append(System.lineSeparator()).append(value.getErrorOutput()).append(System.lineSeparator());
            errorMessage.append("Return Code: ").append(System.lineSeparator()).append(value.getReturnCode()).append(System.lineSeparator());
            errorMessage.append(System.lineSeparator());
        });
        throw new RuntimeException(errorMessage.toString());
    }
    return executableOutput;
}
Also used : ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with ExecutableOutput

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

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

the class SbtDotExtractor method extract.

public Extraction extract(File directory, ExecutableTarget sbt, String sbtCommandAdditionalArguments) {
    try {
        List<String> sbtArgs = sbtCommandArgumentGenerator.generateSbtCmdArgs(sbtCommandAdditionalArguments, "dependencyDot");
        Executable dotExecutable = ExecutableUtils.createFromTarget(directory, sbt, sbtArgs);
        ExecutableOutput dotOutput = executableRunner.executeSuccessfully(dotExecutable);
        List<File> dotGraphs = sbtDotOutputParser.parseGeneratedGraphFiles(dotOutput.getStandardOutputAsList());
        Extraction.Builder extraction = new Extraction.Builder();
        for (File dotGraph : dotGraphs) {
            GraphParser graphParser = new GraphParser(FileUtils.openInputStream(dotGraph));
            Set<String> rootIDs = sbtRootNodeFinder.determineRootIDs(graphParser);
            // typically found in project-folder/target/<>.dot so .parent.parent == project folder
            File projectFolder = dotGraph.getParentFile().getParentFile();
            if (rootIDs.size() == 1) {
                String projectId = rootIDs.stream().findFirst().get();
                DependencyGraph graph = sbtGraphParserTransformer.transformDotToGraph(graphParser, projectId);
                Dependency projectDependency = graphNodeParser.nodeToDependency(projectId);
                extraction.codeLocations(new CodeLocation(graph, projectDependency.getExternalId(), projectFolder));
                if (projectFolder.equals(directory)) {
                    extraction.projectName(projectDependency.getName());
                    extraction.projectVersion(projectDependency.getVersion());
                }
            } else {
                logger.warn("Unable to determine which node was the project in an SBT graph: " + dotGraph.toString());
                logger.warn("This may mean you have extraneous dependencies and should consider removing them. The dependencies are: " + String.join(",", rootIDs));
                DependencyGraph graph = sbtGraphParserTransformer.transformDotToGraph(graphParser, rootIDs);
                extraction.codeLocations(new CodeLocation(graph, projectFolder));
            }
        }
        return extraction.success().build();
    } catch (IOException | DetectableException | ExecutableFailedException e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : ExecutableFailedException(com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) GraphParser(com.paypal.digraph.parser.GraphParser) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) IOException(java.io.IOException) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 14 with ExecutableOutput

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

the class ClangPackageManagerRunnerTest method runTest.

private PackageDetailsResult runTest(ClangPackageManagerInfo packageManagerInfo, ClangPackageManagerResolver packageResolver, String pkgName, String pkgArch, boolean archBuried, String pkgMgrOwnerQueryResultPattern, String pkgMgrDetailsQueryResultPattern, File dependencyFile) throws ExecutableRunnerException {
    ClangPackageManager currentPackageManager = new ClangPackageManager(packageManagerInfo, packageResolver);
    File workingDirectory = new File("test");
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    List<String> fileSpecificGetOwnerArgs = new ArrayList<>(packageManagerInfo.getPkgMgrGetOwnerCmdArgs());
    fileSpecificGetOwnerArgs.add(dependencyFile.getAbsolutePath());
    if (packageManagerInfo.getPkgInfoArgs().isPresent() && (pkgMgrDetailsQueryResultPattern != null)) {
        List<String> fileSpecificGetDetailsArgs = new ArrayList<>(packageManagerInfo.getPkgInfoArgs().get());
        // If absent or buried, the detail query should omit arch; otherwise: the detail query should include arch
        if (archBuried || pkgArch == null) {
            fileSpecificGetDetailsArgs.add(pkgName);
        } else {
            fileSpecificGetDetailsArgs.add(pkgName + ":" + pkgArch);
        }
        String pkgMgrGetDetailsQueryFileOutput = String.format(pkgMgrDetailsQueryResultPattern, dependencyFile);
        ExecutableOutput pkgMgrGetDetailsQueryFileResult = new ExecutableOutput(0, pkgMgrGetDetailsQueryFileOutput, "");
        Mockito.when(executableRunner.execute(workingDirectory, packageManagerInfo.getPkgMgrCmdString(), fileSpecificGetDetailsArgs)).thenReturn(pkgMgrGetDetailsQueryFileResult);
    }
    String pkgMgrGetOwnerQueryFileOutput = String.format(pkgMgrOwnerQueryResultPattern, dependencyFile.getAbsolutePath());
    ExecutableOutput pkgMgrGetOwnerQueryFileResult = new ExecutableOutput(0, pkgMgrGetOwnerQueryFileOutput, "");
    Mockito.when(executableRunner.execute(workingDirectory, packageManagerInfo.getPkgMgrCmdString(), fileSpecificGetOwnerArgs)).thenReturn(pkgMgrGetOwnerQueryFileResult);
    ClangPackageManagerRunner runner = new ClangPackageManagerRunner();
    // Test
    return runner.getPackages(currentPackageManager, workingDirectory, executableRunner, dependencyFile);
}
Also used : ClangPackageManager(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManager) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) ClangPackageManagerRunner(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManagerRunner) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) File(java.io.File)

Example 15 with ExecutableOutput

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

the class GoModCliExtractorTest method handleMultipleReplacementsForOneComponentTest.

// These tests weren't updated to use the new json format for go the first call of go list, yet somehow still passing?
// With the removal of -u, Mockito kept returning null for that first go list call instead of ExecutableOutput.
// I think this test is redundant with the existence of GoModDetectableMinusWhyTest
// Leaving it here for now to review one day. JM-01/2022
@Test
public void handleMultipleReplacementsForOneComponentTest() 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" };

        @Override
        public ExecutableOutput answer(InvocationOnMock invocation) {
            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 {
                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 : 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) Test(org.junit.jupiter.api.Test)

Aggregations

ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)53 File (java.io.File)18 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)14 ArrayList (java.util.ArrayList)14 Extraction (com.synopsys.integration.detectable.extraction.Extraction)13 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)13 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)10 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)9 Executable (com.synopsys.integration.executable.Executable)9 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)8 Test (org.junit.jupiter.api.Test)7 NameVersion (com.synopsys.integration.util.NameVersion)6 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)5 HashMap (java.util.HashMap)5 List (java.util.List)5 StringUtils (org.apache.commons.lang3.StringUtils)5 ExecutableUtils (com.synopsys.integration.detectable.ExecutableUtils)4 IOException (java.io.IOException)4 Optional (java.util.Optional)4 Gson (com.google.gson.Gson)3