tags:

views:

682

answers:

2

I'm creating a gem which has

  • several scripts in the bin directory
  • the utility classes in the lib directory
  • and several tests in the test directory
supertool
 bin
   toolA
   toolB
 lib
   supertool
     supertool.rb
     helper.rb
 test
   tc_main.rb
   tc_etc.rb

Now, to run the tests before I even install the gem, I have the following snippet at the top of my tests:

base = File.basename(Dir.pwd)
if base == 'test' || base =~ /supertool/
   Dir.chdir('..') if base == 'test'
   $LOAD_PATH.unshift(Dir.pwd + '/lib')
   Dir.chdir('test') if base =~ /supertool/
end

This seems tedious though, especially if I have to put these in the scripts in the bin directory too. Is there a better way of setting up the environment so we can test gems before they are installed? I'm sure it's something simple that I just can't find. A simple link to the right place would help a lot :)

A: 

Take a look at hoe gem, it is a helper for other gems.

VitalieL
+1  A: 

I'm not sure what you're trying to achieve with that script. It doesn't seem to have anything to do with gems...

Is it so that you can run ruby tc_main.rb from within the test directory (or ruby test/tc_main.rb from the base dir), and have it set the load path appropriately? If so, here's a much nicer way:

In your test directory, create a test_helper.rb file. In that file, put this

$LOAD_PATH << File.expand_path( File.dirname(__FILE__) + '/../lib' )

And in all your test files, set the first line to

require 'test_helper'

If you have subdirectories inside your test dir, then files in those subdirs can just do

require '../test_helper'
Orion Edwards