Skip to content

Instantly share code, notes, and snippets.

View flavorjones's full-sized avatar

Mike Dalessio flavorjones

View GitHub Profile
#! /usr/bin/ruby
require 'rubygems'
require 'nokogiri'
HTML = '<b>http://foo.com/</b><i>http://bar.com</i><b>no match here</b>'
doc = Nokogiri::HTML(HTML)
puts doc.xpath("//b[contains(text(),'http')]") # => <b>http://foo.com/</b>

Parallelize Your RSpec Suite

We all have multi-core machine these days, but most rspec suites still run in one sequential stream. Let's parallelize it!

The big hurdle here is managing multiple test databases. When multiple specs are running simultaneously, they each need to have exclusive access to the database, so that one spec's setup doesn't clobber the records of another spec's setup. We could create and manage multiple test database within our RDBMS. But I'd prefer something a little more ... ephemeral, that won't hang around after we're done, or require any manual management.

Enter SQLite's in-memory database, which is a full SQLite instance, created entirely within the invoking process's own memory footprint.

(Note #1: the gist for this blog is at http://gist.github.com/108780)

#! /usr/bin/ruby
# reproducing bug reported in http://github.com/tenderlove/nokogiri/issues/#issue/35
require 'rubygems'
require 'nokogiri'
iframe = %Q{<iframe style="width: 0%; height: 0px" src="http://someurl" frameborder=0 scrolling=no allowtransparency></iframe>}
doc = Nokogiri::HTML "<html><body><p></p></body></html>"
$ sloccount libxml2-2.7.3
Categorizing files.
Finding a working MD5 command....
Found a working MD5 command.
Computing results.
SLOC Directory SLOC-by-Language (Sorted)
166816 top_dir ansic=155006,sh=9090,python=2653,perl=67
35079 python ansic=20154,python=14925
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>foo</p>
</body>
</html>
<?xml version="1.0"?>
<root>
<inventory xmlns="http://alicesautosupply.example.com/">
<tire name="super slick racing tire" />
<tire name="all weather tire" />
</inventory>
<inventory xmlns="http://bobsbikes.com">
<tire name="narrow street tire" />
<tire name="mountain trail tire" />
</inventory>
require 'nokogiri'
#
# must set the document root node. one line change to original:
#
n = Nokogiri::XML::Node.new('root', Nokogiri::XML::Document.new)
n.document.root = n
n << Nokogiri::XML::Node.new('child', n.document)
n << (c = Nokogiri::XML::Node.new('child', n.document))
c.default_namespace = 'foo:bar'
#! /usr/bin/ruby
#
# I've got a string containing newlines. I've also got a regex that I
# want to match at the start of the string, but NOT at the start of a
# line.
#
# Contrived example:
# - strings: "first string\nshould match", "second string should not match\nfirst string"
# - regex: /^first string/
$ rake test
(in /home/mike/code/nokogiri)
rake-compiler must be configured first to enable cross-compilation
cp tmp/i486-linux/nokogiri/nokogiri.so lib/nokogiri/nokogiri.so
/home/mike/code/nokogiri/test/helper.rb:11: libxml version info: {"nokogiri"=>"1.2.4", "warnings"=>[], "libxml"=>{"compiled"=>"2.6.31", "loaded"=>"2.6.31", "binding"=>"extension"}}
Loaded suite -e
Started
......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
# exception from johnson now, using JS_ReportError
test_jquery(JQuery::TestJQuery):
Johnson::Error: #<NoMethodError: undefined method `insertBefore' for #<Method: Nokogiri::HTML::Document#documentElement>> at (/home/mike/code/taka/vendor/jquery/jquery/dist/jquery.js):1384
__noSuchMethod__("insertBefore",[object Array])@:0
()@/home/mike/code/taka/vendor/jquery/jquery/dist/jquery.js:1384
()@/home/mike/code/taka/vendor/jquery/jquery/dist/jquery.js:1376
([object JSLandProxy])@/home/mike/code/taka/vendor/jquery/jquery/dist/jquery.js:646
@/home/mike/code/taka/vendor/jquery/jquery/dist/jquery.js:4669
../johnson/lib/johnson/spidermonkey/js_land_proxy.rb:6:in `evaluate_compiled_script'