ruby-changes:34097
From: akr <ko1@a...>
Date: Tue, 27 May 2014 22:45:15 +0900 (JST)
Subject: [ruby-changes:34097] akr:r46178 (trunk): * test/rexml: Avoid fd leaks.
akr 2014-05-27 22:45:04 +0900 (Tue, 27 May 2014) New Revision: 46178 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=46178 Log: * test/rexml: Avoid fd leaks. Modified files: trunk/ChangeLog trunk/test/rexml/test_contrib.rb trunk/test/rexml/test_core.rb trunk/test/rexml/test_encoding.rb trunk/test/rexml/test_jaxen.rb trunk/test/rexml/test_lightparser.rb trunk/test/rexml/test_listener.rb trunk/test/rexml/test_order.rb trunk/test/rexml/test_rexml_issuezilla.rb trunk/test/rexml/test_sax.rb trunk/test/rexml/xpath/test_base.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 46177) +++ ChangeLog (revision 46178) @@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue May 27 22:44:20 2014 Tanaka Akira <akr@f...> + + * test/rexml: Avoid fd leaks. + Tue May 27 22:24:25 2014 Kouhei Sutou <kou@c...> * test/rexml/test_document.rb: Indent. Index: test/rexml/test_lightparser.rb =================================================================== --- test/rexml/test_lightparser.rb (revision 46177) +++ test/rexml/test_lightparser.rb (revision 46178) @@ -6,9 +6,10 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_lightparser.rb#L6 include REXMLTestUtils include REXML def test_parsing - f = File.new(fixture_path("documentation.xml")) - parser = REXML::Parsers::LightParser.new( f ) - parser.parse + File.open(fixture_path("documentation.xml")) do |f| + parser = REXML::Parsers::LightParser.new( f ) + parser.parse + end end end end Index: test/rexml/test_encoding.rb =================================================================== --- test/rexml/test_encoding.rb (revision 46177) +++ test/rexml/test_encoding.rb (revision 46178) @@ -88,7 +88,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_encoding.rb#L88 end def test_ticket_110 - utf16 = REXML::Document.new(File.new(fixture_path("ticket_110_utf16.xml"))) + utf16 = File.open(fixture_path("ticket_110_utf16.xml")) do |f| + REXML::Document.new(f) + end assert_equal(utf16.encoding, "UTF-16") assert( utf16[0].kind_of?(REXML::XMLDecl)) end Index: test/rexml/test_core.rb =================================================================== --- test/rexml/test_core.rb (revision 46177) +++ test/rexml/test_core.rb (revision 46178) @@ -227,8 +227,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L227 assert_equal(correct, test) # OK, the BIG doctype test, numba wun - docin = File.new(fixture_path("doctype_test.xml")) - doc = Document.new(docin) + doc = File.open(fixture_path("doctype_test.xml")) do |docin| + Document.new(docin) + end doc.write(test="") assert_equal(31, doc.doctype.size) end @@ -248,8 +249,7 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L249 assert_instance_of DocType, doc.doctype assert_equal doc.version, "1.0" - source = File.new(fixture_path("dash.xml")) - doc = Document.new source + doc = File.open(fixture_path("dash.xml")) {|source| Document.new source } assert_equal "content-2", doc.elements["//content-2"].name end @@ -339,7 +339,7 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L339 assert_equal doc.root.name.to_s, "xsa" # Testing IO source - doc = Document.new File.new(fixture_path("project.xml")) + doc = File.open(fixture_path("project.xml")) {|f| Document.new f } assert_equal doc.root.name.to_s, "Project" end @@ -436,7 +436,7 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L436 # enormous. def test_element_access # Testing each_element - doc = Document.new File.new(fixture_path("project.xml")) + doc = File.open(fixture_path("project.xml")) {|f| Document.new f } each_test( doc, "/", 1 ) { |child| assert_equal doc.name, child.name @@ -601,22 +601,23 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L601 def test_big_documentation - f = File.new(fixture_path("documentation.xml")) - d = Document.new f + d = File.open(fixture_path("documentation.xml")) {|f| Document.new f } assert_equal "Sean Russell", d.elements["documentation/head/author"].text.tr("\n\t", " ").squeeze(" ") out = "" d.write out end def test_tutorial - doc = Document.new File.new(fixture_path("tutorial.xml")) + doc = File.open(fixture_path("tutorial.xml")) {|f| Document.new f } out = "" doc.write out end def test_stream c = Listener.new - Document.parse_stream( File.new(fixture_path("documentation.xml")), c ) + File.open(fixture_path("documentation.xml")) do |f| + Document.parse_stream( f, c ) + end assert(c.ts, "Stream parsing apparantly didn't parse the whole file") assert(c.te, "Stream parsing dropped end tag for documentation") @@ -627,12 +628,15 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L628 end def test_line - Document.new File.new(fixture_path("bad.xml")) + f = File.new(fixture_path("bad.xml")) + Document.new f assert_fail "There should have been an error" rescue Exception # We should get here assert($!.line == 5, "Should have been an error on line 5, "+ "but was reported as being on line #{$!.line}" ) + ensure + f.close if f end def test_substitution @@ -836,8 +840,7 @@ EOL https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L840 end def test_attlist_write - file=File.new(fixture_path("foo.xml")) - doc=Document.new file + doc = File.open(fixture_path("foo.xml")) {|file| Document.new file } out = '' doc.write(out) end @@ -1281,11 +1284,11 @@ EOL https://github.com/ruby/ruby/blob/trunk/test/rexml/test_core.rb#L1284 end def test_ticket_63 - Document.new(File.new(fixture_path("t63-1.xml"))) + File.open(fixture_path("t63-1.xml")) {|f| Document.new(f) } end def test_ticket_75 - d = REXML::Document.new(File.new(fixture_path("t75.xml"))) + d = File.open(fixture_path("t75.xml")) {|f| REXML::Document.new(f) } assert_equal("tree", d.root.name) end Index: test/rexml/test_contrib.rb =================================================================== --- test/rexml/test_contrib.rb (revision 46177) +++ test/rexml/test_contrib.rb (revision 46178) @@ -276,7 +276,7 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rexml/test_contrib.rb#L276 f.write( tn, Output.new(o = "", "ISO-8859-1") ) assert_equal(expected_iso, o.strip) - doc = Document.new File.new(fixture_path('xmlfile-bug.xml')) + doc = File.open(fixture_path('xmlfile-bug.xml')) {|file| Document.new file } tn = XPath.first(doc, "//nebenspalte/text()[2]") assert_equal(expected_utf, tn.to_s.strip) f.write( tn, Output.new(o = "", "ISO-8859-1") ) @@ -310,9 +310,10 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rexml/test_contrib.rb#L310 # Alun ap Rhisiart def test_less_than_in_element_content - source = File.new(fixture_path('ProductionSupport.xml')) + doc = File.open(fixture_path('ProductionSupport.xml')) do |source| + REXML::Document.new source + end h = Hash.new - doc = REXML::Document.new source doc.elements.each("//CommonError") { |el| h[el.elements['Key'].text] = 'okay' } Index: test/rexml/test_jaxen.rb =================================================================== --- test/rexml/test_jaxen.rb (revision 46177) +++ test/rexml/test_jaxen.rb (revision 46178) @@ -39,8 +39,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_jaxen.rb#L39 def test( fname ) # Dir.entries( xml_dir ).each { |fname| # if fname =~ /\.xml$/ - file = File.new(fixture_path(fname+".xml")) - doc = Document.new( file ) + doc = File.open(fixture_path(fname+".xml")) do |file| + Document.new(file) + end XPath.each( doc, "/tests/document" ) {|e| handleDocument(e)} # end # } Index: test/rexml/xpath/test_base.rb =================================================================== --- test/rexml/xpath/test_base.rb (revision 46177) +++ test/rexml/xpath/test_base.rb (revision 46178) @@ -130,7 +130,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/xpath/test_base.rb#L130 assert_equal("b", XPath::first(c, "..").name) assert_equal("a", XPath::first(@@doc, "a/b/..").name) - doc = REXML::Document.new(File.new(fixture_path("project.xml"))) + doc = File.open(fixture_path("project.xml")) do |f| + REXML::Document.new(f) + end c = each_test(doc.root, "./Description" ) { |child| assert_equal("Description",child.name) } @@ -213,11 +215,11 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/xpath/test_base.rb#L215 xmlsource = fixture_path("testsrc.xml") xpathtests = fixture_path("xp.tst") - doc = REXML::Document.new(File.new(xmlsource)) + doc = File.open(xmlsource) {|f| REXML::Document.new(f) } #results = "" results = REXML::Document.new results.add_element "test-results" - for line in File.new(xpathtests) + File.foreach(xpathtests) do |line| line.strip! begin doc.root @@ -315,7 +317,7 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/xpath/test_base.rb#L317 end def test_lang - doc = Document.new(File.new(fixture_path("lang0.xml"))) + doc = File.open(fixture_path("lang0.xml")) {|f| Document.new(f) } #puts IO.read( "test/lang.xml" ) #puts XPath.match( doc, "//language/*" ).size @@ -936,10 +938,14 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rexml/xpath/test_base.rb#L938 def test_ticket_43 #url = http://news.search.yahoo.com/news/rss?p=market&ei=UTF-8&fl=0&x=wrt - sum = Document.new(File.new(fixture_path("yahoo.xml"))).elements.to_a("//item").size + sum = File.open(fixture_path("yahoo.xml")) do |f| + Document.new(f).elements.to_a("//item").size + end assert_equal( 10, sum ) - text = Document.new(File.new(fixture_path("yahoo.xml"))).elements.to_a(%Q{//title[contains(text(), "'")]}).collect{|e| e.text}.join + text = File.open(fixture_path("yahoo.xml")) do |f| + Document.new(f).elements.to_a(%Q{//title[contains(text(), "'")]}).collect{|e| e.text}.join + end assert_equal( "Broward labor market's a solid performer (Miami Herald)", text ) end @@ -997,14 +1003,16 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/rexml/xpath/test_base.rb#L1003 end def test_ticket_61_text - file = File.open(fixture_path("ticket_61.xml")) - doc = REXML::Document.new file + doc = File.open(fixture_path("ticket_61.xml")) do |file| + REXML::Document.new file + end ticket_61_fixture( doc, "//div[text()='Add' and @class='ButtonText']" ) end def test_ticket_61_contains - file = File.open(fixture_path("ticket_61.xml")) - doc = REXML::Document.new file + doc = File.open(fixture_path("ticket_61.xml")) do |file| + REXML::Document.new file + end ticket_61_fixture( doc, "//div[contains(.,'Add') and @class='ButtonText']" ) end Index: test/rexml/test_listener.rb =================================================================== --- test/rexml/test_listener.rb (revision 46177) +++ test/rexml/test_listener.rb (revision 46178) @@ -93,10 +93,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_listener.rb#L93 a.value.force_encoding('binary') end assert_equal( "\xC3\xA9", a.value) - doc = REXML::Document.parse_stream( - File::new(fixture_path("stream_accents.xml")), - AccentListener::new - ) + doc = File::open(fixture_path("stream_accents.xml")) do |f| + REXML::Document.parse_stream(f, AccentListener::new) + end end end Index: test/rexml/test_rexml_issuezilla.rb =================================================================== --- test/rexml/test_rexml_issuezilla.rb (revision 46177) +++ test/rexml/test_rexml_issuezilla.rb (revision 46178) @@ -5,7 +5,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_rexml_issuezilla.rb#L5 class TestIssuezillaParsing < Test::Unit::TestCase include REXMLTestUtils def test_rexml - doc = REXML::Document.new(File.new(fixture_path("ofbiz-issues-full-177.xml"))) + doc = File.open(fixture_path("ofbiz-issues-full-177.xml")) do |f| + REXML::Document.new(f) + end ctr = 1 doc.root.each_element('//issue') do |issue| assert_equal( ctr, issue.elements['issue_id'].text.to_i ) Index: test/rexml/test_sax.rb =================================================================== --- test/rexml/test_sax.rb (revision 46177) +++ test/rexml/test_sax.rb (revision 46178) @@ -32,63 +32,62 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_sax.rb#L32 end def test_sax2 - f = File.new(fixture_path("documentation.xml")) - parser = Parsers::SAX2Parser.new( f ) - # Listen to all events on the following elements - count = 0 - blok = proc { |uri,localname,qname,attributes| - assert %w{ bugs todo }.include?(localname), - "Mismatched name; we got '#{qname}'\nArgs were:\n\tURI: #{uri}\n\tLOCALNAME: #{localname}\n\tQNAME: #{qname}\n\tATTRIBUTES: #{attributes.inspect}\n\tSELF=#{blok}" - count += 1 - } - - start_document = 0 - end_document = 0 - parser.listen( :start_document ) { start_document += 1 } - parser.listen( :end_document ) { end_document += 1 } - parser.listen( :start_element, %w{ changelog bugs todo }, &blok ) - # Listen to all events on the following elements. Synonymous with - # listen( :start_element, %w{ ... } ) - parser.listen( %w{ changelog bugs todo }, &blok ) - # Listen for all start element events - parser.listen( :start_element ) { |uri,localname,qname,attributes| - } - listener = MySAX2Listener.new - # Listen for all events - parser.listen( listener ) - # Listen for all events on the given elements. Does not include children - # events. Regular expressions work as well! - parser.listen( %w{ /change/ bugs todo }, listener ) - # Test the deafening method - blok = proc { |uri,localname,qname,attributes| - assert_fail "This listener should have been deafened!" - } - parser.listen( %w{ changelog }, &blok ) - parser.deafen( &blok ) - - tc = 0 - parser.listen( :characters, %w{version} ) {|text| - assert(text=~/@ANT_VERSION@/, "version was '#{text}'") - tc += 1 - } - - begin - parser.parse - rescue => exception - if exception.kind_of? Test::Unit::AssertionFailedError - raise exception + File.open(fixture_path("documentation.xml")) do |f| + parser = Parsers::SAX2Parser.new( f ) + # Listen to all events on the following elements + count = 0 + blok = proc { |uri,localname,qname,attributes| + assert %w{ bugs todo }.include?(localname), + "Mismatched name; we got '#{qname}'\nArgs were:\n\tURI: #{uri}\n\tLOCALNAME: #{localname}\n\tQNAME: #{qname}\n\tATTRIBUTES: #{attributes.inspect}\n\tSELF=#{blok}" + count += 1 + } + + start_document = 0 + end_document = 0 + parser.listen( :start_document ) { start_document += 1 } + parser.listen( :end_document ) { end_document += 1 } + parser.listen( :start_element, %w{ changelog bugs todo }, &blok ) + # Listen to all events on the following elements. Synonymous with + # listen( :start_element, %w{ ... } ) + parser.listen( %w{ changelog bugs todo }, &blok ) + # Listen for all start element events + parser.listen( :start_element ) { |uri,localname,qname,attributes| + } + listener = MySAX2Listener.new + # Listen for all events + parser.listen( listener ) + # Listen for all events on the given elements. Does not include children + # events. Regular expressions work as well! + parser.listen( %w{ /change/ bugs todo }, listener ) + # Test the deafening method + blok = proc { |uri,localname,qname,attributes| + assert_fail "This listener should have been deafened!" + } + parser.listen( %w{ changelog }, &blok ) + parser.deafen( &blok ) + + tc = 0 + parser.listen( :characters, %w{version} ) {|text| + assert(text=~/@ANT_VERSION@/, "version was '#{text}'") + tc += 1 + } + + begin + parser.parse + rescue => exception + if exception.kind_of? Test::Unit::AssertionFailedError + raise exception + end + puts $! + puts exception.backtrace end - puts $! - puts exception.backtrace + assert_equal 2, count + assert_equal 1, tc + assert_equal 1, start_document + assert_equal 1, end_document end - assert_equal 2, count - assert_equal 1, tc - assert_equal 1, start_document - assert_equal 1, end_document end - - # used by test_simple_doctype_listener # submitted by Jeff Barczewski class SimpleDoctypeListener @@ -223,21 +222,26 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_sax.rb#L222 def test_socket require 'socket' - server = TCPServer.new('127.0.0.1', 0) - socket = TCPSocket.new('127.0.0.1', server.addr[1]) - - ok = false - session = server.accept - session << '<foo>' - parser = REXML::Parsers::SAX2Parser.new(socket) - Fiber.new do - parser.listen(:start_element) do - ok = true - Fiber.yield + TCPServer.open('127.0.0.1', 0) do |server| + TCPSocket.open('127.0.0.1', server.addr[1]) do |socket| + ok = false + session = server.accept + begin + session << '<foo>' + parser = REXML::Parsers::SAX2Parser.new(socket) + Fiber.new do + parser.listen(:start_element) do + ok = true + Fiber.yield + end + parser.parse + end.resume + assert(ok) + ensure + session.close + end end - parser.parse - end.resume - assert(ok) + end end def test_char_ref_sax2() @@ -261,15 +265,17 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_sax.rb#L265 include REXML::SAX2Listener end def test_ticket_68 - parser = REXML::Parsers::SAX2Parser.new(File.new(fixture_path('ticket_68.xml'))) - parser.listen( Ticket68.new ) - begin - parser.parse - rescue - p parser.source.position - p parser.source.current_line - puts $!.backtrace.join("\n") - flunk $!.message + File.open(fixture_path('ticket_68.xml')) do |f| + parser = REXML::Parsers::SAX2Parser.new(f) + parser.listen( Ticket68.new ) + begin + parser.parse + rescue + p parser.source.position + p parser.source.current_line + puts $!.backtrace.join("\n") + flunk $!.message + end end end end Index: test/rexml/test_order.rb =================================================================== --- test/rexml/test_order.rb (revision 46177) +++ test/rexml/test_order.rb (revision 46178) @@ -47,7 +47,9 @@ END https://github.com/ruby/ruby/blob/trunk/test/rexml/test_order.rb#L47 end # Provided by Tom Talbott def test_more_ordering - doc = REXML::Document.new(Zlib::GzipReader.open(fixture_path('LostineRiver.kml.gz'), encoding: 'utf (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/