From 79718eb4c3d53ce6ea263a5334470007cf06f1a9 Mon Sep 17 00:00:00 2001 From: John Bender Date: Sat, 27 Feb 2010 22:49:43 -0800 Subject: [PATCH] --include added to package tests passing, but still needs manual verification --- bin/vagrant-package | 5 ++++- lib/vagrant/actions/vm/package.rb | 6 +++++- lib/vagrant/commands.rb | 4 ++-- lib/vagrant/vm.rb | 4 ++-- test/vagrant/actions/vm/package_test.rb | 18 ++++++++++++++++-- test/vagrant/commands_test.rb | 11 +++++++++-- test/vagrant/vm_test.rb | 2 +- 7 files changed, 39 insertions(+), 11 deletions(-) diff --git a/bin/vagrant-package b/bin/vagrant-package index 782229d43..a9bd08adb 100755 --- a/bin/vagrant-package +++ b/bin/vagrant-package @@ -21,7 +21,10 @@ Usage: #{command.full_name} #{all_options_string} Package the current vagrant environment into a box. EOS + + opt :include, "files to include in the package", :type => :strings + run do |command| - Vagrant::Commands.package(command.argv[0]) + Vagrant::Commands.package(command.argv[0], command.opts[:include]) end end diff --git a/lib/vagrant/actions/vm/package.rb b/lib/vagrant/actions/vm/package.rb index d9dc3e0e7..13369d709 100644 --- a/lib/vagrant/actions/vm/package.rb +++ b/lib/vagrant/actions/vm/package.rb @@ -3,11 +3,13 @@ module Vagrant module VM class Package < Base attr_accessor :out_path + attr_accessor :include_files attr_accessor :temp_path - def initialize(vm, out_path = nil, *args) + def initialize(vm, out_path = nil, include_files = [], *args) super @out_path = out_path || "package" + @include_files = include_files @temp_path = nil end @@ -29,6 +31,8 @@ module Vagrant logger.info "Packaging VM into #{tar_path} ..." Tar.open(tar_path, File::CREAT | File::WRONLY, 0644, Tar::GNU) do |tar| begin + @include_files.each { |f| tar.append_file(f) } + # Append tree will append the entire directory tree unless a relative folder reference is used current_dir = FileUtils.pwd FileUtils.cd(temp_path) diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 59c41051d..85f193f2d 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -113,14 +113,14 @@ error # Export and package the current vm # # This command requires that an instance be powered off - def package(out_path=nil) + def package(out_path=nil, include_files=[]) Env.load! Env.require_persisted_vm error_and_exit(<<-error) unless Env.persisted_vm.powered_off? The vagrant virtual environment you are trying to package must be powered off error - Env.persisted_vm.package(out_path) + Env.persisted_vm.package(out_path, include_files) end # Manages the `vagrant box` command, allowing the user to add diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index 68645e232..b81382d62 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -19,9 +19,9 @@ module Vagrant @vm = vm end - def package(out_path) + def package(out_path, include_files=[]) add_action(Actions::VM::Export) - add_action(Actions::VM::Package, out_path) + add_action(Actions::VM::Package, out_path, include_files) execute! end diff --git a/test/vagrant/actions/vm/package_test.rb b/test/vagrant/actions/vm/package_test.rb index 36cbc63d9..26025a9ef 100644 --- a/test/vagrant/actions/vm/package_test.rb +++ b/test/vagrant/actions/vm/package_test.rb @@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') class PackageActionTest < Test::Unit::TestCase setup do - @wrapper_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing") + @wrapper_vm, @vm, @action = mock_action(Vagrant::Actions::VM::Package, "bing", []) mock_config @temp_path = "temp_path" @@ -82,8 +82,22 @@ class PackageActionTest < Test::Unit::TestCase @action.compress } end - end + should "add included files when passed" do + include_files = ['foo', 'bar'] + action = mock_action(Vagrant::Actions::VM::Package, "bing", include_files).last + @tar.expects(:append_tree).with(".") + include_files.each { |f| @tar.expects(:append_file).with(f) } + action.compress + end + + should "not add files when none are specified" do + @tar.expects(:append_tree).with(".") + @tar.expects(:append_file).never + @action.compress + end + end + context "export callback to set temp path" do should "save to the temp_path directory" do foo = mock("foo") diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 1a066b39f..8461d754e 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -174,9 +174,16 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.package end - should "pass in the out path to the package method" do + should "pass the out path and include_files to the package method" do out_path = mock("out_path") - @persisted_vm.expects(:package).with(out_path).once + include_files = mock("include_files") + @persisted_vm.expects(:package).with(out_path, include_files).once + Vagrant::Commands.package(out_path, include_files) + end + + should "default to an empty array when not include_files are specified" do + out_path = mock("out_path") + @persisted_vm.expects(:package).with(out_path, []).once Vagrant::Commands.package(out_path) end end diff --git a/test/vagrant/vm_test.rb b/test/vagrant/vm_test.rb index 10204c448..7fb6abca5 100644 --- a/test/vagrant/vm_test.rb +++ b/test/vagrant/vm_test.rb @@ -42,7 +42,7 @@ class VMTest < Test::Unit::TestCase out_path = mock("out_path") action_seq = sequence("actions") @vm.expects(:add_action).with(Vagrant::Actions::VM::Export).once.in_sequence(action_seq) - @vm.expects(:add_action).with(Vagrant::Actions::VM::Package, out_path).once.in_sequence(action_seq) + @vm.expects(:add_action).with(Vagrant::Actions::VM::Package, out_path, []).once.in_sequence(action_seq) @vm.expects(:execute!).in_sequence(action_seq) @vm.package(out_path) end