diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c1197e839..053d4023e 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -145,6 +145,16 @@ module Vagrant end end + # Loads on initial access and reads data from the global data store. + # The global data store is global to Vagrant everywhere (in every environment), + # so it can be used to store system-wide information. Note that "system-wide" + # typically means "for this user" since the location of the global data + # store is in the home directory. + def global_data + return parent.global_data if parent + @global_data ||= DataStore.new(File.expand_path("global_data.json", home_path)) + end + # Loads (on initial access) and reads data from the local data # store. This file is always at the root path as the file "~/.vagrant" # and contains a JSON dump of a hash. See {DataStore} for more diff --git a/test/vagrant/environment_test.rb b/test/vagrant/environment_test.rb index 839fc2a8a..06d564e04 100644 --- a/test/vagrant/environment_test.rb +++ b/test/vagrant/environment_test.rb @@ -245,6 +245,28 @@ class EnvironmentTest < Test::Unit::TestCase end end + context "global data" do + setup do + @env = mock_environment + end + + should "lazy load the data store only once" do + result = mock("result") + Vagrant::DataStore.expects(:new).with(File.expand_path("global_data.json", @env.home_path)).returns(result).once + assert_equal result, @env.global_data + assert_equal result, @env.global_data + assert_equal result, @env.global_data + end + + should "return the parent's local data if a parent exists" do + @env.stubs(:parent).returns(mock_environment) + result = @env.parent.global_data + + Vagrant::DataStore.expects(:new).never + assert_equal result, @env.global_data + end + end + context "loading logger" do should "lazy load the logger only once" do result = Vagrant::Util::ResourceLogger.new("vagrant", mock_environment)