in javatests/com/google/cloud/deploymentmanager/autogen/VerifyAutogenFileContent.java [56:96]
public void goldenVerifyFile() throws Exception {
File tempDir = Files.createTempDir();
File actualFile = new File(tempDir, fileRelativePath);
Files.createParentDirs(actualFile);
File goldenFile = new File(goldenFolder, fileRelativePath);
if (actualFile.getPath().endsWith(".png") || actualFile.getPath().endsWith(".jpg")) {
Files.write(BaseEncoding.base64().decode(fileContent), actualFile);
String expected = BaseEncoding.base64().encode(Files.toByteArray(goldenFile));
assertWithMessage(generateDiffMessage(tempDir)).that(fileContent).isEqualTo(expected);
} else {
Files.asCharSink(actualFile, StandardCharsets.UTF_8).write(fileContent);
String expected = Files.asCharSource(goldenFile, StandardCharsets.UTF_8).read();
// From StringUtils.difference docs:
// "returns the remainder of the second String, starting from where it's different from the
// first. This means that the difference between "abc" and "ab" is the empty String and not
// "c".
// Because of that we need to compare actual vs expected as well
String diffMessage = generateDiffMessage(tempDir);
// This overwrites the test files when using bazel run
if (System.getenv("BUILD_WORKSPACE_DIRECTORY") != null
&& (!StringUtils.difference(expected, fileContent).isEmpty()
|| !StringUtils.difference(fileContent, expected).isEmpty())) {
File overwriteFile =
new File(
Path.of(
System.getenv("BUILD_WORKSPACE_DIRECTORY"),
".",
AutogenMediumTestsSuite.RELATIVE_TESTDATA_PATH,
Path.of(solutionName).getParent().toString(),
Path.of(goldenFolder).getFileName().toString(),
fileRelativePath)
.toString());
Files.write(fileContent.getBytes(UTF_8), overwriteFile);
}
assertWithMessage(diffMessage).that(StringUtils.difference(expected, fileContent)).isEmpty();
assertWithMessage(diffMessage).that(StringUtils.difference(fileContent, expected)).isEmpty();
}
}