Aruba Documentation

repository·main·Indexed 21 days ago

https://github.com/cucumber/aruba

Aruba is an extension for TDD and BDD frameworks, including Cucumber, RSpec, and Minitest, designed to simplify the testing of command-line applications. It provides helpers to manipulate the file system and process environment while ensuring state is reset between tests. The library includes a CLI for project initialization and a comprehensive API for managing command execution, filesystem state, and process lifecycles, along with specialized RSpec matchers for verifying exit statuses, timeouts, and stdout/stderr output.

Tokens
4.9K
Snippets
27
Records
27
Agent score
75%

What's inside Aruba

  1. Use Aruba with RSpec

    main

    To integrate Aruba with RSpec:

    1. Require the Aruba RSpec extension in your spec/spec_helper.rb file:
      require 'aruba/rspec'
    2. Define your specs using the :type => :aruba metadata.
    3. Use helper methods like write_file, run_command, and matchers on last_command_started (e.g., have_output).
    4. Run your tests using bundle exec rspec.
    # spec/spec_helper.rb
    require 'aruba/rspec'
    
    # Example spec
    RSpec.describe 'First Run', :type => :aruba do
      let(:file) { 'file.txt' }
      let(:content) { 'Hello, Aruba!' }
    
      before { write_file file, content }
      before { run_command('aruba-test-cli file.txt') }
    
      it { expect(last_command_started).to have_output content }
    end
  2. Use Aruba with Minitest

    main

    To integrate Aruba with Minitest:

    1. Require the Aruba API in your test/test_helper.rb file:
      require 'aruba/api'
    2. Include Aruba::Api in your test class.
    3. Call aruba.setup in your setup method to initialize the environment.
    4. Use helper methods like write_file and run_command_and_stop.
    5. Run your tests using bundle exec ruby -I lib:test test/your_test.rb.

    Note: You should implement a teardown mechanism to ensure state is reset between tests.

    # test/test_helper.rb
    require 'aruba/api'
    
    # Example test
    class FirstRun < Minitest::Test
      include Aruba::Api
    
      def setup
        aruba.setup
      end
    
      def test_getting_started_with_aruba
        file = 'file.txt'
        content = 'Hello, Aruba!'
        write_file file, content
        run_command_and_stop 'aruba-test-cli file.txt'
        assert_equal last_command_started.output.chomp, content
      end
    end
  3. Use Aruba with Cucumber

    main

    To integrate Aruba with Cucumber:

    1. Require the Aruba Cucumber extension in your features/support/env.rb file:
      require 'aruba/cucumber'
    2. Use Aruba's Gherkin steps (e.g., Given a file named "...", When I run ..., Then the file "..." should contain: ...) in your .feature files.
    3. Run your tests using bundle exec cucumber.
    # features/support/env.rb
    require 'aruba/cucumber'
  4. Initialize an existing project with Aruba

    main

    Aruba provides an initializer to quickly set up your project based on your chosen testing framework. Ensure your project is under version control and all changes are committed before running the command.

    Use the aruba init command with the --test-framework flag specifying one of the supported frameworks: rspec, cucumber, or minitest.

    aruba init --test-framework rspec
    aruba init --test-framework cucumber
    aruba init --test-framework minitest
  5. Access the Aruba API

    main

    The Aruba::Api module serves as the primary entry point for interacting with Aruba. It aggregates several specialized modules to provide a unified interface for managing commands, processes, environments, filesystems, text matching, and Bundler integration. To use Aruba's features, you interact with the methods provided by this module.

    # The Aruba::Api module includes the following functional areas:
    # - Core
    # - Commands
    # - Environment
    # - Filesystem
    # - Text
    # - Bundler
  6. Configure Aruba settings using the configure method

    main

    Aruba configuration objects can be initialized and then customized using the configure method. This method yields the configuration instance to a block, allowing you to set options or define hooks.

    # Assuming 'config' is an instance of an Aruba configuration class
    config.configure do |c|
      # Set options or define hooks here
    end
    config.configure do |c|
    end
  7. Configure Aruba settings

    main

    You can configure Aruba's global settings using the Aruba.configure method. This method accepts a block where you can set various configuration options on the configuration object.

    Aruba.configure do |config|
      config.option_name = value
    end
    Aruba.configure do |config|
      config.exit_timeout = 30
    end
  8. Safely set options with set_if_option

    main

    If you are working with dynamic option names and want to avoid errors if an option does not exist, use set_if_option. This method checks if the option is defined before attempting to call its setter.

    # Only sets the value if :my_option is a valid configuration option
    config.set_if_option(:my_option, 'new_value')
    config.set_if_option(:my_option, 'new_value')
  9. Verify command execution duration with `have_finished_in_time`

    main

    Aruba provides RSpec matchers to verify if a command completed within its expected time limits without timing out.

    Use the have_finished_in_time matcher to assert that the last_command_started did not time out.

    Alternatively, you can use the negated matcher run_too_long to assert that a command exceeded its allowed execution time. For example, if a timeout is set to 10 seconds and the command takes 15 seconds, run_too_long will succeed.

    Note: The object being tested must respond to the timed_out? method.

    RSpec.describe do
      it { expect(last_command_started).to have_finished_in_time }
      it { expect(last_command_started).not_to have_finished_in_time }
      it { expect(last_command_started).to run_too_long }
      it { expect(last_command_started).not_to run_too_long }
    end
  10. Use the Aruba API

    main

    Aruba is a tool for testing command-line applications. The primary entry point for interacting with the library is through the Aruba module, which exposes its functionality via aruba/api. To use Aruba in your test suite, you should require aruba and then use the provided API methods to manage command-line execution, filesystem state, and process lifecycle.

    require 'aruba'
    
    # Aruba functionality is accessed through the Aruba module
  11. Verify stdout content with `have_output_on_stdout`

    main

    In RSpec tests, use the have_output_on_stdout matcher to verify that the last command started by Aruba produced specific output on stdout.

    This matcher checks the stdout property of the command object. If you provide an argument to the matcher, it will perform a value match against the captured stdout text. If no argument is provided, it effectively checks if the command produced any output (depending on how values_match? behaves with the default expected value).

    Note: The matcher expects the object being tested (usually the result of last_command_started) to respond to the .stdout method.

    RSpec.describe do
      it { expect(last_command_started).to have_output_on_stdout }
    end