summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMarat Radchenko <marat@slonopotamus.org>2020-03-01 16:21:27 +0300
committerGitHub <noreply@github.com>2020-03-01 16:21:27 +0300
commit62b0405ecbf2ae01eeba2bc69f093bc17b152f7a (patch)
tree8bc9ac1824e03e05f666285bdea7f42ad8e01b73 /spec
parenta377be4ddcb25849bf363133e8f013e971585570 (diff)
Add support for heredoc documents in test suite (PR #313)
Diffstat (limited to 'spec')
-rw-r--r--spec/cli_spec.rb14
-rw-r--r--spec/converter_spec.rb50
-rw-r--r--spec/image_spec.rb12
-rw-r--r--spec/inline_quoted_spec.rb2
-rw-r--r--spec/reproducible_spec.rb12
-rw-r--r--spec/spec_helper.rb31
-rw-r--r--spec/stem_spec.rb4
-rw-r--r--spec/xref_spec.rb12
8 files changed, 77 insertions, 60 deletions
diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb
index f3b39a2..e8980e3 100644
--- a/spec/cli_spec.rb
+++ b/spec/cli_spec.rb
@@ -10,7 +10,7 @@ describe 'asciidoctor-epub3' do
end
it 'exits with 1 when given nonexistent path' do
- _, err, res = to_epub '/nonexistent'
+ _, err, res = to_epub Pathname.new('/nonexistent')
expect(res.exitstatus).to eq(1)
expect(err).to match(/input file \/nonexistent( is)? missing/)
end
@@ -21,8 +21,8 @@ describe 'asciidoctor-epub3' do
_, err, res = run_command asciidoctor_epub3_bin,
'--failure-level=ERROR',
'-a', 'ebook-validate',
- fixture_file('invalid.adoc'),
- '-o', temp_file('invalid.epub')
+ fixture_file('invalid.adoc').to_s,
+ '-o', temp_file('invalid.epub').to_s
expect(res.exitstatus).to eq(1)
# Error from epubcheck
expect(err).to include 'ERROR(RSC-012)'
@@ -66,13 +66,13 @@ describe 'asciidoctor-epub3' do
end
def to_mobi in_file, out_file
- skip_if_darwin
- run_command asciidoctor_epub3_bin, '-a', 'ebook-format=mobi', in_file, '-o', out_file
+ skip_unless_has_kindlegen
+ run_command asciidoctor_epub3_bin, '-a', 'ebook-format=mobi', in_file.to_s, '-o', out_file.to_s
end
def to_epub in_file, out_file = nil
- argv = asciidoctor_epub3_bin + ['-a', 'ebook-validate', in_file]
- argv += ['-o', out_file] unless out_file.nil?
+ argv = asciidoctor_epub3_bin + ['-a', 'ebook-validate', in_file.to_s]
+ argv += ['-o', out_file.to_s] unless out_file.nil?
run_command argv
end
end
diff --git a/spec/converter_spec.rb b/spec/converter_spec.rb
index 7674f35..f8eb47c 100644
--- a/spec/converter_spec.rb
+++ b/spec/converter_spec.rb
@@ -5,15 +5,27 @@ require_relative 'spec_helper'
describe Asciidoctor::Epub3::Converter do
describe '#convert' do
it 'converts empty file to epub without exceptions' do
- to_epub 'empty.adoc'
+ to_epub fixture_file('empty.adoc')
end
it 'converts empty file to mobi without exceptions' do
- to_mobi 'empty.adoc'
+ to_mobi fixture_file('empty.adoc')
+ end
+
+ it 'converts empty heredoc document to epub without exceptions' do
+ to_epub <<~EOS
+ EOS
+ end
+
+ it 'converts minimal heredoc document to epub without exceptions' do
+ book = to_epub <<~EOS
+ = Title
+ EOS
+ expect(book).to be_a(GEPUB::Book)
end
it 'converts chapter with unicode title to unicode filename' do
- _, out_file = to_epub 'unicode/book.adoc'
+ _, out_file = to_epub fixture_file('unicode/book.adoc')
Zip::File.open out_file do |zip|
expect(zip.find_entry('EPUB/test-é.xhtml')).not_to be_nil
end
@@ -21,26 +33,26 @@ describe Asciidoctor::Epub3::Converter do
it 'uses current date as fallback when date attributes cannot be parsed' do
# TODO: assert that error log contains 'failed to parse revdate' error when we add test infrastructure for logs
- book, = to_epub 'minimal/book.adoc', attributes: { 'revdate' => 'garbage' }
+ book, = to_epub fixture_file('minimal/book.adoc'), attributes: { 'revdate' => 'garbage' }
expect(book.metadata.date.content).not_to be_nil
end
it 'adds listing captions by default' do
- book, = to_epub 'listing/book.adoc'
+ book, = to_epub fixture_file('listing/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<figcaption>Listing 1. .gitattributes</figcaption>'
end
it 'increments listing numbering across chapters' do
- book, = to_epub 'listing-chapter/book.adoc'
+ book, = to_epub fixture_file('listing-chapter/book.adoc')
chapter_b = book.item_by_href 'chapter-b.xhtml'
expect(chapter_b).not_to be_nil
expect(chapter_b.content).to include '<figcaption>Listing 2. .gitattributes</figcaption>'
end
it 'adds preamble chapter' do
- book, = to_epub 'preamble/book.adoc'
+ book, = to_epub fixture_file('preamble/book.adoc')
spine = book.spine.itemref_list
expect(spine).to have_size(2)
@@ -51,7 +63,7 @@ describe Asciidoctor::Epub3::Converter do
end
it 'converts appendix to a separate book chapter' do
- book, = to_epub 'appendix.adoc'
+ book, = to_epub fixture_file('appendix.adoc')
spine = book.spine.itemref_list
expect(spine).to have_size(2)
@@ -61,7 +73,7 @@ describe Asciidoctor::Epub3::Converter do
end
it 'converts multi-part book' do
- book, = to_epub 'multi-part.adoc'
+ book, = to_epub fixture_file('multi-part.adoc')
spine = book.spine.itemref_list
expect(spine).to have_size(4)
@@ -74,13 +86,13 @@ describe Asciidoctor::Epub3::Converter do
end
it 'populates ebook subject from keywords' do
- book, = to_epub 'keywords/book.adoc'
+ book, = to_epub fixture_file('keywords/book.adoc')
keywords = book.subject_list.map(&:content)
expect(keywords).to eq(%w(a b c))
end
it 'adds front matter page with images' do
- book, = to_epub 'front-matter/book.adoc'
+ book, = to_epub fixture_file('front-matter/book.adoc')
spine = book.spine.itemref_list
expect(spine).to have_size(2)
@@ -92,7 +104,7 @@ describe Asciidoctor::Epub3::Converter do
end
it 'adds multiple front matter page with images' do
- book, = to_epub 'front-matter-multi/book.adoc'
+ book, = to_epub fixture_file('front-matter-multi/book.adoc')
spine = book.spine.itemref_list
expect(spine).to have_size(3)
@@ -110,7 +122,7 @@ describe Asciidoctor::Epub3::Converter do
end
it 'places footnotes in the same chapter' do
- book, = to_epub 'footnote/book.adoc'
+ book, = to_epub fixture_file('footnote/book.adoc')
chapter_a = book.item_by_href 'chapter-a.xhtml'
chapter_b = book.item_by_href 'chapter-b.xhtml'
expect(chapter_a).not_to be_nil
@@ -125,20 +137,20 @@ describe Asciidoctor::Epub3::Converter do
end
it 'resolves deep includes relative to document that contains include directive' do
- book, = to_epub 'deep-include/book.adoc'
+ book, = to_epub fixture_file('deep-include/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<p>Hello</p>'
end
it 'adds no book authors if there are none' do
- book, = to_epub 'author/book-no-author.adoc'
+ book, = to_epub fixture_file('author/book-no-author.adoc')
expect(book.creator).to be_nil
expect(book.creator_list.size).to eq(0)
end
it 'adds a single book author' do
- book, = to_epub 'author/book-one-author.adoc'
+ book, = to_epub fixture_file('author/book-one-author.adoc')
expect(book.creator).not_to be_nil
expect(book.creator.content).to eq('Author One')
expect(book.creator.role.content).to eq('aut')
@@ -146,7 +158,7 @@ describe Asciidoctor::Epub3::Converter do
end
it 'adds multiple book authors' do
- book, = to_epub 'author/book-multiple-authors.adoc'
+ book, = to_epub fixture_file('author/book-multiple-authors.adoc')
expect(book.metadata.creator).not_to be_nil
expect(book.metadata.creator.content).to eq('Author One')
expect(book.metadata.creator.role.content).to eq('aut')
@@ -156,13 +168,13 @@ describe Asciidoctor::Epub3::Converter do
end
it 'adds the publisher if both publisher and producer are defined' do
- book, = to_epub 'author/book-one-author.adoc'
+ book, = to_epub fixture_file('author/book-one-author.adoc')
expect(book.publisher).not_to be_nil
expect(book.publisher.content).to eq('MyPublisher')
end
it 'adds the producer as publisher if no publisher is defined' do
- book, = to_epub 'author/book-no-author.adoc'
+ book, = to_epub fixture_file('author/book-no-author.adoc')
expect(book.publisher).not_to be_nil
expect(book.publisher.content).to eq('MyProducer')
end
diff --git a/spec/image_spec.rb b/spec/image_spec.rb
index 9d3dcab..af2c6b7 100644
--- a/spec/image_spec.rb
+++ b/spec/image_spec.rb
@@ -5,7 +5,7 @@ require 'asciidoctor-diagram'
describe 'Asciidoctor::Epub3::Converter - Image' do
it 'supports imagesoutdir != imagesdir != "{base_dir}/images"' do
- book, out_file = to_epub 'diagram/book.adoc'
+ book, out_file = to_epub fixture_file('diagram/book.adoc')
out_dir = out_file.dirname
expect(out_dir.join('a', 'a.png')).to exist
@@ -20,7 +20,7 @@ describe 'Asciidoctor::Epub3::Converter - Image' do
end
it 'supports inline images' do
- book, out_file = to_epub 'inline-image/book.adoc'
+ book, out_file = to_epub fixture_file('inline-image/book.adoc')
out_dir = out_file.dirname
expect(out_dir.join('imagez', 'inline-diag.png')).to exist
@@ -31,14 +31,14 @@ describe 'Asciidoctor::Epub3::Converter - Image' do
end
it 'converts font-based icons to CSS' do
- book, = to_epub 'icon/book.adoc'
+ book, = to_epub fixture_file('icon/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '.i-commenting::before { content: "\f4ad"; }'
end
it 'adds front cover image' do
- book, = to_epub 'front-cover-image/book.adoc'
+ book, = to_epub fixture_file('front-cover-image/book.adoc')
cover_image = book.item_by_href 'jacket/cover.png'
expect(cover_image).not_to be_nil
cover_page = book.item_by_href 'cover.xhtml'
@@ -47,7 +47,7 @@ describe 'Asciidoctor::Epub3::Converter - Image' do
end
it 'supports image width/height' do
- book, = to_epub 'image-dimensions/book.adoc'
+ book, = to_epub fixture_file('image-dimensions/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<img src="square.png" alt="100x100" width="100" />'
@@ -57,7 +57,7 @@ describe 'Asciidoctor::Epub3::Converter - Image' do
# If this test fails for you, make sure you're using gepub >= 1.0.11
it 'adds SVG attribute to EPUB manifest if chapter contains SVG images' do
- book, = to_epub 'svg/book.adoc'
+ book, = to_epub fixture_file('svg/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
properties = chapter['properties']
diff --git a/spec/inline_quoted_spec.rb b/spec/inline_quoted_spec.rb
index 07ed8fa..68e500b 100644
--- a/spec/inline_quoted_spec.rb
+++ b/spec/inline_quoted_spec.rb
@@ -9,7 +9,7 @@ describe 'Asciidoctor::Epub3::Converter - Inline Quoted' do
let(:node) { Asciidoctor::Inline.new parent, :bar, 'text', type: type }
it 'resolves inline quotes' do
- book, = to_epub 'inline-quote/book.adoc'
+ book, = to_epub fixture_file('inline-quote/book.adoc')
chapter = book.item_by_href 'chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<p><span class="inline-quote">Knowledge kills action; action requires the veils of illusion.</span></p>'
diff --git a/spec/reproducible_spec.rb b/spec/reproducible_spec.rb
index d9d2771..5c5b86e 100644
--- a/spec/reproducible_spec.rb
+++ b/spec/reproducible_spec.rb
@@ -6,19 +6,19 @@ describe Asciidoctor::Epub3::Converter do
it 'produces stable output for reproducible books' do
out_file1 = temp_file 'book1.epub'
out_file2 = temp_file 'book2.epub'
- to_epub 'reproducible/book.adoc', to_file: out_file1
+ to_epub fixture_file('reproducible/book.adoc'), to_file: out_file1.to_s
sleep 2
- to_epub 'reproducible/book.adoc', to_file: out_file2
- expect(FileUtils.compare_file(out_file1, out_file2)).to be true
+ to_epub fixture_file('reproducible/book.adoc'), to_file: out_file2.to_s
+ expect(FileUtils.compare_file(out_file1.to_s, out_file2.to_s)).to be true
end
it %(doesn't include date for reproducible books) do
- book, = to_epub 'reproducible/book.adoc'
+ book, = to_epub fixture_file('reproducible/book.adoc')
expect(book.date).to be_nil
end
it 'uses fixed lastmodified date for reproducible books' do
- book, = to_epub 'reproducible/book.adoc'
+ book, = to_epub fixture_file('reproducible/book.adoc')
expect(Time.parse(book.lastmodified.content)).to eq (Time.at 0).utc
end
@@ -26,7 +26,7 @@ describe Asciidoctor::Epub3::Converter do
old_source_date_epoch = ENV.delete 'SOURCE_DATE_EPOCH'
begin
ENV['SOURCE_DATE_EPOCH'] = '1234123412'
- book, = to_epub 'minimal/book.adoc'
+ book, = to_epub fixture_file('minimal/book.adoc')
expect(book.date.content).to eq('2009-02-08T20:03:32Z')
expect(book.lastmodified.content).to eq('2009-02-08T20:03:32Z')
ensure
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index b93bcfe..f5ba833 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -38,27 +38,27 @@ RSpec.configure do |config|
end
def temp_dir
- File.join __dir__, 'temp'
+ Pathname.new(__dir__).join 'temp'
end
def temp_file *path
- File.join temp_dir, *path
+ temp_dir.join(*path)
end
def fixtures_dir
- File.join __dir__, 'fixtures'
+ Pathname.new(__dir__).join 'fixtures'
end
def fixture_file *path
- File.join fixtures_dir, path
+ fixtures_dir.join(*path)
end
def examples_dir
- File.join __dir__, '..', 'data', 'samples'
+ Pathname.new(__dir__).join '..', 'data', 'samples'
end
def example_file *path
- File.join examples_dir, path
+ examples_dir.join(*path)
end
def has_logger?
@@ -73,30 +73,35 @@ RSpec.configure do |config|
RbConfig::CONFIG['host_os'] =~ /darwin/
end
- def skip_if_darwin
+ def skip_unless_has_kindlegen
# TODO: https://github.com/asciidoctor/asciidoctor-epub3/issues/236
skip '#236: Kindlegen is unavailable for-bit MacOS' if darwin_platform?
end
def convert input, opts = {}
- input = fixture_file input if String === input
- opts[:to_dir] = temp_dir unless opts.key?(:to_dir) || opts.key?(:to_file)
opts[:backend] = 'epub3'
opts[:header_footer] = true
opts[:mkdirs] = true
opts[:safe] = Asciidoctor::SafeMode::UNSAFE unless opts.key? :safe
- Asciidoctor.convert_file input, opts
+
+ if Pathname === input
+ opts[:to_dir] = temp_dir.to_s unless opts.key?(:to_dir) || opts.key?(:to_file)
+ Asciidoctor.convert_file input.to_s, opts
+ else
+ Asciidoctor.convert input, opts
+ end
end
def to_epub input, opts = {}
- doc = convert input, opts
- output = Pathname.new doc.attr('outfile')
+ result = convert input, opts
+ return result if GEPUB::Book === result
+ output = Pathname.new result.attr('outfile')
book = GEPUB::Book.parse output
[book, output]
end
def to_mobi input, opts = {}
- skip_if_darwin
+ skip_unless_has_kindlegen
(opts[:attributes] ||= {})['ebook-format'] = 'mobi'
doc = convert input, opts
output = Pathname.new(doc.attr('outfile')).sub_ext '.mobi'
diff --git a/spec/stem_spec.rb b/spec/stem_spec.rb
index 2f2cac7..0bf8575 100644
--- a/spec/stem_spec.rb
+++ b/spec/stem_spec.rb
@@ -4,14 +4,14 @@ require_relative 'spec_helper'
describe 'Asciidoctor::Epub3::Converter - Stem' do
it 'converts stem block to <code>' do
- book, = to_epub 'stem/book.adoc'
+ book, = to_epub fixture_file('stem/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<code>\sqrt(4) = 2</code>'
end
it 'converts inline stem to <code>' do
- book, = to_epub 'inline-stem/book.adoc'
+ book, = to_epub fixture_file('inline-stem/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<code class="literal">y=x^2 sqrt(4)</code>'
diff --git a/spec/xref_spec.rb b/spec/xref_spec.rb
index f82e082..4dc88a4 100644
--- a/spec/xref_spec.rb
+++ b/spec/xref_spec.rb
@@ -5,28 +5,28 @@ require_relative 'spec_helper'
describe 'Asciidoctor::Epub3::Converter - Xref' do
context 'inter-chapter' do
it 'resolves xref to top of chapter' do
- book, = to_epub 'inter-chapter-xref/book.adoc'
+ book, = to_epub fixture_file('inter-chapter-xref/book.adoc')
chapter_a = book.item_by_href 'chapter-a.xhtml'
expect(chapter_a).not_to be_nil
expect(chapter_a.content).to include '<a id="xref--chapter-b" href="chapter-b.xhtml" class="xref">Chapter B</a>'
end
it 'resolves xref to section inside chapter' do
- book, = to_epub 'inter-chapter-xref-to-subsection/book.adoc'
+ book, = to_epub fixture_file('inter-chapter-xref-to-subsection/book.adoc')
chapter_a = book.item_by_href 'chapter-a.xhtml'
expect(chapter_a).not_to be_nil
expect(chapter_a.content).to include '<a id="xref--chapter-b--getting-started" href="chapter-b.xhtml#getting-started" class="xref">Getting Started</a>'
end
it 'resolves xref between subchapter include files' do
- book, = to_epub 'inter-subchapter-xref/book.adoc'
+ book, = to_epub fixture_file('inter-subchapter-xref/book.adoc')
chapter_a = book.item_by_href 'chapter-a.xhtml'
expect(chapter_a).not_to be_nil
expect(chapter_a.content).to include '<a id="xref--chapter-b--anchor" href="chapter-b.xhtml#anchor" class="xref">label</a>'
end
it 'resolves xref to inline anchor' do
- book, = to_epub 'inline-anchor-xref/book.adoc'
+ book, = to_epub fixture_file('inline-anchor-xref/book.adoc')
chapter = book.item_by_href 'chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<a id="item1"></a>foo::bar'
@@ -34,7 +34,7 @@ describe 'Asciidoctor::Epub3::Converter - Xref' do
end
it 'resolves xref to bibliography anchor' do
- book, = to_epub 'bibliography-xref/book.adoc'
+ book, = to_epub fixture_file('bibliography-xref/book.adoc')
chapter = book.item_by_href 'chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<a id="item1"></a>[item1] foo::bar'
@@ -42,7 +42,7 @@ describe 'Asciidoctor::Epub3::Converter - Xref' do
end
it 'resolves xref to bibliography chapter' do
- book, = to_epub 'bibliography-chapter/book.adoc'
+ book, = to_epub fixture_file('bibliography-chapter/book.adoc')
chapter = book.item_by_href 'chapter.xhtml'
expect(chapter).not_to be_nil
expect(chapter.content).to include '<a id="xref--bibliography--pp" href="bibliography.xhtml#pp" class="xref">[pp]</a>'