From 60908a83630131e79eb5b3bb6772481262b75922 Mon Sep 17 00:00:00 2001 From: Satoshi Kojima Date: Fri, 26 Apr 2024 17:50:07 +0900 Subject: [PATCH 1/4] fix spec; use tempdir instead of spec on generating epub files --- .gitignore | 2 -- spec/example_spec.rb | 18 +++++++++++++----- spec/gepub_deprectad_api_spec.rb | 15 ++++++++++----- spec/gepub_spec.rb | 30 ++++++++++++++++++------------ 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 5a0999e..d130cbd 100644 --- a/.gitignore +++ b/.gitignore @@ -19,8 +19,6 @@ rdoc pkg ## PROJECT::SPECIFIC -testepub*.epub -example_test*.epub ## BUNDLER Gemfile.lock diff --git a/spec/example_spec.rb b/spec/example_spec.rb index 29a4d5e..549b44f 100644 --- a/spec/example_spec.rb +++ b/spec/example_spec.rb @@ -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 { @@ -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 @@ -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 @@ -106,7 +114,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(File.dirname(__FILE__), 'example_test.epub') + epubname = File.join(@tempdir, 'example_test.epub') book.generate_epub(epubname) epubcheck(epubname) end @@ -124,7 +132,7 @@ item.add_content StringIO.new('c1

the first page

') end - epubname = File.join(File.dirname(__FILE__), 'example_test.epub') + epubname = File.join(@tempdir, 'example_test.epub') book.generate_epub(epubname) epubcheck(epubname) end @@ -167,7 +175,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(File.dirname(__FILE__), 'example_test.epub') + epubname = File.join(@tempdir, 'example_test.epub') # check nav doc xml = Nokogiri::XML::Document.parse book.nav_doc diff --git a/spec/gepub_deprectad_api_spec.rb b/spec/gepub_deprectad_api_spec.rb index a5d43c5..0fc1e6f 100644 --- a/spec/gepub_deprectad_api_spec.rb +++ b/spec/gepub_deprectad_api_spec.rb @@ -75,6 +75,11 @@ 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 @@ -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 @@ -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" @@ -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('')) @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" diff --git a/spec/gepub_spec.rb b/spec/gepub_spec.rb index 7532060..d85bf0e 100644 --- a/spec/gepub_spec.rb +++ b/spec/gepub_spec.rb @@ -73,8 +73,14 @@ 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 @@ -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 @@ -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" @@ -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('')) @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" @@ -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) @@ -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) @@ -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) @@ -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| @@ -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 @@ -297,7 +303,7 @@ @book = GEPUB::Book.new @book.identifier = 'test' @book.add_ordered_item('foobar.xhtml', content: StringIO.new('')).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) From 47c6ebdbe36a75a1838eb436095cca64876a34cc Mon Sep 17 00:00:00 2001 From: Satoshi Kojima Date: Fri, 26 Apr 2024 17:50:33 +0900 Subject: [PATCH 2/4] do not exclude windows test --- .github/workflows/test.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 490a649..8b30842 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,8 +6,6 @@ jobs: matrix: ruby: [2.7, 3.0, 3.1, 3.2, 3.3, jruby] platform: [ubuntu-latest, macos-latest, windows-latest] - exclude: - - platform: windows-latest runs-on: ${{ matrix.platform }} env: CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} From 6dd633e665e1313c0832a41cc52ba98a8d7f3bc4 Mon Sep 17 00:00:00 2001 From: Satoshi Kojima Date: Fri, 26 Apr 2024 18:12:36 +0900 Subject: [PATCH 3/4] add context to GEPUB:Book test --- spec/gepub_spec.rb | 443 +++++++++++++++++++++++---------------------- 1 file changed, 222 insertions(+), 221 deletions(-) diff --git a/spec/gepub_spec.rb b/spec/gepub_spec.rb index d85bf0e..2714bc5 100644 --- a/spec/gepub_spec.rb +++ b/spec/gepub_spec.rb @@ -39,24 +39,25 @@ end describe GEPUB::Book do - before do - @book = GEPUB::Book.new('OEPBS/package.opf') - @book.title = 'thetitle' - @book.creator = "theauthor" - @book.contributor = "contributors contributors!" - @book.publisher = "thepublisher" - @book.date = "2010-05-05" - @book.identifier = "http://example.jp/foobar/" - @book.language = 'ja' - item1 = @book.add_item('text/foobar.xhtml',nil, id: 'c1', content: StringIO.new('c1

