Skip to main content

Testing $HOME with Cucumber and Aruba

·224 words·2 mins· ·
Programming Cucumber Aruba
Ariejan de Vroom
Author
Ariejan de Vroom
Jack of all Trades, Professional Software Craftsman
Table of Contents

Cucumber and Aruba are awesome tools to write acceptance tests for your command line application. The allow you to do things like this:

Scenario: Exit with 0 when no examples are run
    Given a file named "a_no_examples_spec.rb" with:
        """ruby
        """
    When I run `rspec a_no_examples_spec.rb`
    Then the exit status should be 0
    And the output should contain "0 examples"

This example was taken from rspec-core.

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:

Scenario: use .my-app configuration
    Given a file named "~/.my-app" with:
        """
        awesome_enabled: true
        """
    When I run `my-app`
    Then the output should contain "AWESOME"

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:

Before do
  set_env 'HOME', File.expand_path(File.join(current_dir, 'home'))
  FileUtils.mkdir_p ENV['HOME']
end

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.

Related

Dealing With Technical Debt
·1111 words·6 mins
Programming Craftsmanship
Fast specs - Run your specs in less than 1 second
·1208 words·6 mins
Ruby Rails Rspec Testing Bdd Tdd Cucumber Fast_spec Fastspec Coreyhaines