Search in sources :

Example 1 with GoModCliExtractor

use of com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor in project synopsys-detect by blackducksoftware.

the class DetectableFactory method goModCliExtractor.

private GoModCliExtractor goModCliExtractor(GoModCliDetectableOptions options) {
    GoModCommandRunner goModCommandRunner = new GoModCommandRunner(executableRunner);
    GoListParser goListParser = new GoListParser(gson);
    GoGraphParser goGraphParser = new GoGraphParser();
    GoModWhyParser goModWhyParser = new GoModWhyParser();
    GoVersionParser goVersionParser = new GoVersionParser();
    GoModGraphGenerator goModGraphGenerator = new GoModGraphGenerator(externalIdFactory);
    return new GoModCliExtractor(goModCommandRunner, goListParser, goGraphParser, goModWhyParser, goVersionParser, goModGraphGenerator, externalIdFactory, options.getExcludedDependencyTypes());
}
Also used : GoListParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoListParser) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) GoModWhyParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoModWhyParser) GoModGraphGenerator(com.synopsys.integration.detectable.detectables.go.gomod.process.GoModGraphGenerator) GoModCommandRunner(com.synopsys.integration.detectable.detectables.go.gomod.GoModCommandRunner) GoVersionParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoVersionParser) GoGraphParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoGraphParser)

Example 2 with GoModCliExtractor

use of com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor 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 3 with GoModCliExtractor

use of com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor 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 4 with GoModCliExtractor

use of com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor in project synopsys-detect by blackducksoftware.

the class GoModCliExtractorTest method buildGoModCliExtractor.

private GoModCliExtractor buildGoModCliExtractor(DetectableExecutableRunner executableRunner, Answer<ExecutableOutput> executableAnswer) throws ExecutableRunnerException {
    Mockito.doAnswer(executableAnswer).when(executableRunner).execute(Mockito.any(Executable.class));
    GoModWhyParser goModWhyParser = new GoModWhyParser();
    GoVersionParser goVersionParser = new GoVersionParser();
    GoModCommandRunner goModCommandRunner = new GoModCommandRunner(executableRunner);
    GoModGraphGenerator goModGraphGenerator = new GoModGraphGenerator(new ExternalIdFactory());
    GoListParser goListParser = new GoListParser(new GsonBuilder().create());
    GoGraphParser goGraphParser = new GoGraphParser();
    ExternalIdFactory externalIdFactory = new ExternalIdFactory();
    return new GoModCliExtractor(goModCommandRunner, goListParser, goGraphParser, goModWhyParser, goVersionParser, goModGraphGenerator, externalIdFactory, GoModDependencyType.UNUSED);
}
Also used : GoListParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoListParser) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) GoModWhyParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoModWhyParser) GoModGraphGenerator(com.synopsys.integration.detectable.detectables.go.gomod.process.GoModGraphGenerator) GsonBuilder(com.google.gson.GsonBuilder) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) GoModCommandRunner(com.synopsys.integration.detectable.detectables.go.gomod.GoModCommandRunner) GoVersionParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoVersionParser) Executable(com.synopsys.integration.executable.Executable) GoGraphParser(com.synopsys.integration.detectable.detectables.go.gomod.parse.GoGraphParser)

Aggregations

GoModCliExtractor (com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor)4 Executable (com.synopsys.integration.executable.Executable)3 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)2 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)2 GoModCommandRunner (com.synopsys.integration.detectable.detectables.go.gomod.GoModCommandRunner)2 GoGraphParser (com.synopsys.integration.detectable.detectables.go.gomod.parse.GoGraphParser)2 GoListParser (com.synopsys.integration.detectable.detectables.go.gomod.parse.GoListParser)2 GoModWhyParser (com.synopsys.integration.detectable.detectables.go.gomod.parse.GoModWhyParser)2 GoVersionParser (com.synopsys.integration.detectable.detectables.go.gomod.parse.GoVersionParser)2 GoModGraphGenerator (com.synopsys.integration.detectable.detectables.go.gomod.process.GoModGraphGenerator)2 Extraction (com.synopsys.integration.detectable.extraction.Extraction)2 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)2 File (java.io.File)2 Test (org.junit.jupiter.api.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 GsonBuilder (com.google.gson.GsonBuilder)1 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)1 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)1 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)1