Archive for November, 2007

autotest-ing your Rails Plugin

autotest is a great tool to easily test your Rails application. autotest runs in the background and continuously test your app, and notify you of the results, thus leaving you to build your app with the confidence of knowing that it isn’t going to break without your knowledge, and as soon as possible. It makes writing your tests easier, and the easier it is, the more likely you’ll end up doing it.

Note: I have only used autotest with RSpec, and all details are based on that. I also assume that your plugin is installed in vendor/plugins

I have been working on a plugin that uses RSpec to help me test the plugin’s integrity. After a while I got a little tired of continuously running a rake task to test it out.

Sadly by default, autotest doesn’t test your plugin directory. What a shame, but it also provides a challenge!

Enabling autotest on your Rails plugin

Now go into your plugin directory and create a folder called autotest.
cd vendor/plugins/secret_sauce/
mkdir autotest

Inside the autotest directory, create a file called discover.rb and dump this little gem inside:

While your in the root directory of your plugin, in my case its secret_sauce, just run autotest.
autotest

Boom! You are now autotest-ing your Rails plugin! Sweet.

Using your Application’s RSpec Options

You’ll notice that your autotests lack a bit of color… Or perhaps you want it to run the same options as your application. Have no worry soldier! First go to your spec directory in your plugin, and create a symbolic link back to the original spec.opts file.

Using your application’s RSpec options:
cd vendor/plugins/secret_sauce/spec
ln -s ../../../../spec/spec.opts

Note: This only works for Unix-like operating systems, thats Mac OS X, Linux, and FreeBSD to name a few. For you Windows folks, you will have to just create a spec.opts file, or change your OS.

If you want to run under another set of options, just create a spec.opts file in the spec directory of your plugin, and fill in the details.

One shortcoming

Sure one shortcoming is that it’s not integrated when calling autotest in your application root directory, but something is always better than nothing.

Comments

Creating File Paths the Easy Way

The Traditional Way

The traditional way to create file paths that are cross platform in Ruby, was to join them up as arguments in a File.join call:

Result:
"./../../config/database.yml"

Now thats all nice and everything but if you want to change it, you would have the inconvenience of having to add or remove commas, or single|double quotes, which can be a bit of a hassle.

Prettying it Up

So how about we pretty it up a little?

Result:
"./../../config/database.yml"

Now doesn’t that look a whole better?

What in the world is %w[...]?

Why does this work just as well?

The %w[...] creates an array of Strings separated by the whitespaces in the list.

Result:
["..", "..", "config", "database.yml"]

But what if you have a space in the file name? Well you can use a backslash as an escape key and presto!

Result:
["..", "..", "config", "a file"]

Note: I’m not so sure what this operator is called, and any help giving it a name would be greatly appreciated.

Comments (3)

Next entries »