Driving rspec from a Ruby script

I have very little experience with Ruby. I am using RSpec to test a
cross platform C++ library. I am using a shell script (and batch file)
to run the tests with several different compilers.

I do no want to put the details of the different compilers in the RSpec
files, but am thinking about rewriting to driver scripts in Ruby. Can
someone with more Ruby experience give me some guidance as to how best
do this.

Thank you,

-EdK

On 16 Jan 2009, at 17:44, Ed Keith wrote:

I have very little experience with Ruby. I am using RSpec to test a
cross platform C++ library. I am using a shell script (and batch file)
to run the tests with several different compilers.

I do no want to put the details of the different compilers in the
RSpec
files, but am thinking about rewriting to driver scripts in Ruby. Can
someone with more Ruby experience give me some guidance as to how best
do this.

You don’t provide enough information for me to be sure, but what you
describe sounds sufficiently high-level enough to make Cucumber[1]
worth looking into.

If you search the archives of this list I posted a very bad example of
using Cucumber to spec binaries written in other languages (I used
sort, I think).

Ashley

[1] http://wiki.github.com/aslakhellesoy/cucumber


http://www.patchspace.co.uk/

http://twitter.com/ashleymoran

Ashley M. wrote:

On 16 Jan 2009, at 17:44, Ed Keith wrote:

You don’t provide enough information for me to be sure, but what you
describe sounds sufficiently high-level enough to make Cucumber[1]
worth looking into.

If you search the archives of this list I posted a very bad example of
using Cucumber to spec binaries written in other languages (I used
sort, I think).

Ashley

[1] http://wiki.github.com/aslakhellesoy/cucumber


http://www.patchspace.co.uk/
http://aviewfromafar.net/
http://twitter.com/ashleymoran

I looked at Cucumber, I’m not clear on what it does, but I do not think
it is what I need.

Going into the details: I am testing a C++ library. There are many test
classes to test different aspects of the library. Some of the test cases
as supposed to fail to compile when the library is misused. At first I
used ruby to run the compilers and check the return value against 0 for
success. For the tested that were supposed to fail to compile, that was
all I needed. For the tests that were supposed to work I then ran the
generated executable and tested the return value. So far everything is
great.

The library is supposed to be portable. So I am testing it with several
different compilers on several different operating systems. I do not
want to put platform specific information into the RSpec files, so I
wrote shell scripts of Unix and batch files on windows to set up the
environment for each compiler and then call RSpec with each
configuration environment. Now I have two problems: 1) Batch language is
real pain to do anything nontrivial in; 2) I have two sets of driver
scripts to maintain.

I should be able to solve both problems by using a portable scripting
language for the driver scripts. My first thought was to use Python,
since I know it. This will work, but there are two things that make me
think it is suboptimal: 1) It will require that Python be installed on
all test systems; 2) It will require that the python interpreter invoke
the Ruby interpreter, this is very inefficient, not to mention ugly.

I can easily solve the first problem by writing the driver scrips in
Ruby since Ruby must be installed to run RSpec, and I intended to learn
Ruby eventually anyway. My first, naive, attempt works, but I am running
the ruby interpreter inside the ruby interpreter recursively when I call
RSpec (“system spec spec1.rb”). There must be a better way to invoke
RSpec without recursively invoking the Ruby interpreter.

Rake has been on my list of tools to learn for a while not. This might
be the time.

Thank you.

-EdK

On Sat, Jan 17, 2009 at 8:48 AM, Ed Keith [email protected] wrote:

I looked at Cucumber, I’m not clear on what it does, but I do not think

language for the driver scripts. My first thought was to use Python,
RSpec without recursively invoking the Ruby interpreter.
You probably just need the spec command:
http://rspec.info/documentation/tools/spec.html

By default, it will load up any files in a directory (or its subs)
named xxx_spec.rb. So in this case, if you change spec1.rb to
xyz_spec.rb and stick it in an examples directory and type:

spec examples

