Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add file extension at the end of filename if not provided #20

Merged
merged 10 commits into from
Dec 31, 2023
10 changes: 6 additions & 4 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,6 @@ func (e *Epub) addSection(parentFilename string, body string, sectionTitle strin
return "", &ParentDoesNotExistError{Filename: parentFilename}
}

// TODO: if filename exist with customename it should change that automatically or retrurn error
//for example add section with "Batman.xhtml" should be handle

// Generate a filename if one isn't provided
if internalFilename == "" {
index := 1
Expand All @@ -355,7 +352,12 @@ func (e *Epub) addSection(parentFilename string, body string, sectionTitle strin
}
}
} else {
// if internalFilename is not empty check that is not duplicate
// if internalFilename is not empty, check that it has .xhtml at the end.
// if it doesn't have add .xhtml at the end
// than if it is duplicate return error
if !strings.HasSuffix(internalFilename, ".xhtml") {
fmartingr marked this conversation as resolved.
Show resolved Hide resolved
internalFilename += ".xhtml"
}
if keyExists(filenamelist, internalFilename) {
return "", &FilenameAlreadyUsedError{Filename: internalFilename}
}
Expand Down
79 changes: 79 additions & 0 deletions epub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,16 @@ func TestAddSubSection(t *testing.T) {
if err != nil {
t.Errorf("Error adding subsection: %s", err)
}
// append subsection to a subsection
testSubSection2Path, err := e.AddSubSection(testSection2Path, testSectionBody, testSectionTitle, "", "")
if err != nil {
t.Errorf("Error adding subsection: %s", err)
}
// Append subsection to a parent that not exist
_, err2 := e.AddSubSection("ParentNotExist", testSectionBody, testSectionTitle, "", "")
if err2.Error() != "Parent with the internal filename ParentNotExist does not exist" {
t.Errorf("Error adding subsection: %s", err)
}

tempDir := writeAndExtractEpub(t, e, testEpubFilename)

Expand All @@ -540,6 +550,19 @@ func TestAddSubSection(t *testing.T) {
t.Errorf("Unexpected error reading section file: %s", err)
}

if trimAllSpace(string(contents)) != trimAllSpace(testSectionContents) {
t.Errorf(
"Section file contents don't match\n"+
"Got: %s\n"+
"Expected: %s",
contents,
testSectionContents)
}
contents, err = storage.ReadFile(filesystem, filepath.Join(tempDir, contentFolderName, xhtmlFolderName, testSubSection2Path))
if err != nil {
t.Errorf("Unexpected error reading section file: %s", err)
}

if trimAllSpace(string(contents)) != trimAllSpace(testSectionContents) {
t.Errorf(
"Section file contents don't match\n"+
Expand Down Expand Up @@ -820,6 +843,10 @@ func TestSetCover(t *testing.T) {
if err != nil {
t.Error(err)
}
err = e.SetCover(testImagePath, testCSSPath)
if err != nil {
t.Error(err)
}

tempDir := writeAndExtractEpub(t, e, testEpubFilename)

Expand All @@ -841,6 +868,18 @@ func TestSetCover(t *testing.T) {
cleanup(testEpubFilename, tempDir)
}

func TestSectionAppenderParrentNotFound(t *testing.T) {
sections := []*epubSection{}

parentFilename := "parent.html"
targetSection := &epubSection{}

err := sectionAppender(sections, parentFilename, targetSection)
if err.Error() != "parent section not found" {
t.Errorf("Error adding subsection: %s", err)
}
}

func TestManifestItems(t *testing.T) {
fs := http.FileServer(http.Dir("./testdata/"))

Expand Down Expand Up @@ -1342,3 +1381,43 @@ func writeAndExtractEpub(t testing.TB, e *Epub, epubFilename string) string {

return tempDir
}

func TestAddSubSectionWithCustomFilename(t *testing.T) {
e, err := NewEpub(testEpubTitle)
if err != nil {
t.Error(err)
}

testSection1Path, err := e.AddSection(testSectionBody, testSectionTitle, "firstsection.xhtml", "")
if err != nil {
t.Errorf("Error adding section: %s", err)
}

testSection2Path, err := e.AddSubSection(testSection1Path, testSectionBody, testSectionTitle, "", "")
if err != nil {
t.Errorf("Error adding subsection: %s", err)
}
// append subsection to a subsection
testSection3Path, err := e.AddSubSection(testSection2Path, testSectionBody, testSectionTitle, "someNameWithoutxhtml", "")
if err != nil {
t.Errorf("Error adding subsection: %s", err)
}
_, err = e.AddSubSection(testSection2Path, testSectionBody, testSectionTitle, "someNameWithoutxhtml.xhtml", "")
if err.Error() != "Filename already used: someNameWithoutxhtml.xhtml" {
t.Errorf("you should not add same file twice : %s", err)
}

tempDir := writeAndExtractEpub(t, e, testEpubFilename)

if testSection1Path != "firstsection.xhtml" {
t.Errorf("Expected section1Path to be 'firstsection.xhtml', got '%s'", testSection1Path)
}
if testSection2Path != "section0001.xhtml" {
t.Errorf("Expected section2Path to be 'section0001', got '%s'", testSection2Path)
}
if testSection3Path != "someNameWithoutxhtml.xhtml" {
t.Errorf("Expected section3Path to be 'someNameWithoutxhtml.xhtml', got '%s'", testSection3Path)
}

cleanup(testEpubFilename, tempDir)
}