Permalink
Browse files

Fixed rdoc -E

rdoc -E depended on the original file existing.  In the case it didn't
it would report the file as unparseable.  Original patch by Nobuyoshi
Nakada as ruby commit r35532.

The patch was updated to handle modeline support and rearrange the tests
to work properly.  Now can_parse will return a parser which will get
checked later by RDoc::Parser::for.

Ruby bug #6392
  • Loading branch information...
1 parent 6ed04d6 commit 99b693cd68baaaa808314d9c416fcdf321b60c1b @drbrain drbrain committed Jan 4, 2013
View
@@ -16,6 +16,7 @@ to build HTML documentation when installing gems.)
* Bug fixes
* Fixed parsing of multibyte files with incomplete characters at byte 1024.
Ruby bug #6393 by nobu, patch by Nobuyoshi Nakada and Yui NARUSE.
+ * Fixed rdoc -E. Ruby Bug #6392 and (modified) patch by Nobuyoshi Nakada
* Added link handling to Markdown output. Bug #160 by burningTyger.
* Fixed HEREDOC output for the limited case of a heredoc followed by a line
end. When a HEREDOC is not followed by a line end RDoc is not currently
View
@@ -58,7 +58,7 @@ def self.alias_extension(old_ext, new_ext)
old_ext = old_ext.sub(/^\.(.*)/, '\1')
new_ext = new_ext.sub(/^\.(.*)/, '\1')
- parser = can_parse "xxx.#{old_ext}"
+ parser = can_parse_by_name "xxx.#{old_ext}"
return false unless parser
RDoc::Parser.parsers.unshift [/\.#{new_ext}$/, parser]
@@ -131,23 +131,36 @@ def self.zip? file
zip_signature == "PK\x03\x04" or
zip_signature == "PK\x05\x06" or
zip_signature == "PK\x07\x08"
+ rescue
+ false
end
##
# Return a parser that can handle a particular extension
- def self.can_parse(file_name)
- parser = RDoc::Parser.parsers.find { |regexp,| regexp =~ file_name }.last
+ def self.can_parse file_name
+ parser = can_parse_by_name file_name
# HACK Selenium hides a jar file using a .txt extension
return if parser == RDoc::Parser::Simple and zip? file_name
+ parser
+ end
+
+ ##
+ # Returns a parser that can handle the extension for +file_name+. This does
+ # not depend upon the file being readable.
+
+ def self.can_parse_by_name file_name
+ _, parser = RDoc::Parser.parsers.find { |regexp,| regexp =~ file_name }
+
# The default parser must not parse binary files
ext_name = File.extname file_name
return parser if ext_name.empty?
+
if parser == RDoc::Parser::Simple and ext_name !~ /txt|rdoc/ then
case check_modeline file_name
- when 'rdoc' then # continue
+ when nil, 'rdoc' then # continue
else return nil
end
end
@@ -173,6 +186,8 @@ def self.check_modeline file_name
type = $1
end
+ return nil if /coding:/i =~ type
+
type.downcase
rescue ArgumentError # invalid byte sequence, etc.
end
@@ -204,6 +219,8 @@ def self.for top_level, file_name, content, options, stats
return unless parser
parser.new top_level, file_name, content, options, stats
+ rescue SystemCallError
+ nil
end
##
@@ -549,6 +549,15 @@ def test_parse_write_options
FileUtils.rm_rf tmpdir
end
+ def test_parse_extension_alias
+ out, err = capture_io do
+ @options.parse %w[--extension foobar=rdoc]
+ end
+
+ assert_empty out
+ assert_empty err
+ end
+
def test_setup_generator
test_generator = Class.new do
def self.setup_options op
@@ -33,11 +33,16 @@ def test_class_binary_japanese_text
end
def test_class_binary_large_japanese_rdoc
- extenc, Encoding.default_external = Encoding.default_external, Encoding::US_ASCII
- file_name = File.expand_path '../test.ja.largedoc', __FILE__
- assert !@RP.binary?(file_name)
- ensure
- Encoding.default_external = extenc
+ capture_io do
+ begin
+ extenc, Encoding.default_external =
+ Encoding.default_external, Encoding::US_ASCII
+ file_name = File.expand_path '../test.ja.largedoc', __FILE__
+ assert !@RP.binary?(file_name)
+ ensure
+ Encoding.default_external = extenc
+ end
+ end
end
def test_class_binary_japanese_rdoc
@@ -54,7 +59,7 @@ def test_class_can_parse
assert_equal @RP::Simple, @RP.can_parse(readme_file_name)
- assert_nil @RP.can_parse(@binary_dat)
+ assert_equal @RP::Simple, @RP.can_parse(@binary_dat)
jtest_file_name = File.expand_path '../test.ja.txt', __FILE__
assert_equal @RP::Simple, @RP.can_parse(jtest_file_name)
@@ -64,27 +69,20 @@ def test_class_can_parse
readme_file_name = File.expand_path '../README', __FILE__
assert_equal @RP::Simple, @RP.can_parse(readme_file_name)
- end
- def test_class_can_parse_forbidden
- skip 'chmod not supported' if Gem.win_platform?
+ jtest_largerdoc_file_name = File.expand_path '../test.ja.largedoc', __FILE__
+ assert_equal @RP::Simple, @RP.can_parse(jtest_largerdoc_file_name)
- Tempfile.open 'forbidden' do |io|
- begin
- File.chmod 0000, io.path
-
- assert_nil @RP.can_parse io.path
- ensure
- File.chmod 0400, io.path
- end
- end
+ @RP.alias_extension 'rdoc', 'largedoc'
+ assert_equal @RP::Simple, @RP.can_parse(jtest_largerdoc_file_name)
end
def test_class_for_executable
temp_dir do
content = "#!/usr/bin/env ruby -w\n"
open 'app', 'w' do |io| io.write content end
app = @store.add_file 'app'
+
parser = @RP.for app, 'app', content, @options, :stats
assert_kind_of RDoc::Parser::Ruby, parser
@@ -93,6 +91,23 @@ def test_class_for_executable
end
end
+ def test_class_for_forbidden
+ skip 'chmod not supported' if Gem.win_platform?
+
+ Tempfile.open 'forbidden' do |io|
+ begin
+ File.chmod 0000, io.path
+ forbidden = @store.add_file io.path
+
+ parser = @RP.for forbidden, 'forbidden', '', @options, :stats
+
+ assert_nil parser
+ ensure
+ File.chmod 0400, io.path
+ end
+ end
+ end
+
def test_can_parse_modeline
readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
@@ -129,6 +144,18 @@ def test_check_modeline
File.unlink readme_ext
end
+ def test_check_modeline_coding
+ readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
+
+ open readme_ext, 'w' do |io|
+ io.puts "# -*- coding: utf-8 -*-"
+ end
+
+ assert_nil @RP.check_modeline readme_ext
+ ensure
+ File.unlink readme_ext
+ end
+
def test_check_modeline_with_other
readme_ext = File.join Dir.tmpdir, "README.EXT.#{$$}"
@@ -64,17 +64,40 @@ def teardown
def test_class_can_parse
c_parser = RDoc::Parser::C
- assert_equal c_parser, c_parser.can_parse('file.C')
- assert_equal c_parser, c_parser.can_parse('file.CC')
- assert_equal c_parser, c_parser.can_parse('file.H')
- assert_equal c_parser, c_parser.can_parse('file.HH')
- assert_equal c_parser, c_parser.can_parse('file.c')
- assert_equal c_parser, c_parser.can_parse('file.cc')
- assert_equal c_parser, c_parser.can_parse('file.cpp')
- assert_equal c_parser, c_parser.can_parse('file.cxx')
- assert_equal c_parser, c_parser.can_parse('file.h')
- assert_equal c_parser, c_parser.can_parse('file.hh')
- assert_equal c_parser, c_parser.can_parse('file.y')
+ temp_dir do
+ FileUtils.touch 'file.C'
+ assert_equal c_parser, c_parser.can_parse('file.C')
+
+ FileUtils.touch 'file.CC'
+ assert_equal c_parser, c_parser.can_parse('file.CC')
+
+ FileUtils.touch 'file.H'
+ assert_equal c_parser, c_parser.can_parse('file.H')
+
+ FileUtils.touch 'file.HH'
+ assert_equal c_parser, c_parser.can_parse('file.HH')
+
+ FileUtils.touch 'file.c'
+ assert_equal c_parser, c_parser.can_parse('file.c')
+
+ FileUtils.touch 'file.cc'
+ assert_equal c_parser, c_parser.can_parse('file.cc')
+
+ FileUtils.touch 'file.cpp'
+ assert_equal c_parser, c_parser.can_parse('file.cpp')
+
+ FileUtils.touch 'file.cxx'
+ assert_equal c_parser, c_parser.can_parse('file.cxx')
+
+ FileUtils.touch 'file.h'
+ assert_equal c_parser, c_parser.can_parse('file.h')
+
+ FileUtils.touch 'file.hh'
+ assert_equal c_parser, c_parser.can_parse('file.hh')
+
+ FileUtils.touch 'file.y'
+ assert_equal c_parser, c_parser.can_parse('file.y')
+ end
end
def test_initialize
@@ -25,11 +25,15 @@ def mu_pp obj
def test_class_can_parse
parser = RDoc::Parser::ChangeLog
- assert_equal parser, parser.can_parse('ChangeLog')
+ temp_dir do
+ FileUtils.touch 'ChangeLog'
+ assert_equal parser, parser.can_parse('ChangeLog')
- assert_equal parser, parser.can_parse(@tempfile.path)
+ assert_equal parser, parser.can_parse(@tempfile.path)
- assert_equal RDoc::Parser::Ruby, parser.can_parse('ChangeLog.rb')
+ FileUtils.touch 'ChangeLog.rb'
+ assert_equal RDoc::Parser::Ruby, parser.can_parse('ChangeLog.rb')
+ end
end
def test_continue_entry_body
@@ -27,11 +27,17 @@ def test_file
end
def test_class_can_parse
- assert_equal @RP::Markdown, @RP.can_parse('foo.md')
- assert_equal @RP::Markdown, @RP.can_parse('foo.md.ja')
-
- assert_equal @RP::Markdown, @RP.can_parse('foo.markdown')
- assert_equal @RP::Markdown, @RP.can_parse('foo.markdown.ja')
+ temp_dir do
+ FileUtils.touch 'foo.md'
+ assert_equal @RP::Markdown, @RP.can_parse('foo.md')
+ FileUtils.touch 'foo.md.ja'
+ assert_equal @RP::Markdown, @RP.can_parse('foo.md.ja')
+
+ FileUtils.touch 'foo.markdown'
+ assert_equal @RP::Markdown, @RP.can_parse('foo.markdown')
+ FileUtils.touch 'foo.markdown.ja'
+ assert_equal @RP::Markdown, @RP.can_parse('foo.markdown.ja')
+ end
end
def test_scan
@@ -34,8 +34,13 @@ def test_file
end
def test_class_can_parse
- assert_equal @RP::RD, @RP.can_parse('foo.rd')
- assert_equal @RP::RD, @RP.can_parse('foo.rd.ja')
+ temp_dir do
+ FileUtils.touch 'foo.rd'
+ assert_equal @RP::RD, @RP.can_parse('foo.rd')
+
+ FileUtils.touch 'foo.rd.ja'
+ assert_equal @RP::RD, @RP.can_parse('foo.rd.ja')
+ end
end
def test_scan
@@ -14,19 +14,22 @@ def setup
def test_document # functional test
options = RDoc::Options.new
- options.files = [File.expand_path('../xref_data.rb')]
+ options.files = [File.expand_path('../xref_data.rb', __FILE__)]
options.setup_generator 'ri'
options.main_page = 'MAIN_PAGE.rdoc'
+ options.root = Pathname File.expand_path('..', __FILE__)
options.title = 'title'
rdoc = RDoc::RDoc.new
temp_dir do
+ options.op_dir = 'ri'
+
capture_io do
rdoc.document options
end
- assert File.directory? 'doc'
+ assert File.directory? 'ri'
assert_equal rdoc, rdoc.store.rdoc
end

0 comments on commit 99b693c

Please sign in to comment.