Skip to content

Commit

Permalink
Ensure EPUB files are replaced
Browse files Browse the repository at this point in the history
This fixes an issue where EPUB compilation would fail to execute if
destination files already existed.

Fix: #20
  • Loading branch information
io7m committed Feb 3, 2022
1 parent 841681f commit 5ea7f66
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -34,13 +34,18 @@
<c:change date="2021-06-27T00:00:00+00:00" summary="Fix a schema bug that rejected valid footnotes in documents"/>
</c:changes>
</c:release>
<c:release date="2022-02-03T22:01:54+00:00" is-open="true" ticket-system="com.github.io7m.xstructural" version="1.4.0">
<c:release date="2022-02-03T22:13:27+00:00" is-open="true" ticket-system="com.github.io7m.xstructural" version="1.4.0">
<c:changes>
<c:change date="2022-02-03T22:01:54+00:00" summary="Fix a bug where documents with comments would be rejected">
<c:change date="2022-02-03T00:00:00+00:00" summary="Fix a bug where documents with comments would be rejected">
<c:tickets>
<c:ticket id="18"/>
</c:tickets>
</c:change>
<c:change date="2022-02-03T22:13:27+00:00" summary="Ensure existing files are replaced when producing EPUBs">
<c:tickets>
<c:ticket id="20"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
Expand Up @@ -991,4 +991,75 @@ public void testTransformXHTMLEPUBOKExample0_80()
"EPUB file exists"
);
}

@Test
public void testTransformXHTMLEPUBOKExampleTwice_80()
throws Exception
{
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"poppy.jpg"
);
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"missing.jpg"
);
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"woods.jpg"
);

{
final var main = new Main(new String[]{
"epub",
"--sourceFile",
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"example0_80.xml")
.toString(),
"--outputDirectory",
this.outputDirectory.toString(),
"--traceFile",
this.directory.resolve("trace.xml").toString(),
"--messagesFile",
this.directory.resolve("messages.log").toString(),
"--verbose",
"trace"
});

final var capture =
XSOutputCaptured.capture(main::run);

Assertions.assertEquals(0, main.exitCode());
}

{
final var main = new Main(new String[]{
"epub",
"--sourceFile",
XSTestDirectories.resourceOf(
XSCommandLineTest.class,
this.sourceDirectory,
"example0_80.xml")
.toString(),
"--outputDirectory",
this.outputDirectory.toString(),
"--traceFile",
this.directory.resolve("trace.xml").toString(),
"--messagesFile",
this.directory.resolve("messages.log").toString(),
"--verbose",
"trace"
});

final var capture =
XSOutputCaptured.capture(main::run);

Assertions.assertEquals(0, main.exitCode());
}
}
}
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

public final class XSTestDirectories
Expand Down Expand Up @@ -70,7 +71,7 @@ public static Path resourceOf(
LOGGER.debug("copy {} {}", name, target);

try (var stream = url.openStream()) {
Files.copy(stream, target);
Files.copy(stream, target, StandardCopyOption.REPLACE_EXISTING);
}
return target;
}
Expand Down
Expand Up @@ -44,6 +44,10 @@
import java.util.zip.ZipEntry;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

/**
* An EPUB creator.
Expand Down Expand Up @@ -466,7 +470,7 @@ private void copyOEBPS()
this.oebpsDirectory.resolve(file.getFileName().toString());

LOG.info("copy {} {}", file, outputFile);
Files.copy(file, outputFile);
Files.copy(file, outputFile, REPLACE_EXISTING);
}
}
}
Expand All @@ -488,7 +492,11 @@ private void writeContainerFile()
throws IOException
{
try (var stream = resource("container.xml")) {
Files.copy(stream, this.metaDirectory.resolve("container.xml"));
Files.copy(
stream,
this.metaDirectory.resolve("container.xml"),
REPLACE_EXISTING
);
}
}

Expand All @@ -498,7 +506,10 @@ private void writeMimetypeFile()
Files.writeString(
this.epubDirectory.resolve("mimetype"),
"application/epub+zip",
UTF_8
UTF_8,
WRITE,
CREATE,
TRUNCATE_EXISTING
);
}

Expand Down

0 comments on commit 5ea7f66

Please sign in to comment.