Search in sources :

Example 6 with Executable

use of com.synopsys.integration.executable.Executable 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 Executable

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

use of com.synopsys.integration.executable.Executable 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)

Example 9 with Executable

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

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

the class DockerExtractorTest method testExtractTarReturningContainerFileSystem.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractTarReturningContainerFileSystem() throws ExecutableRunnerException, IOException {
    String image = null;
    String imageId = null;
    String tar = fakeDockerTarFile.getAbsolutePath();
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, fakeContainerFileSystemFile, null, fakeResultsFile, executableRunner);
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get().endsWith("testDockerTarfile.tar"));
    assertTrue(extraction.getMetaData(DockerExtractor.CONTAINER_FILESYSTEM_META_DATA).get().getName().endsWith("_containerfilesystem.tar.gz"));
    ArgumentCaptor<Executable> executableArgumentCaptor = ArgumentCaptor.forClass(Executable.class);
    Mockito.verify(executableRunner).execute(executableArgumentCaptor.capture());
    Executable executableToVerify = executableArgumentCaptor.getValue();
    List<String> command = executableToVerify.getCommandWithArguments();
    assertTrue(command.get(0).endsWith("/fake/test/java"));
    assertEquals("-jar", command.get(1));
    assertTrue(command.get(2).endsWith("/fake/test/dockerinspector.jar"));
    assertTrue(command.get(3).startsWith("--spring.config.location="));
    assertTrue(command.get(3).endsWith("/application.properties"));
    assertTrue(command.get(4).startsWith("--docker.tar="));
    assertTrue(command.get(4).endsWith("testDockerTarfile.tar"));
}
Also used : DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test)

Aggregations

Executable (com.synopsys.integration.executable.Executable)18 Extraction (com.synopsys.integration.detectable.extraction.Extraction)11 File (java.io.File)9 ArrayList (java.util.ArrayList)8 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)7 HashMap (java.util.HashMap)7 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)6 Test (org.junit.jupiter.api.Test)6 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)4 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)3 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)3 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)3 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)3 ProcessBuilderRunner (com.synopsys.integration.executable.ProcessBuilderRunner)3 GoModCliExtractor (com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor)2 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)2 IOException (java.io.IOException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 GraphParser (com.paypal.digraph.parser.GraphParser)1