From 8801bc7b1becf906d174fc162ca1672588d9fb47 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 4 Dec 2011 11:39:44 -0800 Subject: [PATCH] Starting to revamp BoxCollection to not depend on env --- lib/vagrant/box_collection.rb | 16 +++++++----- lib/vagrant/environment.rb | 5 ++-- test/unit/support/isolated_environment.rb | 6 ++--- test/unit/vagrant/box_collection_test.rb | 32 +++++++++++++++++++++++ test/unit/vagrant/environment_test.rb | 12 ++++++--- 5 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 test/unit/vagrant/box_collection_test.rb diff --git a/lib/vagrant/box_collection.rb b/lib/vagrant/box_collection.rb index 7045b4b2b..dc3198d66 100644 --- a/lib/vagrant/box_collection.rb +++ b/lib/vagrant/box_collection.rb @@ -16,12 +16,13 @@ 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 - @boxes = [] + # Initializes the class to search for boxes in the given directory. + def initialize(directory) + @directory = directory + @boxes = [] reload! end @@ -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 diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 080671a70..fcde3a6ea 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -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 diff --git a/test/unit/support/isolated_environment.rb b/test/unit/support/isolated_environment.rb index 61e85e102..7b5d2366a 100644 --- a/test/unit/support/isolated_environment.rb +++ b/test/unit/support/isolated_environment.rb @@ -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 diff --git a/test/unit/vagrant/box_collection_test.rb b/test/unit/vagrant/box_collection_test.rb new file mode 100644 index 000000000..1d21ef59d --- /dev/null +++ b/test/unit/vagrant/box_collection_test.rb @@ -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 diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 748c56f95..881a08453 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -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)