There are numerous command line options (described in the doc ref’d
above) to tweak the run and its output.

If those don’t give you the flexibility you want, you can use the
SpecTask in a rake file:
http://rspec.info/documentation/tools/rake.html, which gives you a
place to write any arbitrary Ruby code you need to set up your
environment and then configure rspec to run in a variety of different
ways.

HTH,
David

Pat M. wrote:

On Sat, Jan 17, 2009 at 6:48 AM, Ed Keith [email protected] wrote:

What about…Ruby?

I think it would make a lot of sense to define a couple hashes/objects
that represent each compiler. If you’re just using different strings,
you can use a hash.

gcc = {:name => ‘gcc’, :command => ‘gcc’, :flags => ‘…’}
pcc = {:name => ‘pcc’, :command => ‘pcc’, :flags => ‘…’}
watcom = {:name => ‘watcom’, :command => ‘wcm’, :flags => ‘…’}

[gcc, pcc, watcom].each do |compiler|
describe “#{compiler[:name] compiler” do
it “should build the binary” do
Dir.chdir(project_dir) { exec “make
COMPILER=#{compiler[:command]} FLAGS='#{compiler[:flags]}” }
build_binary.exit_code.should == 0
end
end

You probably wouldn’t put the compiler definitions right in there, but
you could if you wanted to. But putting them in another file is easy
and good.

If you need more complex setup, create helper classes.

class GccCompiler
def setup
# create some files…
end

def name; “gcc” end
def command; “gcc” end
end

Same thing then, you create a new instance of each of these classes,
iterate through, call their setup method, etc.

Pat

This is close to what I was thinking of, but I am doing the compilation
inside RSpec. My specifications look like this:

it 'fail to compile if foo is assigned to int' do
   x = system ENV[COMPILER] test_int_assign_fail.cpp
   x.should_not == 0
end

it 'allow use of foo as bool' do
   x = system ENV[COMPILER] test_use_as_bool.cpp
   x.should == 0
   x = system test_use_as_bool
   x.should == 0
end

My shell script or batch file sets the environment variables needed to
compile.

My problem is that I do not like the following line in Ruby :

system spec spec1.rb

it looks very inefficient, so I am looking for a better solution.

-EdK

On Sat, Jan 17, 2009 at 6:48 AM, Ed Keith [email protected] wrote:

I looked at Cucumber, I’m not clear on what it does, but I do not think

language for the driver scripts. My first thought was to use Python
What about…Ruby?

I think it would make a lot of sense to define a couple hashes/objects
that represent each compiler. If you’re just using different strings,
you can use a hash.

gcc = {:name => ‘gcc’, :command => ‘gcc’, :flags => ‘…’}
pcc = {:name => ‘pcc’, :command => ‘pcc’, :flags => ‘…’}
watcom = {:name => ‘watcom’, :command => ‘wcm’, :flags => ‘…’}

[gcc, pcc, watcom].each do |compiler|
describe “#{compiler[:name] compiler” do
it “should build the binary” do
Dir.chdir(project_dir) { exec “make
COMPILER=#{compiler[:command]} FLAGS='#{compiler[:flags]}” }
build_binary.exit_code.should == 0
end
end

You probably wouldn’t put the compiler definitions right in there, but
you could if you wanted to. But putting them in another file is easy
and good.

If you need more complex setup, create helper classes.

class GccCompiler
def setup
# create some files…
end

def name; “gcc” end
def command; “gcc” end
end

Same thing then, you create a new instance of each of these classes,
iterate through, call their setup method, etc.

Pat

On 18 Jan 2009, at 13:29, Ed Keith wrote:

My problem is that I do not like the following line in Ruby :

system spec spec1.rb

it looks very inefficient, so I am looking for a better solution.

I think you might be able to just do this instead:

 load 'spec1.rb'

Does that look any better?

Failing that, have a look at the source code for the ‘spec’ command -
it’s all Ruby - there will be something in there you can plagiarise,
I’m sure.

Matt W.
http://blog.mattwynne.net