"Fossies" - the Fresh Open Source Software Archive

Member "ruby-2.7.4/test/rdoc/test_rdoc_rdoc.rb" (7 Jul 2021, 13200 Bytes) of package /linux/misc/ruby-2.7.4.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. See also the latest Fossies "Diffs" side-by-side code changes report for "test_rdoc_rdoc.rb": 2.7.3_vs_2.7.4.

    1 # frozen_string_literal: true
    2 require_relative 'helper'
    3 
    4 class TestRDocRDoc < RDoc::TestCase
    5 
    6   def setup
    7     super
    8 
    9     @rdoc = RDoc::RDoc.new
   10     @rdoc.options = RDoc::Options.new
   11 
   12     @stats = RDoc::Stats.new @store, 0, 0
   13     @rdoc.instance_variable_set :@stats, @stats
   14   end
   15 
   16   def test_document # functional test
   17     options = RDoc::Options.new
   18     options.files = [File.expand_path('../xref_data.rb', __FILE__)]
   19     options.setup_generator 'ri'
   20     options.main_page = 'MAIN_PAGE.rdoc'
   21     options.root      = Pathname File.expand_path('..', __FILE__)
   22     options.title     = 'title'
   23 
   24     rdoc = RDoc::RDoc.new
   25 
   26     temp_dir do
   27       options.op_dir = 'ri'
   28 
   29       capture_output do
   30         rdoc.document options
   31       end
   32 
   33       assert File.directory? 'ri'
   34       assert_equal rdoc, rdoc.store.rdoc
   35     end
   36 
   37     store = rdoc.store
   38 
   39     assert_equal 'MAIN_PAGE.rdoc', store.main
   40     assert_equal 'title',          store.title
   41   end
   42 
   43   def test_document_with_dry_run # functional test
   44     options = RDoc::Options.new
   45     options.files = [File.expand_path('../xref_data.rb', __FILE__)]
   46     options.setup_generator 'darkfish'
   47     options.main_page = 'MAIN_PAGE.rdoc'
   48     options.root      = Pathname File.expand_path('..', __FILE__)
   49     options.title     = 'title'
   50     options.dry_run = true
   51 
   52     rdoc = RDoc::RDoc.new
   53 
   54     out = nil
   55     temp_dir do
   56       out, = capture_output do
   57         rdoc.document options
   58       end
   59 
   60       refute File.directory? 'doc'
   61       assert_equal rdoc, rdoc.store.rdoc
   62     end
   63     assert_includes out, '100%'
   64 
   65     store = rdoc.store
   66 
   67     assert_equal 'MAIN_PAGE.rdoc', store.main
   68     assert_equal 'title',          store.title
   69   end
   70 
   71   def test_gather_files
   72     a = File.expand_path __FILE__
   73     b = File.expand_path '../test_rdoc_text.rb', __FILE__
   74 
   75     assert_equal [a, b], @rdoc.gather_files([b, a, b])
   76   end
   77 
   78   def test_handle_pipe
   79     $stdin = StringIO.new "hello"
   80 
   81     out, = capture_output do
   82       @rdoc.handle_pipe
   83     end
   84 
   85     assert_equal "\n<p>hello</p>\n", out
   86   ensure
   87     $stdin = STDIN
   88   end
   89 
   90   def test_handle_pipe_rd
   91     $stdin = StringIO.new "=begin\nhello\n=end"
   92 
   93     @rdoc.options.markup = 'rd'
   94 
   95     out, = capture_output do
   96       @rdoc.handle_pipe
   97     end
   98 
   99     assert_equal "\n<p>hello</p>\n", out
  100   ensure
  101     $stdin = STDIN
  102   end
  103 
  104   def test_load_options
  105     temp_dir do
  106       options = RDoc::Options.new
  107       options.markup = 'tomdoc'
  108       options.write_options
  109 
  110       options = @rdoc.load_options
  111 
  112       assert_equal 'tomdoc', options.markup
  113     end
  114   end
  115 
  116   def test_load_options_invalid
  117     temp_dir do
  118       File.open '.rdoc_options', 'w' do |io|
  119         io.write "a: !ruby.yaml.org,2002:str |\nfoo"
  120       end
  121 
  122       e = assert_raise RDoc::Error do
  123         @rdoc.load_options
  124       end
  125 
  126       options_file = File.expand_path '.rdoc_options'
  127       assert_equal "#{options_file} is not a valid rdoc options file", e.message
  128     end
  129   end
  130 
  131   def load_options_no_file
  132     temp_dir do
  133       options = @rdoc.load_options
  134 
  135       assert_kind_of RDoc::Options, options
  136     end
  137   end
  138 
  139   def test_normalized_file_list
  140     test_path = File.expand_path(__FILE__)
  141     files = temp_dir do |dir|
  142       flag_file = @rdoc.output_flag_file dir
  143 
  144       FileUtils.touch flag_file
  145 
  146       @rdoc.normalized_file_list [test_path, flag_file]
  147     end
  148 
  149     files = files.map { |file| File.expand_path file }
  150 
  151     assert_equal [test_path], files
  152   end
  153 
  154   def test_normalized_file_list_not_modified
  155     @rdoc.last_modified[__FILE__] = File.stat(__FILE__).mtime
  156 
  157     files = @rdoc.normalized_file_list [__FILE__]
  158 
  159     assert_empty files
  160   end
  161 
  162   def test_normalized_file_list_non_file_directory
  163     dev = File::NULL
  164     omit "#{dev} is not a character special" unless
  165       File.chardev? dev
  166 
  167     files = nil
  168 
  169     out, err = verbose_capture_output do
  170       files = @rdoc.normalized_file_list [dev]
  171     end
  172 
  173     files = files.map { |file| File.expand_path file }
  174 
  175     assert_empty files
  176 
  177     assert_empty out
  178     assert_match %r"^rdoc can't parse", err
  179     assert_match %r"#{dev}$",           err
  180   end
  181 
  182   def test_normalized_file_list_with_dot_doc
  183     expected_files = []
  184     files = temp_dir do |dir|
  185       a = File.expand_path('a.rb')
  186       b = File.expand_path('b.rb')
  187       c = File.expand_path('c.rb')
  188       FileUtils.touch a
  189       FileUtils.touch b
  190       FileUtils.touch c
  191       # Use Dir.glob to convert short path of Dir.tmpdir to long path.
  192       a = Dir.glob(a).first
  193       b = Dir.glob(b).first
  194       c = Dir.glob(c).first
  195 
  196       dot_doc = File.expand_path('.document')
  197       FileUtils.touch dot_doc
  198       open(dot_doc, 'w') do |f|
  199         f.puts 'a.rb'
  200         f.puts 'b.rb'
  201       end
  202       expected_files << a
  203       expected_files << b
  204 
  205       @rdoc.normalized_file_list [File.realpath(dir)]
  206     end
  207 
  208     files = files.map { |file| File.expand_path file }
  209 
  210     assert_equal expected_files, files
  211   end
  212 
  213   def test_normalized_file_list_with_dot_doc_overridden_by_exclude_option
  214     expected_files = []
  215     files = temp_dir do |dir|
  216       a = File.expand_path('a.rb')
  217       b = File.expand_path('b.rb')
  218       c = File.expand_path('c.rb')
  219       FileUtils.touch a
  220       FileUtils.touch b
  221       FileUtils.touch c
  222       # Use Dir.glob to convert short path of Dir.tmpdir to long path.
  223       a = Dir.glob(a).first
  224       b = Dir.glob(b).first
  225       c = Dir.glob(c).first
  226 
  227       dot_doc = File.expand_path('.document')
  228       FileUtils.touch dot_doc
  229       open(dot_doc, 'w') do |f|
  230         f.puts 'a.rb'
  231         f.puts 'b.rb'
  232       end
  233       expected_files << a
  234 
  235       @rdoc.options.exclude = Regexp.new(['b.rb'].join('|'))
  236       @rdoc.normalized_file_list [File.realpath(dir)]
  237     end
  238 
  239     files = files.map { |file| File.expand_path file }
  240 
  241     assert_equal expected_files, files
  242   end
  243 
  244   def test_parse_file
  245     @rdoc.store = RDoc::Store.new
  246 
  247     temp_dir do |dir|
  248       @rdoc.options.root = Pathname(Dir.pwd)
  249 
  250       File.open 'test.txt', 'w' do |io|
  251         io.puts 'hi'
  252       end
  253 
  254       top_level = @rdoc.parse_file 'test.txt'
  255 
  256       assert_equal 'test.txt', top_level.absolute_name
  257       assert_equal 'test.txt', top_level.relative_name
  258     end
  259   end
  260 
  261   def test_parse_file_binary
  262     @rdoc.store = RDoc::Store.new
  263 
  264     root = File.dirname __FILE__
  265 
  266     @rdoc.options.root = Pathname root
  267 
  268     out, err = capture_output do
  269       Dir.chdir root do
  270         assert_nil @rdoc.parse_file 'binary.dat'
  271       end
  272     end
  273 
  274     assert_empty out
  275     assert_empty err
  276   end
  277 
  278   def test_parse_file_include_root
  279     @rdoc.store = RDoc::Store.new
  280 
  281     test_path = File.expand_path('..', __FILE__)
  282     top_level = nil
  283     temp_dir do |dir|
  284       @rdoc.options.parse %W[--root #{test_path}]
  285 
  286       File.open 'include.txt', 'w' do |io|
  287         io.puts ':include: test.txt'
  288       end
  289 
  290       out, err = capture_output do
  291         top_level = @rdoc.parse_file 'include.txt'
  292       end
  293       assert_empty out
  294       assert_empty err
  295     end
  296     assert_equal "test file", top_level.comment.text
  297   end
  298 
  299   def test_parse_file_page_dir
  300     @rdoc.store = RDoc::Store.new
  301 
  302     temp_dir do |dir|
  303       FileUtils.mkdir 'pages'
  304       @rdoc.options.page_dir = Pathname('pages')
  305       @rdoc.options.root = Pathname(Dir.pwd)
  306 
  307       File.open 'pages/test.txt', 'w' do |io|
  308         io.puts 'hi'
  309       end
  310 
  311       top_level = @rdoc.parse_file 'pages/test.txt'
  312 
  313       assert_equal 'pages/test.txt', top_level.absolute_name
  314       assert_equal 'test.txt',       top_level.relative_name
  315     end
  316   end
  317 
  318   def test_parse_file_relative
  319     pwd = Dir.pwd
  320 
  321     @rdoc.store = RDoc::Store.new
  322 
  323     temp_dir do |dir|
  324       @rdoc.options.root = Pathname(dir)
  325 
  326       File.open 'test.txt', 'w' do |io|
  327         io.puts 'hi'
  328       end
  329 
  330       test_txt = File.join dir, 'test.txt'
  331 
  332       Dir.chdir pwd do
  333         top_level = @rdoc.parse_file test_txt
  334 
  335         assert_equal test_txt,   top_level.absolute_name
  336         assert_equal 'test.txt', top_level.relative_name
  337       end
  338     end
  339   end
  340 
  341   def test_parse_file_encoding
  342     @rdoc.options.encoding = Encoding::ISO_8859_1
  343     @rdoc.store = RDoc::Store.new
  344 
  345     tf = Tempfile.open 'test.txt' do |io|
  346       io.write 'hi'
  347       io.rewind
  348 
  349       top_level = @rdoc.parse_file io.path
  350 
  351       assert_equal Encoding::ISO_8859_1, top_level.absolute_name.encoding
  352       io
  353     end
  354     tf.close!
  355   end
  356 
  357   def test_parse_file_forbidden
  358     omit 'chmod not supported' if Gem.win_platform?
  359     omit "assumes that euid is not root" if Process.euid == 0
  360 
  361     @rdoc.store = RDoc::Store.new
  362 
  363     tf = Tempfile.open 'test.txt' do |io|
  364       io.write 'hi'
  365       io.rewind
  366 
  367       File.chmod 0000, io.path
  368 
  369       begin
  370         top_level = :bug
  371 
  372         _, err = capture_output do
  373           top_level = @rdoc.parse_file io.path
  374         end
  375 
  376         assert_match "Unable to read #{io.path},", err
  377 
  378         assert_nil top_level
  379       ensure
  380         File.chmod 0400, io.path
  381       end
  382       io
  383     end
  384     tf.close!
  385   end
  386 
  387   def test_remove_unparseable
  388     file_list = %w[
  389       blah.class
  390       blah.eps
  391       blah.erb
  392       blah.scpt.txt
  393       blah.svg
  394       blah.ttf
  395       blah.yml
  396     ]
  397 
  398     assert_empty @rdoc.remove_unparseable file_list
  399   end
  400 
  401   def test_remove_unparseable_tags_emacs
  402     temp_dir do
  403       File.open 'TAGS', 'wb' do |io| # emacs
  404         io.write "\f\nlib/foo.rb,43\n"
  405       end
  406 
  407       file_list = %w[
  408         TAGS
  409       ]
  410 
  411       assert_empty @rdoc.remove_unparseable file_list
  412     end
  413   end
  414 
  415   def test_remove_unparseable_tags_vim
  416     temp_dir do
  417       File.open 'TAGS', 'w' do |io| # emacs
  418         io.write "!_TAG_"
  419       end
  420 
  421       file_list = %w[
  422         TAGS
  423       ]
  424 
  425       assert_empty @rdoc.remove_unparseable file_list
  426     end
  427   end
  428 
  429   def test_remove_unparseable_CVE_2021_31799
  430     omit 'for Un*x platforms' if Gem.win_platform?
  431     temp_dir do
  432       file_list = ['| touch evil.txt && echo tags']
  433       file_list.each do |f|
  434         FileUtils.touch f
  435       end
  436 
  437       assert_equal file_list, @rdoc.remove_unparseable(file_list)
  438       assert_equal file_list, Dir.children('.')
  439     end
  440   end
  441 
  442   def test_setup_output_dir
  443     Dir.mktmpdir {|d|
  444       path = File.join d, 'testdir'
  445 
  446       last = @rdoc.setup_output_dir path, false
  447 
  448       assert_empty last
  449 
  450       assert File.directory? path
  451       assert File.exist? @rdoc.output_flag_file path
  452     }
  453   end
  454 
  455   def test_setup_output_dir_dry_run
  456     @rdoc.options.dry_run = true
  457 
  458     Dir.mktmpdir do |d|
  459       path = File.join d, 'testdir'
  460 
  461       @rdoc.setup_output_dir path, false
  462 
  463       refute File.exist? path
  464     end
  465   end
  466 
  467   def test_setup_output_dir_exists
  468     Dir.mktmpdir {|path|
  469       File.open @rdoc.output_flag_file(path), 'w' do |io|
  470         io.puts Time.at 0
  471         io.puts "./lib/rdoc.rb\t#{Time.at 86400}"
  472       end
  473 
  474       last = @rdoc.setup_output_dir path, false
  475 
  476       assert_equal 1, last.size
  477       assert_equal Time.at(86400), last['./lib/rdoc.rb']
  478     }
  479   end
  480 
  481   def test_setup_output_dir_exists_empty_created_rid
  482     Dir.mktmpdir {|path|
  483       File.open @rdoc.output_flag_file(path), 'w' do end
  484 
  485       e = assert_raise RDoc::Error do
  486         @rdoc.setup_output_dir path, false
  487       end
  488 
  489       assert_match %r%Directory #{Regexp.escape path} already exists%, e.message
  490     }
  491   end
  492 
  493   def test_setup_output_dir_exists_file
  494     tf = Tempfile.open 'test_rdoc_rdoc' do |tempfile|
  495       path = tempfile.path
  496 
  497       e = assert_raise RDoc::Error do
  498         @rdoc.setup_output_dir path, false
  499       end
  500 
  501       assert_match(%r%#{Regexp.escape path} exists and is not a directory%,
  502                    e.message)
  503       tempfile
  504     end
  505     tf.close!
  506   end
  507 
  508   def test_setup_output_dir_exists_not_rdoc
  509     Dir.mktmpdir do |dir|
  510       e = assert_raise RDoc::Error do
  511         @rdoc.setup_output_dir dir, false
  512       end
  513 
  514       assert_match %r%Directory #{Regexp.escape dir} already exists%, e.message
  515     end
  516   end
  517 
  518   def test_update_output_dir
  519     Dir.mktmpdir do |d|
  520       @rdoc.update_output_dir d, Time.now, {}
  521 
  522       assert File.exist? "#{d}/created.rid"
  523     end
  524   end
  525 
  526   def test_update_output_dir_dont
  527     Dir.mktmpdir do |d|
  528       @rdoc.options.update_output_dir = false
  529       @rdoc.update_output_dir d, Time.now, {}
  530 
  531       refute File.exist? "#{d}/created.rid"
  532     end
  533   end
  534 
  535   def test_update_output_dir_dry_run
  536     Dir.mktmpdir do |d|
  537       @rdoc.options.dry_run = true
  538       @rdoc.update_output_dir d, Time.now, {}
  539 
  540       refute File.exist? "#{d}/created.rid"
  541     end
  542   end
  543 
  544   def test_update_output_dir_with_reproducible_time
  545     Dir.mktmpdir do |d|
  546       backup_epoch = ENV['SOURCE_DATE_EPOCH']
  547       ruby_birthday = Time.parse 'Wed, 24 Feb 1993 21:00:00 +0900'
  548       ENV['SOURCE_DATE_EPOCH'] = ruby_birthday.to_i.to_s
  549 
  550       @rdoc.update_output_dir d, Time.now, {}
  551 
  552       assert File.exist? "#{d}/created.rid"
  553 
  554       f = File.open("#{d}/created.rid", 'r')
  555       head_timestamp = Time.parse f.gets.chomp
  556       f.close
  557       assert_equal ruby_birthday, head_timestamp
  558 
  559       ENV['SOURCE_DATE_EPOCH'] = backup_epoch
  560     end
  561   end
  562 
  563   def test_normalized_file_list_removes_created_rid_dir
  564     temp_dir do |d|
  565       FileUtils.mkdir "doc"
  566       flag_file = @rdoc.output_flag_file "doc"
  567       file = File.join "doc", "test"
  568       FileUtils.touch flag_file
  569       FileUtils.touch file
  570 
  571       file_list = ["doc"]
  572 
  573       output = @rdoc.normalized_file_list file_list
  574 
  575       assert_empty output
  576     end
  577   end
  578 end