qonfig 0.24.0 → 0.27.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +62 -0
  3. data/.gitignore +0 -2
  4. data/.rubocop.yml +19 -1
  5. data/CHANGELOG.md +53 -0
  6. data/Gemfile +2 -0
  7. data/Gemfile.lock +129 -0
  8. data/LICENSE.txt +1 -1
  9. data/README.md +115 -5
  10. data/Rakefile +0 -1
  11. data/gemfiles/with_external_deps.gemfile +2 -0
  12. data/gemfiles/with_external_deps.gemfile.lock +123 -0
  13. data/gemfiles/without_external_deps.gemfile.lock +113 -0
  14. data/lib/qonfig/commands/definition/load_from_env/value_converter.rb +2 -0
  15. data/lib/qonfig/commands/definition/load_from_env.rb +2 -0
  16. data/lib/qonfig/data_set.rb +3 -0
  17. data/lib/qonfig/imports/direct_key.rb +8 -2
  18. data/lib/qonfig/imports/mappings.rb +8 -2
  19. data/lib/qonfig/loaders/basic.rb +2 -0
  20. data/lib/qonfig/loaders/json.rb +2 -1
  21. data/lib/qonfig/loaders/yaml.rb +3 -1
  22. data/lib/qonfig/plugins/pretty_print/requirements.rb +3 -0
  23. data/lib/qonfig/plugins/pretty_print.rb +1 -0
  24. data/lib/qonfig/plugins/toml/commands/definition/expose_toml.rb +3 -3
  25. data/lib/qonfig/plugins/vault/commands/definition/expose_vault.rb +142 -0
  26. data/lib/qonfig/plugins/vault/commands/definition/load_from_vault.rb +53 -0
  27. data/lib/qonfig/plugins/vault/dsl.rb +35 -0
  28. data/lib/qonfig/plugins/vault/errors.rb +9 -0
  29. data/lib/qonfig/plugins/vault/loaders/vault.rb +74 -0
  30. data/lib/qonfig/plugins/vault.rb +24 -0
  31. data/lib/qonfig/plugins.rb +1 -0
  32. data/lib/qonfig/settings/key_matcher.rb +3 -1
  33. data/lib/qonfig/settings.rb +83 -22
  34. data/lib/qonfig/uploaders/base.rb +2 -0
  35. data/lib/qonfig/uploaders/yaml.rb +2 -0
  36. data/lib/qonfig/version.rb +1 -1
  37. data/lib/qonfig.rb +4 -1
  38. data/qonfig.gemspec +9 -8
  39. data/sig/.keep +0 -0
  40. data/spec/artifacts/.keep +0 -0
  41. data/spec/features/clear_options_spec.rb +92 -0
  42. data/spec/features/compacted_config_spec.rb +308 -0
  43. data/spec/features/composition_spec.rb +207 -0
  44. data/spec/features/config_definition_and_representation_spec.rb +535 -0
  45. data/spec/features/definition_order_spec.rb +69 -0
  46. data/spec/features/dig_functionality_spec.rb +47 -0
  47. data/spec/features/dot_notation_spec.rb +159 -0
  48. data/spec/features/export_settings_spec.rb +138 -0
  49. data/spec/features/expose_json_spec.rb +281 -0
  50. data/spec/features/expose_self/format_option_dynamic_spec.rb +69 -0
  51. data/spec/features/expose_self/format_option_json_spec.rb +74 -0
  52. data/spec/features/expose_self/format_option_unsupported_spec.rb +27 -0
  53. data/spec/features/expose_self/format_option_yaml_spec.rb +77 -0
  54. data/spec/features/expose_self_spec.rb +97 -0
  55. data/spec/features/expose_yaml_spec.rb +263 -0
  56. data/spec/features/freeze_state_spec.rb +122 -0
  57. data/spec/features/get_config_keys_spec.rb +62 -0
  58. data/spec/features/get_config_values_spec.rb +41 -0
  59. data/spec/features/has_a_key_spec.rb +48 -0
  60. data/spec/features/import_settings_spec.rb +323 -0
  61. data/spec/features/indifferent_access_spec.rb +57 -0
  62. data/spec/features/inheritance_spec.rb +110 -0
  63. data/spec/features/instantiation_without_class_definition_spec.rb +59 -0
  64. data/spec/features/iteration_over_setting_keys_spec.rb +48 -0
  65. data/spec/features/load_from_env_spec.rb +240 -0
  66. data/spec/features/load_from_json_spec.rb +97 -0
  67. data/spec/features/load_from_self/format_option_json_spec.rb +31 -0
  68. data/spec/features/load_from_self/format_option_unsupported_spec.rb +27 -0
  69. data/spec/features/load_from_self/format_option_yaml_spec.rb +49 -0
  70. data/spec/features/load_from_self/with_erb_instructions_spec.rb +33 -0
  71. data/spec/features/load_from_self/with_hash_like_data_representation_spec.rb +66 -0
  72. data/spec/features/load_from_self/with_non_hash_like_data_representation_spec.rb +19 -0
  73. data/spec/features/load_from_self/without_end_data_spec.rb +11 -0
  74. data/spec/features/load_from_yaml_spec.rb +110 -0
  75. data/spec/features/load_setting_values_from_file/by_instance_method_examples.rb +171 -0
  76. data/spec/features/load_setting_values_from_file/by_macros_examples.rb +165 -0
  77. data/spec/features/load_setting_values_from_file/load_from_json_spec.rb +21 -0
  78. data/spec/features/load_setting_values_from_file/load_from_self/json_format/end_data_with_env_spec.rb +100 -0
  79. data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_end_data_spec.rb +129 -0
  80. data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_incorrect_end_data_spec.rb +34 -0
  81. data/spec/features/load_setting_values_from_file/load_from_self/json_format/without_end_data_spec.rb +65 -0
  82. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/end_data_with_env_spec.rb +94 -0
  83. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_end_data_spec.rb +126 -0
  84. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_incorrect_end_data_spec.rb +32 -0
  85. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/without_end_data_spec.rb +65 -0
  86. data/spec/features/load_setting_values_from_file/load_from_yaml_spec.rb +21 -0
  87. data/spec/features/load_setting_values_from_file/shared_behavior_spec.rb +33 -0
  88. data/spec/features/mixin_spec.rb +387 -0
  89. data/spec/features/non_redefineable_core_methods_spec.rb +29 -0
  90. data/spec/features/plugins/pretty_print_spec.rb +86 -0
  91. data/spec/features/plugins/toml/expose_self/format_option_toml_spec.rb +71 -0
  92. data/spec/features/plugins/toml/expose_toml_spec.rb +221 -0
  93. data/spec/features/plugins/toml/load_from_self/format_option_toml_spec.rb +27 -0
  94. data/spec/features/plugins/toml/load_from_toml_spec.rb +109 -0
  95. data/spec/features/plugins/toml/load_setting_values_from_file/load_from_toml_spec.rb +27 -0
  96. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/end_data_with_env_spec.rb +95 -0
  97. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_end_data_spec.rb +125 -0
  98. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_incorrect_end_data_spec.rb +34 -0
  99. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/without_end_data_spec.rb +65 -0
  100. data/spec/features/plugins/toml/load_setting_values_from_file/shared_behavior_spec.rb +34 -0
  101. data/spec/features/plugins/toml/save_to_toml_spec.rb +149 -0
  102. data/spec/features/plugins/vault/expose_vault_spec.rb +117 -0
  103. data/spec/features/plugins/vault/load_from_vault_spec.rb +80 -0
  104. data/spec/features/plugins_spec.rb +89 -0
  105. data/spec/features/reload_spec.rb +75 -0
  106. data/spec/features/run_code_with_temporary_settings_spec.rb +104 -0
  107. data/spec/features/save_to_file/save_to_json_spec.rb +157 -0
  108. data/spec/features/save_to_file/save_to_yaml_spec.rb +189 -0
  109. data/spec/features/settings_as_predicates_spec.rb +47 -0
  110. data/spec/features/settings_redefinition_spec.rb +30 -0
  111. data/spec/features/slice_functionality_spec.rb +69 -0
  112. data/spec/features/subset_functionality_spec.rb +49 -0
  113. data/spec/features/validation_spec.rb +919 -0
  114. data/spec/fixtures/array_settings.yml +3 -0
  115. data/spec/fixtures/expose_json/incompatible_root_structure.json +6 -0
  116. data/spec/fixtures/expose_json/incompatible_structure.json +4 -0
  117. data/spec/fixtures/expose_json/project.development.json +7 -0
  118. data/spec/fixtures/expose_json/project.json +30 -0
  119. data/spec/fixtures/expose_json/project.production.json +7 -0
  120. data/spec/fixtures/expose_json/project.staging.json +7 -0
  121. data/spec/fixtures/expose_json/project.test.json +7 -0
  122. data/spec/fixtures/expose_yaml/incompatible_structure.yml +2 -0
  123. data/spec/fixtures/expose_yaml/project.development.yml +4 -0
  124. data/spec/fixtures/expose_yaml/project.production.yml +4 -0
  125. data/spec/fixtures/expose_yaml/project.staging.yml +4 -0
  126. data/spec/fixtures/expose_yaml/project.test.yml +4 -0
  127. data/spec/fixtures/expose_yaml/project.yml +25 -0
  128. data/spec/fixtures/json_array_sample.json +14 -0
  129. data/spec/fixtures/json_object_sample.json +9 -0
  130. data/spec/fixtures/json_with_empty_object.json +6 -0
  131. data/spec/fixtures/json_with_erb.json +6 -0
  132. data/spec/fixtures/plugins/toml/expose_toml/project.development.toml +4 -0
  133. data/spec/fixtures/plugins/toml/expose_toml/project.production.toml +4 -0
  134. data/spec/fixtures/plugins/toml/expose_toml/project.staging.toml +4 -0
  135. data/spec/fixtures/plugins/toml/expose_toml/project.test.toml +4 -0
  136. data/spec/fixtures/plugins/toml/expose_toml/project.toml +27 -0
  137. data/spec/fixtures/plugins/toml/mini_file.toml +6 -0
  138. data/spec/fixtures/plugins/toml/toml_sample_with_all_types.toml +72 -0
  139. data/spec/fixtures/plugins/toml/values_file/with_env.toml +13 -0
  140. data/spec/fixtures/plugins/toml/values_file/without_env.toml +6 -0
  141. data/spec/fixtures/rubocop_settings.yml +12 -0
  142. data/spec/fixtures/shared_settings_with_aliases.yml +10 -0
  143. data/spec/fixtures/travis_settings.yml +6 -0
  144. data/spec/fixtures/values_file/with_env.json +19 -0
  145. data/spec/fixtures/values_file/with_env.yml +17 -0
  146. data/spec/fixtures/values_file/without_env.json +8 -0
  147. data/spec/fixtures/values_file/without_env.yml +5 -0
  148. data/spec/fixtures/with_empty_hash.yml +4 -0
  149. data/spec/fixtures/with_erb_instructions.yml +3 -0
  150. data/spec/spec_helper.rb +46 -0
  151. data/spec/support/meta_scopes.rb +20 -0
  152. data/spec/support/spec_support.rb +42 -0
  153. metadata +181 -15
  154. data/.travis.yml +0 -47
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Plugins(toml): .values_file / #load_from_self (without __END__ data)', plugin: :toml do
4
+ describe 'DSL macros' do
5
+ context 'strict behavior' do
6
+ let(:config_klass) do
7
+ Class.new(Qonfig::DataSet) do
8
+ values_file :self, format: :toml, strict: true
9
+ setting :user, 'D@iVeR'
10
+ end
11
+ end
12
+
13
+ specify 'fails with Qonfig::SelfDataNotFoundError' do
14
+ expect { config_klass.new }.to raise_error(Qonfig::SelfDataNotFoundError)
15
+ end
16
+ end
17
+
18
+ context 'non-strict behavior (default)' do
19
+ let(:config_klass) do
20
+ Class.new(Qonfig::DataSet) do
21
+ values_file :self, format: :toml
22
+ setting :user, 'D@iVeR'
23
+ end
24
+ end
25
+
26
+ specify 'config instantiation works well :)' do
27
+ config = nil
28
+ expect { config = config_klass.new }.not_to raise_error
29
+ expect(config.settings.user).to eq('D@iVeR')
30
+ end
31
+ end
32
+ end
33
+
34
+ describe 'Instance method' do
35
+ context 'strict behavior (default)' do
36
+ let(:config_klass) do
37
+ Class.new(Qonfig::DataSet) { setting :user, 'D@iVeR' }
38
+ end
39
+
40
+ specify 'fails with Qonfig::SelfDataNotFoundError' do
41
+ config = config_klass.new
42
+
43
+ expect do
44
+ config.load_from_self(format: :toml, strict: true)
45
+ end.to raise_error(Qonfig::SelfDataNotFoundError)
46
+ end
47
+ end
48
+
49
+ context 'non-strict behavior' do
50
+ let(:config_klass) do
51
+ Class.new(Qonfig::DataSet) { setting :user, 'D@iVeR' }
52
+ end
53
+
54
+ specify 'no errors :)' do
55
+ config = config_klass.new
56
+
57
+ expect do
58
+ config.load_from_self(format: :toml, strict: false)
59
+ end.not_to raise_error
60
+
61
+ expect(config.settings.user).to eq('D@iVeR')
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Plugin(:toml) .values_file / #load_from_file (shared behavior)', plugin: :toml do
4
+ describe 'unsupported format failures' do
5
+ describe 'DSL macros' do
6
+ specify 'fails on unsupported format' do
7
+ expect do
8
+ Qonfig::DataSet.build do
9
+ values_file :self
10
+ end
11
+ end.to raise_error(Qonfig::DynamicLoaderParseError)
12
+ end
13
+ end
14
+
15
+ describe 'Instance method' do
16
+ specify 'fails on unsupported format' do
17
+ expect do
18
+ Qonfig::DataSet.build.load_from_self
19
+ end.to raise_error(Qonfig::DynamicLoaderParseError)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ __END__
26
+
27
+ user: 0exp
28
+ ASDF|"asSDF"
29
+ [test]
30
+ <xml>
31
+ <data>
32
+ <key>123</key>
33
+ </data>
34
+ </xml>
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Plugins(toml): Save to .toml (TOML)', plugin: :toml do
4
+ describe 'saving' do
5
+ let(:config_file_name) { "#{SecureRandom.uuid}.yml" }
6
+ let(:config_file_path) { SpecSupport.artifact_path(config_file_name) }
7
+ let(:config_klass) do
8
+ Class.new(Qonfig::DataSet) do
9
+ setting :sentry do
10
+ setting :user, 'D@iVeR'
11
+ setting :callback, -> { 'loaded' }
12
+ end
13
+
14
+ setting :server_port, 123
15
+ setting :cost, 123.456
16
+ setting :enabled, true
17
+
18
+ setting :sub_configurations, [
19
+ { 'server' => 'google', 'ping' => false },
20
+ { 'server' => 'shoklude', 'ping' => true }
21
+ ]
22
+ end
23
+ end
24
+ let(:config) { config_klass.new }
25
+
26
+ before do
27
+ config.save_to_toml(path: config_file_path) do |value|
28
+ value.is_a?(Proc) ? value.call : value
29
+ end
30
+ end
31
+
32
+ specify 'correctly represents config as YAML' do
33
+ file_data = File.read(config_file_path)
34
+
35
+ expect(file_data).to eq(<<~TOML.strip << "\n")
36
+ cost = 123.456
37
+ enabled = true
38
+ server_port = 123
39
+ [sentry]
40
+ callback = "loaded"
41
+ user = "D@iVeR"
42
+ [[sub_configurations]]
43
+ ping = false
44
+ server = "google"
45
+ [[sub_configurations]]
46
+ ping = true
47
+ server = "shoklude"
48
+ TOML
49
+ end
50
+
51
+ specify 'rewrites existing file' do
52
+ config_a = Class.new(Qonfig::DataSet) do
53
+ setting :kek, 'kek'
54
+ end.new
55
+
56
+ config_b = Class.new(Qonfig::DataSet) do
57
+ setting :pek, 'pek'
58
+ end.new
59
+
60
+ # first save (initial write)
61
+ config_a.save_to_toml(path: config_file_path)
62
+ file_data = File.read(config_file_path) # NOTE: initial path
63
+ expect(file_data).to eq(<<~TOML.strip << "\n")
64
+ kek = "kek"
65
+ TOML
66
+
67
+ # subsequent save (rewrite)
68
+ config_b.save_to_toml(path: config_file_path)
69
+ file_data = File.read(config_file_path) # NOTE: same path
70
+ expect(file_data).to eq(<<~TOML.strip << "\n")
71
+ pek = "pek"
72
+ TOML
73
+ end
74
+ end
75
+
76
+ describe 'data representation' do
77
+ let(:config_file_name) { "#{SecureRandom.uuid}.yml" }
78
+ let(:config_file_path) { SpecSupport.artifact_path(config_file_name) }
79
+
80
+ context 'config with supported toml types' do
81
+ let(:config) do
82
+ Class.new(Qonfig::DataSet) do
83
+ setting :true_boolean, true
84
+ setting :false_boolean, false
85
+ setting :empty_object, {}
86
+ setting :filled_object, { a: 1, b: nil, 'c' => true, d: '1', e: false }
87
+ setting :null_data, nil
88
+ setting :float_value, 123.456
89
+ setting :collection, [%w[1 2], [3, 4], [true, false], []]
90
+ setting :time, Time.utc(2031, 0o5, 27, 0o7, 32, 0)
91
+ end.new
92
+ end
93
+
94
+ specify 'correctly represents YAML data types' do
95
+ # NOTE: step 1) save config
96
+ config.save_to_toml(path: config_file_path)
97
+
98
+ # NOTE: step 2) read saved file
99
+ file_data = File.read(config_file_path)
100
+
101
+ expect(file_data).to eq(<<~TOML.strip << "\n")
102
+ collection = [["1", "2"], [3, 4], [true, false], []]
103
+ false_boolean = false
104
+ float_value = 123.456
105
+ time = 2031-05-27T07:32:00Z
106
+ true_boolean = true
107
+ [empty_object]
108
+ [filled_object]
109
+ a = 1
110
+ c = true
111
+ d = "1"
112
+ e = false
113
+ TOML
114
+ end
115
+
116
+ specify 'support for Pathname in filepath' do
117
+ config.save_to_toml(path: Pathname.new(config_file_path))
118
+
119
+ file_data = File.read(config_file_path)
120
+
121
+ expect(file_data).to eq(<<~TOML.strip << "\n")
122
+ collection = [["1", "2"], [3, 4], [true, false], []]
123
+ false_boolean = false
124
+ float_value = 123.456
125
+ time = 2031-05-27T07:32:00Z
126
+ true_boolean = true
127
+ [empty_object]
128
+ [filled_object]
129
+ a = 1
130
+ c = true
131
+ d = "1"
132
+ e = false
133
+ TOML
134
+ end
135
+ end
136
+
137
+ context 'config with unsupported toml types' do
138
+ let(:config) do
139
+ Class.new(Qonfig::DataSet) do
140
+ setting :collection, [[3, 4], %w[6 4], nil]
141
+ end.new
142
+ end
143
+
144
+ specify 'fails with parser error' do
145
+ expect { config.save_to_toml(path: config_file_path) }.to raise_error(::TomlRB::ParseError)
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Plugins(vault): expose vault', plugin: :vault do
4
+ before { stub_const('VaultConfig', vault_class) }
5
+
6
+ before { allow(Vault).to receive(:logical).and_return(logical_double) }
7
+
8
+ let(:logical_double) { instance_double(Vault::Logical) }
9
+
10
+ let(:returned_data) do
11
+ instance_double(Vault::Secret).tap do |instance|
12
+ allow(instance).to receive(:data).and_return(secret_data)
13
+ end
14
+ end
15
+ let(:secret_data) do
16
+ { data: { production: { kek: 'pek', cheburek: true }, other_key: '<%= 1 + 1 %>' } }
17
+ end
18
+
19
+ let(:vault_class) do
20
+ Class.new(Qonfig::DataSet) do
21
+ setting :based_on_path do
22
+ expose_vault 'kv/data/path_based', via: :path, env: :production
23
+ end
24
+
25
+ setting :based_on_env_key do
26
+ expose_vault 'kv/data/env_key', via: :env_key, env: 'production'
27
+ end
28
+ end
29
+ end
30
+
31
+ specify 'defines config object by vault instructions and specific environment settings' do
32
+ expect(Vault.logical).to(
33
+ receive(:read).with('kv/data/production/path_based').and_return(returned_data)
34
+ )
35
+ expect(Vault.logical).to receive(:read).with('kv/data/env_key').and_return(returned_data)
36
+
37
+ VaultConfig.new.settings.tap do |conf|
38
+ expect(conf.based_on_path.other_key).to eq(2)
39
+ expect(conf.based_on_path.production).to be_a(Qonfig::Settings)
40
+ expect(conf.based_on_env_key).to have_attributes(kek: 'pek', cheburek: true)
41
+ end
42
+ end
43
+
44
+ context 'when provided env argument is an Object' do
45
+ specify 'raises an error' do
46
+ expect do
47
+ Class.new(Qonfig::DataSet) do
48
+ expose_vault 'kv/data/path_based', via: Object.new, env: :production
49
+ end
50
+ end.to raise_error(Qonfig::ArgumentError)
51
+ end
52
+ end
53
+
54
+ context 'when provided via argument is an Object' do
55
+ specify 'raises an error' do
56
+ expect do
57
+ Class.new(Qonfig::DataSet) do
58
+ expose_vault 'kv/data/path_based', via: :path, env: Object.new
59
+ end
60
+ end.to raise_error(Qonfig::ArgumentError)
61
+ end
62
+ end
63
+
64
+ context 'when provided via is not supported' do
65
+ specify 'raises an error' do
66
+ expect do
67
+ Class.new(Qonfig::DataSet) do
68
+ expose_vault 'kv/data/path_based', via: :kek, env: :production
69
+ end
70
+ end.to raise_error(Qonfig::ArgumentError)
71
+ end
72
+ end
73
+
74
+ context 'when provided env is empty' do
75
+ specify 'raises an error' do
76
+ expect do
77
+ Class.new(Qonfig::DataSet) do
78
+ expose_vault 'kv/data/path_based', via: :path, env: ''
79
+ end
80
+ end.to raise_error(Qonfig::ArgumentError)
81
+ end
82
+ end
83
+
84
+ context "when provided key doesn't exist" do
85
+ let(:vault_class) do
86
+ Class.new(Qonfig::DataSet) do
87
+ expose_vault 'kv/data/env_key', via: :env_key, env: 'kekduction'
88
+ end
89
+ end
90
+
91
+ specify 'raises an error' do
92
+ expect(Vault.logical).to receive(:read).with('kv/data/env_key').and_return(returned_data)
93
+
94
+ expect { VaultConfig.new }.to raise_error(/does not contain settings with <kekduction>/)
95
+ end
96
+ end
97
+
98
+ context 'with not strict mode' do
99
+ let(:vault_class) do
100
+ Class.new(Qonfig::DataSet) do
101
+ setting :based_on_env_key do
102
+ expose_vault 'kv/data/env_key', via: :env_key, env: 'production', strict: false
103
+ end
104
+ end
105
+ end
106
+
107
+ let(:secret_data) { Hash[] }
108
+
109
+ specify "doesn't fail and uses empty config" do
110
+ expect(Vault.logical).to receive(:read).with('kv/data/env_key').and_return(returned_data)
111
+
112
+ conf = nil
113
+ expect { conf = VaultConfig.new }.not_to raise_error
114
+ expect(conf.to_h['based_on_env_key']).to be_empty
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Plugins(vault): Load from vault kv store', plugin: :vault do
4
+ before { stub_const('VaultConfig', vault_class) }
5
+
6
+ before { allow(Vault).to receive(:logical).and_return(logical_double) }
7
+
8
+ let(:returned_data) do
9
+ instance_double(Vault::Secret).tap do |instance|
10
+ allow(instance).to receive(:data).and_return(secret_data)
11
+ end
12
+ end
13
+ let(:logical_double) { instance_double(Vault::Logical) }
14
+ let(:secret_data) { Hash[data: { kek: true, pek: 'cheburek', nested: Hash[key: 123] }] }
15
+
16
+ let(:vault_class) do
17
+ Class.new(Qonfig::DataSet) do
18
+ load_from_vault 'kv/data/development'
19
+ end
20
+ end
21
+
22
+ specify 'defines config object by vault instructions' do
23
+ expect(Vault.logical).to receive(:read).with('kv/data/development').and_return(returned_data)
24
+
25
+ VaultConfig.new.settings.tap do |conf|
26
+ expect(conf).to have_attributes(kek: true, pek: 'cheburek')
27
+ expect(conf.nested.key).to eq(123)
28
+ end
29
+ end
30
+
31
+ context 'with not exist path' do
32
+ let(:expected_error_args) do
33
+ [Qonfig::FileNotFoundError, 'No such file or directory - Path kv/data/development not exist']
34
+ end
35
+
36
+ specify 'raises error' do
37
+ expect(Vault.logical).to receive(:read).with('kv/data/development').and_return(nil)
38
+ expect { VaultConfig.new }.to raise_error(*expected_error_args)
39
+ end
40
+ end
41
+
42
+ context 'with Pathname at path argument' do
43
+ let(:vault_class) do
44
+ Class.new(Qonfig::DataSet) do
45
+ load_from_vault Pathname('kv/data/development')
46
+ end
47
+ end
48
+
49
+ specify 'converts it to string' do
50
+ expect(Vault.logical).to receive(:read).with('kv/data/development').and_return(returned_data)
51
+
52
+ VaultConfig.new
53
+ end
54
+ end
55
+
56
+ context 'when strict set to false' do
57
+ let(:vault_class) do
58
+ Class.new(Qonfig::DataSet) do
59
+ load_from_vault 'kv/data/development', strict: false
60
+ end
61
+ end
62
+
63
+ specify "doesn't fail and uses empty config" do
64
+ expect(Vault.logical).to receive(:read).with('kv/data/development').and_return(nil).twice
65
+
66
+ expect { VaultConfig.new }.not_to raise_error
67
+ expect(VaultConfig.new.to_h).to eq({})
68
+ end
69
+ end
70
+
71
+ context 'when VaultError is raised' do
72
+ let(:raised_error) { Vault::VaultError.new('Cool error') }
73
+
74
+ specify 'raises VaultLoaderError' do
75
+ expect(Vault.logical).to receive(:read).with('kv/data/development').and_raise(raised_error)
76
+
77
+ expect { VaultConfig.new }.to raise_error(Qonfig::VaultLoaderError, 'Cool error')
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Plugins' do
4
+ specify 'plguin regsitration, load and resolving' do
5
+ # plugins are not registered
6
+ expect(Qonfig::Plugins.names).not_to include('internal_test_plugin', 'external_test_plugin')
7
+ expect(Qonfig.plugins).not_to include('internal_test_plugin', 'external_test_plugin')
8
+
9
+ InternalTestPluginInterceptor = Class.new { def self.invoke; end }
10
+ ExternalTestPluginInterceptor = Class.new { def self.call; end }
11
+
12
+ module Qonfig::Plugins
13
+ class InternalTestPlugin < Abstract
14
+ def self.install!
15
+ InternalTestPluginInterceptor.invoke
16
+ end
17
+ end
18
+
19
+ class ExternalTestPlugin < Abstract
20
+ def self.install!
21
+ ExternalTestPluginInterceptor.call
22
+ end
23
+ end
24
+
25
+ # register new plugins
26
+ register_plugin(:internal_test_plugin, InternalTestPlugin)
27
+ register_plugin(:external_test_plugin, ExternalTestPlugin)
28
+ end
29
+
30
+ # plugins are registered
31
+ expect(Qonfig::Plugins.names).to include('internal_test_plugin', 'external_test_plugin')
32
+ expect(Qonfig.plugins).to include('internal_test_plugin', 'external_test_plugin')
33
+
34
+ # new plugins is not included in #loaded_plugins list
35
+ expect(Qonfig.loaded_plugins).not_to include('internal_test_plugin')
36
+ expect(Qonfig.loaded_plugins).not_to include('external_test_plugin')
37
+ expect(Qonfig.enabled_plugins).not_to include('internal_test_plugin')
38
+ expect(Qonfig.enabled_plugins).not_to include('external_test_plugin')
39
+ expect(Qonfig.loaded_plugins).to eq(Qonfig.enabled_plugins)
40
+
41
+ # plugin can be loaded
42
+ expect(InternalTestPluginInterceptor).to receive(:invoke).exactly(4).times
43
+ Qonfig::Plugins.load(:internal_test_plugin)
44
+ Qonfig::Plugins.load('internal_test_plugin')
45
+ Qonfig.plugin(:internal_test_plugin)
46
+ Qonfig.plugin('internal_test_plugin')
47
+ expect(Qonfig.loaded_plugins).to include('internal_test_plugin')
48
+ expect(Qonfig.loaded_plugins).not_to include('external_test_plugin')
49
+ expect(Qonfig.enabled_plugins).to include('internal_test_plugin')
50
+ expect(Qonfig.enabled_plugins).not_to include('external_test_plugin')
51
+ expect(Qonfig.loaded_plugins).to eq(Qonfig.enabled_plugins)
52
+
53
+ # plugin can be loaded
54
+ expect(ExternalTestPluginInterceptor).to receive(:call).exactly(4).times
55
+ Qonfig::Plugins.load(:external_test_plugin)
56
+ Qonfig::Plugins.load('external_test_plugin')
57
+ Qonfig.enable(:external_test_plugin)
58
+ Qonfig.enable('external_test_plugin')
59
+ expect(Qonfig.loaded_plugins).to include('external_test_plugin')
60
+ expect(Qonfig.loaded_plugins).to include('internal_test_plugin')
61
+ expect(Qonfig.enabled_plugins).to include('external_test_plugin')
62
+ expect(Qonfig.enabled_plugins).to include('internal_test_plugin')
63
+ expect(Qonfig.loaded_plugins).to eq(Qonfig.enabled_plugins)
64
+
65
+ # fails when there is an attempt to register a plugin with already used name
66
+ expect do
67
+ module Qonfig::Plugins
68
+ register_plugin(:internal_test_plugin, Object)
69
+ end
70
+ end.to raise_error(Qonfig::AlreadyRegisteredPluginError)
71
+
72
+ # fails when there is an attempt to register a plugin with already used name
73
+ expect do
74
+ module Qonfig::Plugins
75
+ register_plugin(:external_test_plugin, Object)
76
+ end
77
+ end.to raise_error(Qonfig::AlreadyRegisteredPluginError)
78
+
79
+ # fails when there is an attempt to load an unregistered plugin
80
+ expect do
81
+ Qonfig::Plugins.load(:kek_test_plugin)
82
+ end.to raise_error(Qonfig::UnregisteredPluginError)
83
+
84
+ # fails when there is an attempt to load an unregistered plugin
85
+ expect do
86
+ Qonfig.plugin(:kek_test_plugin)
87
+ end.to raise_error(Qonfig::UnregisteredPluginError)
88
+ end
89
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Config reloading' do
4
+ specify 'config reloading works correctly' do
5
+ class ReloadableConfig < Qonfig::DataSet
6
+ setting :db do
7
+ setting :adapter, 'postgresql'
8
+ end
9
+
10
+ setting :logging, false
11
+ end
12
+
13
+ config = ReloadableConfig.new
14
+
15
+ expect(config.to_h).to match(
16
+ 'db' => { 'adapter' => 'postgresql' },
17
+ 'logging' => false
18
+ )
19
+
20
+ config.configure { |conf| conf.logging = true } # change internal state
21
+
22
+ # re-define and append settings and validations
23
+ class ReloadableConfig
24
+ setting :db do
25
+ setting :adapter, 'mongoid' # re-define defaults
26
+ end
27
+
28
+ setting :enable_api, false # append new setting
29
+
30
+ validate :logging, :boolean, strict: true
31
+ end
32
+
33
+ expect(config.to_h).to match(
34
+ 'db' => { 'adapter' => 'postgresql' },
35
+ 'logging' => true # internal state has initial value (not a changed previously)
36
+ )
37
+
38
+ # new validator is not invoked (logging should be a boolean)
39
+ expect { config.settings.logging = nil }.not_to raise_error
40
+
41
+ # reload config settings
42
+ config.reload!
43
+
44
+ expect(config.to_h).to match(
45
+ 'db' => { 'adapter' => 'mongoid' },
46
+ 'logging' => false,
47
+ 'enable_api' => false
48
+ )
49
+
50
+ # reload with instant configuration
51
+ config.reload! do |conf|
52
+ conf.enable_api = true # changed instantly
53
+ end
54
+
55
+ expect(config.to_h).to match(
56
+ 'db' => { 'adapter' => 'mongoid' },
57
+ 'logging' => false,
58
+ 'enable_api' => true # value from isntant change
59
+ )
60
+
61
+ # reload with hash && proc configuration
62
+ config.reload!(db: { adapter: 'oracloid' }) do |conf|
63
+ conf.enable_api = true
64
+ end
65
+
66
+ expect(config.to_h).to match(
67
+ 'db' => { 'adapter' => 'oracloid' },
68
+ 'logging' => false,
69
+ 'enable_api' => true
70
+ )
71
+
72
+ # reload and set invalid options (logging cant be nil)
73
+ expect { config.reload!(logging: nil) }.to raise_error(Qonfig::ValidationError)
74
+ end
75
+ end