Can a Ruby test get the location of the folder where Rake executed it from?

215 views Asked by At

Can a Ruby test get the location of the folder where Rake executed it from? I want to run Test::Unit unit tests using Rake but my defined "test suites" in Rake need to be able to find locations of libraries relative to the root of my project.

With Maven, I can set a system property like so :

<properties>
    <main.basedir>${project.basedir}</main.basedir>
</properties>

And then Java can reference it like so:

String baseDir = System.getProperty("main.basedir");

Can Ruby do something similar? If so, how? Do I need to use a Rake namespace-require + include ? Not brewing my own framework: just trying to do the most basic test setup. I do have some lib files I created that my tests want to use.

This doesn't work because it hard codes the base dir into the class file:

base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
1

There are 1 answers

2
Mircea On

You can probably do this by injecting it in a test helper and getting the base location relative to the location of the file the test helper is is, but I would like to ask you what are you actually trying to achive?

normally the tests have access to the lib path and you should be able to just require what you want to use in the tests directly. are you using something like rspec or test::unit? are you brewing your own test framework?

so overall the answer is yes you can do it, but you should not have to do it. can show you how once you clarify what test framework you're using.

Edit

For test unit, this describes almost what you want to do: https://github.com/test-unit/test-unit/blob/master/doc/text/how-to.md

The run_test.rb helper is placed in test. You see how the base dir is built by using the path of file (as mentioned above) and how the lib dir is placed on the load path.