Sha256: 4ce94c7d39279860d046b598376ecf041b9982144f3014f7a158aba6af64310b

Contents?: true

Size: 1.97 KB

Versions: 44

Compression:

Stored size: 1.97 KB

Contents

require "fileutils"
require "test_helper"

class DataStoreTest < Test::Unit::TestCase
  setup do
    @klass = Vagrant::DataStore
    @initial_data = { "foo" => "bar" }
    @db_file = File.join(tmp_path, "data_store_test")
    File.open(@db_file, "w") { |f| f.write(@initial_data.to_json) }

    @instance = @klass.new(@db_file)
  end

  teardown do
    File.delete(@db_file) if File.exist?(@db_file)
  end

  should "raise an exception if the db file is a directory" do
    file = tmp_path.join("data_store_folder_test")
    FileUtils.mkdir_p(file)
    assert_raises (Vagrant::Errors::DotfileIsDirectory) {
      @klass.new(file)
    }
  end

  should "be a hash with indifferent access" do
    assert @instance.is_a?(Vagrant::Util::HashWithIndifferentAccess)
  end

  should "just be an empty hash if file doesn't exist" do
    assert @klass.new("NEvERNENVENRNE").empty?
  end

  should "read the data" do
    assert_equal @initial_data["foo"], @instance[:foo]
  end

  should "read the data by stringifying keys" do
    @instance[:bar] = { "baz" => "yay" }
    @instance.commit
    @instance = @klass.new(@db_file)
    assert_equal "yay", @instance[:bar]["baz"]
  end

  should "write the data, but not save it right away" do
    @instance[:foo] = "changed"
    assert_equal "changed", @instance[:foo]
    assert_equal @initial_data["foo"], @klass.new(@db_file)["foo"]
  end

  should "write the data if commit is called" do
    @instance[:foo] = "changed"
    @instance.commit

    assert_equal "changed", @klass.new(@db_file)[:foo]
  end

  should "delete the data store file if the hash is empty" do
    @instance[:foo] = :bar
    @instance.commit
    assert File.exist?(@db_file)

    @instance.clear
    assert @instance.empty?
    @instance.commit
    assert !File.exist?(@db_file)
  end

  should "clean nil and empties if commit is called" do
    @instance[:foo] = { :bar => nil }
    @instance[:bar] = {}
    @instance.commit

    assert !@instance.has_key?(:foo)
    assert !@instance.has_key?(:bar)
  end
end

Version data entries

44 entries across 44 versions & 5 rubygems

Version Path
vagrantup-0.8.6 test/vagrant/data_store_test.rb
vagrantup-0.8.5 test/vagrant/data_store_test.rb
vagrantup-0.8.4 test/vagrant/data_store_test.rb
vagrantup-0.8.3 test/vagrant/data_store_test.rb
vagrantup-0.8.2 test/vagrant/data_store_test.rb
vagrantup-0.8.1 test/vagrant/data_store_test.rb
vagrantup-0.8.0 test/vagrant/data_store_test.rb
vagrantup-0.7.8 test/vagrant/data_store_test.rb
vagrantup-0.7.7 test/vagrant/data_store_test.rb
vagrantup-0.7.6 test/vagrant/data_store_test.rb
vagrantup-0.7.5 test/vagrant/data_store_test.rb
vagrantup-0.7.4 test/vagrant/data_store_test.rb
vagrantup-0.7.3 test/vagrant/data_store_test.rb
vagrantup-0.7.2 test/vagrant/data_store_test.rb
vagrantup-0.7.1 test/vagrant/data_store_test.rb
vagrantup-0.7.0 test/vagrant/data_store_test.rb
vagrantup-0.6.9 test/vagrant/data_store_test.rb
vagrantup-0.6.8 test/vagrant/data_store_test.rb
vagrantup-0.6.7 test/vagrant/data_store_test.rb
vagrantup-0.6.6 test/vagrant/data_store_test.rb