Create the output matcher, switch to RSpec style matchers

This commit is contained in:
Mitchell Hashimoto 2011-11-08 23:03:15 -08:00
parent f3e314bcb7
commit 4443a323e5
9 changed files with 61 additions and 63 deletions

View File

@ -3,9 +3,15 @@ require "rspec/autorun"
require "log4r" require "log4r"
require File.expand_path("../support/base_context", __FILE__) # Add this directory to the load path, since it just makes
require File.expand_path("../support/config", __FILE__) # everything else so much easier.
require File.expand_path("../support/virtualbox", __FILE__) $:.unshift File.expand_path("../", __FILE__)
# Load in the supporting files for our tests
require "support/base_context"
require "support/config"
require "support/virtualbox"
require "support/matchers/match_output"
# If VirtualBox is currently running, fail. # If VirtualBox is currently running, fail.
if Acceptance::VirtualBox.find_vboxsvc if Acceptance::VirtualBox.find_vboxsvc
@ -34,5 +40,5 @@ $acceptance_options = Acceptance::Config.new(ENV["ACCEPTANCE_CONFIG"])
# Configure RSpec # Configure RSpec
RSpec.configure do |c| RSpec.configure do |c|
c.expect_with :stdlib c.expect_with :rspec, :stdlib
end end

View File

@ -11,28 +11,25 @@ describe "vagrant box" do
it "has no boxes by default" do it "has no boxes by default" do
result = execute("vagrant", "box", "list") result = execute("vagrant", "box", "list")
assert(output(result.stdout).no_boxes, result.stdout.should match_output(:no_boxes)
"output should say there are no installed boxes")
end end
it "can add a box from a file" do it "can add a box from a file" do
require_box("default") require_box("default")
# Add the box, which we expect to succeed # Add the box, which we expect to succeed
results = execute("vagrant", "box", "add", "foo", config.boxes["default"]) result = execute("vagrant", "box", "add", "foo", config.boxes["default"])
assert(results.success?, "Box add should succeed.") result.should be_success
# Verify that the box now shows up in the list of available boxes # Verify that the box now shows up in the list of available boxes
results = execute("vagrant", "box", "list") result = execute("vagrant", "box", "list")
assert(output(results.stdout).box_installed("foo"), result.stdout.should match_output(:box_installed, "foo")
"foo box should exist after it is added")
end end
it "gives an error if the file doesn't exist" do it "gives an error if the file doesn't exist" do
results = execute("vagrant", "box", "add", "foo", "/tmp/nope/nope/nope/nonono.box") result = execute("vagrant", "box", "add", "foo", "/tmp/nope/nope/nope/nonono.box")
assert(!results.success?, "Box add should fail.") result.should_not be_success
assert(output(results.stdout).box_path_doesnt_exist, result.stdout.should match_output(:box_path_doesnt_exist)
"Should show an error message about the file not existing.")
end end
it "gives an error if the file is not a valid box" do it "gives an error if the file is not a valid box" do
@ -41,10 +38,9 @@ describe "vagrant box" do
f.write("INVALID!") f.write("INVALID!")
end end
results = execute("vagrant", "box", "add", "foo", invalid.to_s) result = execute("vagrant", "box", "add", "foo", invalid.to_s)
assert(!results.success?, "Box add should fail.") result.should_not be_success
assert(output(results.stdout).box_invalid, result.stdout.should match_output(:box_invalid)
"should say the box is invalid")
end end
it "can add a box from an HTTP server" do it "can add a box from an HTTP server" do
@ -58,10 +54,9 @@ describe "vagrant box" do
# shows up in the list of available boxes. # shows up in the list of available boxes.
execute("vagrant", "box", "add", "foo", config.boxes["default"]) execute("vagrant", "box", "add", "foo", config.boxes["default"])
execute("vagrant", "box", "remove", "foo") execute("vagrant", "box", "remove", "foo")
results = execute("vagrant", "box", "list") result = execute("vagrant", "box", "list")
assert(results.success?, "box list should succeed") result.should be_success
assert(output(results.stdout).no_boxes, result.stdout.should match_output(:no_boxes)
"No boxes should be installed")
end end
it "can repackage a box" do it "can repackage a box" do
@ -77,7 +72,7 @@ describe "vagrant box" do
# By default, repackage should dump into package.box into the CWD # By default, repackage should dump into package.box into the CWD
repackaged_file = environment.workdir.join("package.box") repackaged_file = environment.workdir.join("package.box")
assert(repackaged_file.file?, "package.box should exist in cwd of environment") repackaged_file.file?.should be, "package.box should exist in cwd of environment"
# Compare the sizes # Compare the sizes
repackaged_size = repackaged_file.size repackaged_size = repackaged_file.size

