Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
fmartingr committed Aug 5, 2023
1 parent 6e64d33 commit 772d6fa
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 23 deletions.
42 changes: 24 additions & 18 deletions epub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand All @@ -18,6 +17,7 @@ import (

"github.com/bmaupin/go-epub/internal/storage"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -59,16 +59,16 @@ const (
testFontFromFileSource = "testdata/redacted-script-regular.ttf"
testIdentifierTemplate = `<dc:identifier id="pub-id">%s</dc:identifier>`
testImageFromFileFilename = "testfromfile.png"
testImageFromFileSource = "testdata/gophercolor16x16.png"
testImageFromFileSource = "testdata/sample.png"
testNumberFilenameStart = "01filenametest.png"
testSpaceInFilename = "filename with space.png"
testImageFromURLSource = "https://golang.org/doc/gopher/gophercolor16x16.png"
testImageFromURLSource = "https://file-examples.com/storage/fe072e668b64cd6ce9c9963/2017/10/file_example_PNG_1MB.png"
testVideoFromFileFilename = "testfromfile.mp4"
testVideoFromFileSource = "testdata/sample_640x360.mp4"
testVideoFromURLSource = "https://filesamples.com/samples/video/mp4/sample_640x360.mp4"
testVideoFromURLSource = "https://file-examples.com/storage/fe072e668b64cd6ce9c9963/2017/04/file_example_MP4_640_3MG.mp4"
testAudioFromFileFilename = "sample_audio.wav"
testAudioFromFileSource = "testdata/sample_audio.wav"
testAudioFromURLSource = "https://file-examples.com/storage/fe644084cb644d3709528c4/2017/11/file_example_WAV_1MG.wav"
testAudioFromURLSource = "https://file-examples.com/storage/fe072e668b64cd6ce9c9963/2017/11/file_example_WAV_1MG.wav"
testLangTemplate = `<dc:language>%s</dc:language>`
testDescTemplate = `<dc:description>%s</dc:description>`
testPpdTemplate = `page-progression-direction="%s"`
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestAddImage(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error response from test image URL: %s", err)
}
testImageContents, err = ioutil.ReadAll(resp.Body)
testImageContents, err = io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Unexpected error reading test image file from URL: %s", err)
}
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestAddVideo(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error response from test video URL: %s", err)
}
testVideoContents, err = ioutil.ReadAll(resp.Body)
testVideoContents, err = io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Unexpected error reading test video file from URL: %s", err)
}
Expand Down Expand Up @@ -393,7 +393,7 @@ func TestAddAudio(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error response from test audio URL: %s", err)
}
testAudioContents, err = ioutil.ReadAll(resp.Body)
testAudioContents, err = io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Unexpected error reading test audio file from URL: %s", err)
}
Expand Down Expand Up @@ -751,21 +751,27 @@ func TestSetCover(t *testing.T) {
}

func TestManifestItems(t *testing.T) {
testManifestItems := []string{`id="filenamewithspace.png" href="images/filename with space.png" media-type="image/png"></item>`,
`id="gophercolor16x16.png" href="images/gophercolor16x16.png" media-type="image/png"></item>`,
testManifestItems := []string{
`id="file_example_PNG_1MB.png" href="images/file_example_PNG_1MB.png" media-type="image/png"></item>`,
`id="filenamewithspace.png" href="images/filename with space.png" media-type="image/png"></item>`,
`id="id01filenametest.png" href="images/01filenametest.png" media-type="image/png"></item>`,
`id="image0005.png" href="images/image0005.png" media-type="image/png"></item>`,
`id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"></item>`,
`id="sample.png" href="images/sample.png" media-type="image/png"></item>`,
`id="testfromfile.png" href="images/testfromfile.png" media-type="image/png"></item>`,
}

e := NewEpub(testEpubTitle)
e.AddImage(testImageFromFileSource, testImageFromFileFilename)
e.AddImage(testImageFromFileSource, "")
_, err := e.AddImage(testImageFromFileSource, testImageFromFileFilename)
require.NoError(t, err)
_, err = e.AddImage(testImageFromFileSource, "")
require.NoError(t, err)
// In particular, we want to test these next two, which will be modified by fixXMLId()
e.AddImage(testImageFromFileSource, testNumberFilenameStart)
e.AddImage(testImageFromFileSource, testSpaceInFilename)
e.AddImage(testImageFromURLSource, "")
_, err = e.AddImage(testImageFromFileSource, testNumberFilenameStart)
require.NoError(t, err)
_, err = e.AddImage(testImageFromFileSource, testSpaceInFilename)
require.NoError(t, err)
_, err = e.AddImage(testImageFromURLSource, "")
require.NoError(t, err)

tempDir := writeAndExtractEpub(t, e, testEpubFilename)

Expand Down Expand Up @@ -1000,7 +1006,7 @@ func validateEpub(t testing.TB, epubFilename string) ([]byte, error) {
t.Error("Error getting working directory")
}

items, err := ioutil.ReadDir(cwd)
items, err := os.ReadDir(cwd)
if err != nil {
t.Error("Error getting contents of working directory")
}
Expand All @@ -1012,7 +1018,7 @@ func validateEpub(t testing.TB, epubFilename string) ([]byte, error) {
break

} else if strings.HasPrefix(i.Name(), testEpubcheckPrefix) {
if i.Mode().IsDir() {
if i.IsDir() {
pathToEpubcheck = filepath.Join(i.Name(), testEpubcheckJarfile)
if _, err := os.Stat(pathToEpubcheck); err == nil {
break
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ go 1.20
require (
github.com/gabriel-vasile/mimetype v1.4.2
github.com/gofrs/uuid v4.4.0+incompatible
github.com/stretchr/testify v1.8.4
github.com/vincent-petithory/dataurl v1.0.0
)

require golang.org/x/net v0.13.0 // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/vincent-petithory/dataurl v1.0.0 h1:cXw+kPto8NLuJtlMsI152irrVw9fRDX8AbShPRpg2CI=
github.com/vincent-petithory/dataurl v1.0.0/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
golang.org/x/net v0.13.0 h1:Nvo8UFsZ8X3BhAC9699Z1j7XQ3rsZnUUm7jfBEk1ueY=
golang.org/x/net v0.13.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Binary file added testdata/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified testdata/sample_640x360.mp4
Binary file not shown.
Binary file modified testdata/sample_audio.wav
Binary file not shown.
10 changes: 6 additions & 4 deletions write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package epub
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestEpubWriteTo(t *testing.T) {
Expand All @@ -32,7 +33,7 @@ func TestWriteToErrors(t *testing.T) {
})
t.Run("Image", func(t *testing.T) {
e := NewEpub(testEpubTitle)
testWriteToErrors(t, e, e.AddImage, "gophercolor16x16.png")
testWriteToErrors(t, e, e.AddImage, "sample.png")
})
t.Run("Video", func(t *testing.T) {
e := NewEpub(testEpubTitle)
Expand All @@ -51,11 +52,12 @@ func testWriteToErrors(t *testing.T, e *Epub, adder func(string, string) (string
t.Fatalf("cannot open testdata: %v", err)
}
defer data.Close()
temp, err := ioutil.TempFile("", "temp")
temp, err := os.CreateTemp("", "temp")
if err != nil {
t.Fatalf("unable to create temp file: %v", err)
}
io.Copy(temp, data)
_, err = io.Copy(temp, data)
require.NoError(t, err)
temp.Close()
// Add temp file to epub
if _, err := adder(temp.Name(), ""); err != nil {
Expand Down

0 comments on commit 772d6fa

Please sign in to comment.