use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.
the class PnpmYamlTransformer method buildGraph.
private void buildGraph(DependencyGraph graphBuilder, List<String> rootPackageIds, @Nullable Map<String, PnpmPackageInfo> packageMap, PnpmLinkedPackageResolver linkedPackageResolver, @Nullable String reportingProjectPackagePath) throws IntegrationException {
if (packageMap == null) {
throw new DetectableException("Could not parse 'packages' section of the pnpm-lock.yaml file.");
}
for (Map.Entry<String, PnpmPackageInfo> packageEntry : packageMap.entrySet()) {
String packageId = packageEntry.getKey();
Optional<Dependency> pnpmPackage = buildDependencyFromPackageEntry(packageEntry);
if (!pnpmPackage.isPresent()) {
logger.debug(String.format("Could not add package %s to the graph.", packageId));
continue;
}
if (isRootPackage(packageId, rootPackageIds)) {
graphBuilder.addChildToRoot(pnpmPackage.get());
}
PnpmPackageInfo packageInfo = packageEntry.getValue();
if (dependencyTypeFilter.shouldInclude(packageInfo.getDependencyType())) {
for (Map.Entry<String, String> packageDependency : packageInfo.getDependencies().entrySet()) {
String dependencyPackageId = convertRawEntryToPackageId(packageDependency, linkedPackageResolver, reportingProjectPackagePath);
Optional<Dependency> child = buildDependencyFromPackageId(dependencyPackageId);
child.ifPresent(c -> graphBuilder.addChildWithParent(child.get(), pnpmPackage.get()));
}
}
}
}
use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.
the class GoModCliExtractorTest method handleGoModWhyExceptionTest.
@Test
public void handleGoModWhyExceptionTest() throws ExecutableRunnerException, ExecutableFailedException, DetectableException {
DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
File directory = new File("");
ExecutableTarget goExe = ExecutableTarget.forFile(new File(""));
Answer<ExecutableOutput> executableAnswer = new Answer<ExecutableOutput>() {
String[] goListArgs = { "list", "-m", "-json" };
String[] goListJsonArgs = { "list", "-m", "-json", "all" };
String[] goModGraphArgs = { "mod", "graph" };
String[] goModWhyArgs = { "mod", "why", "-m", "all" };
@Override
public ExecutableOutput answer(InvocationOnMock invocation) throws Throwable {
Executable executable = invocation.getArgument(0, Executable.class);
List<String> commandLine = executable.getCommandWithArguments();
ExecutableOutput result = null;
if (commandLine.containsAll(Arrays.asList(goListJsonArgs))) {
result = goListJsonOutput();
} else if (commandLine.containsAll(Arrays.asList(goListArgs))) {
result = goListOutput();
} else if (commandLine.containsAll(Arrays.asList(goModGraphArgs))) {
result = goModGraphOutput();
} else if (commandLine.containsAll(Arrays.asList(goModWhyArgs))) {
throw new ExecutableRunnerException(new DetectableException("Unit Test Go Mod Why error"));
} else {
result = new ExecutableOutput(0, "", "");
}
return result;
}
};
GoModCliExtractor goModCliExtractor = buildGoModCliExtractor(executableRunner, executableAnswer);
boolean wasSuccessful = true;
Extraction extraction = goModCliExtractor.extract(directory, goExe);
if (extraction.getError() instanceof ArrayIndexOutOfBoundsException) {
wasSuccessful = false;
}
Assertions.assertTrue(wasSuccessful);
}
use of com.synopsys.integration.detectable.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.
the class ExtractableEvaluatorTest method testEvaluationExtractableAlreadyPerformed.
@Test
public void testEvaluationExtractableAlreadyPerformed() throws DetectableException {
DetectorEvaluationOptions evaluationOptions = Mockito.mock(DetectorEvaluationOptions.class);
ExtractionEnvironment extractionEnvironment = Mockito.mock(ExtractionEnvironment.class);
Function<DetectorEvaluation, ExtractionEnvironment> extractionEnvironmentProvider = (detectorEvaluation) -> extractionEnvironment;
ExtractableEvaluator evaluator = new ExtractableEvaluator(evaluationOptions, extractionEnvironmentProvider);
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, true, false);
DetectorAggregateEvaluationResult result = evaluator.evaluate(detectorEvaluationTree);
assertEquals(detectorEvaluationTree, result.getEvaluationTree());
Mockito.verify(detectorEvaluatorListener).extractableStarted(detectorEvaluation);
Mockito.verify(detectorEvaluation).setExtractable(Mockito.any(DetectorResult.class));
Mockito.verify(detectorEvaluatorListener).extractableEnded(detectorEvaluation);
}
use of com.synopsys.integration.detectable.detectable.exception.DetectableException 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.detectable.exception.DetectableException in project synopsys-detect by blackducksoftware.
the class NugetInspectorAirGapCreator method install.
private void install(File container, String targetDirectory, String propertyName) throws DetectUserFriendlyException {
try {
File destination = new File(container, targetDirectory);
// actually downloads it to 'destination/zipName'
File downloaded = inspectorInstaller.downloadZip(propertyName, destination);
// move 'destination/zipName' to 'destination'
FileUtils.copyDirectory(downloaded, destination);
// delete 'destination/zipName'
FileUtils.deleteDirectory(downloaded);
} catch (DetectableException | IOException e) {
throw new DetectUserFriendlyException("An error occurred installing project-inspector to the " + targetDirectory + " folder.", e, ExitCodeType.FAILURE_GENERAL_ERROR);
}
}
Aggregations