use of com.synopsys.integration.detector.rule.DetectorRule 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.DetectorRule 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.DetectorRule in project synopsys-detect by blackducksoftware.
the class ApplicableEvaluatorTest method createEvaluationMocks.
private DetectorEvaluation createEvaluationMocks(DetectorEvaluationOptions evaluationOptions, DetectorEvaluationTree detectorEvaluationTree, boolean alreadyApplicable, boolean searchable) {
DetectorEvaluation detectorEvaluation = Mockito.mock(DetectorEvaluation.class);
List<DetectorEvaluation> detectorEvaluations = Collections.singletonList(detectorEvaluation);
Mockito.when(detectorEvaluationTree.getOrderedEvaluations()).thenReturn(detectorEvaluations);
DetectorRule detectorRule = Mockito.mock(DetectorRule.class);
Mockito.when(detectorRule.getDescriptiveName()).thenReturn("test rule");
Mockito.when(detectorEvaluation.getDetectorRule()).thenReturn(detectorRule);
Mockito.when(detectorEvaluation.isApplicable()).thenReturn(alreadyApplicable);
Mockito.when(detectorEvaluation.isSearchable()).thenReturn(searchable);
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);
Detectable detectable = Mockito.mock(Detectable.class);
Mockito.when(detectorRule.createDetectable(Mockito.any(DetectableEnvironment.class))).thenReturn(detectable);
Mockito.when(detectable.applicable()).thenReturn(new FailedDetectableResult());
return detectorEvaluation;
}
use of com.synopsys.integration.detector.rule.DetectorRule in project synopsys-detect by blackducksoftware.
the class DetectorRuleSetEvaluatorTest method nestableExcept.
@Test
public void nestableExcept() {
DetectorRuleSetBuilder ruleSet = new DetectorRuleSetBuilder();
DetectorRule xcode = ruleSet.addDetector(DetectorType.XCODE, "Xcode", XcodeProjectDetectable.class, (e) -> null).defaults().build();
DetectorRule swift = ruleSet.addDetector(DetectorType.SWIFT, "Swift", SwiftCliDetectable.class, (e) -> null).defaults().nestableExceptTo(DetectorType.XCODE).build();
// XCODE applied at depth 0, we are now scanning a folder at depth 2.
Set<DetectorRule> appliedToParent = Sets.newHashSet(xcode);
Set<DetectorRule> appliedSoFar = Sets.newHashSet();
SearchEnvironment searchEnvironment = new SearchEnvironment(2, (d) -> true, false, false, appliedToParent, appliedSoFar);
DetectorResult result = new DetectorRuleSetEvaluator().evaluateSearchable(ruleSet.build(), swift, searchEnvironment);
Assertions.assertEquals(NotNestableBeneathDetectorResult.class, result.getClass());
}
use of com.synopsys.integration.detector.rule.DetectorRule in project synopsys-detect by blackducksoftware.
the class DetectorRuleSetEvaluatorTest method nestableExceptByDetectorType.
@Test
public void nestableExceptByDetectorType() {
DetectorRuleSetBuilder ruleSet = new DetectorRuleSetBuilder();
DetectorRule workspaceRule = ruleSet.addDetector(DetectorType.XCODE, "Xcode Workspace", XcodeWorkspaceDetectable.class, (e) -> null).defaults().notSelfTypeNestable().build();
DetectorRule projectRule = ruleSet.addDetector(DetectorType.XCODE, "Xcode Project", XcodeProjectDetectable.class, (e) -> null).defaults().notSelfTypeNestable().build();
// XCODE applied at depth 0, we are now scanning a folder at depth 2.
Set<DetectorRule> appliedToParent = Sets.newHashSet(workspaceRule);
Set<DetectorRule> appliedSoFar = Sets.newHashSet();
SearchEnvironment searchEnvironment = new SearchEnvironment(2, (d) -> true, false, false, appliedToParent, appliedSoFar);
DetectorResult result = new DetectorRuleSetEvaluator().evaluateSearchable(ruleSet.build(), projectRule, searchEnvironment);
Assertions.assertEquals(NotSelfTypeNestableDetectorResult.class, result.getClass());
}
Aggregations