Skip to content

Commit

Permalink
skip copying single file if file is a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ancho committed Jan 19, 2019
1 parent 883e110 commit fdd7385
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
10 changes: 7 additions & 3 deletions jbake-core/src/main/java/org/jbake/app/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ public boolean accept(File file) {
*/
public void copySingleFile(File asset) {
try {
String targetPath = config.getDestinationFolder().getCanonicalPath() + File.separatorChar + assetSubPath(asset);
LOGGER.info("Copying single asset file to [" + targetPath + "]");
copyFile(asset, new File(targetPath));
if ( !asset.isDirectory() ) {
String targetPath = config.getDestinationFolder().getCanonicalPath() + File.separatorChar + assetSubPath(asset);
LOGGER.info("Copying single asset file to [{}]", targetPath);
copyFile(asset, new File(targetPath));
} else {
LOGGER.info("Skip copying single asset file [{}]. Is a directory.", asset.getPath());
}
} catch (IOException io) {
LOGGER.error("Failed to copy the asset file.", io);
}
Expand Down
38 changes: 37 additions & 1 deletion jbake-core/src/test/java/org/jbake/app/AssetTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jbake.app;

import ch.qos.logback.classic.spi.LoggingEvent;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.jbake.TestUtils;
Expand All @@ -13,9 +14,14 @@

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;

public class AssetTest {
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class AssetTest extends LoggingTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();
Expand Down Expand Up @@ -67,6 +73,36 @@ public void testCopySingleFile() throws Exception {
Assert.assertTrue("Content img file did not copy", expected.exists());
}

@Test
public void shouldSkipCopyingSingleFileIfDirectory() throws IOException {

Asset asset = new Asset(config);

File emptyDir = folder.newFolder("emptyDir");
File expectedDir = new File(fixtureDir.getCanonicalPath(), "emptyDir");

asset.copySingleFile(emptyDir);

Assert.assertFalse("Directory should be skipped", expectedDir.exists());
}

@Test
public void shouldLogSkipCopyingSingleFileIfDirectory() throws IOException {

Asset asset = new Asset(config);
File emptyDir = folder.newFolder("emptyDir");

asset.copySingleFile(emptyDir);

verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());

LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertThat(loggingEvent.getMessage()).isEqualTo("Skip copying single asset file [{}]. Is a directory.");

}



@Test
public void testCopyCustomFolder() throws Exception {
config.setAssetFolder(new File(config.getSourceFolder(),"/media"));
Expand Down
6 changes: 3 additions & 3 deletions jbake-core/src/test/java/org/jbake/app/LoggingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -26,15 +26,15 @@ public abstract class LoggingTest {
protected Logger root;

@Before
public void setup() {
public void setupBase() {
root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

root.addAppender(mockAppender);
root.setLevel(Level.INFO);
}

@After
public void teardown() {
public void teardownBase() {
root.detachAppender(mockAppender);
}

Expand Down

0 comments on commit fdd7385

Please sign in to comment.