config_files 0.1.7 → 0.2.2

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 (45) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +111 -0
  3. data/.gitignore +2 -5
  4. data/.rubocop.yml +81 -0
  5. data/CHANGELOG.md +166 -0
  6. data/Gemfile +12 -1
  7. data/MULTI_RUBY_SETUP.md +160 -0
  8. data/README.md +260 -23
  9. data/Rakefile +26 -3
  10. data/TESTING.md +226 -0
  11. data/config_files.gemspec +12 -9
  12. data/docker/Dockerfile.test +32 -0
  13. data/lib/config_files/file_factory.rb +7 -3
  14. data/lib/config_files/loader_factory.rb +35 -11
  15. data/lib/config_files/loaders/base_parser.rb +133 -0
  16. data/lib/config_files/loaders/conf.rb +61 -0
  17. data/lib/config_files/loaders/default.rb +3 -1
  18. data/lib/config_files/loaders/ini.rb +48 -0
  19. data/lib/config_files/loaders/json.rb +3 -1
  20. data/lib/config_files/loaders/xml.rb +67 -0
  21. data/lib/config_files/loaders/yaml.rb +12 -1
  22. data/lib/config_files/loaders.rb +6 -1
  23. data/lib/config_files/version.rb +3 -1
  24. data/lib/config_files.rb +52 -17
  25. data/lib/meta.rb +3 -1
  26. data/scripts/install_rubies_asdf.sh +187 -0
  27. data/scripts/test_docker.sh +96 -0
  28. data/scripts/test_multiple_rubies.sh +295 -0
  29. data/test/comprehensive_multi_directory_test.rb +168 -0
  30. data/test/config/dummy.json +10 -0
  31. data/test/config/dummy.yml +6 -0
  32. data/test/config_files_test.rb +8 -6
  33. data/test/etc/dummy.conf +14 -2
  34. data/test/etc/dummy.ini +12 -0
  35. data/test/etc/dummy.xml +13 -0
  36. data/test/load_robustness_test.rb +87 -0
  37. data/test/loader_factory_test.rb +10 -10
  38. data/test/loaders_test.rb +362 -0
  39. data/test/local/dummy.json +10 -0
  40. data/test/local/dummy.yml +6 -0
  41. data/test/mixed_format_test.rb +152 -0
  42. data/test/multi_directory_test.rb +126 -0
  43. data/test/stray_files_test.rb +72 -0
  44. data/test/test_helper.rb +3 -0
  45. metadata +45 -25
