Use output helpers instead of direct regex's in tests.

We're not trying to test the format of the output, we're trying
to test the meaning of the output, so hide that state away in
another class.
This commit is contained in:
Mitchell Hashimoto 2011-11-05 14:59:17 -07:00
parent 55f2ac3f54
commit bb09b249b6
4 changed files with 54 additions and 15 deletions

View File

@ -2,8 +2,9 @@ require "rubygems"
require "contest"
require "log4r"
require File.expand_path("../helpers/isolated_environment", __FILE__)
require File.expand_path("../helpers/config.rb", __FILE__)
require File.expand_path("../helpers/isolated_environment", __FILE__)
require File.expand_path("../helpers/output.rb", __FILE__)
# Enable logging if requested
if ENV["ACCEPTANCE_LOGGING"]
@ -39,6 +40,13 @@ class AcceptanceTest < Test::Unit::TestCase
@environment.execute(*args)
end
# This is a shortcut method to instantiate an Output matcher.
#
# @return [Acceptance::Output]
def output(text)
Acceptance::Output.new(text)
end
setup do
# Setup the environment so that we have an isolated area
# to run Vagrant. We do some configuration here as well in order

View File

@ -9,7 +9,8 @@ class BoxTest < AcceptanceTest
should "have no boxes by default" do
result = execute("vagrant", "box", "list")
assert result.stdout =~ /There are no installed boxes!/
assert(output(result.stdout).no_boxes,
"output should say there are no installed boxes")
end
should "add a box from a file" do
@ -21,14 +22,15 @@ class BoxTest < AcceptanceTest
# Verify that the box now shows up in the list of available boxes
results = execute("vagrant", "box", "list")
assert(results.stdout =~ /^foo$/, "Box should exist after it is added")
assert(output(results.stdout).box_installed("foo"),
"foo box should exist after it is added")
end
should "give an error if the file doesn't exist" do
results = execute("vagrant", "box", "add", "foo", "/tmp/nope/nope/nope/nonono.box")
assert(!results.success?, "Box add should fail.")
assert(results.stdout =~ /^The specified path to a file doesn't exist.$/,
"This should show an error message about the file not existing.")
assert(output(results.stdout).box_path_doesnt_exist,
"Should show an error message about the file not existing.")
end
should "give an error if the file is not a valid box" do
@ -39,8 +41,8 @@ class BoxTest < AcceptanceTest
results = execute("vagrant", "box", "add", "foo", invalid.to_s)
assert(!results.success?, "Box add should fail.")
assert(results.stdout =~ /^The box file you're attempting to add is invalid./,
"should show an error message")
assert(output(results.stdout).box_invalid,
"should say the box is invalid")
end
should "add a box from an HTTP server" do
@ -57,8 +59,8 @@ class BoxTest < AcceptanceTest
execute("vagrant", "box", "remove", "foo")
results = execute("vagrant", "box", "list")
assert(results.success?, "box list should succeed")
assert(results.stdout =~ /^There are no installed boxes!/,
"box list should be empty")
assert(output(results.stdout).no_boxes,
"No boxes should be installed")
end
should "repackage a box" do

View File

@ -0,0 +1,29 @@
module Acceptance
# This class helps with matching against output so that every
# test isn't inherently tied to the output format of Vagrant.
class Output
def initialize(text)
@text = text
end
def box_invalid
@text =~ /^The box file you're attempting to add is invalid./
end
def box_path_doesnt_exist
@text =~ /^The specified path to a file doesn't exist.$/
end
def box_installed(name)
@text =~ /^foo$/
end
def no_boxes
@text =~ /There are no installed boxes!/
end
def is_version?(version)
@text =~ /^Vagrant version #{version}$/
end
end
end

View File

@ -3,19 +3,19 @@ require File.expand_path("../base", __FILE__)
class VersionTest < AcceptanceTest
should "print the version to stdout" do
result = execute("vagrant", "version")
assert(result.stdout =~ /^Vagrant version #{config.vagrant_version}$/,
"output should contain Vagrant version")
assert(output(result.stdout).is_version?(config.vagrant_version),
"output should be version")
end
should "print the version with '-v'" do
result = execute("vagrant", "-v")
assert(result.stdout =~ /^Vagrant version #{config.vagrant_version}$/,
"output should contain Vagrant version")
assert(output(result.stdout).is_version?(config.vagrant_version),
"output should be version")
end
should "print the version with '--version'" do
result = execute("vagrant", "--version")
assert(result.stdout =~ /^Vagrant version #{config.vagrant_version}$/,
"output should contain Vagrant version")
assert(output(result.stdout).is_version?(config.vagrant_version),
"output should be version")
end
end