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
|
2021-05-10 12:50:06 -04:00
|
|
|
class YAML::Store
|
|
|
|
alias :old_load :load
|
|
|
|
|
|
|
|
def load(content)
|
|
|
|
table = YAML.load(content, fallback: false)
|
|
|
|
if table == false
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
table
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless defined?(Psych::Store)
|
|
|
|
Psych::Store = YAML::Store
|
|
|
|
end
|
2011-11-03 11:54:16 -04:00
|
|
|
|
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
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_nil @yamlstore["foo"]
|
|
|
|
assert_nil @yamlstore["bar"]
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
|
|
|
|
@yamlstore.transaction do
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_nil @yamlstore["foo"]
|
|
|
|
assert_nil @yamlstore["bar"]
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_data_should_be_loaded_correctly_when_in_readonly_mode
|
|
|
|
@yamlstore.transaction do
|
2021-05-10 12:50:06 -04:00
|
|
|
@yamlstore["foo"] = "bar"
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
@yamlstore.transaction(true) do
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal "bar", @yamlstore["foo"]
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_data_should_be_loaded_correctly_when_in_readwrite_mode
|
|
|
|
@yamlstore.transaction do
|
2021-05-10 12:50:06 -04:00
|
|
|
@yamlstore["foo"] = "bar"
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
@yamlstore.transaction do
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal "bar", @yamlstore["foo"]
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_changes_after_commit_are_discarded
|
|
|
|
@yamlstore.transaction do
|
2021-05-10 12:50:06 -04:00
|
|
|
@yamlstore["foo"] = "bar"
|
2011-09-25 08:05:06 -04:00
|
|
|
@yamlstore.commit
|
2021-05-10 12:50:06 -04:00
|
|
|
@yamlstore["foo"] = "baz"
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
@yamlstore.transaction(true) do
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_equal "bar", @yamlstore["foo"]
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_changes_are_not_written_on_abort
|
|
|
|
@yamlstore.transaction do
|
2021-05-10 12:50:06 -04:00
|
|
|
@yamlstore["foo"] = "bar"
|
2011-09-25 08:05:06 -04:00
|
|
|
@yamlstore.abort
|
|
|
|
end
|
|
|
|
@yamlstore.transaction(true) do
|
2021-05-10 12:50:06 -04:00
|
|
|
assert_nil @yamlstore["foo"]
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_writing_inside_readonly_transaction_raises_error
|
2021-05-10 06:09:17 -04:00
|
|
|
assert_raise(PStore::Error) do
|
2011-09-25 08:05:06 -04:00
|
|
|
@yamlstore.transaction(true) do
|
2021-05-10 12:50:06 -04:00
|
|
|
@yamlstore["foo"] = "bar"
|
2011-09-25 08:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-03 11:54:16 -04:00
|
|
|
end if defined?(Psych)
|