View File

@ -8,29 +8,26 @@ describe "vagrant init" do
assert(!vagrantfile.exist?, "Vagrantfile shouldn't exist") assert(!vagrantfile.exist?, "Vagrantfile shouldn't exist")
result = execute("vagrant", "init") result = execute("vagrant", "init")
assert(result.success?, "init should succeed") result.should be_success
assert(vagrantfile.exist?, "Vagrantfile should exist") vagrantfile.exist?.should be, "Vagrantfile should exist"
end end
it "creates a Vagrantfile with the box set to the given argument" do it "creates a Vagrantfile with the box set to the given argument" do
vagrantfile = environment.workdir.join("Vagrantfile") vagrantfile = environment.workdir.join("Vagrantfile")
result = execute("vagrant", "init", "foo") result = execute("vagrant", "init", "foo")
assert(result.success?, "init should succeed") result.should be_success
assert(vagrantfile.read =~ /config.vm.box = "foo"$/, vagrantfile.read.should match(/config.vm.box = "foo"$/)
"config.vm.box should be set to 'foo'")
end end
it "creates a Vagrantfile with the box URL set to the given argument" do it "creates a Vagrantfile with the box URL set to the given argument" do
vagrantfile = environment.workdir.join("Vagrantfile") vagrantfile = environment.workdir.join("Vagrantfile")
result = execute("vagrant", "init", "foo", "bar") result = execute("vagrant", "init", "foo", "bar")
assert(result.success?, "init should succeed") result.should be_success
contents = vagrantfile.read contents = vagrantfile.read
assert(contents =~ /config.vm.box = "foo"$/, contents.should match(/config.vm.box = "foo"$/)
"config.vm.box should be set to 'foo'") contents.should match(/config.vm.box_url = "bar"$/)
assert(contents =~ /config.vm.box_url = "bar"$/,
"config.vm.box_url should be set to 'bar'")
end end
end end

View File

@ -5,18 +5,16 @@ describe "vagrant ssh" do
it "fails if no Vagrantfile is found" do it "fails if no Vagrantfile is found" do
result = execute("vagrant", "ssh") result = execute("vagrant", "ssh")
assert(!result.success?, "vagrant ssh should fail") result.should_not be_success
assert(output(result.stdout).no_vagrantfile, result.stdout.should match_output(:no_vagrantfile)
"Vagrant should error since there is no Vagrantfile")
end end
it "fails if the virtual machine is not created" do it "fails if the virtual machine is not created" do
assert_execute("vagrant", "init") assert_execute("vagrant", "init")
result = execute("vagrant", "ssh") result = execute("vagrant", "ssh")
assert(!result.success?, "vagrant ssh should fail") result.should_not be_success
assert(output(result.stdout).error_vm_must_be_created, result.stdout.should match_output(:error_vm_must_be_created)
"Vagrant should error that the VM must be created.")
end end
it "is able to SSH into a running virtual machine" do it "is able to SSH into a running virtual machine" do
@ -33,8 +31,7 @@ describe "vagrant ssh" do
end end
end end
assert_equal("hello", result.stdout.chomp, result.stdout.chomp.should eql("hello"), "Vagrant should bring up a VM to be able to SSH into."
"Vagrant should bring up a virtual machine and be able to SSH in.")
end end
it "is able to execute a single command via the command line" do it "is able to execute a single command via the command line" do
@ -43,6 +40,6 @@ describe "vagrant ssh" do
assert_execute("vagrant", "up") assert_execute("vagrant", "up")
result = assert_execute("vagrant", "ssh", "-c", "echo foo") result = assert_execute("vagrant", "ssh", "-c", "echo foo")
assert_equal("foo\n", result.stdout) result.stdout.should == "foo\n"
end end
end end

