--include added to package tests passing, but still needs manual verification

This commit is contained in:
John Bender 2010-02-27 22:49:43 -08:00
parent 9e41bd9a34
commit 79718eb4c3
7 changed files with 39 additions and 11 deletions

View File

@ -21,7 +21,10 @@ Usage: #{command.full_name} #{all_options_string}
Package the current vagrant environment into a box. Package the current vagrant environment into a box.
EOS EOS
opt :include, "files to include in the package", :type => :strings
run do |command| run do |command|
Vagrant::Commands.package(command.argv[0]) Vagrant::Commands.package(command.argv[0], command.opts[:include])
end end
end end

View File

@ -3,11 +3,13 @@ module Vagrant
module VM module VM
class Package < Base class Package < Base
attr_accessor :out_path attr_accessor :out_path
attr_accessor :include_files
attr_accessor :temp_path attr_accessor :temp_path
def initialize(vm, out_path = nil, *args) def initialize(vm, out_path = nil, include_files = [], *args)
super super
@out_path = out_path || "package" @out_path = out_path || "package"
@include_files = include_files
@temp_path = nil @temp_path = nil
end end
@ -29,6 +31,8 @@ module Vagrant
logger.info "Packaging VM into #{tar_path} ..." logger.info "Packaging VM into #{tar_path} ..."
Tar.open(tar_path, File::CREAT | File::WRONLY, 0644, Tar::GNU) do |tar| Tar.open(tar_path, File::CREAT | File::WRONLY, 0644, Tar::GNU) do |tar|
begin begin
@include_files.each { |f| tar.append_file(f) }
# Append tree will append the entire directory tree unless a relative folder reference is used # Append tree will append the entire directory tree unless a relative folder reference is used
current_dir = FileUtils.pwd current_dir = FileUtils.pwd
FileUtils.cd(temp_path) FileUtils.cd(temp_path)

View File

@ -113,14 +113,14 @@ error
# Export and package the current vm # Export and package the current vm
# #
# This command requires that an instance be powered off # This command requires that an instance be powered off
def package(out_path=nil) def package(out_path=nil, include_files=[])
Env.load! Env.load!
Env.require_persisted_vm Env.require_persisted_vm
error_and_exit(<<-error) unless Env.persisted_vm.powered_off? error_and_exit(<<-error) unless Env.persisted_vm.powered_off?
The vagrant virtual environment you are trying to package must be powered off The vagrant virtual environment you are trying to package must be powered off
error error
Env.persisted_vm.package(out_path) Env.persisted_vm.package(out_path, include_files)
end end
# Manages the `vagrant box` command, allowing the user to add # Manages the `vagrant box` command, allowing the user to add

View File

@ -19,9 +19,9 @@ module Vagrant
@vm = vm @vm = vm
end end
def package(out_path) def package(out_path, include_files=[])
add_action(Actions::VM::Export) add_action(Actions::VM::Export)
add_action(Actions::VM::Package, out_path) add_action(Actions::VM::Package, out_path, include_files)
execute! execute!
end end

View File

@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class PackageActionTest < Test::Unit::TestCase class PackageActionTest < Test::Unit::TestCase
setup do 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 mock_config
@temp_path = "temp_path" @temp_path = "temp_path"
@ -82,6 +82,20 @@ class PackageActionTest < Test::Unit::TestCase
@action.compress @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 end
context "export callback to set temp path" do context "export callback to set temp path" do

View File

@ -174,9 +174,16 @@ class CommandsTest < Test::Unit::TestCase
Vagrant::Commands.package Vagrant::Commands.package
end 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") 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) Vagrant::Commands.package(out_path)
end end
end end

View File

@ -42,7 +42,7 @@ class VMTest < Test::Unit::TestCase
out_path = mock("out_path") out_path = mock("out_path")
action_seq = sequence("actions") 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::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.expects(:execute!).in_sequence(action_seq)
@vm.package(out_path) @vm.package(out_path)
end end