Changed box actions and commands to use the Box class instead of passed in params.

This commit is contained in:
Mitchell Hashimoto 2010-02-22 18:17:43 -08:00
parent 1856d56431
commit 28b48929df
7 changed files with 58 additions and 16 deletions

View File

@ -2,14 +2,8 @@ module Vagrant
module Actions module Actions
module Box module Box
class Add < Base class Add < Base
def initialize(runner, name, path, *args)
super
@name = name
@uri = path
end
def prepare def prepare
@runner.add_action(Download, @uri) @runner.add_action(Download)
end end
end end
end end

View File

@ -7,11 +7,6 @@ module Vagrant
BASENAME = "box" BASENAME = "box"
BUFFERSIZE = 1048576 # 1 MB BUFFERSIZE = 1048576 # 1 MB
def initialize(runner, uri, *args)
super
@uri = uri
end
def execute! def execute!
with_tempfile do |tempfile| with_tempfile do |tempfile|
copy_uri_to(tempfile) copy_uri_to(tempfile)
@ -27,7 +22,7 @@ module Vagrant
def copy_uri_to(f) def copy_uri_to(f)
logger.info "Copying box to temporary location..." logger.info "Copying box to temporary location..."
open(@uri) do |remote_file| open(@runner.uri) do |remote_file|
loop do loop do
break if remote_file.eof? break if remote_file.eof?
f.write(remote_file.read(BUFFERSIZE)) f.write(remote_file.read(BUFFERSIZE))

View File

@ -1,5 +1,20 @@
module Vagrant module Vagrant
class Box < Actions::Runner class Box < Actions::Runner
attr_accessor :name
attr_accessor :uri
attr_accessor :temp_path
class <<self
def add(name, uri)
box = new
box.name = name
box.uri = uri
box.add
end
end
def add
execute!(Actions::Box::Add)
end
end end
end end

View File

@ -158,7 +158,7 @@ error
# Adds a box to the local filesystem, given a URI. # Adds a box to the local filesystem, given a URI.
def box_add(name, path) def box_add(name, path)
Box.execute!(Actions::Box::Add, name, path) Box.add(name, path)
end end
private private

View File

@ -3,7 +3,8 @@ require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper')
class DownloadBoxActionTest < Test::Unit::TestCase class DownloadBoxActionTest < Test::Unit::TestCase
setup do setup do
@uri = "foo.com" @uri = "foo.com"
@wrapper_vm, @vm, @action = mock_action(Vagrant::Actions::Box::Download, @uri) @runner, @vm, @action = mock_action(Vagrant::Actions::Box::Download)
@runner.stubs(:uri).returns(@uri)
mock_config mock_config
Vagrant::Env.stubs(:tmp_path).returns("foo") Vagrant::Env.stubs(:tmp_path).returns("foo")
@ -48,6 +49,11 @@ class DownloadBoxActionTest < Test::Unit::TestCase
@action.stubs(:open).yields(@file) @action.stubs(:open).yields(@file)
end end
should "open with the given uri" do
@action.expects(:open).with(@uri).once
@action.copy_uri_to(@tempfile)
end
should "read from the file and write to the tempfile" do should "read from the file and write to the tempfile" do
data = mock("data") data = mock("data")
write_seq = sequence("write_seq") write_seq = sequence("write_seq")

32
test/vagrant/box_test.rb Normal file
View File

@ -0,0 +1,32 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class BoxTest < Test::Unit::TestCase
context "class methods" do
context "adding" do
setup do
@name = "foo"
@uri = "bar"
end
should "create a new instance, set the variables, and add it" do
box = mock("box")
box.expects(:name=).with(@name)
box.expects(:uri=).with(@uri)
box.expects(:add).once
Vagrant::Box.expects(:new).returns(box)
Vagrant::Box.add(@name, @uri)
end
end
end
context "instance methods" do
setup do
@box = Vagrant::Box.new
end
should "execute the Add action when add is called" do
@box.expects(:execute!).with(Vagrant::Actions::Box::Add).once
@box.add
end
end
end

View File

@ -206,7 +206,7 @@ class CommandsTest < Test::Unit::TestCase
end end
should "execute the add action with the name and path" do should "execute the add action with the name and path" do
Vagrant::Box.expects(:execute!).with(Vagrant::Actions::Box::Add, @name, @path).once Vagrant::Box.expects(:add).with(@name, @path).once
Vagrant::Commands.box_add(@name, @path) Vagrant::Commands.box_add(@name, @path)
end end
end end