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 File.expand_path("../support/base_context", __FILE__)
require File.expand_path("../support/config", __FILE__)
require File.expand_path("../support/virtualbox", __FILE__)
# Add this directory to the load path, since it just makes
# everything else so much easier.
$:.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 Acceptance::VirtualBox.find_vboxsvc
@ -34,5 +40,5 @@ $acceptance_options = Acceptance::Config.new(ENV["ACCEPTANCE_CONFIG"])
# Configure RSpec
RSpec.configure do |c|
c.expect_with :stdlib
c.expect_with :rspec, :stdlib
end

View File

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

View File

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

View File

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

View File

@ -40,13 +40,6 @@ shared_context "acceptance" do
environment.execute(*args, &block)
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
# succeeds. It is a wrapper around `execute` that asserts that the
# 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
# Tests that the output contains a specific Vagrant version.
def is_version?(version)
def version(version)
@text =~ /^Vagrant version #{version}$/
end

View File

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

View File

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