From 4443a323e52ff43a036f5efcf2d07bcd4a014fd7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 8 Nov 2011 23:03:15 -0800 Subject: [PATCH] Create the output matcher, switch to RSpec style matchers --- test/acceptance/base.rb | 14 +++++--- test/acceptance/box_test.rb | 35 ++++++++----------- test/acceptance/init_test.rb | 17 ++++----- test/acceptance/ssh_test.rb | 15 ++++---- test/acceptance/support/base_context.rb | 7 ---- .../support/matchers/match_output.rb | 14 ++++++++ test/acceptance/support/output.rb | 2 +- test/acceptance/up_basic_test.rb | 11 +++--- test/acceptance/version_test.rb | 9 ++--- 9 files changed, 61 insertions(+), 63 deletions(-) create mode 100644 test/acceptance/support/matchers/match_output.rb diff --git a/test/acceptance/base.rb b/test/acceptance/base.rb index bd97fd042..b6119824d 100644 --- a/test/acceptance/base.rb +++ b/test/acceptance/base.rb @@ -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 diff --git a/test/acceptance/box_test.rb b/test/acceptance/box_test.rb index d81115115..6cdc4b69e 100644 --- a/test/acceptance/box_test.rb +++ b/test/acceptance/box_test.rb @@ -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 diff --git a/test/acceptance/init_test.rb b/test/acceptance/init_test.rb index 1f0f96d6a..884b0100b 100644 --- a/test/acceptance/init_test.rb +++ b/test/acceptance/init_test.rb @@ -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 diff --git a/test/acceptance/ssh_test.rb b/test/acceptance/ssh_test.rb index 32842c960..217e4c342 100644 --- a/test/acceptance/ssh_test.rb +++ b/test/acceptance/ssh_test.rb @@ -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 diff --git a/test/acceptance/support/base_context.rb b/test/acceptance/support/base_context.rb index 5983a6229..dd4f0db06 100644 --- a/test/acceptance/support/base_context.rb +++ b/test/acceptance/support/base_context.rb @@ -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. diff --git a/test/acceptance/support/matchers/match_output.rb b/test/acceptance/support/matchers/match_output.rb new file mode 100644 index 000000000..5a64e93d8 --- /dev/null +++ b/test/acceptance/support/matchers/match_output.rb @@ -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 diff --git a/test/acceptance/support/output.rb b/test/acceptance/support/output.rb index efa1168ae..55d448a2b 100644 --- a/test/acceptance/support/output.rb +++ b/test/acceptance/support/output.rb @@ -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 diff --git a/test/acceptance/up_basic_test.rb b/test/acceptance/up_basic_test.rb index 2320573cf..0d7ca501b 100644 --- a/test/acceptance/up_basic_test.rb +++ b/test/acceptance/up_basic_test.rb @@ -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 diff --git a/test/acceptance/version_test.rb b/test/acceptance/version_test.rb index b8215bcbf..40b18b338 100644 --- a/test/acceptance/version_test.rb +++ b/test/acceptance/version_test.rb @@ -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