use of com.synopsys.integration.detectable.ExecutableTarget in project synopsys-detect by blackducksoftware.
the class GoModCliExtractorTest method handleMultipleReplacementsForOneComponentTest.
// These tests weren't updated to use the new json format for go the first call of go list, yet somehow still passing?
// With the removal of -u, Mockito kept returning null for that first go list call instead of ExecutableOutput.
// I think this test is redundant with the existence of GoModDetectableMinusWhyTest
// Leaving it here for now to review one day. JM-01/2022
@Test
public void handleMultipleReplacementsForOneComponentTest() 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" };
@Override
public ExecutableOutput answer(InvocationOnMock invocation) {
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 {
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.ExecutableTarget 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.ExecutableTarget in project synopsys-detect by blackducksoftware.
the class DockerExtractorTest method extract.
private Extraction extract(String image, String imageId, String tar, File returnedContainerFileSystemFile, File returnedSquashedImageFile, File resultsFile, DetectableExecutableRunner executableRunner) throws IOException, ExecutableRunnerException {
FileFinder fileFinder = Mockito.mock(FileFinder.class);
DockerExtractor dockerExtractor = getMockDockerExtractor(executableRunner, fileFinder);
File directory = new File(".");
File outputDirectory = new File("build/tmp/test/DockerExtractorTest");
ExecutableTarget bashExe = null;
ExecutableTarget javaExe = ExecutableTarget.forFile(new File("fake/test/java"));
DockerInspectorInfo dockerInspectorInfo = Mockito.mock(DockerInspectorInfo.class);
Mockito.when(fileFinder.findFile(outputDirectory, DockerExtractor.CONTAINER_FILESYSTEM_FILENAME_PATTERN)).thenReturn(returnedContainerFileSystemFile);
Mockito.when(fileFinder.findFile(outputDirectory, DockerExtractor.SQUASHED_IMAGE_FILENAME_PATTERN)).thenReturn(returnedSquashedImageFile);
Mockito.when(fileFinder.findFile(outputDirectory, DockerExtractor.RESULTS_FILENAME_PATTERN)).thenReturn(resultsFile);
Mockito.when(dockerInspectorInfo.getDockerInspectorJar()).thenReturn(new File("fake/test/dockerinspector.jar"));
return dockerExtractor.extract(directory, outputDirectory, bashExe, javaExe, image, imageId, tar, dockerInspectorInfo, Mockito.mock(DockerProperties.class));
}
use of com.synopsys.integration.detectable.ExecutableTarget 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.ExecutableTarget in project synopsys-detect by blackducksoftware.
the class NpmCliExtractor method extract.
public Extraction extract(File directory, ExecutableTarget npmExe, @Nullable String npmArguments, File packageJsonFile) {
toolVersionLogger.log(directory, npmExe);
PackageJson packageJson;
try {
packageJson = parsePackageJson(packageJsonFile);
} catch (IOException e) {
return new Extraction.Builder().exception(e).build();
}
List<String> exeArgs = new ArrayList<>();
exeArgs.add("ls");
exeArgs.add("-json");
Optional.ofNullable(npmArguments).map(arg -> arg.split(" ")).ifPresent(additionalArguments -> exeArgs.addAll(Arrays.asList(additionalArguments)));
ExecutableOutput npmLsOutput;
try {
npmLsOutput = executableRunner.execute(ExecutableUtils.createFromTarget(directory, npmExe, exeArgs));
} catch (Exception e) {
return new Extraction.Builder().exception(e).build();
}
String standardOutput = npmLsOutput.getStandardOutput();
String errorOutput = npmLsOutput.getErrorOutput();
if (StringUtils.isNotBlank(errorOutput)) {
logger.error("Error when running npm ls -json command");
logger.error(errorOutput);
return new Extraction.Builder().failure("Npm wrote to stderr while running npm ls.").build();
} else if (StringUtils.isNotBlank(standardOutput)) {
logger.debug("Parsing npm ls file.");
logger.debug(standardOutput);
NpmPackagerResult result = npmCliParser.generateCodeLocation(standardOutput, packageJson);
String projectName = result.getProjectName() != null ? result.getProjectName() : packageJson.name;
String projectVersion = result.getProjectVersion() != null ? result.getProjectVersion() : packageJson.version;
return new Extraction.Builder().success(result.getCodeLocation()).projectName(projectName).projectVersion(projectVersion).build();
} else {
logger.error("Nothing returned from npm ls -json command");
return new Extraction.Builder().failure("Npm returned error after running npm ls.").build();
}
}
Aggregations