"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/test/options_test.rb" (1 Jun 2019, 11140 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 require_relative 'test_helper'
    3 require File.join Asciidoctor::LIB_DIR, 'asciidoctor/cli/options'
    4 
    5 context 'Options' do
    6   test 'should print usage and return error code 0 when help flag is present' do
    7     redirect_streams do |stdout, stderr|
    8       exitval = Asciidoctor::Cli::Options.parse!(%w(-h))
    9       assert_equal 0, exitval
   10       assert_match(/^Usage:/, stdout.string)
   11     end
   12   end
   13 
   14   test 'should print usage and return error code 0 when help flag is unknown' do
   15     exitval, output = redirect_streams do |out, _|
   16       [Asciidoctor::Cli::Options.parse!(%w(-h unknown)), out.string]
   17     end
   18     assert_equal 0, exitval
   19     assert_match(/^Usage:/, output)
   20   end
   21 
   22   test 'should dump man page and return error code 0 when help topic is manpage' do
   23     exitval, output = redirect_streams do |out, _|
   24       [Asciidoctor::Cli::Options.parse!(%w(-h manpage)), out.string]
   25     end
   26     assert_equal 0, exitval
   27     assert_includes output, 'Manual: Asciidoctor Manual'
   28     assert_includes output, '.TH "ASCIIDOCTOR"'
   29   end
   30 
   31   test 'should an overview of the AsciiDoc syntax and return error code 0 when help topic is syntax' do
   32     exitval, output = redirect_streams do |out, _|
   33       [Asciidoctor::Cli::Options.parse!(%w(-h syntax)), out.string]
   34     end
   35     assert_equal 0, exitval
   36     assert_includes output, '= AsciiDoc Syntax'
   37     assert_includes output, '== Text Formatting'
   38   end
   39 
   40   test 'should print message and return error code 1 when manpage is not found' do
   41     old_manpage_path = ENV['ASCIIDOCTOR_MANPAGE_PATH']
   42     begin
   43       ENV['ASCIIDOCTOR_MANPAGE_PATH'] = (manpage_path = fixture_path 'no-such-file.1')
   44       redirect_streams do |out, stderr|
   45         exitval = Asciidoctor::Cli::Options.parse!(%w(-h manpage))
   46         assert_equal 1, exitval
   47         assert_equal %(asciidoctor: FAILED: manual page not found: #{manpage_path}), stderr.string.chomp
   48       end
   49     ensure
   50       if old_manpage_path
   51         ENV['ASCIIDOCTOR_MANPAGE_PATH'] = old_manpage_path
   52       else
   53         ENV.delete 'ASCIIDOCTOR_MANPAGE_PATH'
   54       end
   55     end
   56   end
   57 
   58   test 'should return error code 1 when invalid option present' do
   59     redirect_streams do |stdout, stderr|
   60       exitval = Asciidoctor::Cli::Options.parse!(%w(--foobar))
   61       assert_equal 1, exitval
   62       assert_equal 'asciidoctor: invalid option: --foobar', stderr.string.chomp
   63     end
   64   end
   65 
   66   test 'should return error code 1 when option has invalid argument' do
   67     redirect_streams do |stdout, stderr|
   68       exitval = Asciidoctor::Cli::Options.parse!(%w(-d chapter input.ad)) # had to change for #320
   69       assert_equal 1, exitval
   70       assert_equal 'asciidoctor: invalid argument: -d chapter', stderr.string.chomp
   71     end
   72   end
   73 
   74   test 'should return error code 1 when option is missing required argument' do
   75     redirect_streams do |stdout, stderr|
   76       exitval = Asciidoctor::Cli::Options.parse!(%w(-b))
   77       assert_equal 1, exitval
   78       assert_equal 'asciidoctor: option missing argument: -b', stderr.string.chomp
   79     end
   80   end
   81 
   82   test 'should emit warning when unparsed options remain' do
   83     redirect_streams do |stdout, stderr|
   84       options = Asciidoctor::Cli::Options.parse!(%w(-b docbook - -))
   85       assert_kind_of Hash, options
   86       assert_match(/asciidoctor: WARNING: extra arguments .*/, stderr.string.chomp)
   87     end
   88   end
   89 
   90   test 'basic argument assignment' do
   91     options = Asciidoctor::Cli::Options.parse!(%w(-w -v -s -d book test/fixtures/sample.adoc))
   92 
   93     assert_equal 2, options[:verbose]
   94     assert_equal false, options[:standalone]
   95     assert_equal 'book', options[:attributes]['doctype']
   96     assert_equal 1, options[:input_files].size
   97     assert_equal 'test/fixtures/sample.adoc', options[:input_files][0]
   98   end
   99 
  100   test 'standard attribute assignment' do
  101     options = Asciidoctor::Cli::Options.parse!(%w(-a docinfosubs=attributes,replacements -a icons test/fixtures/sample.adoc))
  102 
  103     assert_equal 'attributes,replacements', options[:attributes]['docinfosubs']
  104     assert_equal '', options[:attributes]['icons']
  105   end
  106 
  107   test 'multiple attribute arguments' do
  108     options = Asciidoctor::Cli::Options.parse!(%w(-a imagesdir=images -a icons test/fixtures/sample.adoc))
  109 
  110     assert_equal 'images', options[:attributes]['imagesdir']
  111     assert_equal '', options[:attributes]['icons']
  112   end
  113 
  114   test 'should only split attribute key/value pairs on first equal sign' do
  115     options = Asciidoctor::Cli::Options.parse!(%w(-a name=value=value test/fixtures/sample.adoc))
  116 
  117     assert_equal 'value=value', options[:attributes]['name']
  118   end
  119 
  120   test 'should not fail if value of attribute option is empty' do
  121     options = Asciidoctor::Cli::Options.parse!(['-a', '', 'test/fixtures/sample.adoc'])
  122 
  123     assert_nil options[:attributes]
  124   end
  125 
  126   test 'should not fail if value of attribute option is equal sign' do
  127     options = Asciidoctor::Cli::Options.parse!(['-a', '=', 'test/fixtures/sample.adoc'])
  128 
  129     assert_nil options[:attributes]
  130   end
  131 
  132   test 'should allow safe mode to be specified' do
  133     options = Asciidoctor::Cli::Options.parse!(%w(-S safe test/fixtures/sample.adoc))
  134     assert_equal Asciidoctor::SafeMode::SAFE, options[:safe]
  135   end
  136 
  137   test 'should allow any backend to be specified' do
  138     options = Asciidoctor::Cli::Options.parse!(%w(-b my_custom_backend test/fixtures/sample.adoc))
  139 
  140     assert_equal 'my_custom_backend', options[:attributes]['backend']
  141   end
  142 
  143   test 'article doctype assignment' do
  144     options = Asciidoctor::Cli::Options.parse!(%w(-d article test/fixtures/sample.adoc))
  145     assert_equal 'article', options[:attributes]['doctype']
  146   end
  147 
  148   test 'book doctype assignment' do
  149     options = Asciidoctor::Cli::Options.parse!(%w(-d book test/fixtures/sample.adoc))
  150     assert_equal 'book', options[:attributes]['doctype']
  151   end
  152 
  153   test 'inline doctype assignment' do
  154     options = Asciidoctor::Cli::Options.parse!(%w(-d inline test/fixtures/sample.adoc))
  155     assert_equal 'inline', options[:attributes]['doctype']
  156   end
  157 
  158   test 'template engine assignment' do
  159     options = Asciidoctor::Cli::Options.parse!(%w(-E haml test/fixtures/sample.adoc))
  160     assert_equal 'haml', options[:template_engine]
  161   end
  162 
  163   test 'template directory assignment' do
  164     options = Asciidoctor::Cli::Options.parse!(%w(-T custom-backend test/fixtures/sample.adoc))
  165     assert_equal ['custom-backend'], options[:template_dirs]
  166   end
  167 
  168   test 'multiple template directory assignments' do
  169     options = Asciidoctor::Cli::Options.parse!(%w(-T custom-backend -T custom-backend-hacks test/fixtures/sample.adoc))
  170     assert_equal ['custom-backend', 'custom-backend-hacks'], options[:template_dirs]
  171   end
  172 
  173   test 'multiple -r flags requires specified libraries' do
  174     options = Asciidoctor::Cli::Options.new
  175     redirect_streams do |stdout, stderr|
  176       exitval = options.parse! %w(-r foobar -r foobaz test/fixtures/sample.adoc)
  177       assert_match(%(asciidoctor: FAILED: 'foobar' could not be loaded), stderr.string)
  178       assert_equal 1, exitval
  179       assert_equal ['foobar', 'foobaz'], options[:requires]
  180     end
  181   end
  182 
  183   test '-r flag with multiple values requires specified libraries' do
  184     options = Asciidoctor::Cli::Options.new
  185     redirect_streams do |stdout, stderr|
  186       exitval = options.parse! %w(-r foobar,foobaz test/fixtures/sample.adoc)
  187       assert_match(%(asciidoctor: FAILED: 'foobar' could not be loaded), stderr.string)
  188       assert_equal 1, exitval
  189       assert_equal ['foobar', 'foobaz'], options[:requires]
  190     end
  191   end
  192 
  193   test '-I option appends paths to $LOAD_PATH' do
  194     options = Asciidoctor::Cli::Options.new
  195     old_load_path = $LOAD_PATH.dup
  196     begin
  197       exitval = options.parse! %w(-I foobar -I foobaz test/fixtures/sample.adoc)
  198       refute_equal 1, exitval
  199       assert_equal old_load_path.size + 2, $LOAD_PATH.size
  200       assert_equal File.expand_path('foobar'), $LOAD_PATH[0]
  201       assert_equal File.expand_path('foobaz'), $LOAD_PATH[1]
  202       assert_equal ['foobar', 'foobaz'], options[:load_paths]
  203     ensure
  204       ($LOAD_PATH.size - old_load_path.size).times { $LOAD_PATH.shift }
  205     end
  206   end
  207 
  208   test '-I option appends multiple paths to $LOAD_PATH' do
  209     options = Asciidoctor::Cli::Options.new
  210     old_load_path = $LOAD_PATH.dup
  211     begin
  212       exitval = options.parse! %W(-I foobar#{File::PATH_SEPARATOR}foobaz test/fixtures/sample.adoc)
  213       refute_equal 1, exitval
  214       assert_equal old_load_path.size + 2, $LOAD_PATH.size
  215       assert_equal File.expand_path('foobar'), $LOAD_PATH[0]
  216       assert_equal File.expand_path('foobaz'), $LOAD_PATH[1]
  217       assert_equal ['foobar', 'foobaz'], options[:load_paths]
  218     ensure
  219       ($LOAD_PATH.size - old_load_path.size).times { $LOAD_PATH.shift }
  220     end
  221   end
  222 
  223   test 'should set failure level to FATAL by default' do
  224     options = Asciidoctor::Cli::Options.parse! %W(test/fixtures/sample.adoc)
  225     assert_equal ::Logger::Severity::FATAL, options[:failure_level]
  226   end
  227 
  228   test 'should allow failure level to be set to WARN' do
  229     %w(w warn WARN warning WARNING).each do |val|
  230       options = Asciidoctor::Cli::Options.parse!(%W(--failure-level=#{val} test/fixtures/sample.adoc))
  231       assert_equal ::Logger::Severity::WARN, options[:failure_level]
  232     end
  233   end
  234 
  235   test 'should allow failure level to be set to ERROR' do
  236     %w(e err ERR error ERROR).each do |val|
  237       options = Asciidoctor::Cli::Options.parse!(%W(--failure-level=#{val} test/fixtures/sample.adoc))
  238       assert_equal ::Logger::Severity::ERROR, options[:failure_level]
  239     end
  240   end
  241 
  242   test 'should not allow failure level to be set to unknown value' do
  243     exit_code, messages = redirect_streams do |_, err|
  244       [(Asciidoctor::Cli::Options.parse! %W(--failure-level=foobar test/fixtures/sample.adoc)), err.string]
  245     end
  246     assert_equal 1, exit_code
  247     assert_includes messages, 'invalid argument: --failure-level=foobar'
  248   end
  249 
  250   test 'should set verbose to 2 when -v flag is specified' do
  251     options = Asciidoctor::Cli::Options.parse!(%w(-v test/fixtures/sample.adoc))
  252     assert_equal 2, options[:verbose]
  253   end
  254 
  255   test 'should set verbose to 0 when -q flag is specified' do
  256     options = Asciidoctor::Cli::Options.parse!(%w(-q test/fixtures/sample.adoc))
  257     assert_equal 0, options[:verbose]
  258   end
  259 
  260   test 'should set verbose to 2 when -v flag is specified after -q flag' do
  261     options = Asciidoctor::Cli::Options.parse!(%w(-q -v test/fixtures/sample.adoc))
  262     assert_equal 2, options[:verbose]
  263   end
  264 
  265   test 'should set verbose to 0 when -q flag is specified after -v flag' do
  266     options = Asciidoctor::Cli::Options.parse!(%w(-v -q test/fixtures/sample.adoc))
  267     assert_equal 0, options[:verbose]
  268   end
  269 
  270   test 'should enable warnings when -w flag is specified' do
  271     options = Asciidoctor::Cli::Options.parse!(%w(-w test/fixtures/sample.adoc))
  272     assert options[:warnings]
  273   end
  274 
  275   test 'should enable timings when -t flag is specified' do
  276     options = Asciidoctor::Cli::Options.parse!(%w(-t test/fixtures/sample.adoc))
  277     assert_equal true, options[:timings]
  278   end
  279 
  280   test 'timings option is disable by default' do
  281     options = Asciidoctor::Cli::Options.parse!(%w(test/fixtures/sample.adoc))
  282     assert_equal false, options[:timings]
  283   end
  284 
  285 end