by Nicolas Luck

Wednesday, January 14, 2009

libxml-ruby on windows

At HFT we are working on a (RESTful) webservice extension to our CBRSuite. To test this webservice I chose to use RSpec and Cucumber, which allows to write runnable specifications like

Feature: getting a list of provided graphs
 I want to get a list of provided graphs
 So that I am able to create a simulation
 by referencing a graph to use

Scenario: standard
 When getting '/graphs'
 Then the response is a valid XML document
 And the response has a root element called 'graphs'
 And the response has a root element with at least one child
 And the response's nodes 'graph/graphs/*' all have text

given these steps are implemented in Ruby like

When /^getting '(.*)'$/ do |resource|
 @response = Net::HTTP.get host, resource, 80
end

Then /^the response has a root element called '(.*)'$/ do |rootname|
 doc = REXML::Document.new(@response)
 doc.root.name.should == rootname
end
...

See RSpec and Cucumber for more information on this cool way of writing specifications.

Our webservice responds by sending XML back to the client. These XML fragments are specified by XML Schema Definitions. To check if the genereted XML is valid according to the XSDs I don’t want to check every tag manualy (as in the example above). I want to write something like

Scenario: standard
 When getting '/graphs'
 Then the response is a valid XML document
 And the response is a valid 'http://cbrsuite/graph.xsd' XSD

So I need to check a XML document against a given XSD in Ruby. REXML does not (yet?) support XML validation but libxml-ruby does. But if you are bound to Windows for some reason you may encouter problems when trying to use libxml-ruby (as I did). I had Ruby and Ruby Gems installed via the one-click-installer. All further Ruby packages I had installed via gems. When using libxml-ruby I got

c:/ruby/lib/ruby/gems/1.8/gems/libxml-ruby-0.9.7-x86-mswin32-60/lib/libxml_ruby.so:
127: Die angegebene Prozedur wurde nicht gefunden.
- c:/ruby/lib/ruby/gems/1.8/gems/libxml-ruby-0.9.7-x86-mswin32-60/lib/libxml_ruby.so (LoadError)
Failed to load steps.rb from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
        from c:/ruby/lib/ruby/gems/1.8/gems/libxml-ruby-0.9.7-x86-mswin32-60/lib/libxml.rb:12
        from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from c:/ruby/lib/ruby/gems/1.8/gems/libxml-ruby-0.9.7-x86-mswin32-60/lib/xml.rb:11
        from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
        from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
        from ./steps.rb:4
         ... 6 levels...
        from c:/ruby/lib/ruby/gems/1.8/gems/cucumber-0.1.15/bin/../lib/cucumber/cli.rb:14:in `execute'
        from c:/ruby/lib/ruby/gems/1.8/gems/cucumber-0.1.15/bin/cucumber:6
        from c:/ruby/bin/cucumber:16:in `load'
        from c:/ruby/bin/cucumber:16

In order to fix this I tried installing libiconv manually and reinstalling libxml-ruby afterwards (manually and via gems), but to no avail..

To be able to use libxml-ruby under Windows I had to use cygwin. I installed Ruby, libiconv, libxml and the gcc via the cygwin setup. Then I downloaded Ruby Gems and installed it manually in cygwin. Installing libxml-ruby via the cygwin-gems would not work:

$ gem install libxml-ruby
Building native extensions.  This could take a while...
ERROR:  Error installing libxml-ruby:
 ERROR: Failed to build gem native extension./usr/bin/ruby.exe extconf.rb install libxml-ruby
checking for socket() in -lsocket... no
checking for gethostbyname() in -lnsl... no
checking for atan() in -lm... no
checking for atan() in -lm... yes
checking for inflate() in -lz... yes
checking for iconv_open() in -liconv... no
checking for libiconv_open() in -liconv... yes
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no
checking for xmlParseDoc() in -lxml2... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...

So I downloaded libxml2 and compiled and installed it under cygwin myself. After this gems was able to install libxml-ruby

$ gem install libxml-ruby
Building native extensions.  This could take a while...
Successfully installed libxml-ruby-0.9.7
1 gem installed
Installing ri documentation for libxml-ruby-0.9.7...

Enclosing class/module 'cXMLParser' for class Context not known

Enclosing class/module 'mXPath' for class Context not known

Enclosing class/module 'mXPath' for class Expression not known

Enclosing class/module 'mXPath' for class Object not known
Installing RDoc documentation for libxml-ruby-0.9.7...

Enclosing class/module 'cXMLParser' for class Context not known

Enclosing class/module 'mXPath' for class Context not known

Enclosing class/module 'mXPath' for class Expression not known

Enclosing class/module 'mXPath' for class Object not known

and everything worked fine!

posted by lucksus at 3:44 pm  

2 Comments »

  1. This is a quite valuable article. Do you mind if i translate it into french for my own visitors?

    Comment by roll off dumpster rentals — December 7, 2010 @ 5:55 pm

  2. Thanks. No, I would not mind. Please go ahead!

    Comment by lucksus — February 21, 2011 @ 12:06 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress