From 3356c312cddcc95af59e648c4ad946ec969d9813 Mon Sep 17 00:00:00 2001 From: akr Date: Tue, 27 May 2014 13:45:04 +0000 Subject: * test/rexml: Avoid fd leaks. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/rexml/test_contrib.rb | 7 +- test/rexml/test_core.rb | 33 ++++---- test/rexml/test_encoding.rb | 4 +- test/rexml/test_jaxen.rb | 5 +- test/rexml/test_lightparser.rb | 7 +- test/rexml/test_listener.rb | 7 +- test/rexml/test_order.rb | 4 +- test/rexml/test_rexml_issuezilla.rb | 4 +- test/rexml/test_sax.rb | 152 +++++++++++++++++++----------------- test/rexml/xpath/test_base.rb | 28 ++++--- 10 files changed, 138 insertions(+), 113 deletions(-) (limited to 'test/rexml') diff --git a/test/rexml/test_contrib.rb b/test/rexml/test_contrib.rb index 917502fca6..fa84ebef3a 100644 --- a/test/rexml/test_contrib.rb +++ b/test/rexml/test_contrib.rb @@ -276,7 +276,7 @@ EOF 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 # 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' } diff --git a/test/rexml/test_core.rb b/test/rexml/test_core.rb index f628cb9519..41dcec5d34 100644 --- a/test/rexml/test_core.rb +++ b/test/rexml/test_core.rb @@ -227,8 +227,9 @@ module REXMLTests 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 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 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 # 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 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 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 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 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 diff --git a/test/rexml/test_encoding.rb b/test/rexml/test_encoding.rb index 8205d0f366..3b2a56ac5e 100644 --- a/test/rexml/test_encoding.rb +++ b/test/rexml/test_encoding.rb @@ -88,7 +88,9 @@ module REXMLTests 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 diff --git a/test/rexml/test_jaxen.rb b/test/rexml/test_jaxen.rb index ab66139cb0..1eec73f3e0 100644 --- a/test/rexml/test_jaxen.rb +++ b/test/rexml/test_jaxen.rb @@ -39,8 +39,9 @@ module REXMLTests 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 # } diff --git a/test/rexml/test_lightparser.rb b/test/rexml/test_lightparser.rb index 4aa465b9fa..f8212dcd2e 100644 --- a/test/rexml/test_lightparser.rb +++ b/test/rexml/test_lightparser.rb @@ -6,9 +6,10 @@ module REXMLTests 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 diff --git a/test/rexml/test_listener.rb b/test/rexml/test_listener.rb index ea84e0edf5..309e827880 100644 --- a/test/rexml/test_listener.rb +++ b/test/rexml/test_listener.rb @@ -93,10 +93,9 @@ module REXMLTests 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 diff --git a/test/rexml/test_order.rb b/test/rexml/test_order.rb index 628e75df62..0e84961b37 100644 --- a/test/rexml/test_order.rb +++ b/test/rexml/test_order.rb @@ -47,7 +47,9 @@ END end # Provided by Tom Talbott def test_more_ordering - doc = REXML::Document.new(Zlib::GzipReader.open(fixture_path('LostineRiver.kml.gz'), encoding: 'utf-8')) + doc = Zlib::GzipReader.open(fixture_path('LostineRiver.kml.gz'), encoding: 'utf-8') do |f| + REXML::Document.new(f) + end actual = [ "Head south from Phinney Ave N", "Turn left at N 36th St", diff --git a/test/rexml/test_rexml_issuezilla.rb b/test/rexml/test_rexml_issuezilla.rb index 8098646666..bb634663d5 100644 --- a/test/rexml/test_rexml_issuezilla.rb +++ b/test/rexml/test_rexml_issuezilla.rb @@ -5,7 +5,9 @@ module REXMLTests 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 ) diff --git a/test/rexml/test_sax.rb b/test/rexml/test_sax.rb index f7e66951cb..2d37ec9dfb 100644 --- a/test/rexml/test_sax.rb +++ b/test/rexml/test_sax.rb @@ -32,63 +32,62 @@ module REXMLTests 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 - } + 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 ) + 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 - } + 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 + 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 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 << '' - 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 << '' + 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 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 diff --git a/test/rexml/xpath/test_base.rb b/test/rexml/xpath/test_base.rb index 8ef5e4aff0..a57ba632c4 100644 --- a/test/rexml/xpath/test_base.rb +++ b/test/rexml/xpath/test_base.rb @@ -130,7 +130,9 @@ module REXMLTests 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 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 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 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 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 -- cgit v1.2.3