diff --git a/lib/vagrant/command/init.rb b/lib/vagrant/command/init.rb index 3e2b4b9a8..a6fd4cfad 100644 --- a/lib/vagrant/command/init.rb +++ b/lib/vagrant/command/init.rb @@ -7,7 +7,7 @@ module Vagrant register "init [box_name] [box_url]", "Initializes the current folder for Vagrant usage" def execute - template "Vagrantfile.erb", "Vagrantfile" + template "Vagrantfile.erb", env.cwd.join("Vagrantfile") end end end diff --git a/lib/vagrant/test_helpers.rb b/lib/vagrant/test_helpers.rb index 72cb58849..96adbf4cf 100644 --- a/lib/vagrant/test_helpers.rb +++ b/lib/vagrant/test_helpers.rb @@ -84,6 +84,29 @@ module Vagrant [app, env] end + # Utility method for capturing output streams. + # @example Evaluate the output + # output = capture(:stdout){ env.cli("foo") } + # assert_equal "bar", output + # @example Silence the output + # silence(:stdout){ env.cli("init") } + # @param [:stdout, :stderr] stream The stream to capture + # @yieldreturn String + # @see https://github.com/wycats/thor/blob/master/spec/spec_helper.rb + def capture(stream) + begin + stream = stream.to_s + eval "$#{stream} = StringIO.new" + yield + result = eval("$#{stream}").string + ensure + eval("$#{stream} = #{stream.upcase}") + end + + result + end + alias :silence :capture + #------------------------------------------------------------ # Path helpers #------------------------------------------------------------ diff --git a/test/unit/vagrant/command/init_test.rb b/test/unit/vagrant/command/init_test.rb new file mode 100644 index 000000000..7005a4663 --- /dev/null +++ b/test/unit/vagrant/command/init_test.rb @@ -0,0 +1,10 @@ +require "test_helper" + +class CommandInitCommandTest < Test::Unit::TestCase + should "create a Vagrantfile in the environment's cwd" do + path = vagrant_app + env = Vagrant::Environment.new(:cwd => path) + silence(:stdout) { env.cli("init") } + assert File.exist?(path.join("Vagrantfile")) + end +end