@@ -0,0 +1,168 @@
1
+ $LOAD_PATH.push(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'minitest/autorun'
3
+ require 'config_files'
4
+ require 'fileutils'
5
+
6
+ class ComprehensiveMultiDirectoryTest < Minitest::Test
7
+ def setup
8
+ # Create a comprehensive test scenario with multiple directories and formats
9
+ FileUtils.mkdir_p('test/comprehensive/global')
10
+ FileUtils.mkdir_p('test/comprehensive/environment')
11
+ FileUtils.mkdir_p('test/comprehensive/local')
12
+
13
+ # Global config (YAML)
14
+ File.write('test/comprehensive/global/app.yml', <<~YAML)
15
+ database:
16
+ host: global-db.example.com
17
+ port: 5432
18
+ pool_size: 10
19
+ app:
20
+ name: MyApp
21
+ version: 1.0.0
22
+ debug: false
23
+ features:
24
+ feature_a: true
25
+ feature_b: false
26
+ global_setting: true
27
+ YAML
28
+
29
+ # Environment config (JSON)
30
+ File.write('test/comprehensive/environment/app.json', <<~JSON)
31
+ {
32
+ "database": {
33
+ "host": "staging-db.example.com",
34
+ "username": "staging_user",
35
+ "password": "staging_pass"
36
+ },
37
+ "app": {
38
+ "debug": true,
39
+ "log_level": "debug"
40
+ },
41
+ "features": {
42
+ "feature_b": true,
43
+ "feature_c": true
44
+ },
45
+ "environment_setting": "staging"
46
+ }
47
+ JSON
48
+
49
+ # Local config (both YAML and JSON to test within-directory merging)
50
+ File.write('test/comprehensive/local/app.yml', <<~YAML)
51
+ database:
52
+ host: localhost
53
+ port: 3306
54
+ app:
55
+ debug: false
56
+ local_override: true
57
+ local_yaml_setting: true
58
+ YAML
59
+
60
+ File.write('test/comprehensive/local/app.json', <<~JSON)
61
+ {
62
+ "database": {
63
+ "host": "127.0.0.1",
64
+ "ssl": true
65
+ },
66
+ "app": {
67
+ "environment": "development"
68
+ },
69
+ "local_json_setting": true
70
+ }
71
+ JSON
72
+ end
73
+
74
+ def teardown
75
+ FileUtils.rm_rf('test/comprehensive')
76
+ end
77
+
78
+ def test_comprehensive_multi_directory_multi_format_merging
79
+ # Create test class with all directories
80
+ comprehensive_class = Class.new do
81
+ include ConfigFiles
82
+ config_directories etc: [
83
+ 'test/comprehensive/global',
84
+ 'test/comprehensive/environment',
85
+ 'test/comprehensive/local',
86
+ ]
87
+ static_config_files :app
88
+ end
89
+
90
+ config = comprehensive_class.app
91
+
92
+ # Test that all unique keys from all files are present
93
+ assert config.key?(:global_setting), "Should have global-only setting"
94
+ assert config.key?(:environment_setting), "Should have environment-only setting"
95
+ assert config.key?(:local_yaml_setting), "Should have local YAML setting"
96
+ assert config.key?(:local_json_setting), "Should have local JSON setting"
97
+
98
+ # Test database configuration deep merging with global taking precedence
99
+ db = config[:database]
100
+
101
+ assert_equal 'global-db.example.com', db[:host], "Host should be from global YAML (highest priority)"
102
+ assert_equal 5432, db[:port], "Port should be from global YAML"
103
+ assert_equal 10, db[:pool_size], "Pool size should be from global YAML"
104
+ assert_equal 'staging_user', db[:username], "Username should be from environment JSON"
105
+ assert_equal 'staging_pass', db[:password], "Password should be from environment JSON"
106
+ assert db[:ssl], "SSL should be from local JSON"
107
+
108
+ # Test app configuration merging
109
+ app = config[:app]
110
+
111
+ assert_equal 'MyApp', app[:name], "Name should be from global YAML"
112
+ assert_equal '1.0.0', app[:version], "Version should be from global YAML"
113
+ refute app[:debug], "Debug should be from global YAML (highest priority)"
114
+ assert_equal 'debug', app[:log_level], "Log level should be from environment JSON"
115
+ assert app[:local_override], "Local override should be from local YAML"
116
+ assert_equal 'development', app[:environment], "Environment should be from local JSON"
117
+
118
+ # Test features array merging
119
+ features = config[:features]
120
+
121
+ assert features[:feature_a], "Feature A should be from global YAML"
122
+ refute features[:feature_b], "Feature B should be from global YAML (highest priority)"
123
+ assert features[:feature_c], "Feature C should be from environment JSON"
124
+
125
+ # Test environment-specific setting
126
+ assert_equal 'staging', config[:environment_setting], "Environment setting should be preserved"
127
+ end
128
+
129
+ def test_directory_order_matters
130
+ # Test that directory order affects final values
131
+ reverse_order_class = Class.new do
132
+ include ConfigFiles
133
+ config_directories etc: [
134
+ 'test/comprehensive/local',
135
+ 'test/comprehensive/environment',
136
+ 'test/comprehensive/global',
137
+ ]
138
+ static_config_files :app
139
+ end
140
+
141
+ config = reverse_order_class.app
142
+
143
+ # With reversed order, local should override global (local comes first)
144
+ assert_equal 'localhost', config[:database][:host], "Local should override global when local comes first"
145
+ refute config[:app][:debug], "Local debug setting should override others"
146
+ end
147
+
148
+ def test_missing_directories_handled_gracefully
149
+ # Test with some non-existent directories
150
+ mixed_existence_class = Class.new do
151
+ include ConfigFiles
152
+ config_directories etc: [
153
+ 'test/comprehensive/nonexistent1',
154
+ 'test/comprehensive/global',
155
+ 'test/comprehensive/nonexistent2',
156
+ 'test/comprehensive/local',
157
+ ]
158
+ static_config_files :app
159
+ end
160
+
161
+ config = mixed_existence_class.app
162
+
163
+ # Should still work with existing directories
164
+ assert config.key?(:global_setting), "Should have global settings"
165
+ assert config.key?(:local_yaml_setting), "Should have local settings"
166
+ assert_equal 'global-db.example.com', config[:database][:host], "Should merge from existing directories (global overrides local)"
167
+ end
168
+ end
@@ -0,0 +1,10 @@
1
+ {
2
+ "api": {
3
+ "endpoint": "https://api.config.com",
4
+ "timeout": 30
5
+ },
6
+ "features": {
7
+ "feature_a": true,
8
+ "feature_b": false
9
+ }
10
+ }
@@ -0,0 +1,6 @@
1
+ config_test: config_dir_value
2
+ database:
3
+ host: config.example.com
4
+ port: 5432
5
+ only_in_config: config_only
6
+ shared_key: from_config
@@ -1,4 +1,6 @@
1
- $:.push(File.join(File.dirname(__FILE__), '..', 'lib'))
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push(File.join(File.dirname(__FILE__), '..', 'lib'))
2
4
  require 'minitest/autorun'
3
5
  require 'config_files'
4
6
  class Dummy
@@ -11,7 +13,7 @@ class Dummy2 < Dummy
11
13
  config_directories config: ['test/etc', 'test/nofiles/etc']
12
14
  class << self
13
15
  def config_key
14
- "config"
16
+ 'config'
15
17
  end
16
18
  end
17
19
  end
@@ -21,7 +23,7 @@ class Defaults
21
23
  static_config_files :dummy
22
24
  end
23
25
 
24
- class ConfigFilesTest < MiniTest::Test
26
+ class ConfigFilesTest < Minitest::Test
25
27
  def test_config_key
26
28
  assert_equal(:etc, Dummy.config_key)
27
29
  end
@@ -43,7 +45,7 @@ class ConfigFilesTest < MiniTest::Test
43
45
  end
44
46
 
45
47
  def test_empty_for_missing_files
46
- assert_equal({}, Dummy.broken)
48
+ assert_empty(Dummy.broken)
47
49
  end
48
50
 
49
51
  def test_yaml_and_config_override
@@ -51,6 +53,6 @@ class ConfigFilesTest < MiniTest::Test
51
53
  end
52
54
 
53
55
  def test_defaults_are_picked_up_when_no_directories_used
54
- assert_equal(nil, Defaults.dummy[:config_test])
56
+ assert_nil(Defaults.dummy[:config_test])
55
57
  end
56
- end
58
+ end
data/test/etc/dummy.conf CHANGED
@@ -1,2 +1,14 @@
1
- :config_test: test2
2
- :only_in_conf: conf_file
1
+ # CONF format test file - supports multiple syntaxes
2
+ config_test=test2
3
+ only_in_conf: conf_file
4
+ server.name myserver
5
+ nested.config.value=nested_value
6
+
7
+ [database]
8
+ host: conf-db.example.com
9
+ port 5433
10
+ ssl=true
11
+
12
+ [app]
13
+ name: "CONF App"
14
+ debug false
@@ -0,0 +1,12 @@
1
+ # INI format test file
2
+ global_setting=ini_value
3
+
4
+ [database]
5
+ host=ini-db.example.com
6
+ port=3306
7
+ ssl=true
8
+
9
+ [app]
10
+ name=INI App
11
+ debug=false
12
+ version=2.0
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <global_setting>xml_value</global_setting>
4
+ <database>
5
+ <host>xml-db.example.com</host>
6
+ <port>5432</port>
7
+ <ssl>true</ssl>
8
+ </database>
9
+ <app name="XML App">
10
+ <debug>false</debug>
11
+ <version>3.0</version>
12
+ </app>
13
+ </config>
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require 'minitest/autorun'
5
+ require 'config_files'
6
+ require 'fileutils'
7
+ require 'tmpdir'
8
+
9
+ # Sibling files with the same name (dds.yaml beside dds.yml) must merge
10
+ # without crashing, whatever their content.
11
+ class LoadRobustnessTest < Minitest::Test
12
+ def setup
13
+ @dir = Dir.mktmpdir('config_files_robust')
14
+ @klass = Class.new do
15
+ include ConfigFiles
16
+ end
17
+ @klass.config_directories(etc: [@dir])
18
+ @klass.dynamic_config_files(:dds)
19
+ end
20
+
21
+ def teardown
22
+ FileUtils.rm_rf(@dir)
23
+ end
24
+
25
+ def write(name, content)
26
+ File.write(File.join(@dir, name), content)
27
+ end
28
+
29
+ def test_identical_yaml_and_yml_merge
30
+ content = "a: 1\nnested:\n b: 2\n"
31
+ write('dds.yaml', content)
32
+ write('dds.yml', content)
33
+
34
+ assert_equal({ 'a' => 1, 'nested' => { 'b' => 2 } }, @klass.dds.to_hash)
35
+ end
36
+
37
+ def test_different_yaml_and_yml_merge_with_later_file_winning
38
+ write('dds.yaml', "a: 1\nnested:\n b: 2\n c: 3\n")
39
+ write('dds.yml', "a: 2\nnested:\n b: 20\nx: 9\n")
40
+
41
+ assert_equal({ 'a' => 2, 'nested' => { 'b' => 20, 'c' => 3 }, 'x' => 9 }, @klass.dds.to_hash)
42
+ end
43
+
44
+ def test_empty_file_contributes_nothing
45
+ write('dds.yaml', "a: 1\n")
46
+ write('dds.yml', '')
47
+
48
+ assert_equal({ 'a' => 1 }, @klass.dds.to_hash)
49
+ end
50
+
51
+ def test_comment_only_file_contributes_nothing
52
+ write('dds.yaml', "# placeholder\n")
53
+ write('dds.yml', "a: 1\n")
54
+
55
+ assert_equal({ 'a' => 1 }, @klass.dds.to_hash)
56
+ end
57
+
58
+ def test_top_level_list_raises_clear_error
59
+ write('dds.yaml', "a: 1\n")
60
+ write('dds.yml', "- x\n- y\n")
61
+
62
+ error = assert_raises(ConfigFiles::InvalidConfigError) { @klass.dds }
63
+ assert_includes(error.message, 'dds.yml')
64
+ assert_includes(error.message, 'Array')
65
+ end
66
+
67
+ def test_top_level_scalar_raises_clear_error
68
+ write('dds.json', '"just a string"')
69
+
70
+ error = assert_raises(ConfigFiles::InvalidConfigError) { @klass.dds }
71
+ assert_includes(error.message, 'dds.json')
72
+ assert_includes(error.message, 'String')
73
+ end
74
+
75
+ def test_yaml_anchors_and_aliases_load
76
+ write('dds.yaml', "base: &base\n host: h\nprod:\n <<: *base\n port: 1\n")
77
+
78
+ assert_equal({ 'host' => 'h', 'port' => 1 }, @klass.dds[:prod].to_hash)
79
+ end
80
+
81
+ def test_yaml_dates_and_times_load
82
+ write('dds.yaml', "day: 2026-12-01\nat: 2026-12-01 10:00:00 +00:00\n")
83
+
84
+ assert_equal(Date.new(2026, 12, 1), @klass.dds[:day])
85
+ assert_kind_of(Time, @klass.dds[:at])
86
+ end
87
+ end
@@ -1,40 +1,40 @@
1
- $:.push(File.join(File.dirname(__FILE__), '..', 'lib'))
1
+ $LOAD_PATH.push(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  require 'minitest/autorun'
3
3
  require 'config_files'
4
4
  class LoaderFactoryDummy
5
5
  class << self
6
- def call(file_name, options={})
6
+ def call(_file_name, _options = {})
7
7
  nil
8
8
  end
9
9
  end
10
10
  end
11
11
 
12
- class LoaderFactoryTest < MiniTest::Test
12
+ class LoaderFactoryTest < Minitest::Test
13
13
  def file_locations
14
14
  File.join(__dir__, 'etc')
15
15
  end
16
16
 
17
17
  def test_detect_yaml_file_type
18
- assert_equal(::ConfigFiles::Loaders::Yaml, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.yml')))
18
+ assert_equal(::ConfigFiles::Loaders::Yaml, ::ConfigFiles::LoaderFactory.call(File.join(file_locations, 'dummy.yml')))
19
19
  end
20
20
 
21
21
  def test_detect_json_file_type
22
- assert_equal(::ConfigFiles::Loaders::Json, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.json')))
22
+ assert_equal(::ConfigFiles::Loaders::Json, ::ConfigFiles::LoaderFactory.call(File.join(file_locations, 'dummy.json')))
23
23
  end
24
24
 
25
25
  def test_detect_unknown_file_type
26
- assert_nil(::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.wibble'), default_loader: nil))
26
+ assert_nil(::ConfigFiles::LoaderFactory.call(File.join(file_locations, 'dummy.wibble'), default_loader: nil))
27
27
  end
28
28
 
29
29
  def test_allow_addition_of_file_types_does_not_break_existing
30
- assert_equal(::ConfigFiles::Loaders::Yaml, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.yml'), loaders: {LoaderFactoryDummy=>['wibble']}))
30
+ assert_equal(::ConfigFiles::Loaders::Yaml, ::ConfigFiles::LoaderFactory.call(File.join(file_locations, 'dummy.yml'), loaders: { LoaderFactoryDummy => ['wibble'] }))
31
31
  end
32
32
 
33
33
  def test_allow_addition_of_file_types_breaks_existing_if_specified
34
- assert_nil(::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.yml'), loaders: {LoaderFactoryDummy=>['wibble']}, include_default: false, default_loader: nil))
34
+ assert_nil(::ConfigFiles::LoaderFactory.call(File.join(file_locations, 'dummy.yml'), loaders: { LoaderFactoryDummy => ['wibble'] }, include_default: false, default_loader: nil))
35
35
  end
36
36
 
37
37
  def test_addition_of_new_loaders
38
- assert_equal(LoaderFactoryDummy, ::ConfigFiles::LoaderFactory.(File.join(file_locations,'dummy.wibble'), loaders: {LoaderFactoryDummy=>['wibble']}, include_default: false))
38
+ assert_equal(LoaderFactoryDummy, ::ConfigFiles::LoaderFactory.call(File.join(file_locations, 'dummy.wibble'), loaders: { LoaderFactoryDummy => ['wibble'] }, include_default: false))
39
39
  end
40
- end
40
+ end