the first page

')) - @book.spine.push(item1) - - item2 = @book.add_ordered_item('text/barbar.xhtml', - content: StringIO.new('c2

second page, whith is test chapter.

'), - id: 'c2', - toc_text: 'test chapter') - - nav_string = <c1

the first page

')) + @book.spine.push(item1) + + item2 = @book.add_ordered_item('text/barbar.xhtml', + content: StringIO.new('c2

second page, whith is test chapter.

'), + id: 'c2', + toc_text: 'test chapter') + + nav_string = < Table of contents @@ -72,245 +73,245 @@ EOF - item3 = @book.add_ordered_item('text/nav.xhtml', content: StringIO.new(nav_string), id: 'nav').add_property('nav') + 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 - - it "should generate correct ncx" do - ncx = Nokogiri::XML.parse(@book.ncx_xml).root - expect(ncx.name).to eq('ncx') - expect(ncx.attributes['version'].value).to eq('2005-1') - ncx.namespaces['xmlns'] == 'http://www.daisy.org/z3986/2005/ncx/' - end + @tempdir = Dir.mktmpdir + end - it "should have correct head in ncx" do - head = Nokogiri::XML.parse(@book.ncx_xml).at_xpath('/xmlns:ncx/xmlns:head') - expect(head).not_to be_nil - expect(head.at_xpath("xmlns:meta[@name='dtb:uid']")['content']).to eq("http://example.jp/foobar/") - expect(head.xpath("xmlns:meta[@name='dtb:depth']").size).to be > 0 - expect(head.xpath("xmlns:meta[@name='dtb:totalPageCount']").size).to be > 0 - expect(head.xpath("xmlns:meta[@name='dtb:maxPageNumber']").size).to be > 0 - end + after do + FileUtils.remove_entry_secure @tempdir + end + + it "should have title" do + expect(@book.title.to_s).to eq('thetitle') + end - it "should have correct ncx doctitle" do - doctitle = Nokogiri::XML.parse(@book.ncx_xml).root + it "should generate correct ncx" do + ncx = Nokogiri::XML.parse(@book.ncx_xml).root + expect(ncx.name).to eq('ncx') + expect(ncx.attributes['version'].value).to eq('2005-1') + ncx.namespaces['xmlns'] == 'http://www.daisy.org/z3986/2005/ncx/' + end - expect(doctitle.xpath('xmlns:docTitle').size).to be > 0 - expect(doctitle.at_xpath('xmlns:docTitle/xmlns:text').text).to eq('thetitle') - end + it "should have correct head in ncx" do + head = Nokogiri::XML.parse(@book.ncx_xml).at_xpath('/xmlns:ncx/xmlns:head') + expect(head).not_to be_nil + expect(head.at_xpath("xmlns:meta[@name='dtb:uid']")['content']).to eq("http://example.jp/foobar/") + expect(head.xpath("xmlns:meta[@name='dtb:depth']").size).to be > 0 + expect(head.xpath("xmlns:meta[@name='dtb:totalPageCount']").size).to be > 0 + expect(head.xpath("xmlns:meta[@name='dtb:maxPageNumber']").size).to be > 0 + end - it "should correct ncx navmap" do - ncx = Nokogiri::XML::parse(@book.ncx_xml).root + it "should have correct ncx doctitle" do + doctitle = Nokogiri::XML.parse(@book.ncx_xml).root - expect(ncx.xpath('xmlns:navMap').size).to be > 0 - nav_point = ncx.at_xpath('xmlns:navMap/xmlns:navPoint') - expect(nav_point['id']).to eq('c2_') - expect(nav_point['playOrder']).to eq('1') - - expect(nav_point.at_xpath('xmlns:navLabel/xmlns:text').content).to eq('test chapter') - nav_point.at_xpath('xmlns:content')['src'] == 'foobar2.html' + expect(doctitle.xpath('xmlns:docTitle').size).to be > 0 + expect(doctitle.at_xpath('xmlns:docTitle/xmlns:text').text).to eq('thetitle') + end - end + it "should correct ncx navmap" do + ncx = Nokogiri::XML::parse(@book.ncx_xml).root - it "should create correct opf" do - opf = Nokogiri::XML.parse(@book.opf_xml).root - expect(opf.name).to eq('package') - expect(opf.namespaces['xmlns']).to eq('http://www.idpf.org/2007/opf') - expect(opf['version']).to eq('3.0') - expect(opf['unique-identifier']).to eq('BookId') - end + expect(ncx.xpath('xmlns:navMap').size).to be > 0 + nav_point = ncx.at_xpath('xmlns:navMap/xmlns:navPoint') + expect(nav_point['id']).to eq('c2_') + expect(nav_point['playOrder']).to eq('1') + + expect(nav_point.at_xpath('xmlns:navLabel/xmlns:text').content).to eq('test chapter') + nav_point.at_xpath('xmlns:content')['src'] == 'foobar2.html' - it "should have correct metadata in opf" do - opf = Nokogiri::XML.parse(@book.opf_xml).root - metadata = opf.xpath('xmlns:metadata').first - expect(metadata.at_xpath('dc:language', metadata.namespaces).content).to eq('ja') - #TODO: check metadata - end + end - it "should have correct manifest and spine in opf" do - opf = Nokogiri::XML.parse(@book.opf_xml).root + it "should create correct opf" do + opf = Nokogiri::XML.parse(@book.opf_xml).root + expect(opf.name).to eq('package') + expect(opf.namespaces['xmlns']).to eq('http://www.idpf.org/2007/opf') + expect(opf['version']).to eq('3.0') + expect(opf['unique-identifier']).to eq('BookId') + end - manifest = opf.at_xpath('xmlns:manifest') - expect(manifest.at_xpath('xmlns:item[@id="c1"]')['href']).to eq('text/foobar.xhtml') - expect(manifest.at_xpath('xmlns:item[@id="c1"]')['media-type']).to eq('application/xhtml+xml') + it "should have correct metadata in opf" do + opf = Nokogiri::XML.parse(@book.opf_xml).root + metadata = opf.xpath('xmlns:metadata').first + expect(metadata.at_xpath('dc:language', metadata.namespaces).content).to eq('ja') + #TODO: check metadata + end - spine = opf.at_xpath('xmlns:spine') - expect(spine['toc']).to eq('ncx') - expect(spine.at_xpath('xmlns:itemref')['idref']).to eq('c1') - end + it "should have correct manifest and spine in opf" do + opf = Nokogiri::XML.parse(@book.opf_xml).root - it "should have correct cover image id" do - item = @book.add_item("img/img.jpg").cover_image + manifest = opf.at_xpath('xmlns:manifest') + expect(manifest.at_xpath('xmlns:item[@id="c1"]')['href']).to eq('text/foobar.xhtml') + expect(manifest.at_xpath('xmlns:item[@id="c1"]')['media-type']).to eq('application/xhtml+xml') - opf = Nokogiri::XML.parse(@book.opf_xml).root + spine = opf.at_xpath('xmlns:spine') + expect(spine['toc']).to eq('ncx') + expect(spine.at_xpath('xmlns:itemref')['idref']).to eq('c1') + end - metadata = opf.at_xpath('xmlns:metadata') - meta = metadata.at_xpath("xmlns:meta[@name='cover']") - expect(meta['content']).to eq(item.itemid) - end + it "should have correct cover image id" do + item = @book.add_item("img/img.jpg").cover_image - it "should generate correct epub" do - epubname = File.join(@tempdir, 'testepub.epub') - @book.generate_epub(epubname) - epubcheck(epubname) - end + opf = Nokogiri::XML.parse(@book.opf_xml).root - 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) - end + metadata = opf.at_xpath('xmlns:metadata') + meta = metadata.at_xpath("xmlns:meta[@name='cover']") + expect(meta['content']).to eq(item.itemid) + end - it "should generate correct epub2.0" do - epubname = File.join(@tempdir, 'testepub2.epub') - @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} ) - @book.title = 'thetitle' - @book.creator = "theauthor" - @book.contributor = "contributors contributors!" - @book.publisher = "thepublisher" - @book.date = "2010-05-05" - @book.identifier = "http://example.jp/foobar/" - @book.language = 'ja' - item1 = @book.add_item('text/foobar.xhtml',id: 'c1') - item1.add_content(StringIO.new('c1

