Search in sources :

Example 16 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class ExtractionEvaluatorTest method testEvaluationException.

@Test
public void testEvaluationException() throws DetectableException, ExecutableFailedException, IOException, CycleDetectedException, MissingExternalIdException, ExecutableRunnerException, ParserConfigurationException, SAXException {
    DetectorEvaluationOptions evaluationOptions = Mockito.mock(DetectorEvaluationOptions.class);
    ExtractionEvaluator evaluator = new ExtractionEvaluator(evaluationOptions);
    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, false, true);
    DetectorAggregateEvaluationResult result = evaluator.evaluate(detectorEvaluationTree);
    assertEquals(detectorEvaluationTree, result.getEvaluationTree());
    Mockito.verify(detectorEvaluatorListener).extractionStarted(detectorEvaluation);
    Mockito.verify(detectorEvaluation).setExtraction(Mockito.any(Extraction.class));
    Mockito.verify(detectorEvaluatorListener).extractionEnded(detectorEvaluation);
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) Extraction(com.synopsys.integration.detectable.extraction.Extraction) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 17 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class Evaluator method evaluate.

public DetectorAggregateEvaluationResult evaluate(DetectorEvaluationTree rootEvaluation) {
    DetectorEvaluationTree evaluationTree = performEvaluation(rootEvaluation);
    DetectorAggregateEvaluationResult result = new DetectorAggregateEvaluationResult(evaluationTree);
    executeResultCallbacks(result);
    return result;
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree)

Example 18 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class DetectorFinder method findDetectors.

private Optional<DetectorEvaluationTree> findDetectors(File directory, DetectorRuleSet detectorRuleSet, int depth, DetectorFinderOptions options, FileFinder fileFinder) {
    if (depth > options.getMaximumDepth()) {
        logger.trace("Skipping directory as it exceeds max depth: {}", directory);
        return Optional.empty();
    }
    String directoryString = Optional.ofNullable(directory).map(File::toString).orElse("null");
    if (null == directory || !directory.isDirectory()) {
        logger.trace("Skipping file as it is not a directory: {}", directoryString);
        return Optional.empty();
    }
    if (Files.isSymbolicLink(directory.toPath())) {
        if (!options.followSymLinks()) {
            logger.debug("Skipping file as it is a symbolic link and following symbolic links has been disabled: {}", directoryString);
            return Optional.empty();
        } else {
            logger.debug("Following symbolic link: {}", directoryString);
            Path linkTarget;
            try {
                linkTarget = directory.toPath().toRealPath();
            } catch (IOException e) {
                logger.debug("Symbolic link: {} does not point to a valid directory; skipping it", directoryString);
                return Optional.empty();
            }
            if (!linkTarget.toFile().isDirectory()) {
                logger.debug("Symbolic link: {} does not point to a valid directory; skipping it", directoryString);
                return Optional.empty();
            }
            directory = linkTarget.toFile();
        }
    }
    List<DetectorEvaluation> evaluations = detectorRuleSet.getOrderedDetectorRules().stream().map(DetectorEvaluation::new).collect(Collectors.toList());
    logger.trace("Traversing directory: {}", directory.getPath());
    Set<DetectorEvaluationTree> children = new HashSet<>();
    List<File> subDirectories = fileFinder.findFiles(directory, options.getFileFilter());
    logger.debug("filteredSubDirectories: {}", subDirectories);
    for (File subDirectory : subDirectories) {
        Optional<DetectorEvaluationTree> childEvaluationSet = findDetectors(subDirectory, detectorRuleSet, depth + 1, options, fileFinder);
        childEvaluationSet.ifPresent(children::add);
    }
    return Optional.of(new DetectorEvaluationTree(directory, depth, detectorRuleSet, evaluations, children));
}
Also used : Path(java.nio.file.Path) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) IOException(java.io.IOException) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) File(java.io.File) HashSet(java.util.HashSet)

Example 19 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class DetectorToolTest method executeToolTest.

