Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests to run outside of the gem context with the manpage help topic #2653

Merged
merged 2 commits into from Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/asciidoctor/cli/options.rb
Expand Up @@ -136,11 +136,32 @@ def parse!(args)
'show the command usage if TOPIC is not specified (or not recognized)',
'dump the Asciidoctor man page (in troff/groff format) if TOPIC is manpage') do |topic|
if topic == 'manpage'
if ::File.exist?(manpage_path = (::File.join ::Asciidoctor::ROOT_PATH, 'man', 'asciidoctor.1'))
$stdout.puts(::IO.read manpage_path)
if (manpage_path = ENV['ASCIIDOCTOR_MANPAGE_PATH'])
if ::File.exist? manpage_path
if manpage_path.end_with? '.gz'
require 'zlib' unless defined? ::Zlib
$stdout.puts ::Zlib::GzipReader.open(manpage_path) {|gz| gz.read }
else
$stdout.puts ::IO.read manpage_path
end
else
$stderr.puts %(asciidoctor: FAILED: manual page not found: #{manpage_path})
return 1
end
elsif ::File.exist?(manpage_path = (::File.join ::Asciidoctor::ROOT_PATH, 'man', 'asciidoctor.1'))
$stdout.puts ::IO.read manpage_path
else
$stderr.puts 'asciidoctor: FAILED: man page not found; try `man asciidoctor`'
return 1
require 'open3' unless defined? ::Open3
manpage_path = ::Open3.popen3('man -w asciidoctor') {|_, out| out.read }.chop rescue ''
if manpage_path.empty?
$stderr.puts 'asciidoctor: FAILED: manual page not found; try `man asciidoctor`'
return 1
elsif manpage_path.end_with? '.gz'
require 'zlib' unless defined? ::Zlib
$stdout.puts ::Zlib::GzipReader.open(manpage_path) {|gz| gz.read }
else
$stdout.puts ::IO.read manpage_path
end
end
else
$stdout.puts opts
Expand Down
18 changes: 18 additions & 0 deletions test/options_test.rb
Expand Up @@ -31,6 +31,24 @@
assert_includes output, '.TH "ASCIIDOCTOR"'
end

test 'should print message and return error code 1 when manpage is not found' do
old_manpage_path = ENV['ASCIIDOCTOR_MANPAGE_PATH']
begin
ENV['ASCIIDOCTOR_MANPAGE_PATH'] = (manpage_path = fixture_path 'no-such-file.1')
redirect_streams do |out, stderr|
exitval = Asciidoctor::Cli::Options.parse!(%w(-h manpage))
assert_equal 1, exitval
assert_equal %(asciidoctor: FAILED: manual page not found: #{manpage_path}), stderr.string.chomp
end
ensure
if old_manpage_path
ENV['ASCIIDOCTOR_MANPAGE_PATH'] = old_manpage_path
else
ENV.delete 'ASCIIDOCTOR_MANPAGE_PATH'
end
end
end

test 'should return error code 1 when invalid option present' do
redirect_streams do |stdout, stderr|
exitval = Asciidoctor::Cli::Options.parse!(%w(--foobar))
Expand Down