I have a command-line application which uses thor to handle the parsing of options. I want to unit test the command-line functionality against the code with test-unit and/or minitest.
I can't seem to figure out how to make sure the ARGV array (which normally would hold the options from the command line) holds my test options so they can be tested against the code.
The specific application code:
# myapp/commands/build.rb
require 'thor'
module Myapp
module Commands
# Define build commands for MyApp command line
class Build < Thor::Group
include Thor::Actions
build = ARGV.shift
build = aliases[build] || build
# Define arguments and options
argument :type
class_option :test_framework, :default => :test_unit
# Define source root of application
def self.source_root
File.dirname(__FILE__)
end
case build
# 'build html' generates a html
when 'html'
# The resulting html
puts "<p>HTML</p>"
end
end
end
end
The executable
# bin/myapp
The Test File
# tests/test_build_html.rb
require 'test/unit'
require 'myapp/commands/build'
class TestBuildHtml < Test::Unit::TestCase
include Myapp::Commands
# HERE'S WHAT I'D LIKE TO DO
def test_html_is_built
# THIS SHOULD SIMULATE 'myapp build html' FROM THE COMMAND-LINE
result = MyApp::Commands::Build.run(ARGV << 'html')
assert_equal result, "<p>HTML</p>"
end
end
I have been able to pass an array into ARGV in the test class, but once I call Myapp/Commands/Build the ARGV appears to be empty. I need to make sure the ARGV array is holding 'build' and 'html' in order for the Build command to work and this to pass.