From 55f2ac3f543b3578cfe0e35f02fe220d77eba7a7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 5 Nov 2011 14:44:24 -0700 Subject: [PATCH] Isolated env will log output of command as it comes. Update tests. --- test/acceptance/box_test.rb | 10 +++---- .../helpers/isolated_environment.rb | 28 ++++++++++++++++--- test/acceptance/vagrant_test.rb | 6 ++-- test/acceptance/version_test.rb | 9 ++++-- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/test/acceptance/box_test.rb b/test/acceptance/box_test.rb index 186a42eb5..27fd30dec 100644 --- a/test/acceptance/box_test.rb +++ b/test/acceptance/box_test.rb @@ -9,7 +9,7 @@ class BoxTest < AcceptanceTest should "have no boxes by default" do result = execute("vagrant", "box", "list") - assert result.stdout.read =~ /There are no installed boxes!/ + assert result.stdout =~ /There are no installed boxes!/ end should "add a box from a file" do @@ -21,13 +21,13 @@ class BoxTest < AcceptanceTest # Verify that the box now shows up in the list of available boxes results = execute("vagrant", "box", "list") - assert(results.stdout.read =~ /^foo$/, "Box should exist after it is added") + assert(results.stdout =~ /^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.read =~ /^The specified path to a file doesn't exist.$/, + assert(results.stdout =~ /^The specified path to a file doesn't exist.$/, "This should show an error message about the file not existing.") end @@ -39,7 +39,7 @@ class BoxTest < AcceptanceTest results = execute("vagrant", "box", "add", "foo", invalid.to_s) assert(!results.success?, "Box add should fail.") - assert(results.stdout.read =~ /^The box file you're attempting to add is invalid./, + assert(results.stdout =~ /^The box file you're attempting to add is invalid./, "should show an error message") end @@ -57,7 +57,7 @@ class BoxTest < AcceptanceTest execute("vagrant", "box", "remove", "foo") results = execute("vagrant", "box", "list") assert(results.success?, "box list should succeed") - assert(results.stdout.read =~ /^There are no installed boxes!/, + assert(results.stdout =~ /^There are no installed boxes!/, "box list should be empty") end diff --git a/test/acceptance/helpers/isolated_environment.rb b/test/acceptance/helpers/isolated_environment.rb index 2788c0848..e23b0b9e2 100644 --- a/test/acceptance/helpers/isolated_environment.rb +++ b/test/acceptance/helpers/isolated_environment.rb @@ -55,17 +55,37 @@ module Acceptance # Execute in a separate process, wait for it to complete, and # return the IO streams. - @logger.info("Executing: #{command} #{argN.inspect}") + @logger.info("Executing: #{command} #{argN.inspect}. Output will stream in...") pid, stdin, stdout, stderr = popen4(@env, command, *argN) - _pid, status = Process.waitpid2(pid) - @logger.info("Exit status: #{status.exitstatus}") - return ExecuteProcess.new(status.exitstatus, stdout, stderr) + io_data = { + stdout => "", + stderr => "" + } + + while results = IO.select([stdout, stderr], nil, nil, 5) + rs = results[0] + next if rs.empty? + + rs.each do |r| + data = r.readline + io_data[r] += data + + io_name = r == stdout ? "stdout" : "stderr" + @logger.debug("[#{io_name}] #{data.chomp}") + end + end + + _pid, status = Process.waitpid2(pid) + @logger.debug("Exit status: #{status.exitstatus}") + + return ExecuteProcess.new(status.exitstatus, io_data[stdout], io_data[stderr]) end # Closes the environment, cleans up the temporary directories, etc. def close # Delete the temporary directory + @logger.info("Removing isolated environment: #{@tempdir.path}") FileUtils.rm_rf(@tempdir.path) end diff --git a/test/acceptance/vagrant_test.rb b/test/acceptance/vagrant_test.rb index 2b1611e3b..b3f985002 100644 --- a/test/acceptance/vagrant_test.rb +++ b/test/acceptance/vagrant_test.rb @@ -27,7 +27,7 @@ SCRIPT end result = execute("expect", "color.exp") - assert(has_color?(result.stdout.read), "output should contain color") + assert(has_color?(result.stdout), "output should contain color") end should "not output color if there is a TTY but --no-color is present" do @@ -41,7 +41,7 @@ SCRIPT end result = execute("expect", "color.exp") - assert(!has_color?(result.stdout.read), "output should not contain color") + assert(!has_color?(result.stdout), "output should not contain color") end should "not output color in the absense of a TTY" do @@ -50,6 +50,6 @@ SCRIPT # If `vagrant status` itself is broken, another acceptance test # should catch that. We just assume it works here. result = execute("vagrant", "status") - assert(!has_color?(result.stdout.read), "output should not contain color") + assert(!has_color?(result.stdout), "output should not contain color") end end diff --git a/test/acceptance/version_test.rb b/test/acceptance/version_test.rb index 2b11233c1..02717fc40 100644 --- a/test/acceptance/version_test.rb +++ b/test/acceptance/version_test.rb @@ -3,16 +3,19 @@ require File.expand_path("../base", __FILE__) class VersionTest < AcceptanceTest should "print the version to stdout" do result = execute("vagrant", "version") - assert_equal("Vagrant version #{config.vagrant_version}\n", result.stdout.read) + assert(result.stdout =~ /^Vagrant version #{config.vagrant_version}$/, + "output should contain Vagrant version") end should "print the version with '-v'" do result = execute("vagrant", "-v") - assert_equal("Vagrant version #{config.vagrant_version}\n", result.stdout.read) + assert(result.stdout =~ /^Vagrant version #{config.vagrant_version}$/, + "output should contain Vagrant version") end should "print the version with '--version'" do result = execute("vagrant", "--version") - assert_equal("Vagrant version #{config.vagrant_version}\n", result.stdout.read) + assert(result.stdout =~ /^Vagrant version #{config.vagrant_version}$/, + "output should contain Vagrant version") end end