Skip to content

Commit

Permalink
fix spec; use tempdir instead of spec on generating epub files
Browse files Browse the repository at this point in the history
  • Loading branch information
skoji committed Apr 26, 2024
1 parent 96ba83c commit 60908a8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ rdoc
pkg

## PROJECT::SPECIFIC
testepub*.epub
example_test*.epub

## BUNDLER
Gemfile.lock
Expand Down
18 changes: 13 additions & 5 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
end

context 'On generating EPUB' do
before do
@tempdir = Dir.mktmpdir
end

after do
FileUtils.remove_entry_secure @tempdir
end

it 'should generate simple EPUB3 with Builder and buffer' do
workdir = File.join(File.dirname(__FILE__), 'fixtures', 'testdata')
builder = GEPUB::Builder.new {
Expand All @@ -33,7 +41,7 @@
}
}
}
epubname = File.join(File.dirname(__FILE__), 'example_test_with_builder_buffer.epub')
epubname = File.join(@tempdir, 'example_test_with_builder_buffer.epub')
File.open(epubname, 'wb') { |io| io.write builder.generate_epub_stream.string }
epubcheck(epubname)
end
Expand Down Expand Up @@ -64,7 +72,7 @@
}
}
}
epubname = File.join(File.dirname(__FILE__), 'example_test_with_builder.epub')
epubname = File.join(@tempdir, 'example_test_with_builder.epub')
builder.generate_epub(epubname)
epubcheck(epubname)
end
Expand Down Expand Up @@ -106,7 +114,7 @@
book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>the second page</p></body></html>')) # do not appear on table of contents
book.add_item('text/chap2.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c3</title></head><body><p>the third page</p></body></html>')).toc_text('Chapter 2')
}
epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
epubname = File.join(@tempdir, 'example_test.epub')
book.generate_epub(epubname)
epubcheck(epubname)
end
Expand All @@ -124,7 +132,7 @@
item.add_content StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>')
end

epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
epubname = File.join(@tempdir, 'example_test.epub')
book.generate_epub(epubname)
epubcheck(epubname)
end
Expand Down Expand Up @@ -167,7 +175,7 @@
book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>the second page</p></body></html>')) # do not appear on table of contents
book.add_item('text/chap2.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c3</title></head><body><p>the third page</p></body></html>')).toc_text('Chapter 2')
}
epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
epubname = File.join(@tempdir, 'example_test.epub')

# check nav doc
xml = Nokogiri::XML::Document.parse book.nav_doc
Expand Down
15 changes: 10 additions & 5 deletions spec/gepub_deprectad_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
</html>
EOF
item3 = @book.add_ordered_item('text/nav.xhtml', StringIO.new(nav_string), 'nav').add_property('nav')
@tempdir = Dir.mktmpdir
end

after do
FileUtils.remove_entry_secure @tempdir
end

it "should have title" do
Expand Down Expand Up @@ -155,12 +160,12 @@
end

