Starting to revamp BoxCollection to not depend on env

This commit is contained in:
Mitchell Hashimoto 2011-12-04 11:39:44 -08:00
parent cf7a5db7e7
commit 8801bc7b1b
5 changed files with 55 additions and 16 deletions

View File

@ -16,11 +16,12 @@ module Vagrant
extend Forwardable extend Forwardable
def_delegators :@boxes, :length, :each def_delegators :@boxes, :length, :each
# The environment this box collection belongs to # The directory that the boxes are being searched for.
attr_reader :env attr_reader :directory
def initialize(env) # Initializes the class to search for boxes in the given directory.
@env = env def initialize(directory)
@directory = directory
@boxes = [] @boxes = []
reload! reload!
@ -41,9 +42,10 @@ module Vagrant
def reload! def reload!
@boxes.clear @boxes.clear
Dir.open(env.boxes_path) do |dir| Dir.open(@directory) do |dir|
dir.each do |d| dir.each do |d|
next if d == "." || d == ".." || !File.directory?(env.boxes_path.join(d)) next if d == "." || d == ".." || !@directory.join(d).directory?
# TODO: env????
@boxes << Box.new(env, d) @boxes << Box.new(env, d)
end end
end end

View File

@ -80,7 +80,6 @@ module Vagrant
@logger = Log4r::Logger.new("vagrant::environment") @logger = Log4r::Logger.new("vagrant::environment")
@logger.info("Environment initialized (#{self})") @logger.info("Environment initialized (#{self})")
@logger.info(" - cwd: #{cwd}") @logger.info(" - cwd: #{cwd}")
@logger.info(" - vm: #{vm}")
end end
#--------------------------------------------------------------- #---------------------------------------------------------------
@ -147,7 +146,7 @@ module Vagrant
# #
# @return [BoxCollection] # @return [BoxCollection]
def boxes def boxes
@_boxes ||= BoxCollection.new(self) @_boxes ||= BoxCollection.new(boxes_path)
end end
# Returns the VMs associated with this environment. # Returns the VMs associated with this environment.
@ -299,7 +298,7 @@ module Vagrant
# will trigger the environment to load if it hasn't loaded yet (see # will trigger the environment to load if it hasn't loaded yet (see
# {#load!}). # {#load!}).
# #
# @return [Config::Top] # @return [Config::Container]
def config def config
load! if !loaded? load! if !loaded?
@config @config

View File

@ -50,15 +50,15 @@ module Unit
end end
end end
def box(name, vagrantfile_contents) def box(name, vagrantfile_contents="")
box_dir = boxes_dir.join(name) box_dir = boxes_dir.join(name)
box_dir.mkdir box_dir.mkpath
vagrantfile(vagrantfile_contents, box_dir) vagrantfile(vagrantfile_contents, box_dir)
end end
def boxes_dir def boxes_dir
dir = @homedir.join("boxes") dir = @homedir.join("boxes")
dir.mkdir dir.mkpath
dir dir
end end
end end

View File

@ -0,0 +1,32 @@
require File.expand_path("../../base", __FILE__)
describe Vagrant::BoxCollection do
include_context "unit"
let(:environment) { isolated_environment }
let(:instance) { described_class.new(environment.boxes_dir) }
it "should list all available boxes" do
# No boxes yet.
instance.length.should == 0
# Add some boxes to the environment and try again
environment.box("foo")
environment.box("bar")
instance.reload!
instance.length.should == 2
end
describe "finding" do
it "should return nil if it can't find the box" do
instance.find("foo").should be_nil
end
it "should return a box instance for any boxes it does find" do
environment.box("foo")
result = instance.find("foo")
result.should be_kind_of(Vagrant::Box)
result.name.should == "foo"
end
end
end

View File

@ -7,6 +7,9 @@ require "support/tempdir"
describe Vagrant::Environment do describe Vagrant::Environment do
include_context "unit" include_context "unit"
let(:home_path) { Pathname.new(Tempdir.new.path) }
let(:instance) { described_class.new(:home_path => home_path) }
describe "current working directory" do describe "current working directory" do
it "is the cwd by default" do it "is the cwd by default" do
described_class.new.cwd.should == Pathname.new(Dir.pwd) described_class.new.cwd.should == Pathname.new(Dir.pwd)
@ -35,10 +38,13 @@ describe Vagrant::Environment do
end end
end end
describe "loading configuration" do it "has a box collection pointed to the proper directory" do
let(:home_path) { Pathname.new(Tempdir.new.path) } collection = instance.boxes
let(:instance) { described_class.new(:home_path => home_path) } collection.should be_kind_of(Vagrant::BoxCollection)
collection.directory.should == instance.boxes_path
end
describe "loading configuration" do
it "should load global configuration" do it "should load global configuration" do
environment = isolated_environment do |env| environment = isolated_environment do |env|
env.vagrantfile(<<-VF) env.vagrantfile(<<-VF)