Permalink
Browse files

Work around Selenium bug with a .jar in a .txt file

  • Loading branch information...
1 parent 7e79c1a commit 3164bf884d803ac864512f6c851400af4c2a34b7 @drbrain drbrain committed Feb 6, 2010
Showing with 55 additions and 17 deletions.
  1. +2 −0 History.txt
  2. +18 −4 lib/rdoc/parser.rb
  3. +1 −0 test/hidden.zip.txt
  4. +34 −13 test/test_rdoc_parser.rb
View
2 History.txt
@@ -33,6 +33,8 @@
* Emacs-style <tt>coding:</tt> is handled properly. Patch #27388.
* RubyLex now parses UTF-8 identifiers. Bug #26946, #26947.
* Fixed namespace lookup rules. Bug #26161.
+ * Worked around bug in Selenium where they hide a .jar in a .txt file.
+ Filed Selenium bug #27789.
=== 2.4.3 / 2009-04-01
View
22 lib/rdoc/parser.rb
@@ -78,14 +78,29 @@ def self.binary?(file)
private_class_method :binary?
##
+ # Checks if +file+ is a zip file in disguise. Signatures from
+ # http://www.garykessler.net/library/file_sigs.html
+
+ def self.zip? file
+ zip_signature = File.read file, 4
+
+ zip_signature == "PK\x03\x04" or
+ zip_signature == "PK\x05\x06" or
+ zip_signature == "PK\x07\x08"
+ 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
+ # HACK Selenium hides a jar file using a .txt extension
+ return if parser == RDoc::Parser::Simple and zip? file_name
+
# The default parser must not parse binary files
- return nil if parser == RDoc::Parser::Simple and
- file_name !~ /\.(txt|rdoc)$/ and binary? file_name
+ return if parser == RDoc::Parser::Simple and
+ file_name !~ /\.(txt|rdoc)$/ and binary? file_name
parser
end
@@ -106,8 +121,7 @@ def self.for(top_level, file_name, body, options, stats)
parser = can_parse file_name
- # This method must return a parser.
- parser ||= RDoc::Parser::Simple
+ return unless parser
parser.new top_level, file_name, body, options, stats
end
View
1 test/hidden.zip.txt
@@ -0,0 +1 @@
+PK
View
47 test/test_rdoc_parser.rb
@@ -1,29 +1,50 @@
require 'rubygems'
-require 'minitest/unit'
+require 'minitest/autorun'
require 'rdoc/parser'
require 'rdoc/parser/ruby'
class TestRDocParser < MiniTest::Unit::TestCase
- def test_can_parse
- assert_equal(RDoc::Parser.can_parse(__FILE__), RDoc::Parser::Ruby)
- readme_file_name = File.join(File.dirname(__FILE__), "..", "README.txt")
+ def setup
+ @RP = RDoc::Parser
+ @binary_dat = File.expand_path '../binary.dat', __FILE__
+ end
+
+ def test_class_can_parse
+ assert_equal @RP.can_parse(__FILE__), @RP::Ruby
+
+ readme_file_name = File.expand_path '../README.txt', __FILE__
unless File.exist? readme_file_name then # HACK for tests in trunk :/
- readme_file_name = File.join File.dirname(__FILE__), '..', '..', 'README'
+ readme_file_name = File.expand_path '../../README.txt', __FILE__
end
- assert_equal(RDoc::Parser.can_parse(readme_file_name), RDoc::Parser::Simple)
+ assert_equal @RP.can_parse(readme_file_name), @RP::Simple
- binary_file_name = File.join(File.dirname(__FILE__), "binary.dat")
- assert_equal(RDoc::Parser.can_parse(binary_file_name), nil)
+ assert_nil @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)
+
+ jtest_rdoc_file_name = File.expand_path '../test.ja.rdoc', __FILE__
+ assert_equal @RP::Simple, @RP.can_parse(jtest_rdoc_file_name)
+ end
- jtest_file_name = File.join(File.dirname(__FILE__), "test.ja.txt")
- assert_equal(RDoc::Parser::Simple, RDoc::Parser.can_parse(jtest_file_name))
+ ##
+ # Selenium hides a .jar file using a .txt extension.
- jtest_rdoc_file_name = File.join(File.dirname(__FILE__), "test.ja.rdoc")
- assert_equal(RDoc::Parser::Simple, RDoc::Parser.can_parse(jtest_rdoc_file_name))
+ def test_class_can_parse_zip
+ hidden_zip = File.expand_path '../hidden.zip.txt', __FILE__
+ assert_nil @RP.can_parse(hidden_zip)
end
+
+ def test_class_for_binary
+ rp = @RP.dup
+
+ def rp.can_parse(*args) nil end
+
+ assert_nil @RP.for nil, @binary_dat, nil, nil, nil
+ end
+
end
-MiniTest::Unit.autorun

0 comments on commit 3164bf8

Please sign in to comment.