Skip to content

Commit

Permalink
fix: add file extension at the end of filename if not provided (#20)
Browse files Browse the repository at this point in the history
* add unittest for append subsection to the subsection

* fix #14 if customname don't have .xhtml add that to the end of section filename

* Better name for unittest

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* Update epub_test.go

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>

* better unittest name

* removal of confusing comments

* use filepath.Ext

* add unittest for if cutom filename with extension other than .xhtml

---------

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>
  • Loading branch information
Monirzadeh and fmartingr committed Dec 31, 2023
1 parent 00a59d4 commit 9c10b06
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
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 filepath.Ext(internalFilename) != ".xhtml" {
internalFilename += ".xhtml"
}
if keyExists(filenamelist, internalFilename) {
return "", &FilenameAlreadyUsedError{Filename: internalFilename}
}
Expand Down
86 changes: 86 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,50 @@ 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)
}
testSection4Path, err := e.AddSubSection(testSection2Path, testSectionBody, testSectionTitle, "someNameWitAnyExtentionButnotxhtml.jpg", "")
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)
}
if testSection4Path != "someNameWitAnyExtentionButnotxhtml.jpg.xhtml" {
t.Errorf("Expected section4Path to be 'someNameWitAnyExtentionButnotxhtml.jpg', got '%s'", testSection4Path)
}

cleanup(testEpubFilename, tempDir)
}

0 comments on commit 9c10b06

Please sign in to comment.