View File

@ -40,13 +40,6 @@ shared_context "acceptance" do
environment.execute(*args, &block) environment.execute(*args, &block)
end end
# Returns an output matcher for the given text.
#
# @return [Acceptance::Output]
def output(text)
Acceptance::Output.new(text)
end
# This method is an assertion helper for asserting that a process # This method is an assertion helper for asserting that a process
# succeeds. It is a wrapper around `execute` that asserts that the # succeeds. It is a wrapper around `execute` that asserts that the
# exit status was successful. # exit status was successful.

View File

@ -0,0 +1,14 @@
require "support/output"
# This creates a matcher that is used to match against certain
# Vagrant output. Vagrant output is not what is being tested,
# so all that state is hidden away in Acceptance::Output.
RSpec::Matchers.define :match_output do |expected, *args|
match do |actual|
Acceptance::Output.new(actual).send(expected, *args)
end
failure_message_for_should do |actual|
"expected output to match: #{expected} #{args.inspect}"
end
end

View File

@ -35,7 +35,7 @@ module Acceptance
end end
# Tests that the output contains a specific Vagrant version. # Tests that the output contains a specific Vagrant version.
def is_version?(version) def version(version)
@text =~ /^Vagrant version #{version}$/ @text =~ /^Vagrant version #{version}$/
end end

View File

@ -1,11 +1,12 @@
require File.expand_path("../base", __FILE__) require File.expand_path("../base", __FILE__)
describe "vagrant up", "basics" do describe "vagrant up", "basics" do
include_context "acceptance"
it "fails if not Vagrantfile is found" do it "fails if not Vagrantfile is found" do
result = execute("vagrant", "up") result = execute("vagrant", "up")
assert(!result.success?, "vagrant up should fail") result.should_not be_success
assert(output(result.stdout).no_vagrantfile, result.stdout.should match_output(:no_vagrantfile)
"Vagrant should error since there is no Vagrantfile")
end end
it "brings up a running virtual machine" do it "brings up a running virtual machine" do
@ -13,9 +14,7 @@ describe "vagrant up", "basics" do
assert_execute("vagrant", "init") assert_execute("vagrant", "init")
assert_execute("vagrant", "up") assert_execute("vagrant", "up")
result = assert_execute("vagrant", "status") result = assert_execute("vagrant", "status")
result.stdout.should match_output(:status, "default", "running")
assert(output(result.stdout).status("default", "running"),
"Virtual machine should be running")
end end
=begin =begin

View File

@ -5,19 +5,16 @@ describe "vagrant version" do
it "prints the version to stdout" do it "prints the version to stdout" do
result = execute("vagrant", "version") result = execute("vagrant", "version")
assert(output(result.stdout).is_version?(config.vagrant_version), result.stdout.should match_output(:version, config.vagrant_version)
"output should be version")
end end
it "prints the version when called with '-v'" do it "prints the version when called with '-v'" do
result = execute("vagrant", "-v") result = execute("vagrant", "-v")
assert(output(result.stdout).is_version?(config.vagrant_version), result.stdout.should match_output(:version, config.vagrant_version)
"output should be version")
end end
it "prints the version when called with '--version'" do it "prints the version when called with '--version'" do
result = execute("vagrant", "--version") result = execute("vagrant", "--version")
assert(output(result.stdout).is_version?(config.vagrant_version), result.stdout.should match_output(:version, config.vagrant_version)
"output should be version")
end end
end end