Sha256: 9b519a399e72f322c8041f15383de4cb104f475b35d650902a331ace9843511e

Contents?: true

Size: 1.96 KB

Versions: 23

Compression:

Stored size: 1.96 KB

Contents

require File.expand_path("../../base", __FILE__)

require 'pathname'

describe Vagrant::DataStore do
  include_context "unit"

  let(:db_file) do
    # We create a tempfile and force an explicit close/unlink
    # but save the path so that we can re-use it multiple times
    temp = Tempfile.new("vagrant")
    result = Pathname.new(temp.path)
    temp.close
    temp.unlink

    result
  end

  let(:instance)    { described_class.new(db_file) }

  it "initializes a new DB file" do
    instance[:data] = true
    instance.commit
    instance[:data].should == true

    test = described_class.new(db_file)
    test[:data].should == true
  end

  it "initializes empty if the file contains invalid data" do
    db_file.open("w+") { |f| f.write("NOPE!") }
    described_class.new(db_file).should be_empty
  end

  it "initializes empty if the file doesn't exist" do
    described_class.new("NOPENOPENOPENOPENPEPEPEPE").should be_empty
  end

  it "raises an error if the path given is a directory" do
    db_file.delete if db_file.exist?
    db_file.mkdir

    expect { described_class.new(db_file) }.
      to raise_error(Vagrant::Errors::DotfileIsDirectory)
  end

  it "cleans nil and empties when committing" do
    instance[:data] = { :bar => nil }
    instance[:another] = {}
    instance.commit

    # The instance is now empty because the data was nil
    instance.should be_empty
  end

  it "deletes the data file if the store is empty when saving" do
    instance[:data] = true
    instance.commit

    another = described_class.new(db_file)
    another[:data] = nil
    another.commit

    # The file should no longer exist
    db_file.should_not be_exist
  end

  it "works if the DB file is nil" do
    store = described_class.new(nil)
    store[:foo] = "bar"
    store[:foo].should == "bar"
  end

  it "throws an exception if attempting to commit a data store with no file" do
    store = described_class.new(nil)
    expect { store.commit }.
      to raise_error(StandardError)
  end
end

Version data entries

23 entries across 23 versions & 6 rubygems

Version Path
bmhatfield-vagrant-1.0.10 test/unit/vagrant/data_store_test.rb
bmhatfield-vagrant-1.0.9 test/unit/vagrant/data_store_test.rb
bmhatfield-vagrant-1.0.8 test/unit/vagrant/data_store_test.rb
bmhatfield-vagrant-1.0.7 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.7 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.6 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.5 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.4 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.3 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.2 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.1 test/unit/vagrant/data_store_test.rb
vagrantup-1.0.0 test/unit/vagrant/data_store_test.rb
vagrant-fixed-ssh-1.0.7 test/unit/vagrant/data_store_test.rb
vagrant-1.0.7 test/unit/vagrant/data_store_test.rb
vagrant-1.0.6 test/unit/vagrant/data_store_test.rb
boxcar-0.10005.1 test/unit/vagrant/data_store_test.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/vagrant-1.0.5/test/unit/vagrant/data_store_test.rb
vagrant-1.0.5 test/unit/vagrant/data_store_test.rb
vagrant-1.0.4 test/unit/vagrant/data_store_test.rb
vagrant-1.0.3 test/unit/vagrant/data_store_test.rb