Module: Ace::TestSupport::ConfigHelpers
- Defined in:
- lib/ace/test_support/config_helpers.rb
Overview
Helpers for config testing across all ace-* gems
Instance Method Summary collapse
-
#assert_config_structure(config, expected) ⇒ Object
Assert config has expected structure.
-
#assert_precedence(resolver, key_path, expected_value, source_description) ⇒ Object
Assert config cascade precedence.
-
#complex_yaml ⇒ Object
Create valid but complex YAML.
-
#malformed_yaml ⇒ Object
Create malformed YAML content.
-
#sample_config(gem_name: "core", level: "default", custom: {}) ⇒ Object
Create sample config content.
-
#sample_env_content(vars = {}) ⇒ Object
Create .env file content.
-
#with_cascade_configs(gem_name = "core", configs = {}) ⇒ Object
Create multi-level config setup for any ace-* gem.
-
#with_config(path, content) ⇒ Object
Execute block with temporary config file.
-
#with_env(vars) ⇒ Object
Execute block with temporary environment variables.
-
#with_real_config { ... } ⇒ Object
Execute block with real config (test mode disabled).
-
#with_test_config(mock_config = {}) { ... } ⇒ Object
Execute block with test mode enabled.
Instance Method Details
#assert_config_structure(config, expected) ⇒ Object
Assert config has expected structure
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ace/test_support/config_helpers.rb', line 176 def assert_config_structure(config, expected) assert_kind_of Hash, config expected.each do |key, value| assert config.key?(key), "Config missing key: #{key}" if value.is_a?(Hash) assert_config_structure(config[key], value) else assert_equal value, config[key], "Config value mismatch for #{key}" end end end |
#assert_precedence(resolver, key_path, expected_value, source_description) ⇒ Object
Assert config cascade precedence
191 192 193 194 195 196 197 |
# File 'lib/ace/test_support/config_helpers.rb', line 191 def assert_precedence(resolver, key_path, expected_value, source_description) config = resolver.resolve actual = config.get(*key_path.split(".")) assert_equal expected_value, actual, "Expected #{key_path} to be '#{expected_value}' from #{source_description}, got '#{actual}'" end |
#complex_yaml ⇒ Object
Create valid but complex YAML
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/ace/test_support/config_helpers.rb', line 205 def complex_yaml { "ace" => { "arrays" => [1, 2, 3], "nested" => { "deep" => { "value" => "found" } }, "symbols" => { "key" => :symbol_value } } }.to_yaml end |
#malformed_yaml ⇒ Object
Create malformed YAML content
200 201 202 |
# File 'lib/ace/test_support/config_helpers.rb', line 200 def malformed_yaml "ace:\n invalid: [\n unclosed" end |
#sample_config(gem_name: "core", level: "default", custom: {}) ⇒ Object
Create sample config content
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ace/test_support/config_helpers.rb', line 149 def sample_config(gem_name: "core", level: "default", custom: {}) base = { "ace" => { "level" => level, gem_name => { "version" => "1.0.0", "environment" => "test" } } } deep_merge(base, custom) end |
#sample_env_content(vars = {}) ⇒ Object
Create .env file content
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ace/test_support/config_helpers.rb', line 164 def sample_env_content(vars = {}) default_vars = { "ACE_ENV" => "test", "ACE_DEBUG" => "false" } default_vars.merge(vars).map do |key, value| "#{key}=#{value}" end.join("\n") end |
#with_cascade_configs(gem_name = "core", configs = {}) ⇒ Object
Create multi-level config setup for any ace-* gem
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ace/test_support/config_helpers.rb', line 116 def with_cascade_configs(gem_name = "core", configs = {}) paths = [] old_home = ENV["HOME"] temp_home = nil begin # Create project config if configs[:project] project_path = "./.ace/#{gem_name}/config.yml" FileUtils.mkdir_p(File.dirname(project_path)) File.write(project_path, configs[:project].to_yaml) paths << project_path end # Create home config if configs[:home] temp_home = Dir.mktmpdir("ace-test-home") ENV["HOME"] = temp_home home_path = File.join(temp_home, ".ace", gem_name, "config.yml") FileUtils.mkdir_p(File.dirname(home_path)) File.write(home_path, configs[:home].to_yaml) paths << home_path end yield ensure ENV["HOME"] = old_home FileUtils.rm_rf(temp_home) if temp_home && Dir.exist?(temp_home) paths.each { |path| FileUtils.rm_f(path) if File.exist?(path) } end end |
#with_config(path, content) ⇒ Object
Execute block with temporary config file
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ace/test_support/config_helpers.rb', line 76 def with_config(path, content) FileUtils.mkdir_p(File.dirname(path)) # Handle both Hash and String content file_content = case content when Hash content.to_yaml when String content else raise ArgumentError, "Content must be Hash or String" end File.write(path, file_content) yield ensure FileUtils.rm_f(path) if path && File.exist?(path) end |
#with_env(vars) ⇒ Object
Execute block with temporary environment variables
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ace/test_support/config_helpers.rb', line 96 def with_env(vars) old_values = {} vars.each do |key, value| old_values[key] = ENV[key] ENV[key] = value end yield ensure old_values.each do |key, value| if value.nil? ENV.delete(key) else ENV[key] = value end end end |
#with_real_config { ... } ⇒ Object
Execute block with real config (test mode disabled)
This helper temporarily disables test mode for integration tests that need to test actual filesystem-based config loading.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ace/test_support/config_helpers.rb', line 63 def with_real_config require "ace/support/config" original_test_mode = Ace::Support::Config.test_mode Ace::Support::Config.test_mode = false yield ensure Ace::Support::Config.test_mode = original_test_mode end |
#with_test_config(mock_config = {}) { ... } ⇒ Object
Execute block with test mode enabled
This helper enables Ace::Support::Config test mode for the duration of the block, skipping filesystem searches and returning mock config instead.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ace/test_support/config_helpers.rb', line 32 def with_test_config(mock_config = {}) require "ace/support/config" original_test_mode = Ace::Support::Config.test_mode original_mock = Ace::Support::Config.default_mock Ace::Support::Config.test_mode = true Ace::Support::Config.default_mock = mock_config yield ensure Ace::Support::Config.test_mode = original_test_mode Ace::Support::Config.default_mock = original_mock end |