the first page

')) - @book.spine.push(item1) - item2 = @book.add_ordered_item('text/barbar.xhtml', - content: StringIO.new('c2

second page, whith is test chapter.

'), - id: 'c2') - item2.toc_text 'test chapter' - @book.generate_epub(epubname) - epubcheck(epubname) - end - it 'should generate epub with extra file' do - epubname = File.join(@tempdir, 'testepub3.epub') - @book.add_optional_file('META-INF/foobar.xml', StringIO.new('')) - @book.generate_epub(epubname) - epubcheck(epubname) - end + it "should generate correct epub" do + epubname = File.join(@tempdir, 'testepub.epub') + @book.generate_epub(epubname) + epubcheck(epubname) + end - it 'should generate valid EPUB when @toc is empty' do - epubname = File.join(@tempdir, 'testepub4.epub') - @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} ) - @book.title = 'thetitle' - @book.creator = "theauthor" - @book.contributor = "contributors contributors!" - @book.publisher = "thepublisher" - @book.date = "2015-05-05" - @book.identifier = "http://example.jp/foobar/" - @book.language = 'ja' - item1 = @book.add_item('text/foobar.xhtml',id: 'c1') - item1.add_content(StringIO.new('c1

the first page

')) - @book.spine.push(item1) - 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) - 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) + end - it 'should generate EPUB with specified lastmodified' do - epubname = File.join(@tempdir, '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| - parsed_book = GEPUB::Book.parse(f) - expect(parsed_book.lastmodified.content).to eq mod_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ') + it "should generate correct epub2.0" do + epubname = File.join(@tempdir, 'testepub2.epub') + @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} ) + @book.title = 'thetitle' + @book.creator = "theauthor" + @book.contributor = "contributors contributors!" + @book.publisher = "thepublisher" + @book.date = "2010-05-05" + @book.identifier = "http://example.jp/foobar/" + @book.language = 'ja' + item1 = @book.add_item('text/foobar.xhtml',id: 'c1') + item1.add_content(StringIO.new('c1

