qonfig 0.25.0 → 0.26.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -2
- data/.rubocop.yml +19 -1
- data/.travis.yml +7 -2
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +102 -0
- data/README.md +2 -0
- data/gemfiles/with_external_deps.gemfile.lock +112 -0
- data/gemfiles/without_external_deps.gemfile.lock +102 -0
- data/lib/qonfig.rb +1 -1
- data/lib/qonfig/commands/definition/load_from_env.rb +2 -0
- data/lib/qonfig/commands/definition/load_from_env/value_converter.rb +2 -0
- data/lib/qonfig/imports/direct_key.rb +8 -2
- data/lib/qonfig/imports/mappings.rb +8 -2
- data/lib/qonfig/loaders/basic.rb +2 -0
- data/lib/qonfig/loaders/json.rb +2 -1
- data/lib/qonfig/settings.rb +5 -1
- data/lib/qonfig/settings/key_matcher.rb +1 -1
- data/lib/qonfig/uploaders/base.rb +2 -0
- data/lib/qonfig/uploaders/yaml.rb +2 -0
- data/lib/qonfig/version.rb +1 -1
- data/qonfig.gemspec +4 -6
- data/sig/.keep +0 -0
- data/spec/features/clear_options_spec.rb +92 -0
- data/spec/features/compacted_config_spec.rb +308 -0
- data/spec/features/composition_spec.rb +207 -0
- data/spec/features/config_definition_and_representation_spec.rb +535 -0
- data/spec/features/definition_order_spec.rb +69 -0
- data/spec/features/dig_functionality_spec.rb +47 -0
- data/spec/features/dot_notation_spec.rb +159 -0
- data/spec/features/export_settings_spec.rb +138 -0
- data/spec/features/expose_json_spec.rb +281 -0
- data/spec/features/expose_self/format_option_dynamic_spec.rb +69 -0
- data/spec/features/expose_self/format_option_json_spec.rb +74 -0
- data/spec/features/expose_self/format_option_unsupported_spec.rb +27 -0
- data/spec/features/expose_self/format_option_yaml_spec.rb +77 -0
- data/spec/features/expose_self_spec.rb +97 -0
- data/spec/features/expose_yaml_spec.rb +263 -0
- data/spec/features/freeze_state_spec.rb +122 -0
- data/spec/features/get_config_keys_spec.rb +62 -0
- data/spec/features/get_config_values_spec.rb +41 -0
- data/spec/features/has_a_key_spec.rb +48 -0
- data/spec/features/import_settings_spec.rb +323 -0
- data/spec/features/indifferent_access_spec.rb +57 -0
- data/spec/features/inheritance_spec.rb +110 -0
- data/spec/features/instantiation_without_class_definition_spec.rb +59 -0
- data/spec/features/iteration_over_setting_keys_spec.rb +48 -0
- data/spec/features/load_from_env_spec.rb +240 -0
- data/spec/features/load_from_json_spec.rb +97 -0
- data/spec/features/load_from_self/format_option_json_spec.rb +31 -0
- data/spec/features/load_from_self/format_option_unsupported_spec.rb +27 -0
- data/spec/features/load_from_self/format_option_yaml_spec.rb +49 -0
- data/spec/features/load_from_self/with_erb_instructions_spec.rb +33 -0
- data/spec/features/load_from_self/with_hash_like_data_representation_spec.rb +66 -0
- data/spec/features/load_from_self/with_non_hash_like_data_representation_spec.rb +19 -0
- data/spec/features/load_from_self/without_end_data_spec.rb +11 -0
- data/spec/features/load_from_yaml_spec.rb +110 -0
- data/spec/features/load_setting_values_from_file/by_instance_method_examples.rb +171 -0
- data/spec/features/load_setting_values_from_file/by_macros_examples.rb +165 -0
- data/spec/features/load_setting_values_from_file/load_from_json_spec.rb +21 -0
- data/spec/features/load_setting_values_from_file/load_from_self/json_format/end_data_with_env_spec.rb +100 -0
- data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_end_data_spec.rb +129 -0
- data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_incorrect_end_data_spec.rb +34 -0
- data/spec/features/load_setting_values_from_file/load_from_self/json_format/without_end_data_spec.rb +65 -0
- data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/end_data_with_env_spec.rb +94 -0
- data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_end_data_spec.rb +126 -0
- data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_incorrect_end_data_spec.rb +32 -0
- data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/without_end_data_spec.rb +65 -0
- data/spec/features/load_setting_values_from_file/load_from_yaml_spec.rb +21 -0
- data/spec/features/load_setting_values_from_file/shared_behavior_spec.rb +33 -0
- data/spec/features/mixin_spec.rb +387 -0
- data/spec/features/non_redefineable_core_methods_spec.rb +29 -0
- data/spec/features/plugins/pretty_print_spec.rb +86 -0
- data/spec/features/plugins/toml/expose_self/format_option_toml_spec.rb +71 -0
- data/spec/features/plugins/toml/expose_toml_spec.rb +221 -0
- data/spec/features/plugins/toml/load_from_self/format_option_toml_spec.rb +27 -0
- data/spec/features/plugins/toml/load_from_toml_spec.rb +109 -0
- data/spec/features/plugins/toml/load_setting_values_from_file/load_from_toml_spec.rb +27 -0
- data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/end_data_with_env_spec.rb +95 -0
- data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_end_data_spec.rb +125 -0
- data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_incorrect_end_data_spec.rb +34 -0
- data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/without_end_data_spec.rb +65 -0
- data/spec/features/plugins/toml/load_setting_values_from_file/shared_behavior_spec.rb +34 -0
- data/spec/features/plugins/toml/save_to_toml_spec.rb +149 -0
- data/spec/features/plugins/vault/expose_vault_spec.rb +117 -0
- data/spec/features/plugins/vault/load_from_vault_spec.rb +80 -0
- data/spec/features/plugins_spec.rb +89 -0
- data/spec/features/reload_spec.rb +75 -0
- data/spec/features/run_code_with_temporary_settings_spec.rb +104 -0
- data/spec/features/save_to_file/save_to_json_spec.rb +157 -0
- data/spec/features/save_to_file/save_to_yaml_spec.rb +189 -0
- data/spec/features/settings_as_predicates_spec.rb +47 -0
- data/spec/features/settings_redefinition_spec.rb +30 -0
- data/spec/features/slice_functionality_spec.rb +69 -0
- data/spec/features/subset_functionality_spec.rb +49 -0
- data/spec/features/validation_spec.rb +916 -0
- data/spec/fixtures/array_settings.yml +3 -0
- data/spec/fixtures/expose_json/incompatible_root_structure.json +6 -0
- data/spec/fixtures/expose_json/incompatible_structure.json +4 -0
- data/spec/fixtures/expose_json/project.development.json +7 -0
- data/spec/fixtures/expose_json/project.json +30 -0
- data/spec/fixtures/expose_json/project.production.json +7 -0
- data/spec/fixtures/expose_json/project.staging.json +7 -0
- data/spec/fixtures/expose_json/project.test.json +7 -0
- data/spec/fixtures/expose_yaml/incompatible_structure.yml +2 -0
- data/spec/fixtures/expose_yaml/project.development.yml +4 -0
- data/spec/fixtures/expose_yaml/project.production.yml +4 -0
- data/spec/fixtures/expose_yaml/project.staging.yml +4 -0
- data/spec/fixtures/expose_yaml/project.test.yml +4 -0
- data/spec/fixtures/expose_yaml/project.yml +25 -0
- data/spec/fixtures/json_array_sample.json +14 -0
- data/spec/fixtures/json_object_sample.json +9 -0
- data/spec/fixtures/json_with_empty_object.json +6 -0
- data/spec/fixtures/json_with_erb.json +6 -0
- data/spec/fixtures/plugins/toml/expose_toml/project.development.toml +4 -0
- data/spec/fixtures/plugins/toml/expose_toml/project.production.toml +4 -0
- data/spec/fixtures/plugins/toml/expose_toml/project.staging.toml +4 -0
- data/spec/fixtures/plugins/toml/expose_toml/project.test.toml +4 -0
- data/spec/fixtures/plugins/toml/expose_toml/project.toml +27 -0
- data/spec/fixtures/plugins/toml/mini_file.toml +6 -0
- data/spec/fixtures/plugins/toml/toml_sample_with_all_types.toml +72 -0
- data/spec/fixtures/plugins/toml/values_file/with_env.toml +13 -0
- data/spec/fixtures/plugins/toml/values_file/without_env.toml +6 -0
- data/spec/fixtures/rubocop_settings.yml +12 -0
- data/spec/fixtures/shared_settings_with_aliases.yml +10 -0
- data/spec/fixtures/travis_settings.yml +6 -0
- data/spec/fixtures/values_file/with_env.json +19 -0
- data/spec/fixtures/values_file/with_env.yml +17 -0
- data/spec/fixtures/values_file/without_env.json +8 -0
- data/spec/fixtures/values_file/without_env.yml +5 -0
- data/spec/fixtures/with_empty_hash.yml +4 -0
- data/spec/fixtures/with_erb_instructions.yml +3 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/meta_scopes.rb +20 -0
- data/spec/support/spec_support.rb +42 -0
- metadata +128 -11
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe 'Indifferent Access' do
|
4
|
+
specify 'indifferently accessable options (directly via index; via string / via symbol)' do
|
5
|
+
class IndifferentlyAccessableConfig < Qonfig::DataSet
|
6
|
+
setting :project_id, 10
|
7
|
+
end
|
8
|
+
|
9
|
+
class AnotherIndifferentlyAccessableConfig < Qonfig::DataSet
|
10
|
+
compose IndifferentlyAccessableConfig
|
11
|
+
|
12
|
+
setting 'database' do
|
13
|
+
setting :hostname, 'localhost'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
config = AnotherIndifferentlyAccessableConfig.new
|
18
|
+
|
19
|
+
# indifferent access via string / via symbol
|
20
|
+
expect(config.settings[:project_id]).to eq(10)
|
21
|
+
expect(config.settings['project_id']).to eq(10)
|
22
|
+
expect(config.settings[:database][:hostname]).to eq('localhost')
|
23
|
+
expect(config.settings['database']['hostname']).to eq('localhost')
|
24
|
+
expect(config.settings['database'][:hostname]).to eq('localhost')
|
25
|
+
expect(config.settings[:database]['hostname']).to eq('localhost')
|
26
|
+
|
27
|
+
# direct access via [] on the config object
|
28
|
+
expect(config[:project_id]).to eq(10)
|
29
|
+
expect(config['project_id']).to eq(10)
|
30
|
+
expect(config[:database][:hostname]).to eq('localhost')
|
31
|
+
expect(config['database']['hostname']).to eq('localhost')
|
32
|
+
expect(config['database'][:hostname]).to eq('localhost')
|
33
|
+
expect(config[:database]['hostname']).to eq('localhost')
|
34
|
+
|
35
|
+
# instant configuration with indifferently accessable options
|
36
|
+
config.configure do |conf|
|
37
|
+
conf['project_id'] = 1
|
38
|
+
conf[:database]['hostname'] = 'google.com'
|
39
|
+
end
|
40
|
+
|
41
|
+
# indifferent access via string / via symbol
|
42
|
+
expect(config.settings[:project_id]).to eq(1)
|
43
|
+
expect(config.settings['project_id']).to eq(1)
|
44
|
+
expect(config.settings[:database][:hostname]).to eq('google.com')
|
45
|
+
expect(config.settings['database']['hostname']).to eq('google.com')
|
46
|
+
expect(config.settings['database'][:hostname]).to eq('google.com')
|
47
|
+
expect(config.settings[:database]['hostname']).to eq('google.com')
|
48
|
+
|
49
|
+
# direct access via [] on the config object
|
50
|
+
expect(config[:project_id]).to eq(1)
|
51
|
+
expect(config['project_id']).to eq(1)
|
52
|
+
expect(config[:database][:hostname]).to eq('google.com')
|
53
|
+
expect(config['database']['hostname']).to eq('google.com')
|
54
|
+
expect(config['database'][:hostname]).to eq('google.com')
|
55
|
+
expect(config[:database]['hostname']).to eq('google.com')
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe 'Inheritance' do
|
4
|
+
specify 'config inheritance works correclty' do
|
5
|
+
class FrameworkConfig < Qonfig::DataSet
|
6
|
+
setting :version, '0.1.0'
|
7
|
+
|
8
|
+
setting :defaults do
|
9
|
+
setting :path, '/a/b/c'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class UnknownConfig < Qonfig::DataSet
|
14
|
+
setting :unkown_data, true
|
15
|
+
end
|
16
|
+
|
17
|
+
class SharedConfig < Qonfig::DataSet
|
18
|
+
setting :google_api do
|
19
|
+
setting :token, 'test-google-api'
|
20
|
+
end
|
21
|
+
|
22
|
+
setting :admin_access_required, false
|
23
|
+
|
24
|
+
compose FrameworkConfig
|
25
|
+
end
|
26
|
+
|
27
|
+
class ClientConfig < SharedConfig
|
28
|
+
compose UnknownConfig
|
29
|
+
|
30
|
+
setting :google_api do
|
31
|
+
setting :client_token, 'client-test-google-api'
|
32
|
+
end
|
33
|
+
|
34
|
+
setting :defaults, nil
|
35
|
+
end
|
36
|
+
|
37
|
+
client_config = ClientConfig.new
|
38
|
+
|
39
|
+
client_config.settings.tap do |config|
|
40
|
+
# own settings
|
41
|
+
expect(config.google_api.client_token).to eq('client-test-google-api')
|
42
|
+
expect(config.defaults).to eq(nil)
|
43
|
+
expect(config[:google_api][:client_token]).to eq('client-test-google-api')
|
44
|
+
expect(config[:defaults]).to eq(nil)
|
45
|
+
|
46
|
+
# inherited settings
|
47
|
+
expect(config.google_api.token).to eq('test-google-api')
|
48
|
+
expect(config.admin_access_required).to eq(false)
|
49
|
+
expect(config[:google_api][:token]).to eq('test-google-api')
|
50
|
+
expect(config[:admin_access_required]).to eq(false)
|
51
|
+
|
52
|
+
# inherited composition
|
53
|
+
expect(config.version).to eq('0.1.0')
|
54
|
+
expect(config[:version]).to eq('0.1.0')
|
55
|
+
|
56
|
+
# own composition
|
57
|
+
expect(config.unkown_data).to eq(true)
|
58
|
+
expect(config[:unkown_data]).to eq(true)
|
59
|
+
end
|
60
|
+
|
61
|
+
# hash representation
|
62
|
+
expect(client_config.to_h).to match(
|
63
|
+
'google_api' => {
|
64
|
+
'client_token' => 'client-test-google-api',
|
65
|
+
'token' => 'test-google-api'
|
66
|
+
},
|
67
|
+
'defaults' => nil,
|
68
|
+
'admin_access_required' => false,
|
69
|
+
'version' => '0.1.0',
|
70
|
+
'unkown_data' => true
|
71
|
+
)
|
72
|
+
|
73
|
+
# reconfigure
|
74
|
+
client_config.configure do |config|
|
75
|
+
config.google_api.client_token = 'none'
|
76
|
+
config.defaults = { a: 1 }
|
77
|
+
config.google_api.token = 'anti-hype'
|
78
|
+
config.admin_access_required = true
|
79
|
+
config.version = '0.2.0'
|
80
|
+
config.unkown_data = nil
|
81
|
+
end
|
82
|
+
|
83
|
+
client_config.settings.tap do |config|
|
84
|
+
expect(config.google_api.client_token).to eq('none')
|
85
|
+
expect(config.defaults).to match(a: 1)
|
86
|
+
expect(config.google_api.token).to eq('anti-hype')
|
87
|
+
expect(config.admin_access_required).to eq(true)
|
88
|
+
expect(config.version).to eq('0.2.0')
|
89
|
+
expect(config.unkown_data).to eq(nil)
|
90
|
+
|
91
|
+
expect(config[:google_api][:client_token]).to eq('none')
|
92
|
+
expect(config[:defaults]).to match(a: 1)
|
93
|
+
expect(config[:google_api][:token]).to eq('anti-hype')
|
94
|
+
expect(config[:admin_access_required]).to eq(true)
|
95
|
+
expect(config[:version]).to eq('0.2.0')
|
96
|
+
expect(config[:unkown_data]).to eq(nil)
|
97
|
+
end
|
98
|
+
|
99
|
+
expect(client_config.to_h).to match(
|
100
|
+
'google_api' => {
|
101
|
+
'client_token' => 'none',
|
102
|
+
'token' => 'anti-hype'
|
103
|
+
},
|
104
|
+
'defaults' => { a: 1 },
|
105
|
+
'admin_access_required' => true,
|
106
|
+
'version' => '0.2.0',
|
107
|
+
'unkown_data' => nil
|
108
|
+
)
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe 'Instantiation without class definition' do
|
4
|
+
specify '#build builds a config instance without a class' do
|
5
|
+
config = Qonfig::DataSet.build do
|
6
|
+
setting :credentials do
|
7
|
+
setting :user, 'D@iVeR'
|
8
|
+
setting :password, 'test123'
|
9
|
+
end
|
10
|
+
|
11
|
+
def custom_method(custom_param)
|
12
|
+
custom_param
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
expect(config).to be_a(Qonfig::DataSet)
|
17
|
+
expect(config.settings.credentials.user).to eq('D@iVeR')
|
18
|
+
expect(config.settings.credentials.password).to eq('test123')
|
19
|
+
|
20
|
+
custom_param = rand(1..1000).to_s
|
21
|
+
expect(config.custom_method(custom_param)).to eq(custom_param)
|
22
|
+
end
|
23
|
+
|
24
|
+
specify 'custom Qonfig::DataSet inheritance' do
|
25
|
+
simple_config_klass = Class.new(Qonfig::DataSet) do
|
26
|
+
setting :adapter do
|
27
|
+
setting :engine, :sidekiq
|
28
|
+
setting :options, {}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
config = Qonfig::DataSet.build(simple_config_klass) do
|
33
|
+
setting :credentials do
|
34
|
+
setting :user, '0exp'
|
35
|
+
setting :password, '123test123'
|
36
|
+
end
|
37
|
+
|
38
|
+
setting :adapter do
|
39
|
+
setting :enabled, false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# NOTE: inherited configs
|
44
|
+
expect(config.settings.adapter.engine).to eq(:sidekiq)
|
45
|
+
expect(config.settings.adapter.options).to eq({})
|
46
|
+
|
47
|
+
# NOTE: extended base config
|
48
|
+
expect(config.settings.adapter.enabled).to eq(false)
|
49
|
+
|
50
|
+
# NOTE: own configs
|
51
|
+
expect(config.settings.credentials.user).to eq('0exp')
|
52
|
+
expect(config.settings.credentials.password).to eq('123test123')
|
53
|
+
end
|
54
|
+
|
55
|
+
specify 'custom inheritance fails on non-Qonfig::DataSet base classes' do
|
56
|
+
expect { Qonfig::DataSet.build(Object) }.to raise_error(Qonfig::ArgumentError)
|
57
|
+
expect { Qonfig::DataSet.build(Qonfig::DataSet) }.not_to raise_error
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe 'Iteration over setting keys (#each_setting / #deep_each_setting)' do
|
4
|
+
let(:config) do
|
5
|
+
Class.new(Qonfig::DataSet) do
|
6
|
+
setting :db do
|
7
|
+
setting :creds do
|
8
|
+
setting :user, 'D@iVeR'
|
9
|
+
setting :password, 'test123'
|
10
|
+
setting :data, test: false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
setting :telegraf_url, 'udp://localhost:8094'
|
15
|
+
setting :telegraf_prefix, 'test'
|
16
|
+
end.new
|
17
|
+
end
|
18
|
+
|
19
|
+
specify '#each_setting provides ("key" => value) format' do
|
20
|
+
key_value_pairs = {}.tap do |pairs|
|
21
|
+
config.each_setting do |setting_key, setting_value|
|
22
|
+
pairs[setting_key] = setting_value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
expect(key_value_pairs).to match(
|
27
|
+
'db' => config.settings.db,
|
28
|
+
'telegraf_url' => 'udp://localhost:8094',
|
29
|
+
'telegraf_prefix' => 'test'
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
specify '#deep_each_setting provides ("key.sub_key.subkey" => value) format' do
|
34
|
+
key_value_pairs = {}.tap do |pairs|
|
35
|
+
config.deep_each_setting do |setting_key, setting_value|
|
36
|
+
pairs[setting_key] = setting_value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
expect(key_value_pairs).to match(
|
41
|
+
'db.creds.user' => 'D@iVeR',
|
42
|
+
'db.creds.password' => 'test123',
|
43
|
+
'db.creds.data' => { test: false },
|
44
|
+
'telegraf_url' => 'udp://localhost:8094',
|
45
|
+
'telegraf_prefix' => 'test'
|
46
|
+
)
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,240 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
describe 'Load from ENV' do
|
4
|
+
specify 'defines config object by ENV data' do
|
5
|
+
ENV['QONFIG_SPEC_TRUE_VALUE_V1'] = 'true'
|
6
|
+
ENV['QONFIG_SPEC_TRUE_VALUE_V2'] = 't'
|
7
|
+
ENV['QONFIG_SPEC_TRUE_VALUE_V3'] = 'TRUE'
|
8
|
+
ENV['QONFIG_SPEC_TRUE_VALUE_V4'] = 'T'
|
9
|
+
|
10
|
+
ENV['QONFIG_SPEC_FALSE_VALUE_V1'] = 'false'
|
11
|
+
ENV['QONFIG_SPEC_FALSE_VALUE_V2'] = 'f'
|
12
|
+
ENV['QONFIG_SPEC_FALSE_VALUE_V3'] = 'FALSE'
|
13
|
+
ENV['QONFIG_SPEC_FALSE_VALUE_V4'] = 'F'
|
14
|
+
|
15
|
+
ENV['QONFIG_SPEC_INTEGER_VALUE'] = '1'
|
16
|
+
ENV['QONFIG_SPEC_FLOAT_VALUE'] = '1.55'
|
17
|
+
|
18
|
+
ENV['QONFIG_SPEC_ARRAY_VALUE_V1'] = 'true, t, TRUE ,T , false,f,FALSE,f'
|
19
|
+
ENV['QONFIG_SPEC_ARRAY_VALUE_V2'] = '1, 2, 3.4, 5.6 ,0.0'
|
20
|
+
ENV['QONFIG_SPEC_ARRAY_VALUE_V3'] = 'Qonfig,Settings,DSL'
|
21
|
+
|
22
|
+
ENV['QONFIG_SPEC_QUOTED_STRING_VALUE_V1'] = '"test, please"'
|
23
|
+
ENV['QONFIG_SPEC_QUOTED_STRING_VALUE_V2'] = "'please, test'"
|
24
|
+
|
25
|
+
ENV['QONFIG_SPEC_GENERIC_VALUE_V1'] = '1,66'
|
26
|
+
ENV['QONFIG_SPEC_GENERIC_VALUE_V2'] = '123.456trueTRUEfalseFALSEtTfF.123'
|
27
|
+
|
28
|
+
ENV['NON_QONFIG_SPEC_GENERIC_VALUE'] = 'test'
|
29
|
+
|
30
|
+
class EnvConfig < Qonfig::DataSet
|
31
|
+
setting :default do
|
32
|
+
load_from_env
|
33
|
+
end
|
34
|
+
|
35
|
+
setting :converted do
|
36
|
+
load_from_env convert_values: true
|
37
|
+
end
|
38
|
+
|
39
|
+
setting :prefixed do
|
40
|
+
load_from_env prefix: 'QONFIG_SPEC'
|
41
|
+
end
|
42
|
+
|
43
|
+
setting :prefix_regexp do
|
44
|
+
load_from_env prefix: /\Aqonfig.*\z/i
|
45
|
+
end
|
46
|
+
|
47
|
+
setting :trimmed_prefix do
|
48
|
+
load_from_env prefix: 'QONFIG_SPEC_', trim_prefix: true
|
49
|
+
end
|
50
|
+
|
51
|
+
setting :all_in do
|
52
|
+
load_from_env convert_values: true, prefix: 'QONFIG_SPEC_', trim_prefix: true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
config = EnvConfig.new
|
57
|
+
|
58
|
+
# rubocop:disable Layout/LineLength, Naming/VariableNumber
|
59
|
+
expect(config[:default][:QONFIG_SPEC_TRUE_VALUE_V1]).to eq('true')
|
60
|
+
expect(config[:default][:QONFIG_SPEC_TRUE_VALUE_V2]).to eq('t')
|
61
|
+
expect(config[:default][:QONFIG_SPEC_TRUE_VALUE_V3]).to eq('TRUE')
|
62
|
+
expect(config[:default][:QONFIG_SPEC_TRUE_VALUE_V4]).to eq('T')
|
63
|
+
expect(config[:default][:QONFIG_SPEC_FALSE_VALUE_V1]).to eq('false')
|
64
|
+
expect(config[:default][:QONFIG_SPEC_FALSE_VALUE_V2]).to eq('f')
|
65
|
+
expect(config[:default][:QONFIG_SPEC_FALSE_VALUE_V3]).to eq('FALSE')
|
66
|
+
expect(config[:default][:QONFIG_SPEC_FALSE_VALUE_V4]).to eq('F')
|
67
|
+
expect(config[:default][:QONFIG_SPEC_INTEGER_VALUE]).to eq('1')
|
68
|
+
expect(config[:default][:QONFIG_SPEC_FLOAT_VALUE]).to eq('1.55')
|
69
|
+
expect(config[:default][:QONFIG_SPEC_GENERIC_VALUE_V1]).to eq('1,66')
|
70
|
+
expect(config[:default][:QONFIG_SPEC_GENERIC_VALUE_V2]).to eq('123.456trueTRUEfalseFALSEtTfF.123')
|
71
|
+
expect(config[:default][:QONFIG_SPEC_ARRAY_VALUE_V1]).to eq('true, t, TRUE ,T , false,f,FALSE,f')
|
72
|
+
expect(config[:default][:QONFIG_SPEC_ARRAY_VALUE_V2]).to eq('1, 2, 3.4, 5.6 ,0.0')
|
73
|
+
expect(config[:default][:QONFIG_SPEC_ARRAY_VALUE_V3]).to eq('Qonfig,Settings,DSL')
|
74
|
+
expect(config[:default][:QONFIG_SPEC_QUOTED_STRING_VALUE_V1]).to eq('"test, please"')
|
75
|
+
expect(config[:default][:QONFIG_SPEC_QUOTED_STRING_VALUE_V2]).to eq("'please, test'")
|
76
|
+
expect(config[:default][:NON_QONFIG_SPEC_GENERIC_VALUE]).to eq('test')
|
77
|
+
|
78
|
+
expect(config[:converted][:QONFIG_SPEC_TRUE_VALUE_V1]).to eq(true)
|
79
|
+
expect(config[:converted][:QONFIG_SPEC_TRUE_VALUE_V2]).to eq(true)
|
80
|
+
expect(config[:converted][:QONFIG_SPEC_TRUE_VALUE_V3]).to eq(true)
|
81
|
+
expect(config[:converted][:QONFIG_SPEC_TRUE_VALUE_V4]).to eq(true)
|
82
|
+
expect(config[:converted][:QONFIG_SPEC_FALSE_VALUE_V1]).to eq(false)
|
83
|
+
expect(config[:converted][:QONFIG_SPEC_FALSE_VALUE_V2]).to eq(false)
|
84
|
+
expect(config[:converted][:QONFIG_SPEC_FALSE_VALUE_V3]).to eq(false)
|
85
|
+
expect(config[:converted][:QONFIG_SPEC_FALSE_VALUE_V4]).to eq(false)
|
86
|
+
expect(config[:converted][:QONFIG_SPEC_INTEGER_VALUE]).to eq(1)
|
87
|
+
expect(config[:converted][:QONFIG_SPEC_FLOAT_VALUE]).to eq(1.55)
|
88
|
+
expect(config[:converted][:QONFIG_SPEC_GENERIC_VALUE_V1]).to eq([1, 66])
|
89
|
+
expect(config[:converted][:QONFIG_SPEC_GENERIC_VALUE_V2]).to eq('123.456trueTRUEfalseFALSEtTfF.123')
|
90
|
+
expect(config[:converted][:QONFIG_SPEC_ARRAY_VALUE_V1]).to eq([true, true, true, true, false, false, false, false])
|
91
|
+
expect(config[:converted][:QONFIG_SPEC_ARRAY_VALUE_V2]).to eq([1, 2, 3.4, 5.6, 0.0])
|
92
|
+
expect(config[:converted][:QONFIG_SPEC_ARRAY_VALUE_V3]).to eq(%w[Qonfig Settings DSL])
|
93
|
+
expect(config[:converted][:QONFIG_SPEC_QUOTED_STRING_VALUE_V1]).to eq('test, please')
|
94
|
+
expect(config[:converted][:QONFIG_SPEC_QUOTED_STRING_VALUE_V2]).to eq('please, test')
|
95
|
+
expect(config[:converted][:NON_QONFIG_SPEC_GENERIC_VALUE]).to eq('test')
|
96
|
+
|
97
|
+
expect(config[:prefixed][:QONFIG_SPEC_TRUE_VALUE_V1]).to eq('true')
|
98
|
+
expect(config[:prefixed][:QONFIG_SPEC_TRUE_VALUE_V2]).to eq('t')
|
99
|
+
expect(config[:prefixed][:QONFIG_SPEC_TRUE_VALUE_V3]).to eq('TRUE')
|
100
|
+
expect(config[:prefixed][:QONFIG_SPEC_TRUE_VALUE_V4]).to eq('T')
|
101
|
+
expect(config[:prefixed][:QONFIG_SPEC_FALSE_VALUE_V1]).to eq('false')
|
102
|
+
expect(config[:prefixed][:QONFIG_SPEC_FALSE_VALUE_V2]).to eq('f')
|
103
|
+
expect(config[:prefixed][:QONFIG_SPEC_FALSE_VALUE_V3]).to eq('FALSE')
|
104
|
+
expect(config[:prefixed][:QONFIG_SPEC_FALSE_VALUE_V4]).to eq('F')
|
105
|
+
expect(config[:prefixed][:QONFIG_SPEC_INTEGER_VALUE]).to eq('1')
|
106
|
+
expect(config[:prefixed][:QONFIG_SPEC_FLOAT_VALUE]).to eq('1.55')
|
107
|
+
expect(config[:prefixed][:QONFIG_SPEC_GENERIC_VALUE_V1]).to eq('1,66')
|
108
|
+
expect(config[:prefixed][:QONFIG_SPEC_GENERIC_VALUE_V2]).to eq('123.456trueTRUEfalseFALSEtTfF.123')
|
109
|
+
expect(config[:prefixed][:QONFIG_SPEC_ARRAY_VALUE_V1]).to eq('true, t, TRUE ,T , false,f,FALSE,f')
|
110
|
+
expect(config[:prefixed][:QONFIG_SPEC_ARRAY_VALUE_V2]).to eq('1, 2, 3.4, 5.6 ,0.0')
|
111
|
+
expect(config[:prefixed][:QONFIG_SPEC_ARRAY_VALUE_V3]).to eq('Qonfig,Settings,DSL')
|
112
|
+
expect(config[:prefixed][:QONFIG_SPEC_QUOTED_STRING_VALUE_V1]).to eq('"test, please"')
|
113
|
+
expect(config[:prefixed][:QONFIG_SPEC_QUOTED_STRING_VALUE_V2]).to eq("'please, test'")
|
114
|
+
expect { config[:prefixed][:NON_QONFIG_SPEC_GENERIC_VALUE] }.to raise_error(Qonfig::UnknownSettingError)
|
115
|
+
|
116
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_TRUE_VALUE_V1]).to eq('true')
|
117
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_TRUE_VALUE_V2]).to eq('t')
|
118
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_TRUE_VALUE_V3]).to eq('TRUE')
|
119
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_TRUE_VALUE_V4]).to eq('T')
|
120
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_FALSE_VALUE_V1]).to eq('false')
|
121
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_FALSE_VALUE_V2]).to eq('f')
|
122
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_FALSE_VALUE_V3]).to eq('FALSE')
|
123
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_FALSE_VALUE_V4]).to eq('F')
|
124
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_INTEGER_VALUE]).to eq('1')
|
125
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_FLOAT_VALUE]).to eq('1.55')
|
126
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_GENERIC_VALUE_V1]).to eq('1,66')
|
127
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_GENERIC_VALUE_V2]).to eq('123.456trueTRUEfalseFALSEtTfF.123')
|
128
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_ARRAY_VALUE_V1]).to eq('true, t, TRUE ,T , false,f,FALSE,f')
|
129
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_ARRAY_VALUE_V2]).to eq('1, 2, 3.4, 5.6 ,0.0')
|
130
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_ARRAY_VALUE_V3]).to eq('Qonfig,Settings,DSL')
|
131
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_QUOTED_STRING_VALUE_V1]).to eq('"test, please"')
|
132
|
+
expect(config[:prefix_regexp][:QONFIG_SPEC_QUOTED_STRING_VALUE_V2]).to eq("'please, test'")
|
133
|
+
expect { config[:prefix_regexp][:NON_QONFIG_SPEC_GENERIC_VALUE] }.to raise_error(Qonfig::UnknownSettingError)
|
134
|
+
|
135
|
+
expect(config[:trimmed_prefix][:TRUE_VALUE_V1]).to eq('true')
|
136
|
+
expect(config[:trimmed_prefix][:TRUE_VALUE_V2]).to eq('t')
|
137
|
+
expect(config[:trimmed_prefix][:TRUE_VALUE_V3]).to eq('TRUE')
|
138
|
+
expect(config[:trimmed_prefix][:TRUE_VALUE_V4]).to eq('T')
|
139
|
+
expect(config[:trimmed_prefix][:FALSE_VALUE_V1]).to eq('false')
|
140
|
+
expect(config[:trimmed_prefix][:FALSE_VALUE_V2]).to eq('f')
|
141
|
+
expect(config[:trimmed_prefix][:FALSE_VALUE_V3]).to eq('FALSE')
|
142
|
+
expect(config[:trimmed_prefix][:FALSE_VALUE_V4]).to eq('F')
|
143
|
+
expect(config[:trimmed_prefix][:INTEGER_VALUE]).to eq('1')
|
144
|
+
expect(config[:trimmed_prefix][:FLOAT_VALUE]).to eq('1.55')
|
145
|
+
expect(config[:trimmed_prefix][:GENERIC_VALUE_V1]).to eq('1,66')
|
146
|
+
expect(config[:trimmed_prefix][:GENERIC_VALUE_V2]).to eq('123.456trueTRUEfalseFALSEtTfF.123')
|
147
|
+
expect(config[:trimmed_prefix][:ARRAY_VALUE_V1]).to eq('true, t, TRUE ,T , false,f,FALSE,f')
|
148
|
+
expect(config[:trimmed_prefix][:ARRAY_VALUE_V2]).to eq('1, 2, 3.4, 5.6 ,0.0')
|
149
|
+
expect(config[:trimmed_prefix][:ARRAY_VALUE_V3]).to eq('Qonfig,Settings,DSL')
|
150
|
+
expect(config[:trimmed_prefix][:QUOTED_STRING_VALUE_V1]).to eq('"test, please"')
|
151
|
+
expect(config[:trimmed_prefix][:QUOTED_STRING_VALUE_V2]).to eq("'please, test'")
|
152
|
+
expect { config[:trimmed_prefix][:NON_QONFIG_SPEC_GENERIC_VALUE] }.to raise_error(Qonfig::UnknownSettingError)
|
153
|
+
|
154
|
+
expect(config[:all_in][:TRUE_VALUE_V1]).to eq(true)
|
155
|
+
expect(config[:all_in][:TRUE_VALUE_V2]).to eq(true)
|
156
|
+
expect(config[:all_in][:TRUE_VALUE_V3]).to eq(true)
|
157
|
+
expect(config[:all_in][:TRUE_VALUE_V4]).to eq(true)
|
158
|
+
expect(config[:all_in][:FALSE_VALUE_V1]).to eq(false)
|
159
|
+
expect(config[:all_in][:FALSE_VALUE_V2]).to eq(false)
|
160
|
+
expect(config[:all_in][:FALSE_VALUE_V3]).to eq(false)
|
161
|
+
expect(config[:all_in][:FALSE_VALUE_V4]).to eq(false)
|
162
|
+
expect(config[:all_in][:INTEGER_VALUE]).to eq(1)
|
163
|
+
expect(config[:all_in][:FLOAT_VALUE]).to eq(1.55)
|
164
|
+
expect(config[:all_in][:GENERIC_VALUE_V1]).to eq([1, 66])
|
165
|
+
expect(config[:all_in][:GENERIC_VALUE_V2]).to eq('123.456trueTRUEfalseFALSEtTfF.123')
|
166
|
+
expect(config[:all_in][:ARRAY_VALUE_V1]).to eq([true, true, true, true, false, false, false, false])
|
167
|
+
expect(config[:all_in][:ARRAY_VALUE_V2]).to eq([1, 2, 3.4, 5.6, 0.0])
|
168
|
+
expect(config[:all_in][:ARRAY_VALUE_V3]).to eq(%w[Qonfig Settings DSL])
|
169
|
+
expect(config[:all_in][:QUOTED_STRING_VALUE_V1]).to eq('test, please')
|
170
|
+
expect(config[:all_in][:QUOTED_STRING_VALUE_V2]).to eq('please, test')
|
171
|
+
expect { config[:all_in][:NON_QONFIG_SPEC_GENERIC_VALUE] }.to raise_error(Qonfig::UnknownSettingError)
|
172
|
+
# rubocop:enable Layout/LineLength, Naming/VariableNumber
|
173
|
+
end
|
174
|
+
|
175
|
+
specify 'incorrect definition' do
|
176
|
+
incorrect_convert_value_option_values = [
|
177
|
+
123, '123', 'true', 't', 'false', 'f', Object.new, 123.5, Class.new
|
178
|
+
]
|
179
|
+
|
180
|
+
incorrect_prefix_option_values = [
|
181
|
+
123, Object.new, 123.5, true, false, Class.new
|
182
|
+
]
|
183
|
+
|
184
|
+
incorrect_trim_prefix_option_values = [
|
185
|
+
123, '123', 'true', 't', 'false', 'f', Object.new, 123.5, Class.new
|
186
|
+
]
|
187
|
+
|
188
|
+
correct_convert_value_option_values = [true, false]
|
189
|
+
correct_prefix_option_values = [nil, 'qonfig', /\Aqonfig.*\z/i]
|
190
|
+
correct_trim_prefix_option_values = [true, false]
|
191
|
+
|
192
|
+
incorrect_convert_value_option_values.each do |incorrect_option|
|
193
|
+
expect do
|
194
|
+
Class.new(Qonfig::DataSet) do
|
195
|
+
load_from_env convert_values: incorrect_option
|
196
|
+
end
|
197
|
+
end.to raise_error(Qonfig::ArgumentError)
|
198
|
+
end
|
199
|
+
|
200
|
+
incorrect_prefix_option_values.each do |incorrect_option|
|
201
|
+
expect do
|
202
|
+
Class.new(Qonfig::DataSet) do
|
203
|
+
load_from_env prefix: incorrect_option
|
204
|
+
end
|
205
|
+
end.to raise_error(Qonfig::ArgumentError)
|
206
|
+
end
|
207
|
+
|
208
|
+
incorrect_trim_prefix_option_values.each do |incorrect_option|
|
209
|
+
expect do
|
210
|
+
Class.new(Qonfig::DataSet) do
|
211
|
+
load_from_env trim_prefix: incorrect_option
|
212
|
+
end
|
213
|
+
end.to raise_error(Qonfig::ArgumentError)
|
214
|
+
end
|
215
|
+
|
216
|
+
correct_convert_value_option_values.each do |correct_option|
|
217
|
+
expect do
|
218
|
+
Class.new(Qonfig::DataSet) do
|
219
|
+
load_from_env convert_values: correct_option
|
220
|
+
end
|
221
|
+
end.not_to raise_error
|
222
|
+
end
|
223
|
+
|
224
|
+
correct_prefix_option_values.each do |correct_option|
|
225
|
+
expect do
|
226
|
+
Class.new(Qonfig::DataSet) do
|
227
|
+
load_from_env prefix: correct_option
|
228
|
+
end
|
229
|
+
end.not_to raise_error
|
230
|
+
end
|
231
|
+
|
232
|
+
correct_trim_prefix_option_values.each do |correct_option|
|
233
|
+
expect do
|
234
|
+
Class.new(Qonfig::DataSet) do
|
235
|
+
load_from_env trim_prefix: correct_option
|
236
|
+
end
|
237
|
+
end.not_to raise_error
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|