From bb09b249b69289f20d0c66dedeb66e3e6b4be817 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 5 Nov 2011 14:59:17 -0700 Subject: [PATCH] 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. --- test/acceptance/base.rb | 10 +++++++++- test/acceptance/box_test.rb | 18 ++++++++++-------- test/acceptance/helpers/output.rb | 29 +++++++++++++++++++++++++++++ test/acceptance/version_test.rb | 12 ++++++------ 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 test/acceptance/helpers/output.rb diff --git a/test/acceptance/base.rb b/test/acceptance/base.rb index 68e3dfe2b..23f704258 100644 --- a/test/acceptance/base.rb +++ b/test/acceptance/base.rb @@ -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 diff --git a/test/acceptance/box_test.rb b/test/acceptance/box_test.rb index 27fd30dec..41bac64c4 100644 --- a/test/acceptance/box_test.rb +++ b/test/acceptance/box_test.rb @@ -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 diff --git a/test/acceptance/helpers/output.rb b/test/acceptance/helpers/output.rb new file mode 100644 index 000000000..426331fbb --- /dev/null +++ b/test/acceptance/helpers/output.rb @@ -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 diff --git a/test/acceptance/version_test.rb b/test/acceptance/version_test.rb index 02717fc40..5c03bf319 100644 --- a/test/acceptance/version_test.rb +++ b/test/acceptance/version_test.rb @@ -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