devroom.io/content/posts/2014-04-15-testing-home-with-cucumber-and-aruba.md

61 lines
2.0 KiB
Markdown
Raw Normal View History

2015-03-26 11:28:08 +00:00
+++
date = "2014-04-15"
title = "Testing $HOME with Cucumber and Aruba"
tags = ["programming", "cucumber", "aruba"]
description = "Cucumber and Aruba make it awesome to write acceptance tests for your command line programs. But how do you test your program interacting withfiles from a user's <code>$HOME</code> directory?"
slug = "testing-home-with-cucumber-and-aruba"
+++
[Cucumber][1] and [Aruba][2] are awesome tools to write acceptance tests for your command line application. The allow you to do things like this:
2017-03-20 15:35:19 +00:00
``` cucumber
Scenario: Exit with 0 when no examples are run
Given a file named "a_no_examples_spec.rb" with:
2015-03-26 11:28:08 +00:00
"""ruby
"""
2017-03-20 15:35:19 +00:00
When I run `rspec a_no_examples_spec.rb`
Then the exit status should be 0
And the output should contain "0 examples"
```
2015-03-26 11:28:08 +00:00
2017-03-20 15:35:19 +00:00
_This example was taken from rspec-core._
2015-03-26 11:28:08 +00:00
Aruba basically does three things for you:
* Create and inspect files and directories
* Run commands
* Inspect output and exit status
## The problem
Now, if you are writing a CLI that interacts with a configuration file in the user's home directory, you'd write a cucumber like this:
2017-03-20 15:35:19 +00:00
``` cucumber
Scenario: use .my-app configuration
Given a file named "~/.my-app" with:
2015-03-26 11:28:08 +00:00
"""
awesome_enabled: true
"""
2017-03-20 15:35:19 +00:00
When I run `my-app`
Then the output should contain "AWESOME"
```
2015-03-26 11:28:08 +00:00
But how does your app and Aruba differentiate between this generated test file and your actual configration file in your home directory? It doens't.
## The answer
The solution is quite easy and elegant, in your `Before` set the `$HOME` environment variable to a custom location inside the `tmp/aruba` directory.
In `features/support/env.rb`:
2017-03-20 15:35:19 +00:00
``` ruby
Before do
set_env 'HOME', File.expand_path(File.join(current_dir, 'home'))
FileUtils.mkdir_p ENV['HOME']
end
```
2015-03-26 11:28:08 +00:00
This will set `$HOME` to `tmp/aruba/home` in the context of your cucumbers (and execute binary). `current_dir` automatically points to the right location for the aruba temporary directory.
[1]: https://github.com/cucumber/cucumber
[2]: https://github.com/cucumber/aruba