use of com.synopsys.integration.detectable.DetectableEnvironment in project synopsys-detect by blackducksoftware.
the class NpmPackageJsonParseDetectableTest method testApplicable.
@Test
public void testApplicable() {
DetectableEnvironment environment = MockDetectableEnvironment.empty();
FileFinder fileFinder = MockFileFinder.withFileNamed(PACKAGE_JSON_FILENAME);
EnumListFilter<NpmDependencyType> npmDependencyTypeFilter = EnumListFilter.fromExcluded(NpmDependencyType.DEV, NpmDependencyType.PEER);
NpmPackageJsonParseDetectableOptions npmPackageJsonParseDetectableOptions = new NpmPackageJsonParseDetectableOptions(npmDependencyTypeFilter);
NpmPackageJsonParseDetectable detectable = new NpmPackageJsonParseDetectable(environment, fileFinder, null);
assertTrue(detectable.applicable().getPassed());
}
use of com.synopsys.integration.detectable.DetectableEnvironment in project synopsys-detect by blackducksoftware.
the class NpmCliDetectableTest method testApplicable.
@Test
public void testApplicable() {
NpmResolver npmResolver = null;
NpmCliExtractor npmCliExtractor = null;
DetectableEnvironment environment = MockDetectableEnvironment.empty();
FileFinder fileFinder = MockFileFinder.withFileNamed("package.json");
NpmCliDetectable detectable = new NpmCliDetectable(environment, fileFinder, npmResolver, npmCliExtractor, new NpmCliExtractorOptions(EnumListFilter.fromExcluded(NpmDependencyType.DEV, NpmDependencyType.PEER), ""));
assertTrue(detectable.applicable().getPassed());
}
use of com.synopsys.integration.detectable.DetectableEnvironment in project synopsys-detect by blackducksoftware.
the class PubDepsDetectableTest method testThrowExceptionWhenLockFilePresentButNotYaml.
@Test
public void testThrowExceptionWhenLockFilePresentButNotYaml() throws DetectableException {
FileFinder fileFinder = Mockito.mock(FileFinder.class);
Mockito.when(fileFinder.findFile(null, "pubspec.yaml")).thenReturn(null);
Mockito.when(fileFinder.findFile(null, "pubspec.lock")).thenReturn(new File(""));
DartPubDepDetectable dartPubDepDetectable = new DartPubDepDetectable(new DetectableEnvironment(null), fileFinder, null, null, null, null);
DetectableResult applicable = dartPubDepDetectable.applicable();
Assertions.assertTrue(applicable.getPassed());
DetectableResult extractable = dartPubDepDetectable.extractable();
Assertions.assertTrue(extractable instanceof FileNotFoundDetectableResult);
}
use of com.synopsys.integration.detectable.DetectableEnvironment in project synopsys-detect by blackducksoftware.
the class GradleAirGapCreator method installGradleDependencies.
public void installGradleDependencies(File gradleTemp, File gradleTarget, String inspectorVersion) throws DetectUserFriendlyException {
logger.info("Checking for gradle on the path.");
ExecutableTarget gradle;
try {
gradle = gradleResolver.resolveGradle(new DetectableEnvironment(gradleTemp));
if (gradle == null) {
throw new DetectUserFriendlyException("Gradle must be on the path to make an Air Gap zip.", ExitCodeType.FAILURE_CONFIGURATION);
}
} catch (DetectableException e) {
throw new DetectUserFriendlyException("An error occurred while finding Gradle which is needed to make an Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
}
File gradleOutput = new File(gradleTemp, "dependencies");
logger.info("Using temporary gradle dependency directory: " + gradleOutput);
File buildGradle = new File(gradleTemp, "build.gradle");
File settingsGradle = new File(gradleTemp, "settings.gradle");
logger.info("Using temporary gradle build file: " + buildGradle);
logger.info("Using temporary gradle settings file: " + settingsGradle);
FileUtil.createMissingParentDirectories(buildGradle);
FileUtil.createMissingParentDirectories(settingsGradle);
logger.info("Writing to temporary gradle build file.");
try {
Map<String, String> gradleScriptData = new HashMap<>();
gradleScriptData.put("gradleOutput", StringEscapeUtils.escapeJava(gradleOutput.getCanonicalPath()));
Template gradleScriptTemplate = configuration.getTemplate("create-gradle-airgap-script.ftl");
try (Writer fileWriter = new FileWriter(buildGradle)) {
gradleScriptTemplate.process(gradleScriptData, fileWriter);
}
FileUtils.writeStringToFile(settingsGradle, "", StandardCharsets.UTF_8);
} catch (IOException | TemplateException e) {
throw new DetectUserFriendlyException("An error occurred creating the temporary build.gradle while creating the Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
}
logger.info("Invoking gradle install on temporary directory.");
try {
ExecutableOutput executableOutput = executableRunner.execute(ExecutableUtils.createFromTarget(gradleTemp, gradle, "installDependencies"));
if (executableOutput.getReturnCode() != 0) {
throw new DetectUserFriendlyException("Gradle returned a non-zero exit code while installing Air Gap dependencies.", ExitCodeType.FAILURE_CONFIGURATION);
}
} catch (ExecutableRunnerException e) {
throw new DetectUserFriendlyException("An error occurred using Gradle to make an Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
}
try {
logger.info("Moving generated dependencies to final gradle folder: " + gradleTarget.getCanonicalPath());
FileUtils.moveDirectory(gradleOutput, gradleTarget);
FileUtils.deleteDirectory(gradleTemp);
} catch (IOException e) {
throw new DetectUserFriendlyException("An error occurred moving gradle dependencies to Air Gap folder.", ExitCodeType.FAILURE_CONFIGURATION);
}
}
use of com.synopsys.integration.detectable.DetectableEnvironment in project synopsys-detect by blackducksoftware.
the class ApplicableEvaluator method searchAndApplicableEvaluation.
public void searchAndApplicableEvaluation(DetectorEvaluationTree detectorEvaluationTree, Set<DetectorRule> appliedInParent) {
logger.trace("Determining applicable detectors on the directory: {}", detectorEvaluationTree.getDirectory());
Set<DetectorRule> appliedSoFar = new HashSet<>();
for (DetectorEvaluation detectorEvaluation : detectorEvaluationTree.getOrderedEvaluations()) {
getDetectorEvaluatorListener().ifPresent(it -> it.applicableStarted(detectorEvaluation));
DetectorRule detectorRule = detectorEvaluation.getDetectorRule();
logger.trace("Evaluating detector: {}", detectorRule.getDescriptiveName());
SearchEnvironment searchEnvironment = new SearchEnvironment(detectorEvaluationTree.getDepthFromRoot(), getEvaluationOptions().getDetectorFilter(), getEvaluationOptions().isForceNested(), getEvaluationOptions().isFollowSymLinks(), appliedInParent, appliedSoFar);
detectorEvaluation.setSearchEnvironment(searchEnvironment);
DetectorResult searchableResult = detectorRuleSetEvaluator.evaluateSearchable(detectorEvaluationTree.getDetectorRuleSet(), detectorEvaluation.getDetectorRule(), searchEnvironment);
detectorEvaluation.setSearchable(searchableResult);
if (detectorEvaluation.isSearchable()) {
logger.trace("Searchable passed, will continue evaluating.");
// TODO: potential todo, this could be invoked as part of the rule - ie we make a DetectableEnvironmentCreatable and the file could be given to the creatable (detectorRule.createEnvironment(file)
DetectableEnvironment detectableEnvironment = new DetectableEnvironment(detectorEvaluationTree.getDirectory());
detectorEvaluation.setDetectableEnvironment(detectableEnvironment);
Detectable detectable = detectorRule.createDetectable(detectableEnvironment);
detectorEvaluation.setDetectable(detectable);
DetectableResult applicable = detectable.applicable();
DetectorResult applicableResult = new DetectorResult(applicable.getPassed(), applicable.toDescription(), applicable.getClass(), applicable.getExplanation(), applicable.getRelevantFiles());
detectorEvaluation.setApplicable(applicableResult);
if (detectorEvaluation.isApplicable()) {
logger.trace("Found applicable detector: {}", detectorRule.getDescriptiveName());
appliedSoFar.add(detectorRule);
} else {
logger.trace("Applicable did not pass: {}", detectorEvaluation.getApplicabilityMessage());
}
} else {
logger.trace("Searchable did not pass: {}", detectorEvaluation.getSearchabilityMessage());
}
getDetectorEvaluatorListener().ifPresent(it -> it.applicableEnded(detectorEvaluation));
}
if (!appliedSoFar.isEmpty()) {
// TODO: Perfect log level also matters here. To little and we may appear stuck, but we may also be flooding the logs.
logger.debug("Found ({}) applicable detectors in: {}", appliedSoFar.size(), detectorEvaluationTree.getDirectory());
}
Set<DetectorRule> nextAppliedInParent = new HashSet<>();
nextAppliedInParent.addAll(appliedInParent);
nextAppliedInParent.addAll(appliedSoFar);
for (DetectorEvaluationTree childDetectorEvaluationTree : detectorEvaluationTree.getChildren()) {
searchAndApplicableEvaluation(childDetectorEvaluationTree, nextAppliedInParent);
}
}
Aggregations