diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2014-02-28 18:30:26 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2014-02-28 18:30:26 -0700 |
| commit | f87bc738c4bd03c5049194449cacd854ed729430 (patch) | |
| tree | 3ca51a03d3da86b7b64d343fc09a0c3a7991f68b | |
| parent | 9dd91712bf4c193b91411dcbea1d2f044ee82b4a (diff) | |
don't fail if seeking input is not permitted; add test
| -rw-r--r-- | lib/asciidoctor.rb | 4 | ||||
| -rw-r--r-- | lib/asciidoctor/cli/invoker.rb | 6 | ||||
| -rw-r--r-- | test/invoker_test.rb | 12 |
3 files changed, 18 insertions, 4 deletions
diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb index ce21f5e8..2755cfb7 100644 --- a/lib/asciidoctor.rb +++ b/lib/asciidoctor.rb @@ -1223,7 +1223,9 @@ module Asciidoctor attributes['doctime'] = doctime = input_mtime.strftime('%H:%M:%S %Z') attributes['docdatetime'] = %(#{docdate} #{doctime}) elsif input.respond_to? :readlines - input.rewind if input.respond_to? :rewind + # NOTE tty, pipes & sockets can't be rewound, but can't be sniffed easily either + # just fail the rewind operation silently to handle all cases + input.rewind rescue nil lines = input.readlines elsif input.is_a? ::String lines = input.lines.entries diff --git a/lib/asciidoctor/cli/invoker.rb b/lib/asciidoctor/cli/invoker.rb index 585fd408..403748c3 100644 --- a/lib/asciidoctor/cli/invoker.rb +++ b/lib/asciidoctor/cli/invoker.rb @@ -67,10 +67,10 @@ module Asciidoctor } if infiles.size == 1 && infiles[0] == '-' - # allows use of block to supply stdin, particularly useful for tests - inputs = [block_given? ? yield : STDIN] + # allows use of block to supply stdin, particularly useful for tests + inputs = [block_given? ? yield : STDIN] else - inputs = infiles.map {|infile| ::File.new infile, 'r'} + inputs = infiles.map {|infile| ::File.new infile, 'r'} end # NOTE: if infile is stdin, default to outfile as stout diff --git a/test/invoker_test.rb b/test/invoker_test.rb index daa8745e..78e354f7 100644 --- a/test/invoker_test.rb +++ b/test/invoker_test.rb @@ -61,6 +61,18 @@ context 'Invoker' do assert_xpath '/*[@class="paragraph"]/p[text()="content"]', output, 1 end + test 'should not fail to rewind input if reading document from stdin' do + io = STDIN.dup + class << io + def readlines + ['paragraph'] + end + end + invoker = invoke_cli_to_buffer(%w(-s), '-') { io } + assert_equal 0, invoker.code + assert_equal 1, invoker.document.blocks.size + end + test 'should accept document from stdin and write to output file' do sample_outpath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample-output.html')) begin |