private DetectorToolResult executeToolTest(Extraction extraction, DetectableResult extractionResult, String projectBomTool) throws DetectableException, ExecutableFailedException {
    ExtractionEnvironmentProvider extractionEnvironmentProvider = Mockito.mock(ExtractionEnvironmentProvider.class);
    DetectorFinder detectorFinder = Mockito.mock(DetectorFinder.class);
    EventSystem eventSystem = Mockito.mock(EventSystem.class);
    CodeLocationConverter codeLocationConverter = Mockito.mock(CodeLocationConverter.class);
    DetectorIssuePublisher detectorIssuePublisher = Mockito.mock(DetectorIssuePublisher.class);
    StatusEventPublisher statusEventPublisher = Mockito.mock(StatusEventPublisher.class);
    ExitCodePublisher exitCodePublisher = Mockito.mock(ExitCodePublisher.class);
    DetectorEventPublisher detectorEventPublisher = Mockito.mock(DetectorEventPublisher.class);
    DetectorTool tool = new DetectorTool(detectorFinder, extractionEnvironmentProvider, eventSystem, codeLocationConverter, detectorIssuePublisher, statusEventPublisher, exitCodePublisher, detectorEventPublisher);
    File directory = new File(".");
    GoModCliDetectable detectable = createDetectable(extraction, extractionResult);
    DetectorRule<GoModCliDetectable> rule = createRule(detectable);
    DetectorRuleSet detectorRuleSet = createRuleSet(rule);
    DetectorFinderOptions detectorFinderOptions = createFinderOptions();
    DetectorEvaluationOptions evaluationOptions = createEvaluationOptions();
    DetectorEvaluationTree evaluationTree = createEvaluationTree(extraction, extractionResult, directory, rule, detectorRuleSet);
    Mockito.when(detectorFinder.findDetectors(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Optional.of(evaluationTree));
    return tool.performDetectors(directory, detectorRuleSet, detectorFinderOptions, evaluationOptions, projectBomTool, new ArrayList<>(), new SimpleFileFinder());
}
Also used : DetectorEvaluationOptions(com.synopsys.integration.detector.evaluation.DetectorEvaluationOptions) GoModCliDetectable(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliDetectable) DetectorFinderOptions(com.synopsys.integration.detector.finder.DetectorFinderOptions) ExtractionEnvironmentProvider(com.synopsys.integration.detect.tool.detector.extraction.ExtractionEnvironmentProvider) ExitCodePublisher(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) DetectorFinder(com.synopsys.integration.detector.finder.DetectorFinder) StatusEventPublisher(com.synopsys.integration.detect.workflow.status.StatusEventPublisher) EventSystem(com.synopsys.integration.detect.workflow.event.EventSystem) File(java.io.File)

Example 20 with DetectorEvaluationTree

use of com.synopsys.integration.detector.base.DetectorEvaluationTree in project synopsys-detect by blackducksoftware.

the class DetectorFinderTest method testSymLinks.

private void testSymLinks(boolean followSymLinks) throws IOException {
    Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS);
    File initialDirectory = createDirWithSymLink("testSymLinks");
    DetectorRuleSet detectorRuleSet = new DetectorRuleSet(new ArrayList<>(0), new HashMap<>(0));
    DetectorFinderOptions options = createFinderOptions(followSymLinks);
    DetectorFinder finder = new DetectorFinder();
    Optional<DetectorEvaluationTree> tree = finder.findDetectors(initialDirectory, detectorRuleSet, options, new SimpleFileFinder());
    // make sure the symlink was omitted from results
    // final Set<DetectorEvaluationTree> subDirResults = tree.get().getChildren().iterator().next().getChildren();
    Set<DetectorEvaluationTree> testDirs = tree.get().getChildren();
    DetectorEvaluationTree symLinkTestDir = null;
    for (DetectorEvaluationTree testDir : testDirs) {
        if (testDir.getDirectory().getName().equals("testSymLinks")) {
            symLinkTestDir = testDir;
            break;
        }
    }
    Set<DetectorEvaluationTree> subDirResults = symLinkTestDir.getChildren();
    if (followSymLinks) {
        assertEquals(2, subDirResults.size());
    } else {
        assertEquals(1, subDirResults.size());
        String subDirContentsName = subDirResults.iterator().next().getDirectory().getName();
        assertEquals("regularDir", subDirContentsName);
    }
    FileUtils.deleteDirectory(initialDirectory);
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) File(java.io.File) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet)

Aggregations

DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)37 DetectorEvaluation (com.synopsys.integration.detector.base.DetectorEvaluation)28 File (java.io.File)18 Test (org.junit.jupiter.api.Test)17 DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)11 Detectable (com.synopsys.integration.detectable.Detectable)10 DetectorRule (com.synopsys.integration.detector.rule.DetectorRule)9 DetectorResult (com.synopsys.integration.detector.result.DetectorResult)8 DetectorRuleSet (com.synopsys.integration.detector.rule.DetectorRuleSet)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 ExtractionEnvironment (com.synopsys.integration.detectable.extraction.ExtractionEnvironment)7 Predicate (java.util.function.Predicate)7 Mockito (org.mockito.Mockito)7 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)6 List (java.util.List)6 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)5 Extraction (com.synopsys.integration.detectable.extraction.Extraction)5 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 HashSet (java.util.HashSet)5