qonfig 0.25.0 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -2
  3. data/.rubocop.yml +19 -1
  4. data/.travis.yml +7 -2
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile.lock +102 -0
  7. data/README.md +2 -0
  8. data/gemfiles/with_external_deps.gemfile.lock +112 -0
  9. data/gemfiles/without_external_deps.gemfile.lock +102 -0
  10. data/lib/qonfig.rb +1 -1
  11. data/lib/qonfig/commands/definition/load_from_env.rb +2 -0
  12. data/lib/qonfig/commands/definition/load_from_env/value_converter.rb +2 -0
  13. data/lib/qonfig/imports/direct_key.rb +8 -2
  14. data/lib/qonfig/imports/mappings.rb +8 -2
  15. data/lib/qonfig/loaders/basic.rb +2 -0
  16. data/lib/qonfig/loaders/json.rb +2 -1
  17. data/lib/qonfig/settings.rb +5 -1
  18. data/lib/qonfig/settings/key_matcher.rb +1 -1
  19. data/lib/qonfig/uploaders/base.rb +2 -0
  20. data/lib/qonfig/uploaders/yaml.rb +2 -0
  21. data/lib/qonfig/version.rb +1 -1
  22. data/qonfig.gemspec +4 -6
  23. data/sig/.keep +0 -0
  24. data/spec/features/clear_options_spec.rb +92 -0
  25. data/spec/features/compacted_config_spec.rb +308 -0
  26. data/spec/features/composition_spec.rb +207 -0
  27. data/spec/features/config_definition_and_representation_spec.rb +535 -0
  28. data/spec/features/definition_order_spec.rb +69 -0
  29. data/spec/features/dig_functionality_spec.rb +47 -0
  30. data/spec/features/dot_notation_spec.rb +159 -0
  31. data/spec/features/export_settings_spec.rb +138 -0
  32. data/spec/features/expose_json_spec.rb +281 -0
  33. data/spec/features/expose_self/format_option_dynamic_spec.rb +69 -0
  34. data/spec/features/expose_self/format_option_json_spec.rb +74 -0
  35. data/spec/features/expose_self/format_option_unsupported_spec.rb +27 -0
  36. data/spec/features/expose_self/format_option_yaml_spec.rb +77 -0
  37. data/spec/features/expose_self_spec.rb +97 -0
  38. data/spec/features/expose_yaml_spec.rb +263 -0
  39. data/spec/features/freeze_state_spec.rb +122 -0
  40. data/spec/features/get_config_keys_spec.rb +62 -0
  41. data/spec/features/get_config_values_spec.rb +41 -0
  42. data/spec/features/has_a_key_spec.rb +48 -0
  43. data/spec/features/import_settings_spec.rb +323 -0
  44. data/spec/features/indifferent_access_spec.rb +57 -0
  45. data/spec/features/inheritance_spec.rb +110 -0
  46. data/spec/features/instantiation_without_class_definition_spec.rb +59 -0
  47. data/spec/features/iteration_over_setting_keys_spec.rb +48 -0
  48. data/spec/features/load_from_env_spec.rb +240 -0
  49. data/spec/features/load_from_json_spec.rb +97 -0
  50. data/spec/features/load_from_self/format_option_json_spec.rb +31 -0
  51. data/spec/features/load_from_self/format_option_unsupported_spec.rb +27 -0
  52. data/spec/features/load_from_self/format_option_yaml_spec.rb +49 -0
  53. data/spec/features/load_from_self/with_erb_instructions_spec.rb +33 -0
  54. data/spec/features/load_from_self/with_hash_like_data_representation_spec.rb +66 -0
  55. data/spec/features/load_from_self/with_non_hash_like_data_representation_spec.rb +19 -0
  56. data/spec/features/load_from_self/without_end_data_spec.rb +11 -0
  57. data/spec/features/load_from_yaml_spec.rb +110 -0
  58. data/spec/features/load_setting_values_from_file/by_instance_method_examples.rb +171 -0
  59. data/spec/features/load_setting_values_from_file/by_macros_examples.rb +165 -0
  60. data/spec/features/load_setting_values_from_file/load_from_json_spec.rb +21 -0
  61. data/spec/features/load_setting_values_from_file/load_from_self/json_format/end_data_with_env_spec.rb +100 -0
  62. data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_end_data_spec.rb +129 -0
  63. data/spec/features/load_setting_values_from_file/load_from_self/json_format/with_incorrect_end_data_spec.rb +34 -0
  64. data/spec/features/load_setting_values_from_file/load_from_self/json_format/without_end_data_spec.rb +65 -0
  65. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/end_data_with_env_spec.rb +94 -0
  66. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_end_data_spec.rb +126 -0
  67. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/with_incorrect_end_data_spec.rb +32 -0
  68. data/spec/features/load_setting_values_from_file/load_from_self/yaml_format/without_end_data_spec.rb +65 -0
  69. data/spec/features/load_setting_values_from_file/load_from_yaml_spec.rb +21 -0
  70. data/spec/features/load_setting_values_from_file/shared_behavior_spec.rb +33 -0
  71. data/spec/features/mixin_spec.rb +387 -0
  72. data/spec/features/non_redefineable_core_methods_spec.rb +29 -0
  73. data/spec/features/plugins/pretty_print_spec.rb +86 -0
  74. data/spec/features/plugins/toml/expose_self/format_option_toml_spec.rb +71 -0
  75. data/spec/features/plugins/toml/expose_toml_spec.rb +221 -0
  76. data/spec/features/plugins/toml/load_from_self/format_option_toml_spec.rb +27 -0
  77. data/spec/features/plugins/toml/load_from_toml_spec.rb +109 -0
  78. data/spec/features/plugins/toml/load_setting_values_from_file/load_from_toml_spec.rb +27 -0
  79. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/end_data_with_env_spec.rb +95 -0
  80. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_end_data_spec.rb +125 -0
  81. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/with_incorrect_end_data_spec.rb +34 -0
  82. data/spec/features/plugins/toml/load_setting_values_from_file/load_toml_from_self/without_end_data_spec.rb +65 -0
  83. data/spec/features/plugins/toml/load_setting_values_from_file/shared_behavior_spec.rb +34 -0
  84. data/spec/features/plugins/toml/save_to_toml_spec.rb +149 -0
  85. data/spec/features/plugins/vault/expose_vault_spec.rb +117 -0
  86. data/spec/features/plugins/vault/load_from_vault_spec.rb +80 -0
  87. data/spec/features/plugins_spec.rb +89 -0
  88. data/spec/features/reload_spec.rb +75 -0
  89. data/spec/features/run_code_with_temporary_settings_spec.rb +104 -0
  90. data/spec/features/save_to_file/save_to_json_spec.rb +157 -0
  91. data/spec/features/save_to_file/save_to_yaml_spec.rb +189 -0
  92. data/spec/features/settings_as_predicates_spec.rb +47 -0
  93. data/spec/features/settings_redefinition_spec.rb +30 -0
  94. data/spec/features/slice_functionality_spec.rb +69 -0
  95. data/spec/features/subset_functionality_spec.rb +49 -0
  96. data/spec/features/validation_spec.rb +916 -0
  97. data/spec/fixtures/array_settings.yml +3 -0
  98. data/spec/fixtures/expose_json/incompatible_root_structure.json +6 -0
  99. data/spec/fixtures/expose_json/incompatible_structure.json +4 -0
  100. data/spec/fixtures/expose_json/project.development.json +7 -0
  101. data/spec/fixtures/expose_json/project.json +30 -0
  102. data/spec/fixtures/expose_json/project.production.json +7 -0
  103. data/spec/fixtures/expose_json/project.staging.json +7 -0
  104. data/spec/fixtures/expose_json/project.test.json +7 -0
  105. data/spec/fixtures/expose_yaml/incompatible_structure.yml +2 -0
  106. data/spec/fixtures/expose_yaml/project.development.yml +4 -0
  107. data/spec/fixtures/expose_yaml/project.production.yml +4 -0
  108. data/spec/fixtures/expose_yaml/project.staging.yml +4 -0
  109. data/spec/fixtures/expose_yaml/project.test.yml +4 -0
  110. data/spec/fixtures/expose_yaml/project.yml +25 -0
  111. data/spec/fixtures/json_array_sample.json +14 -0
  112. data/spec/fixtures/json_object_sample.json +9 -0
  113. data/spec/fixtures/json_with_empty_object.json +6 -0
  114. data/spec/fixtures/json_with_erb.json +6 -0
  115. data/spec/fixtures/plugins/toml/expose_toml/project.development.toml +4 -0
  116. data/spec/fixtures/plugins/toml/expose_toml/project.production.toml +4 -0
  117. data/spec/fixtures/plugins/toml/expose_toml/project.staging.toml +4 -0
  118. data/spec/fixtures/plugins/toml/expose_toml/project.test.toml +4 -0
  119. data/spec/fixtures/plugins/toml/expose_toml/project.toml +27 -0
  120. data/spec/fixtures/plugins/toml/mini_file.toml +6 -0
  121. data/spec/fixtures/plugins/toml/toml_sample_with_all_types.toml +72 -0
  122. data/spec/fixtures/plugins/toml/values_file/with_env.toml +13 -0
  123. data/spec/fixtures/plugins/toml/values_file/without_env.toml +6 -0
  124. data/spec/fixtures/rubocop_settings.yml +12 -0
  125. data/spec/fixtures/shared_settings_with_aliases.yml +10 -0
  126. data/spec/fixtures/travis_settings.yml +6 -0
  127. data/spec/fixtures/values_file/with_env.json +19 -0
  128. data/spec/fixtures/values_file/with_env.yml +17 -0
  129. data/spec/fixtures/values_file/without_env.json +8 -0
  130. data/spec/fixtures/values_file/without_env.yml +5 -0
  131. data/spec/fixtures/with_empty_hash.yml +4 -0
  132. data/spec/fixtures/with_erb_instructions.yml +3 -0
  133. data/spec/spec_helper.rb +27 -0
  134. data/spec/support/meta_scopes.rb +20 -0
  135. data/spec/support/spec_support.rb +42 -0
  136. metadata +128 -11
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Load from JSON' do
4
+ specify 'defines config object by json instructions' do
5
+ class JSONBasedConfig < Qonfig::DataSet
6
+ load_from_json SpecSupport.fixture_path('json_object_sample.json')
7
+
8
+ setting :nested do
9
+ load_from_json SpecSupport.fixture_path('json_object_sample.json')
10
+ end
11
+
12
+ setting :with_empty_objects do
13
+ load_from_json SpecSupport.fixture_path('json_with_empty_object.json')
14
+ end
15
+
16
+ setting :with_erb do
17
+ load_from_json SpecSupport.fixture_path('json_with_erb.json')
18
+ end
19
+ end
20
+
21
+ JSONBasedConfig.new.settings.tap do |conf|
22
+ expect(conf.user).to eq('D@iVeR')
23
+ expect(conf.maxAuthCount).to eq(55)
24
+ expect(conf.rubySettings.allowedVersions).to eq(['2.3', '2.4.2', '1.9.8'])
25
+ expect(conf.rubySettings.gitLink).to eq(nil)
26
+ expect(conf.rubySettings.withAdditionals).to eq(false)
27
+
28
+ expect(conf.nested.user).to eq('D@iVeR')
29
+ expect(conf.nested.maxAuthCount).to eq(55)
30
+ expect(conf.nested.rubySettings.allowedVersions).to eq(['2.3', '2.4.2', '1.9.8'])
31
+ expect(conf.nested.rubySettings.gitLink).to eq(nil)
32
+ expect(conf.nested.rubySettings.withAdditionals).to eq(false)
33
+
34
+ expect(conf.with_empty_objects.requirements).to eq({})
35
+ expect(conf.with_empty_objects.credentials.excluded).to eq({})
36
+
37
+ expect(conf.with_erb.count).to eq(10_000)
38
+ expect(conf.with_erb.credentials.excluded).to eq("some string here")
39
+ end
40
+ end
41
+
42
+ specify 'fails when json object has non-hash-like structure' do
43
+ class IncompatibleJSONConfig < Qonfig::DataSet
44
+ load_from_json SpecSupport.fixture_path('json_array_sample.json')
45
+ end
46
+
47
+ expect { IncompatibleJSONConfig.new }.to raise_error(Qonfig::IncompatibleJSONStructureError)
48
+ end
49
+
50
+ specify 'support for Pathname in file path' do
51
+ class PathanmeJSONLoadCheckConfig < Qonfig::DataSet
52
+ load_from_json Pathname.new(SpecSupport.fixture_path('json_object_sample.json'))
53
+ end
54
+
55
+ config = PathanmeJSONLoadCheckConfig.new
56
+
57
+ expect(config.settings.user).to eq('D@iVeR')
58
+ expect(config.settings.maxAuthCount).to eq(55)
59
+ expect(config.settings.rubySettings.allowedVersions).to eq(['2.3', '2.4.2', '1.9.8'])
60
+ expect(config.settings.rubySettings.gitLink).to eq(nil)
61
+ expect(config.settings.rubySettings.withAdditionals).to eq(false)
62
+ end
63
+
64
+ describe ':strict mode option (when file doesnt exist)' do
65
+ context 'when :strict => true (by default)' do
66
+ specify 'fails with corresponding error' do
67
+ # check default behaviour (strict: true)
68
+ class FailingJSONConfig < Qonfig::DataSet
69
+ load_from_json 'no_file.json'
70
+ end
71
+
72
+ expect { FailingJSONConfig.new }.to raise_error(Qonfig::FileNotFoundError)
73
+
74
+ class ExplicitlyStrictedJSONCOnfig < Qonfig::DataSet
75
+ load_from_json 'no_file.json', strict: true
76
+ end
77
+
78
+ expect { ExplicitlyStrictedJSONCOnfig.new }.to raise_error(Qonfig::FileNotFoundError)
79
+ end
80
+ end
81
+
82
+ context 'when :strict => false' do
83
+ specify 'does not fail - empty config' do
84
+ class NonFailingJSONConfig < Qonfig::DataSet
85
+ load_from_json 'no_file.json', strict: false
86
+
87
+ setting :nested do
88
+ load_from_json 'no_file.json', strict: false
89
+ end
90
+ end
91
+
92
+ expect { NonFailingJSONConfig.new }.not_to raise_error
93
+ expect(NonFailingJSONConfig.new.to_h).to eq('nested' => {})
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe '#load_from_self => format: :json' do
4
+ specify ':json-format support' do
5
+ class JsonEndDataConfig < Qonfig::DataSet
6
+ load_from_self format: :json
7
+ end
8
+
9
+ config = JsonEndDataConfig.new
10
+
11
+ expect(config.settings.credentials.user).to eq('admin')
12
+ expect(config.settings.credentials.password).to eq('123')
13
+ expect(config.settings.credentials.enabled).to eq(true)
14
+ expect(config.settings.amount).to eq(123.456)
15
+ expect(config.settings.data).to eq(nil)
16
+ expect(config.settings.methods).to eq([1, 'test', nil, true, false, ['data']])
17
+ end
18
+ end
19
+
20
+ __END__
21
+
22
+ {
23
+ "credentials": {
24
+ "user": "admin",
25
+ "password": "123",
26
+ "enabled": true
27
+ },
28
+ "amount": 123.456,
29
+ "data": null,
30
+ "methods": [1, "test", null, true, false, ["data"]]
31
+ }
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe '#load_from_self => format: <unsupported>' do
4
+ specify 'fails on unsupported formats' do
5
+ expect do
6
+ Class.new(Qonfig::DataSet) do
7
+ load_from_self format: :atata
8
+ end
9
+ end.to raise_error(Qonfig::UnsupportedLoaderFormatError)
10
+ end
11
+
12
+ specify 'fails on empty format parameter' do
13
+ expect do
14
+ Class.new(Qonfig::DataSet) do
15
+ load_from_self format: ''
16
+ end
17
+ end.to raise_error(Qonfig::UnsupportedLoaderFormatError)
18
+ end
19
+
20
+ specify 'fails on incorrect format parameter' do
21
+ expect do
22
+ Class.new(Qonfig::DataSet) do
23
+ load_from_self format: Object.new
24
+ end
25
+ end.to raise_error(Qonfig::ArgumentError)
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe '#load_from_self => format: :yaml' do
4
+ specify ':yaml/:yml-format support' do
5
+ class YamlEndDataConfig < Qonfig::DataSet
6
+ setting :by_yml do
7
+ load_from_self format: :yml # the same as :yaml
8
+ end
9
+
10
+ setting :by_yaml do
11
+ load_from_self format: :yaml # the same as :yml
12
+ end
13
+ end
14
+
15
+ config = YamlEndDataConfig.new
16
+
17
+ # NOTE: format: :yml
18
+ expect(config.settings.by_yml.credentials.user).to eq('admin')
19
+ expect(config.settings.by_yml.credentials.password).to eq('123')
20
+ expect(config.settings.by_yml.credentials.enabled).to eq(true)
21
+ expect(config.settings.by_yml.amount).to eq(123.456)
22
+ expect(config.settings.by_yml.data).to eq(nil)
23
+ expect(config.settings.by_yml.methods).to eq([1, 'test', nil, true, false, ['data']])
24
+
25
+ # NOTE: format: :yaml
26
+ expect(config.settings.by_yaml.credentials.user).to eq('admin')
27
+ expect(config.settings.by_yaml.credentials.password).to eq('123')
28
+ expect(config.settings.by_yaml.credentials.enabled).to eq(true)
29
+ expect(config.settings.by_yaml.amount).to eq(123.456)
30
+ expect(config.settings.by_yaml.data).to eq(nil)
31
+ expect(config.settings.by_yaml.methods).to eq([1, 'test', nil, true, false, ['data']])
32
+ end
33
+ end
34
+
35
+ __END__
36
+
37
+ credentials:
38
+ user: 'admin'
39
+ password: '123'
40
+ enabled: true
41
+ amount: 123.456
42
+ data: ~
43
+ methods:
44
+ - 1
45
+ - 'test'
46
+ - ~
47
+ - true
48
+ - false
49
+ - ['data']
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Load from self (hash-like __END__ data representation with ERB inserts)' do
4
+ specify 'defines config object by self-contained __END__ yaml data with ERB inserts' do
5
+ class SelfDefinedWithErbConfig < Qonfig::DataSet
6
+ load_from_self
7
+ end
8
+
9
+ SelfDefinedWithErbConfig.new.settings.tap do |conf|
10
+ expect(conf.defaults.host).to eq('localhost')
11
+ expect(conf.defaults.user).to eq('0exp')
12
+ expect(conf.defaults.password).to eq('password4')
13
+ expect(conf.defaults.credentials).to eq({})
14
+
15
+ expect(conf.staging.host).to eq('yandex.ru')
16
+ expect(conf.staging.user).to eq('0exp')
17
+ expect(conf.staging.password).to eq('password4')
18
+ expect(conf.staging.credentials).to eq({})
19
+ end
20
+ end
21
+ end
22
+
23
+ __END__
24
+
25
+ defaults: &defaults
26
+ host: localhost
27
+ user: <%= '0exp' %>
28
+ password: <%= "password#{(2 * 2)}" %>
29
+ credentials: <%= {} %>
30
+
31
+ staging:
32
+ <<: *defaults
33
+ host: yandex.ru
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Load from self (hash-like __END__ data representation)' do
4
+ specify 'defines config object by self-contained __END__ yaml data' do
5
+ class SelfDefinedConfig < Qonfig::DataSet
6
+ load_from_self # yaml format will be automatically resolved
7
+
8
+ setting :with_nesting do
9
+ load_from_self
10
+ end
11
+ end
12
+
13
+ SelfDefinedConfig.new.settings.tap do |conf|
14
+ # access via method
15
+ expect(conf.secret_key).to eq('top-mega-secret')
16
+ expect(conf.api_host).to eq('super.puper-google.com')
17
+ expect(conf.requirements).to eq({})
18
+ expect(conf.connection_timeout.seconds).to eq(10)
19
+ expect(conf.connection_timeout.enabled).to eq(false)
20
+ expect(conf.defaults.port).to eq(12_345)
21
+ expect(conf.defaults.host).to eq('localhost')
22
+ expect(conf.staging.port).to eq(12_345)
23
+ expect(conf.staging.host).to eq('google.kek')
24
+
25
+ # access via index
26
+ expect(conf['secret_key']).to eq('top-mega-secret')
27
+ expect(conf['api_host']).to eq('super.puper-google.com')
28
+ expect(conf['requirements']).to eq({})
29
+ expect(conf[:connection_timeout]['seconds']).to eq(10)
30
+ expect(conf[:connection_timeout]['enabled']).to eq(false)
31
+ expect(conf['defaults']['port']).to eq(12_345)
32
+ expect(conf['defaults']['host']).to eq('localhost')
33
+ expect(conf['staging']['port']).to eq(12_345)
34
+ expect(conf['staging']['host']).to eq('google.kek')
35
+
36
+ expect(conf.with_nesting.secret_key).to eq('top-mega-secret')
37
+ expect(conf.with_nesting.api_host).to eq('super.puper-google.com')
38
+ expect(conf.with_nesting.connection_timeout.seconds).to eq(10)
39
+ expect(conf.with_nesting.connection_timeout.enabled).to eq(false)
40
+ expect(conf.with_nesting.requirements).to eq({})
41
+
42
+ expect(conf[:with_nesting]['secret_key']).to eq('top-mega-secret')
43
+ expect(conf[:with_nesting]['api_host']).to eq('super.puper-google.com')
44
+ expect(conf[:with_nesting][:requirements]).to eq({})
45
+ expect(conf[:with_nesting][:connection_timeout]['seconds']).to eq(10)
46
+ expect(conf[:with_nesting][:connection_timeout]['enabled']).to eq(false)
47
+ end
48
+ end
49
+ end
50
+
51
+ __END__
52
+
53
+ defaults: &defaults
54
+ port: 12345
55
+ host: 'localhost'
56
+
57
+ staging:
58
+ <<: *defaults
59
+ host: 'google.kek'
60
+
61
+ secret_key: top-mega-secret
62
+ api_host: super.puper-google.com
63
+ requirements: {}
64
+ :connection_timeout:
65
+ seconds: 10
66
+ enabled: false
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Load from self (non-hash-like __END__ data representation)' do
4
+ specify 'fails when yaml data is not represented as a hash' do
5
+ class IncompatibleSelfDataConfig < Qonfig::DataSet
6
+ load_from_self
7
+ end
8
+
9
+ expect { IncompatibleSelfDataConfig.new }.to raise_error(
10
+ Qonfig::IncompatibleEndDataStructureError
11
+ )
12
+ end
13
+ end
14
+
15
+ __END__
16
+
17
+ - user
18
+ - password
19
+ - 123
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Load from self (without __END__ data)' do
4
+ specify 'fails when yaml data is not represented as a hash' do
5
+ class MissingSelfDataConfig < Qonfig::DataSet
6
+ load_from_self
7
+ end
8
+
9
+ expect { MissingSelfDataConfig.new }.to raise_error(Qonfig::SelfDataNotFoundError)
10
+ end
11
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe 'Load from YAML' do
4
+ specify 'defines config object by yaml instructions' do
5
+ class CISettings < Qonfig::DataSet
6
+ load_from_yaml SpecSupport.fixture_path('shared_settings_with_aliases.yml')
7
+
8
+ setting :travis do
9
+ load_from_yaml SpecSupport.fixture_path('travis_settings.yml')
10
+ end
11
+
12
+ setting :rubocop do
13
+ load_from_yaml SpecSupport.fixture_path('rubocop_settings.yml')
14
+ end
15
+
16
+ setting :with_erb do
17
+ load_from_yaml SpecSupport.fixture_path('with_erb_instructions.yml')
18
+ end
19
+
20
+ setting :with_empty_hash do
21
+ load_from_yaml SpecSupport.fixture_path('with_empty_hash.yml')
22
+ end
23
+ end
24
+
25
+ CISettings.new.settings.tap do |conf|
26
+ # shared_settings_with_aliases.yml
27
+ expect(conf.enable_api).to eq(false)
28
+ expect(conf.run_sidekiq).to eq(true)
29
+ expect(conf.default.test).to eq(true)
30
+ expect(conf.default.engine).to eq('rspec')
31
+ expect(conf.staging.test).to eq(true)
32
+ expect(conf.staging.engine).to eq('minitest')
33
+
34
+ # travis_settings.yml
35
+ expect(conf.travis.language).to eq('ruby')
36
+ expect(conf.travis.rvm).to contain_exactly('2.5.1', 'ruby-head', 'jruby-head')
37
+ expect(conf.travis.sudo).to eq(false)
38
+
39
+ # rubocop_settings.yml
40
+ expect(conf['rubocop']['require']).to eq('rubocop-rspec')
41
+ expect(conf['rubocop']['AllCops']['Include']).to contain_exactly('lib/**/*', 'spec/**/*')
42
+ expect(conf['rubocop']['AllCops']['Exclude']).to contain_exactly('bin/**/*', 'Gemfile')
43
+ expect(conf['rubocop']['Metrics/LineLength']['Max']).to eq(100)
44
+
45
+ # with_erb_instructions.yml
46
+ expect(conf['with_erb']['user']).to eq('D@iVeR')
47
+ expect(conf['with_erb']['max_auth_count']).to eq(2)
48
+ expect(conf['with_erb']['ruby_version']).to eq(RUBY_VERSION)
49
+
50
+ # with_empty_hash.yml
51
+ expect(conf['with_empty_hash']['settings']).to eq({})
52
+ expect(conf['with_empty_hash']['another_settings']['option_a']).to eq({})
53
+ expect(conf['with_empty_hash']['another_settings']['option_b']).to eq(1)
54
+ end
55
+ end
56
+
57
+ specify 'fails when yaml settings is not represented as a hash' do
58
+ class IncompatibleYAMLConfig < Qonfig::DataSet
59
+ load_from_yaml SpecSupport.fixture_path('array_settings.yml')
60
+ end
61
+
62
+ expect { IncompatibleYAMLConfig.new }.to raise_error(Qonfig::IncompatibleYAMLStructureError)
63
+ end
64
+
65
+ specify 'support for Pathname in file path' do
66
+ class PathnameYAMLLoadCheck < Qonfig::DataSet
67
+ load_from_yaml Pathname.new(SpecSupport.fixture_path('travis_settings.yml'))
68
+ end
69
+
70
+ config = PathnameYAMLLoadCheck.new
71
+
72
+ expect(config.settings.sudo).to eq(false)
73
+ expect(config.settings.language).to eq('ruby')
74
+ expect(config.settings.rvm).to contain_exactly('2.5.1', 'ruby-head', 'jruby-head')
75
+ end
76
+
77
+ describe ':strict mode option (when file does not exist)' do
78
+ context 'when :strict => true (by default)' do
79
+ specify 'fails with corresponding error' do
80
+ # check default behaviour (strict: true)
81
+ class FailingYAMLConfig < Qonfig::DataSet
82
+ load_from_yaml 'no_file.yml'
83
+ end
84
+
85
+ expect { FailingYAMLConfig.new }.to raise_error(Qonfig::FileNotFoundError)
86
+
87
+ class ExplicitlyStrictedYAMLConfig < Qonfig::DataSet
88
+ load_from_yaml 'no_file.yml', strict: true
89
+ end
90
+
91
+ expect { ExplicitlyStrictedYAMLConfig.new }.to raise_error(Qonfig::FileNotFoundError)
92
+ end
93
+ end
94
+
95
+ context 'when :strict => false' do
96
+ specify 'does not fail - empty config' do
97
+ class NonFailingYAMLConfig < Qonfig::DataSet
98
+ load_from_yaml 'no_file.yml', strict: false
99
+
100
+ setting :nested do
101
+ load_from_yaml 'no_file.yml', strict: false
102
+ end
103
+ end
104
+
105
+ expect { NonFailingYAMLConfig.new }.not_to raise_error
106
+ expect(NonFailingYAMLConfig.new.to_h).to eq('nested' => {})
107
+ end
108
+ end
109
+ end
110
+ end