Skip to content

Commit

Permalink
Merge pull request #144 from aef/generalize_temporary_directory
Browse files Browse the repository at this point in the history
Generalize temporary directory handling
  • Loading branch information
skoji authored Jul 31, 2024
2 parents 20cd228 + 22da3be commit 0a2d5a3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 105 deletions.
39 changes: 16 additions & 23 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@
context 'On using Builder' do
end

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

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 @@ -40,9 +33,9 @@
}
}
}
epubname = File.join(@tempdir, 'example_test_with_builder_buffer.epub')
File.open(epubname, 'wb') { |io| io.write builder.generate_epub_stream.string }
epubcheck(epubname)
epub_file = @temporary_directory / 'example_test_with_builder_buffer.epub'
epub_file.open('wb') { |io| io.write builder.generate_epub_stream.string }
epubcheck(epub_file)
end

it 'should generate simple EPUB3 with Builder' do
Expand Down Expand Up @@ -71,9 +64,9 @@
}
}
}
epubname = File.join(@tempdir, 'example_test_with_builder.epub')
builder.generate_epub(epubname)
epubcheck(epubname)
epub_file = @temporary_directory / 'example_test_with_builder.epub'
builder.generate_epub(epub_file)
epubcheck(epub_file)
end

it 'should generate simple EPUB3 with rather complicated matadata' do
Expand Down Expand Up @@ -113,9 +106,9 @@
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(@tempdir, 'example_test.epub')
book.generate_epub(epubname)
epubcheck(epubname)
epub_file = @temporary_directory / 'example_test.epub'
book.generate_epub(epub_file)
epubcheck(epub_file)
end

it 'should generate simple EPUB3 with some nil metadata' do
Expand All @@ -131,9 +124,9 @@
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(@tempdir, 'example_test.epub')
book.generate_epub(epubname)
epubcheck(epubname)
epub_file = @temporary_directory / 'example_test.epub'
book.generate_epub(epub_file)
epubcheck(epub_file)
end

it 'should generate simple EPUB3 with landmarks' do
Expand Down Expand Up @@ -174,7 +167,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(@tempdir, 'example_test.epub')
epub_file = @temporary_directory / 'example_test.epub'

# check nav doc
xml = Nokogiri::XML::Document.parse book.nav_doc
Expand All @@ -192,8 +185,8 @@
expect(landmarks[0]['href']).to eq 'text/cover.xhtml'
expect(landmarks[1]['epub:type']).to eq 'bodymatter'
expect(landmarks[1]['href']).to eq 'text/chap1.xhtml'
book.generate_epub(epubname)
epubcheck(epubname)
book.generate_epub(epub_file)
epubcheck(epub_file)
end
end
end
46 changes: 21 additions & 25 deletions spec/gepub_deprectad_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@
</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 @@ -158,22 +153,23 @@
expect(meta['content']).to eq(item.itemid)
end

it "should generate correct epub" do
epubname = File.join(@tempdir, 'testepub.epub')
@book.generate_epub(epubname)
epubcheck(epubname)
it "should generate correct epub", :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub.epub'
@book.generate_epub(epub_file)
epubcheck(epub_file)
end
it "should generate correct epub with buffer" do
epubname = File.join(@tempdir, 'testepub_buf.epub')
File.open(epubname, 'wb') {

it "should generate correct epub with buffer", :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub_buf.epub'
File.open(epub_file, 'wb') {
|io|
io.write @book.generate_epub_stream.string
}
epubcheck(epubname)
epubcheck(epub_file)
end

it "should generate correct epub2.0" do
epubname = File.join(@tempdir, 'testepub2.epub')
it "should generate correct epub2.0", :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub2.epub'
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -189,18 +185,18 @@
StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
'c2')
item2.toc_text 'test chapter'
@book.generate_epub(epubname)
epubcheck(epubname)
@book.generate_epub(epub_file)
epubcheck(epub_file)
end
it 'should generate epub with extra file' do
epubname = File.join(@tempdir, 'testepub3.epub')
it 'should generate epub with extra file', :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub3.epub'
@book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
@book.generate_epub(epubname)
epubcheck(epubname)
@book.generate_epub(epub_file)
epubcheck(epub_file)
end

it 'should generate valid EPUB when @toc is empty' do
epubname = File.join(@tempdir, 'testepub4.epub')
it 'should generate valid EPUB when @toc is empty', :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub4.epub'
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -215,8 +211,8 @@
item2 = @book.add_ordered_item('text/barbar.xhtml',
StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
'c2')
@book.generate_epub(epubname)
epubcheck(epubname)
@book.generate_epub(epub_file)
epubcheck(epub_file)

end
end
103 changes: 47 additions & 56 deletions spec/gepub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,8 @@
</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 @@ -158,23 +152,20 @@
expect(meta['content']).to eq(item.itemid)
end

it "should generate correct epub" do
epubname = File.join(@tempdir, 'testepub.epub')
@book.generate_epub(epubname)
epubcheck(epubname)
it "should generate correct epub", :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub.epub'
@book.generate_epub(epub_file)
epubcheck(epub_file)
end

