Search in sources :

Example 6 with DetectableEnvironment

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());
}
Also used : NpmPackageJsonParseDetectableOptions(com.synopsys.integration.detectable.detectables.npm.packagejson.NpmPackageJsonParseDetectableOptions) MockFileFinder(com.synopsys.integration.detectable.util.MockFileFinder) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) NpmDependencyType(com.synopsys.integration.detectable.detectables.npm.NpmDependencyType) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) MockDetectableEnvironment(com.synopsys.integration.detectable.util.MockDetectableEnvironment) NpmPackageJsonParseDetectable(com.synopsys.integration.detectable.detectables.npm.packagejson.NpmPackageJsonParseDetectable) Test(org.junit.jupiter.api.Test)

Example 7 with DetectableEnvironment

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());
}
Also used : NpmCliExtractorOptions(com.synopsys.integration.detectable.detectables.npm.cli.NpmCliExtractorOptions) NpmResolver(com.synopsys.integration.detectable.detectable.executable.resolver.NpmResolver) NpmCliExtractor(com.synopsys.integration.detectable.detectables.npm.cli.NpmCliExtractor) NpmCliDetectable(com.synopsys.integration.detectable.detectables.npm.cli.NpmCliDetectable) MockFileFinder(com.synopsys.integration.detectable.util.MockFileFinder) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) MockDetectableEnvironment(com.synopsys.integration.detectable.util.MockDetectableEnvironment) Test(org.junit.jupiter.api.Test)

Example 8 with DetectableEnvironment

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);
}
Also used : FileNotFoundDetectableResult(com.synopsys.integration.detectable.detectable.result.FileNotFoundDetectableResult) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) FileNotFoundDetectableResult(com.synopsys.integration.detectable.detectable.result.FileNotFoundDetectableResult) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) File(java.io.File) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) DartPubDepDetectable(com.synopsys.integration.detectable.detectables.dart.pubdep.DartPubDepDetectable) Test(org.junit.jupiter.api.Test)

Example 9 with DetectableEnvironment

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);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Template(freemarker.template.Template) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 10 with DetectableEnvironment

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);
    }
}
Also used : DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DetectorRule(com.synopsys.integration.detector.rule.DetectorRule) Detectable(com.synopsys.integration.detectable.Detectable) DetectableResult(com.synopsys.integration.detectable.detectable.result.DetectableResult) DetectorResult(com.synopsys.integration.detector.result.DetectorResult) DetectorEvaluation(com.synopsys.integration.detector.base.DetectorEvaluation) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) HashSet(java.util.HashSet)

Aggregations

DetectableEnvironment (com.synopsys.integration.detectable.DetectableEnvironment)18 Test (org.junit.jupiter.api.Test)14 FileFinder (com.synopsys.integration.common.util.finder.FileFinder)11 MockDetectableEnvironment (com.synopsys.integration.detectable.util.MockDetectableEnvironment)9 MockFileFinder (com.synopsys.integration.detectable.util.MockFileFinder)8 File (java.io.File)6 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)5 NugetInspectorResolver (com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspectorResolver)3 NugetInspectorExtractor (com.synopsys.integration.detectable.detectables.nuget.NugetInspectorExtractor)3 NugetInspectorOptions (com.synopsys.integration.detectable.detectables.nuget.NugetInspectorOptions)3 DetectorEvaluation (com.synopsys.integration.detector.base.DetectorEvaluation)3 DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)3 DetectorResult (com.synopsys.integration.detector.result.DetectorResult)3 Detectable (com.synopsys.integration.detectable.Detectable)2 NugetSolutionDetectable (com.synopsys.integration.detectable.detectables.nuget.NugetSolutionDetectable)2 SimpleFileFinder (com.synopsys.integration.common.util.finder.SimpleFileFinder)1 DetectInfo (com.synopsys.integration.detect.configuration.DetectInfo)1 DetectUserFriendlyException (com.synopsys.integration.detect.configuration.DetectUserFriendlyException)1 DetectorToolResult (com.synopsys.integration.detect.tool.detector.DetectorToolResult)1 EventSystem (com.synopsys.integration.detect.workflow.event.EventSystem)1