[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / test / psych / test_yamlstore.rb
blob1a1be3700eff9f0127a8ca37c1a59a8c4ec18fa5
1 # frozen_string_literal: true
2 require_relative 'helper'
3 require 'yaml/store'
4 require 'tmpdir'
6 module Psych
7   class YAML::Store
8     alias :old_load :load
10     def load(content)
11       table = YAML.load(content, fallback: false)
12       if table == false
13         {}
14       else
15         table
16       end
17     end
18   end
20   unless defined?(Psych::Store)
21     Psych::Store = YAML::Store
22   end
24   class YAMLStoreTest < TestCase
25     def setup
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)
30     end
32     def teardown
33       FileUtils.remove_entry_secure @dir
34     end
36     def make_tmp_filename(prefix)
37       @dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test"
38     end
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"]
44       end
45     end
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"]
51       end
52     end
54     def test_data_should_be_loaded_correctly_when_in_readonly_mode
55       @yamlstore.transaction do
56         @yamlstore["foo"] = "bar"
57       end
58       @yamlstore.transaction(true) do
59         assert_equal "bar", @yamlstore["foo"]
60       end
61     end
63     def test_data_should_be_loaded_correctly_when_in_readwrite_mode
64       @yamlstore.transaction do
65         @yamlstore["foo"] = "bar"
66       end
67       @yamlstore.transaction do
68         assert_equal "bar", @yamlstore["foo"]
69       end
70     end
72     def test_changes_after_commit_are_discarded
73       @yamlstore.transaction do
74         @yamlstore["foo"] = "bar"
75         @yamlstore.commit
76         @yamlstore["foo"] = "baz"
77       end
78       @yamlstore.transaction(true) do
79         assert_equal "bar", @yamlstore["foo"]
80       end
81     end
83     def test_changes_are_not_written_on_abort
84       @yamlstore.transaction do
85         @yamlstore["foo"] = "bar"
86         @yamlstore.abort
87       end
88       @yamlstore.transaction(true) do
89         assert_nil @yamlstore["foo"]
90       end
91     end
93     def test_writing_inside_readonly_transaction_raises_error
94       assert_raise(PStore::Error) do
95         @yamlstore.transaction(true) do
96           @yamlstore["foo"] = "bar"
97         end
98       end
99     end
100   end
101 end if defined?(Psych)