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
def_delegators :@boxes, :length, :each
# The environment this box collection belongs to
attr_reader :env
# The directory that the boxes are being searched for.
attr_reader :directory
def initialize(env)
@env = env
# Initializes the class to search for boxes in the given directory.
def initialize(directory)
@directory = directory
@boxes = []
reload!
@ -41,9 +42,10 @@ module Vagrant
def reload!
@boxes.clear
Dir.open(env.boxes_path) do |dir|
Dir.open(@directory) do |dir|
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)
end
end

View File

@ -80,7 +80,6 @@ module Vagrant
@logger = Log4r::Logger.new("vagrant::environment")
@logger.info("Environment initialized (#{self})")
@logger.info(" - cwd: #{cwd}")
@logger.info(" - vm: #{vm}")
end
#---------------------------------------------------------------
@ -147,7 +146,7 @@ module Vagrant
#
# @return [BoxCollection]
def boxes
@_boxes ||= BoxCollection.new(self)
@_boxes ||= BoxCollection.new(boxes_path)
end
# 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
# {#load!}).
#
# @return [Config::Top]
# @return [Config::Container]
def config
load! if !loaded?
@config

View File

@ -50,15 +50,15 @@ module Unit
end
end
def box(name, vagrantfile_contents)
def box(name, vagrantfile_contents="")
box_dir = boxes_dir.join(name)
box_dir.mkdir
box_dir.mkpath
vagrantfile(vagrantfile_contents, box_dir)
end
def boxes_dir
dir = @homedir.join("boxes")
dir.mkdir
dir.mkpath
dir
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
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
it "is the cwd by default" do
described_class.new.cwd.should == Pathname.new(Dir.pwd)
@ -35,10 +38,13 @@ describe Vagrant::Environment do
end
end
describe "loading configuration" do
let(:home_path) { Pathname.new(Tempdir.new.path) }
let(:instance) { described_class.new(:home_path => home_path) }
it "has a box collection pointed to the proper directory" do
collection = instance.boxes
collection.should be_kind_of(Vagrant::BoxCollection)
collection.directory.should == instance.boxes_path
end
describe "loading configuration" do
it "should load global configuration" do
environment = isolated_environment do |env|
env.vagrantfile(<<-VF)