1 # frozen_string_literal: true
2 require_relative 'helper'
11 table = YAML.load(content, fallback: false)
20 unless defined?(Psych::Store)
21 Psych::Store = YAML::Store
24 class YAMLStoreTest < TestCase
26 @dir = Dir.mktmpdir("rubytest-file")
27 File.chown(-1, Process.gid, @dir)
28 @yamlstore_file = make_tmp_filename("yamlstore")
29 @yamlstore = YAML::Store.new(@yamlstore_file)
33 FileUtils.remove_entry_secure @dir
36 def make_tmp_filename(prefix)
37 @dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test"
40 def test_opening_new_file_in_readonly_mode_should_result_in_empty_values
41 @yamlstore.transaction(true) do
42 assert_nil @yamlstore["foo"]
43 assert_nil @yamlstore["bar"]
47 def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
48 @yamlstore.transaction do
49 assert_nil @yamlstore["foo"]
50 assert_nil @yamlstore["bar"]
54 def test_data_should_be_loaded_correctly_when_in_readonly_mode
55 @yamlstore.transaction do
56 @yamlstore["foo"] = "bar"
58 @yamlstore.transaction(true) do
59 assert_equal "bar", @yamlstore["foo"]
63 def test_data_should_be_loaded_correctly_when_in_readwrite_mode
64 @yamlstore.transaction do
65 @yamlstore["foo"] = "bar"
67 @yamlstore.transaction do
68 assert_equal "bar", @yamlstore["foo"]
72 def test_changes_after_commit_are_discarded
73 @yamlstore.transaction do
74 @yamlstore["foo"] = "bar"
76 @yamlstore["foo"] = "baz"
78 @yamlstore.transaction(true) do
79 assert_equal "bar", @yamlstore["foo"]
83 def test_changes_are_not_written_on_abort
84 @yamlstore.transaction do
85 @yamlstore["foo"] = "bar"
88 @yamlstore.transaction(true) do
89 assert_nil @yamlstore["foo"]
93 def test_writing_inside_readonly_transaction_raises_error
94 assert_raise(PStore::Error) do
95 @yamlstore.transaction(true) do
96 @yamlstore["foo"] = "bar"
101 end if defined?(Psych)