2017-07-14 02:15:58 -04:00
|
|
|
# frozen_string_literal: true
|
2013-03-26 10:55:04 -04:00
|
|
|
require_relative 'helper'
|
2011-09-25 08:05:06 -04:00
|
|
|
require 'yaml/store'
|
2011-09-27 15:44:02 -04:00
|
|
|
require 'tmpdir'
|
|
|
|
|
2011-09-25 08:05:06 -04:00
|
|
|
module Psych
|
2011-11-03 11:54:16 -04:00
|
|
|
Psych::Store = YAML::Store unless defined?(Psych::Store)
|
|
|
|
|
2011-11-07 06:27:27 -05:00
|
|
|
class YAMLStoreTest < TestCase
|
2011-09-25 08:05:06 -04:00
|
|
|
def setup
|
2011-09-27 15:44:02 -04:00
|
|
|
@dir = Dir.mktmpdir("rubytest-file")
|
|
|
|
File.chown(-1, Process.gid, @dir)
|
|
|
|
@yamlstore_file = make_tmp_filename("yamlstore")
|
2011-09-25 08:05:06 -04:00
|
|
|
@yamlstore = YAML::Store.new(@yamlstore_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2011-09-27 15:44:02 -04:00
|
|
|
FileUtils.remove_entry_secure @dir
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_tmp_filename(prefix)
|
|
|
|
@dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test"
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_opening_new_file_in_readonly_mode_should_result_in_empty_values
|
|
|
|
@yamlstore.transaction(true) do
|
|
|
|
assert_nil @yamlstore[:foo]
|
|
|
|
assert_nil @yamlstore[:bar]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
|
|
|
|
@yamlstore.transaction do
|
|
|
|
assert_nil @yamlstore[:foo]
|
|
|
|
assert_nil @yamlstore[:bar]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_data_should_be_loaded_correctly_when_in_readonly_mode
|
|
|
|
@yamlstore.transaction do
|
|
|
|
@yamlstore[:foo] = "bar"
|
|
|
|
end
|
|
|
|
@yamlstore.transaction(true) do
|
|
|
|
assert_equal "bar", @yamlstore[:foo]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_data_should_be_loaded_correctly_when_in_readwrite_mode
|
|
|
|
@yamlstore.transaction do
|
|
|
|
@yamlstore[:foo] = "bar"
|
|
|
|
end
|
|
|
|
@yamlstore.transaction do
|
|
|
|
assert_equal "bar", @yamlstore[:foo]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_changes_after_commit_are_discarded
|
|
|
|
@yamlstore.transaction do
|
|
|
|
@yamlstore[:foo] = "bar"
|
|
|
|
@yamlstore.commit
|
|
|
|
@yamlstore[:foo] = "baz"
|
|
|
|
end
|
|
|
|
@yamlstore.transaction(true) do
|
|
|
|
assert_equal "bar", @yamlstore[:foo]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_changes_are_not_written_on_abort
|
|
|
|
@yamlstore.transaction do
|
|
|
|
@yamlstore[:foo] = "bar"
|
|
|
|
@yamlstore.abort
|
|
|
|
end
|
|
|
|
@yamlstore.transaction(true) do
|
|
|
|
assert_nil @yamlstore[:foo]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writing_inside_readonly_transaction_raises_error
|
2011-11-07 06:27:27 -05:00
|
|
|
assert_raises(PStore::Error) do
|
2011-09-25 08:05:06 -04:00
|
|
|
@yamlstore.transaction(true) do
|
|
|
|
@yamlstore[:foo] = "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-03 11:54:16 -04:00
|
|
|
end if defined?(Psych)
|