it "should generate correct epub" do
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')
@book.generate_epub(epubname)
epubcheck(epubname)
end
it "should generate correct epub with buffer" do
epubname = File.join(File.dirname(__FILE__), 'testepub_buf.epub')
epubname = File.join(@tempdir, 'testepub_buf.epub')
File.open(epubname, 'wb') {
|io|
io.write @book.generate_epub_stream.string
Expand All @@ -169,7 +174,7 @@
end

it "should generate correct epub2.0" do
epubname = File.join(File.dirname(__FILE__), 'testepub2.epub')
epubname = File.join(@tempdir, 'testepub2.epub')
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -189,14 +194,14 @@
epubcheck(epubname)
end
it 'should generate epub with extra file' do
epubname = File.join(File.dirname(__FILE__), 'testepub3.epub')
epubname = File.join(@tempdir, 'testepub3.epub')
@book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
@book.generate_epub(epubname)
epubcheck(epubname)
end

it 'should generate valid EPUB when @toc is empty' do
epubname = File.join(File.dirname(__FILE__), 'testepub4.epub')
epubname = File.join(@tempdir, 'testepub4.epub')
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand Down
30 changes: 18 additions & 12 deletions spec/gepub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@
</html>
EOF
item3 = @book.add_ordered_item('text/nav.xhtml', content: StringIO.new(nav_string), id: 'nav').add_property('nav')

@tempdir = Dir.mktmpdir
end

after do
FileUtils.remove_entry_secure @tempdir
end

it "should have title" do
expect(@book.title.to_s).to eq('thetitle')
end
Expand Down Expand Up @@ -153,13 +159,13 @@
end

it "should generate correct epub" do
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')
@book.generate_epub(epubname)
epubcheck(epubname)
end

it "should generate correct epub with buffer" do
epubname = File.join(File.dirname(__FILE__), 'testepub_buf.epub')
epubname = File.join(@tempdir, 'testepub_buf.epub')
File.open(epubname, 'wb') {
|io|
io.write @book.generate_epub_stream.string
Expand All @@ -168,7 +174,7 @@
end

it "should generate correct epub2.0" do
epubname = File.join(File.dirname(__FILE__), 'testepub2.epub')
epubname = File.join(@tempdir, 'testepub2.epub')
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -188,14 +194,14 @@
epubcheck(epubname)
end
it 'should generate epub with extra file' do
epubname = File.join(File.dirname(__FILE__), 'testepub3.epub')
epubname = File.join(@tempdir, 'testepub3.epub')
@book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
@book.generate_epub(epubname)
epubcheck(epubname)
end

it 'should generate valid EPUB when @toc is empty' do
epubname = File.join(File.dirname(__FILE__), 'testepub4.epub')
epubname = File.join(@tempdir, 'testepub4.epub')
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -215,7 +221,7 @@
end

it 'should generate EPUB with specified lastmodified' do
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')
mod_time = Time.mktime(2010,5,5,8,10,15)
@book.lastmodified = mod_time
@book.generate_epub(epubname)
Expand All @@ -227,7 +233,7 @@


it 'should generate EPUB with specified lastmodified by string' do
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')
mod_time = "2010-05-05T08:10:15Z"
@book.lastmodified = mod_time
@book.generate_epub(epubname)
Expand All @@ -239,7 +245,7 @@

it 'should generate parsed and generated EPUB with renewed lastmodified' do
originalfile = File.join(File.dirname(__FILE__), 'fixtures/testdata/wasteland-20120118.epub')
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')

original_book = File.open(originalfile) do |f|
GEPUB::Book.parse(f)
Expand All @@ -256,7 +262,7 @@

it 'should generate parsed and generated EPUB with newly set lastmodified' do
originalfile = File.join(File.dirname(__FILE__), 'fixtures/testdata/wasteland-20120118.epub')
epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')
mod_time = Time.mktime(2010,5,5,8,10,15)

original_book = File.open(originalfile) do |f|
Expand All @@ -281,8 +287,8 @@
end

it 'should produce deterministic output when lastmodified is specified' do
epubname1 = File.join(File.dirname(__FILE__), 'testepub1.epub')
epubname2 = File.join(File.dirname(__FILE__), 'testepub2.epub')
epubname1 = File.join(@tempdir, 'testepub1.epub')
epubname2 = File.join(@tempdir, 'testepub2.epub')
mod_time = "2010-05-05T08:10:15Z"
@book.lastmodified = mod_time

Expand All @@ -297,7 +303,7 @@
@book = GEPUB::Book.new
@book.identifier = 'test'
@book.add_ordered_item('foobar.xhtml', content: StringIO.new('<html><img src="image.svg" /></html>')).add_property 'svg'
epubname = File.join(__dir__, 'testepub.epub')
epubname = File.join(@tempdir, 'testepub.epub')
@book.generate_epub(epubname)
File.open(epubname) do |f|
parsed_book = GEPUB::Book.parse(f)
Expand Down

0 comments on commit 60908a8

Please sign in to comment.