From 22da3be86abe42cb6bf4d04a474aa448c9e9951e Mon Sep 17 00:00:00 2001 From: "Alexander E. Fischer" Date: Mon, 15 Jul 2024 17:49:30 +0200 Subject: [PATCH] Generalize temporary directory handling --- spec/example_spec.rb | 39 +++++------- spec/gepub_deprectad_api_spec.rb | 46 +++++++------- spec/gepub_spec.rb | 103 ++++++++++++++----------------- spec/spec_helper.rb | 9 ++- 4 files changed, 92 insertions(+), 105 deletions(-) diff --git a/spec/example_spec.rb b/spec/example_spec.rb index 549b44f..e127df3 100644 --- a/spec/example_spec.rb +++ b/spec/example_spec.rb @@ -6,15 +6,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 { @@ -41,9 +34,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 @@ -72,9 +65,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 @@ -114,9 +107,9 @@ book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('c2

the second page

')) # do not appear on table of contents book.add_item('text/chap2.xhtml').add_content(StringIO.new('c3

the third page

')).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 @@ -132,9 +125,9 @@ item.add_content StringIO.new('c1

the first page

') 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 @@ -175,7 +168,7 @@ book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('c2

the second page

')) # do not appear on table of contents book.add_item('text/chap2.xhtml').add_content(StringIO.new('c3

the third page

')).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 @@ -193,8 +186,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 diff --git a/spec/gepub_deprectad_api_spec.rb b/spec/gepub_deprectad_api_spec.rb index 0fc1e6f..cdbfc52 100644 --- a/spec/gepub_deprectad_api_spec.rb +++ b/spec/gepub_deprectad_api_spec.rb @@ -75,11 +75,6 @@ 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 @@ -159,22 +154,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" @@ -190,18 +186,18 @@ StringIO.new('c2

second page, whith is test chapter.

'), '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('')) - @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" @@ -216,8 +212,8 @@ item2 = @book.add_ordered_item('text/barbar.xhtml', StringIO.new('c2

second page, whith is test chapter.

'), 'c2') - @book.generate_epub(epubname) - epubcheck(epubname) + @book.generate_epub(epub_file) + epubcheck(epub_file) end end diff --git a/spec/gepub_spec.rb b/spec/gepub_spec.rb index 2714bc5..3e45253 100644 --- a/spec/gepub_spec.rb +++ b/spec/gepub_spec.rb @@ -74,14 +74,8 @@ 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 @@ -159,23 +153,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" @@ -191,18 +182,19 @@ content: StringIO.new('c2

second page, whith is test chapter.

'), 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('')) - @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" @@ -217,43 +209,42 @@ item2 = @book.add_ordered_item('text/barbar.xhtml', content: StringIO.new('c2

second page, whith is test chapter.

'), 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 @@ -261,17 +252,17 @@ 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 @@ -287,26 +278,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('')).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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad390c5..4d59ac6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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'