the first page

')) + @book.spine.push(item1) + item2 = @book.add_ordered_item('text/barbar.xhtml', + content: StringIO.new('c2

second page, whith is test chapter.

'), + id: 'c2') + item2.toc_text 'test chapter' + @book.generate_epub(epubname) + epubcheck(epubname) + end + it 'should generate epub with extra file' do + epubname = File.join(@tempdir, 'testepub3.epub') + @book.add_optional_file('META-INF/foobar.xml', StringIO.new('')) + @book.generate_epub(epubname) + epubcheck(epubname) end - end + it 'should generate valid EPUB when @toc is empty' do + epubname = File.join(@tempdir, 'testepub4.epub') + @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} ) + @book.title = 'thetitle' + @book.creator = "theauthor" + @book.contributor = "contributors contributors!" + @book.publisher = "thepublisher" + @book.date = "2015-05-05" + @book.identifier = "http://example.jp/foobar/" + @book.language = 'ja' + item1 = @book.add_item('text/foobar.xhtml',id: 'c1') + item1.add_content(StringIO.new('c1

the first page

')) + @book.spine.push(item1) + 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) + end - it 'should generate EPUB with specified lastmodified by string' do - epubname = File.join(@tempdir, 'testepub.epub') - mod_time = "2010-05-05T08:10:15Z" - @book.lastmodified = mod_time - @book.generate_epub(epubname) - File.open(epubname) do |f| - parsed_book = GEPUB::Book.parse(f) - expect(parsed_book.lastmodified.content).to eq mod_time + it 'should generate EPUB with specified lastmodified' do + epubname = File.join(@tempdir, '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| + 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 - end - 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(@tempdir, '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| - parsed_book = GEPUB::Book.parse(f) - parsed_time = parsed_book.lastmodified.content - original_time = original_lastmodified - expect(parsed_time).to be > original_time + it 'should generate EPUB with specified lastmodified by string' do + epubname = File.join(@tempdir, 'testepub.epub') + mod_time = "2010-05-05T08:10:15Z" + @book.lastmodified = mod_time + @book.generate_epub(epubname) + File.open(epubname) do |f| + parsed_book = GEPUB::Book.parse(f) + expect(parsed_book.lastmodified.content).to eq mod_time + end end - end - 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(@tempdir, 'testepub.epub') - mod_time = Time.mktime(2010,5,5,8,10,15) - - original_book = File.open(originalfile) do |f| - GEPUB::Book.parse(f) + 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(@tempdir, '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| + 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 - original_book.lastmodified = mod_time - original_book.generate_epub(epubname) - File.open(epubname) 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') + + 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(@tempdir, '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| + 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 - end - it 'should produce empty EPUB2 book' do - @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'}) - @book.generate_epub_stream - end + it 'should produce empty EPUB2 book' do + @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'}) + @book.generate_epub_stream + end - it 'should produce empty EPUB3 book' do - @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'}) - @book.generate_epub_stream - end + it 'should produce empty EPUB3 book' do + @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'}) + @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') - mod_time = "2010-05-05T08:10:15Z" - @book.lastmodified = mod_time + it 'should produce deterministic output when lastmodified is specified' do + epubname1 = File.join(@tempdir, 'testepub1.epub') + epubname2 = File.join(@tempdir, 'testepub2.epub') + mod_time = "2010-05-05T08:10:15Z" + @book.lastmodified = mod_time - @book.generate_epub(epubname1) - sleep 2 - @book.generate_epub(epubname2) + @book.generate_epub(epubname1) + sleep 2 + @book.generate_epub(epubname2) - expect(FileUtils.compare_file(epubname1, epubname2)).to be true - end + expect(FileUtils.compare_file(epubname1, epubname2)).to be true + end - it 'should not forget svg attribute when parsing book' 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| - parsed_book = GEPUB::Book.parse(f) - item = parsed_book.item_by_href 'foobar.xhtml' - expect(item).not_to be_nil - expect(item['properties']).to include 'svg' + it 'should not forget svg attribute when parsing book' 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| + parsed_book = GEPUB::Book.parse(f) + item = parsed_book.item_by_href 'foobar.xhtml' + expect(item).not_to be_nil + expect(item['properties']).to include 'svg' + end end end - end From 07e8d72646c5d240ce4cb8d55e65353997a5cf65 Mon Sep 17 00:00:00 2001 From: Satoshi Kojima Date: Fri, 26 Apr 2024 18:27:47 +0900 Subject: [PATCH 4/4] use rm_rf --- lib/gepub/book.rb | 4 ++-- spec/gepub_spec.rb | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/gepub/book.rb b/lib/gepub/book.rb index d003ba6..40ff37f 100644 --- a/lib/gepub/book.rb +++ b/lib/gepub/book.rb @@ -102,14 +102,14 @@ def self.parse(io) package = nil package_path = nil book = nil - Zip::File.open_buffer(io) { + Zip::File.open_buffer(io) do |zip_file| package, package_path = parse_container(zip_file, files) check_consistency_of_package(package, package_path) parse_files_into_package(files, package) book = Book.new(package.path) book.instance_eval { @package = package; @optional_files = files } - } + end book end diff --git a/spec/gepub_spec.rb b/spec/gepub_spec.rb index 2714bc5..f290b5c 100644 --- a/spec/gepub_spec.rb +++ b/spec/gepub_spec.rb @@ -79,7 +79,8 @@ end after do - FileUtils.remove_entry_secure @tempdir + # workaround; rubyzip opened files could not be deleted with remove_entry_secure on windows. + FileUtils.rm_rf @tempdir end it "should have title" do