use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.
the class DetectorRuleSetEvaluator method evaluateSearchable.
public DetectorResult evaluateSearchable(DetectorRuleSet detectorRuleSet, DetectorRule detectorRule, SearchEnvironment environment) {
if (!environment.getDetectorFilter().test(detectorRule)) {
return new ExcludedDetectorResult();
}
int maxDepth = detectorRule.getMaxDepth();
if (environment.getDepth() > maxDepth) {
return new MaxDepthExceededDetectorResult(environment.getDepth(), maxDepth);
}
Set<DetectorRule> yieldTo = environment.getAppliedSoFar().stream().filter(it -> detectorRuleSet.getYieldsTo(detectorRule).contains(it)).collect(Collectors.toSet());
if (yieldTo.size() > 0) {
return new YieldedDetectorResult(yieldTo.stream().map(DetectorRule::getName).collect(Collectors.toSet()));
}
boolean nestable = detectorRule.isNestable();
boolean selfNestable = detectorRule.isSelfNestable();
boolean selfTypeNestable = detectorRule.isSelfTypeNestable();
DetectorType detectorType = detectorRule.getDetectorType();
Set<DetectorType> notNestableBeneath = detectorRule.getNotNestableBeneath();
if (environment.isForceNestedSearch()) {
return new ForcedNestedPassedDetectorResult();
} else if (nestable) {
if (!selfNestable && environment.getAppliedToParent().stream().anyMatch(detectorRule::equals)) {
return new NotSelfNestableDetectorResult();
}
if (!selfTypeNestable && environment.getAppliedToParent().stream().map(DetectorRule::getDetectorType).anyMatch(detectorType::equals)) {
return new NotSelfTypeNestableDetectorResult(detectorType);
}
if (notNestableBeneath.size() > 0) {
Optional<DetectorType> notNestableBeneathType = environment.getAppliedToParent().stream().map(DetectorRule::getDetectorType).filter(notNestableBeneath::contains).findAny();
if (notNestableBeneathType.isPresent()) {
return new NotNestableBeneathDetectorResult(notNestableBeneathType.get());
}
}
} else if (environment.getAppliedToParent().stream().anyMatch(it -> !it.isNestInvisible())) {
return new NotNestableDetectorResult();
}
return new PassedDetectorResult();
}
use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.
the class HelpJsonManager method createHelpJsonDetectorList.
private List<HelpJsonDetector> createHelpJsonDetectorList(boolean buildless) {
DetectorRuleFactory ruleFactory = new DetectorRuleFactory();
// TODO: Is there a better way to build a fake set of rules?
DetectDetectableFactory mockFactory = new DetectDetectableFactory(null, null, null, null, null, null, null, null);
DetectorRuleSet ruleSet = ruleFactory.createRules(mockFactory, buildless);
return ruleSet.getOrderedDetectorRules().stream().map(detectorRule -> convertDetectorRule(detectorRule, ruleSet)).collect(Collectors.toList());
}
use of com.synopsys.integration.detector.rule.DetectorRuleSet 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());
}
use of com.synopsys.integration.detector.rule.DetectorRuleSet 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);
}
use of com.synopsys.integration.detector.rule.DetectorRuleSet in project synopsys-detect by blackducksoftware.
the class ExtractableEvaluatorTest method createEvaluationMocks.
private DetectorEvaluation createEvaluationMocks(DetectorEvaluationOptions evaluationOptions, DetectorEvaluationTree detectorEvaluationTree, boolean extractable, boolean throwException) throws DetectableException {
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);
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(detectorEvaluation.isSearchable()).thenReturn(true);
Mockito.when(detectorEvaluation.isApplicable()).thenReturn(true);
Mockito.when(detectorEvaluation.isExtractable()).thenReturn(extractable);
Mockito.when(detectorRule.createDetectable(Mockito.any(DetectableEnvironment.class))).thenReturn(detectable);
Mockito.when(detectable.applicable()).thenReturn(new PassedDetectableResult());
if (throwException) {
Mockito.when(detectable.extractable()).thenThrow(new DetectableException("JUnit Expected Exception."));
} else {
Mockito.when(detectable.extractable()).thenReturn(detectableExtractableResult);
}
return detectorEvaluation;
}
Aggregations