it "should generate correct epub with buffer" do
epubname = File.join(@tempdir, 'testepub_buf.epub')
File.open(epubname, 'wb') {
|io|
io.write @book.generate_epub_stream.string
}
epubcheck(epubname)
it "should generate correct epub with buffer", :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub_buf.epub'
epub_file.open('wb') {|io| io.write @book.generate_epub_stream.string }
epubcheck(epub_file)
end

it "should generate correct epub2.0" do
epubname = File.join(@tempdir, 'testepub2.epub')
it "should generate correct epub2.0", :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub2.epub'
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -190,18 +181,19 @@
content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
id: 'c2')
item2.toc_text 'test chapter'
@book.generate_epub(epubname)
epubcheck(epubname)
@book.generate_epub(epub_file)
epubcheck(epub_file)
end
it 'should generate epub with extra file' do
epubname = File.join(@tempdir, 'testepub3.epub')

it 'should generate epub with extra file', :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub3.epub'
@book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
@book.generate_epub(epubname)
epubcheck(epubname)
@book.generate_epub(epub_file)
epubcheck(epub_file)
end

it 'should generate valid EPUB when @toc is empty' do
epubname = File.join(@tempdir, 'testepub4.epub')
it 'should generate valid EPUB when @toc is empty', :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub4.epub'
@book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
@book.title = 'thetitle'
@book.creator = "theauthor"
Expand All @@ -216,61 +208,60 @@
item2 = @book.add_ordered_item('text/barbar.xhtml',
content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
id: 'c2')
@book.generate_epub(epubname)
epubcheck(epubname)
@book.generate_epub(epub_file)
epubcheck(epub_file)
end

it 'should generate EPUB with specified lastmodified' do
epubname = File.join(@tempdir, 'testepub.epub')
it 'should generate EPUB with specified lastmodified', :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub.epub'
mod_time = Time.mktime(2010,5,5,8,10,15)
@book.lastmodified = mod_time
@book.generate_epub(epubname)
File.open(epubname) do |f|
@book.generate_epub(epub_file)
File.open(epub_file) do |f|
parsed_book = GEPUB::Book.parse(f)
expect(parsed_book.lastmodified.content).to eq mod_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end
end


it 'should generate EPUB with specified lastmodified by string' do
epubname = File.join(@tempdir, 'testepub.epub')
it 'should generate EPUB with specified lastmodified by string', :uses_temporary_directory do
epub_file = @temporary_directory / 'testepub.epub'
mod_time = "2010-05-05T08:10:15Z"
@book.lastmodified = mod_time
@book.generate_epub(epubname)
File.open(epubname) do |f|
@book.generate_epub(epub_file)
File.open(epub_file) do |f|
parsed_book = GEPUB::Book.parse(f)
expect(parsed_book.lastmodified.content).to eq mod_time
end
end

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

original_book = File.open(originalfile) do |f|
GEPUB::Book.parse(f)
end
original_lastmodified = original_book.lastmodified.content
original_book.generate_epub(epubname)
File.open(epubname) do |f|
original_book.generate_epub(epub_file)
File.open(epub_file) do |f|
parsed_book = GEPUB::Book.parse(f)
parsed_time = parsed_book.lastmodified.content
original_time = original_lastmodified
expect(parsed_time).to be > original_time
end
end

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

original_book = File.open(originalfile) do |f|
GEPUB::Book.parse(f)
end
original_book.lastmodified = mod_time
original_book.generate_epub(epubname)
File.open(epubname) do |f|
original_book.generate_epub(epub_file)
epub_file.open do |f|
parsed_book = GEPUB::Book.parse(f)
expect(parsed_book.lastmodified.content).to eq mod_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end
Expand All @@ -286,26 +277,26 @@
@book.generate_epub_stream
end

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

@book.generate_epub(epubname1)
@book.generate_epub(epub_file1)
sleep 2
@book.generate_epub(epubname2)
@book.generate_epub(epub_file2)

expect(FileUtils.compare_file(epubname1, epubname2)).to be true
expect(FileUtils.compare_file(epub_file1, epub_file2)).to be true
end

it 'should not forget svg attribute when parsing book' do
it 'should not forget svg attribute when parsing book', :uses_temporary_directory do
@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(@tempdir, 'testepub.epub')
@book.generate_epub(epubname)
File.open(epubname) do |f|
epub_file = @temporary_directory / 'testepub.epub'
@book.generate_epub(epub_file)
File.open(epub_file) do |f|
parsed_book = GEPUB::Book.parse(f)
item = parsed_book.item_by_href 'foobar.xhtml'
expect(item).not_to be_nil
Expand Down
9 changes: 8 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ def epubcheck(epubname)
end
expect(stdout).to include("No errors or warnings detected.")
end


config.around(:example, :uses_temporary_directory) do |example|
@temporary_directory = Pathname(Dir.mktmpdir("gepub_spec"))
example.run
ensure
@temporary_directory.rmtree
end

end

require 'rspec/core/formatters/base_text_formatter'
Expand Down

0 comments on commit 0a2d5a3

Please sign in to comment.