Commit 021ae5c3 authored by Daniel Bluhm's avatar Daniel Bluhm

Add explicit option for overwriting files if present

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent cf3fceb0
......@@ -11,7 +11,6 @@
package org.org.eclipse.ice.dev.dependencyscraper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.maven.artifact.Artifact;
......@@ -78,11 +77,37 @@ public class DependencyScraper extends AbstractMojo {
)
private List<String> includes;
/**
* Whether files should be overwritten if they already exist.
*/
@Parameter(
property = "clobber",
required = false,
defaultValue = "false"
)
private boolean clobber;
/**
* Set of jar files that will be searched for matching files.
*/
private Set<File> jarFiles;
/**
* Setter for includes.
* @param includes to set.
*/
public void setIncludes(List<String> includes) {
this.includes = includes;
}
/**
* Setter for clobber.
* @param clobber to set.
*/
public void setClobber(boolean clobber) {
this.clobber = clobber;
}
/**
* Setter for jarFiles.
* @param jarFiles to set.
......@@ -134,16 +159,28 @@ public class DependencyScraper extends AbstractMojo {
File target = new File(outputDirectory, relativePath);
try {
if (target.exists()) {
// Replace target if contents differ
File tempFile = File.createTempFile(fullPath, null);
FileUtils.copyInputStreamToFile(
jar.getInputStream(fileInJar), tempFile
);
if (!FileUtils.contentEquals(tempFile, target)) {
FileUtils.forceDelete(target);
FileUtils.moveFile(tempFile, target);
if (clobber) { // Overwrite existing file
getLog().info(String.format(
"File %s already exists and clobber is set; overwriting.",
target.getName()
));
// Replace target only if contents differ
File tempFile = File.createTempFile(fullPath, null);
FileUtils.copyInputStreamToFile(
jar.getInputStream(fileInJar), tempFile
);
if (!FileUtils.contentEquals(tempFile, target)) {
FileUtils.forceDelete(target);
FileUtils.moveFile(tempFile, target);
} else {
tempFile.delete();
}
} else {
tempFile.delete();
getLog().info(String.format(
"File %s already exists and clobber is not set; skipping.",
target.getName()
));
}
} else {
FileUtils.copyInputStreamToFile(
......
package org.org.eclipse.ice.dev.dependencyscraper;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.testing.MojoRule;
import org.junit.After;
import org.junit.Rule;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Set;
public class DependencyScraperTest
{
private static final Path PROJECT = Path.of("target/test-classes/project-to-test");
private static final Path OUTPUT = Path.of("target/test-classes/project-to-test/test");
@Rule
public MojoRule rule = new MojoRule() {
@Override
......@@ -20,27 +29,124 @@ public class DependencyScraperTest
};
/**
* @throws Exception if any
* Get an instance of the DependencyScraper with pretend jar dep already
* set.
* @return
* @throws Exception
*/
@Test
public void testSomething() throws Exception {
File pom = new File("target/test-classes/project-to-test/");
private DependencyScraper getMojo() throws Exception {
File pom = PROJECT.toFile();
assertNotNull(pom);
assertTrue(pom.exists());
DependencyScraper myMojo = (DependencyScraper) rule.lookupConfiguredMojo(pom, "scrape");
assertNotNull(myMojo);
myMojo.setJarFiles(Set.of(new File(
"target/test-classes/project-to-test/pretend_dependency.jar"
)));
myMojo.setJarFiles(Set.of(
PROJECT.resolve("pretend_dependency.jar").toFile()
));
return myMojo;
}
@After
public void clearOutputDirectory() throws IOException {
if (Files.exists(OUTPUT)) {
FileUtils.cleanDirectory(OUTPUT.toFile());
}
}
/**
* Test that the DependencyScraper reads the pom and copies the expected
* file.
* @throws Exception if any
*/
@Test
public void testExecuteWorks() throws Exception {
DependencyScraper myMojo = getMojo();
myMojo.execute();
File outputDirectory = (File) rule.getVariableValueFromObject(myMojo, "outputDirectory");
assertNotNull(outputDirectory);
assertTrue(outputDirectory.exists());
assertTrue(OUTPUT.toFile().exists());
assertTrue(OUTPUT.resolve("test.txt").toFile().exists());
}
/**
* Test that the DependencyScraper reads the pom and runs but does not do
* anything when no matching jars to scrape.
* @throws Exception if any
*/
@Test
public void testExecuteWhenNoJars() throws Exception {
DependencyScraper myMojo = getMojo();
myMojo.setJarFiles(null);
myMojo.execute();
File test = new File(outputDirectory, "test.txt");
assertTrue(test.exists());
assertFalse(OUTPUT.toFile().exists());
}
}
/**
* Test that the clobber option is respected.
* @throws Exception if any
*/
@Test
public void testClobberOverwrites() throws Exception {
DependencyScraper mojo = getMojo();
mojo.setClobber(true);
Path srcFile = PROJECT.resolve("already_exists_but_differs.txt");
Path destFile = OUTPUT.resolve("already_exists_but_differs.txt");
FileUtils.copyFile(srcFile.toFile(), destFile.toFile());
String contents = Files.readString(destFile);
assertEquals("a", contents);
mojo.execute();
contents = Files.readString(destFile);
assertEquals("b", contents);
}
/**
* Test that the clobber won't overwrite if contents are the same.
* @throws Exception if any
*/
@Test
public void testClobberNoOverwriteOnSameContents() throws Exception {
DependencyScraper mojo = getMojo();
mojo.setClobber(true);
Path srcFile = PROJECT.resolve("already_exists_but_is_same.txt");
Path destFile = OUTPUT.resolve("already_exists_but_is_same.txt");
FileUtils.copyFile(srcFile.toFile(), destFile.toFile());
String contents = Files.readString(destFile);
assertEquals("a", contents);
BasicFileAttributes beforeAttrs = Files.readAttributes(
destFile, BasicFileAttributes.class
);
mojo.execute();
BasicFileAttributes afterAttrs = Files.readAttributes(
destFile, BasicFileAttributes.class
);
contents = Files.readString(destFile);
assertEquals("a", contents);
assertEquals(beforeAttrs.creationTime(), afterAttrs.creationTime());
}
/**
* Test that the clobber option is respected.
* @throws Exception if any
*/
@Test
public void testNoClobberPreserves() throws Exception {
DependencyScraper mojo = getMojo();
mojo.setClobber(false);
Path srcFile = PROJECT.resolve("already_exists_but_differs.txt");
Path destFile = OUTPUT.resolve("already_exists_but_differs.txt");
FileUtils.copyFile(srcFile.toFile(), destFile.toFile());
String contents = Files.readString(destFile);
assertEquals("a", contents);
mojo.execute();
contents = Files.readString(destFile);
assertEquals("a", contents);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment