runger_config 4.0.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/bin/release +27 -0
  4. data/lib/generators/runger/app_config/app_config_generator.rb +6 -10
  5. data/lib/generators/runger/config/config_generator.rb +44 -41
  6. data/lib/generators/runger/install/install_generator.rb +35 -37
  7. data/lib/runger/auto_cast.rb +3 -3
  8. data/lib/runger/config.rb +114 -94
  9. data/lib/runger/dynamic_config.rb +21 -23
  10. data/lib/runger/ejson_parser.rb +24 -24
  11. data/lib/runger/env.rb +50 -52
  12. data/lib/runger/ext/deep_dup.rb +33 -36
  13. data/lib/runger/ext/deep_freeze.rb +28 -32
  14. data/lib/runger/ext/flatten_names.rb +23 -27
  15. data/lib/runger/ext/hash.rb +26 -29
  16. data/lib/runger/ext/string_constantize.rb +12 -15
  17. data/lib/runger/loaders/base.rb +11 -15
  18. data/lib/runger/loaders/doppler.rb +38 -42
  19. data/lib/runger/loaders/ejson.rb +65 -63
  20. data/lib/runger/loaders/env.rb +6 -10
  21. data/lib/runger/loaders/yaml.rb +69 -66
  22. data/lib/runger/loaders.rb +69 -71
  23. data/lib/runger/option_parser_builder.rb +16 -18
  24. data/lib/runger/optparse_config.rb +11 -10
  25. data/lib/runger/rails/autoload.rb +24 -26
  26. data/lib/runger/rails/config.rb +13 -17
  27. data/lib/runger/rails/loaders/credentials.rb +53 -57
  28. data/lib/runger/rails/loaders/secrets.rb +21 -25
  29. data/lib/runger/rails/loaders/yaml.rb +1 -6
  30. data/lib/runger/rails/loaders.rb +3 -3
  31. data/lib/runger/rails/settings.rb +49 -49
  32. data/lib/runger/rails.rb +9 -11
  33. data/lib/runger/railtie.rb +3 -2
  34. data/lib/runger/rbs.rb +29 -29
  35. data/lib/runger/settings.rb +82 -84
  36. data/lib/runger/testing/helpers.rb +26 -28
  37. data/lib/runger/testing.rb +2 -2
  38. data/lib/runger/tracing.rb +143 -136
  39. data/lib/runger/type_casting.rb +16 -11
  40. data/lib/runger/utils/which.rb +10 -12
  41. data/lib/runger/version.rb +1 -1
  42. data/lib/runger.rb +1 -1
  43. data/lib/runger_config.rb +34 -27
  44. metadata +20 -19
@@ -1,35 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Runger
4
- module Rails
5
- module Loaders
6
- class Secrets < Runger::Loaders::Base
7
- def call(name:, **_options)
8
- return {} unless ::Rails.application.respond_to?(:secrets)
3
+ class Runger::Rails::Loaders::Secrets < Runger::Loaders::Base
4
+ def call(name:, **_options)
5
+ return {} unless ::Rails.application.respond_to?(:secrets)
9
6
 
10
- # Create a new hash cause secrets are mutable!
11
- config = {}
7
+ # Create a new hash cause secrets are mutable!
8
+ config = {}
12
9
 
13
- trace!(:secrets) do
14
- secrets.public_send(name)
15
- end.then do |secrets|
16
- Utils.deep_merge!(config, secrets) if secrets
17
- end
10
+ trace!(:secrets) do
11
+ secrets.public_send(name)
12
+ end.then do |secrets|
13
+ ::Runger::Utils.deep_merge!(config, secrets) if secrets
14
+ end
15
+
16
+ config
17
+ end
18
18
 
19
- config
20
- end
19
+ private
21
20
 
22
- private
21
+ def secrets
22
+ @secrets ||=
23
+ ::Rails.application.secrets.tap do |_|
24
+ # Reset secrets state if the app hasn't been initialized
25
+ # See https://github.com/palkan/runger_config/issues/14
26
+ next if ::Rails.application.initialized?
23
27
 
24
- def secrets
25
- @secrets ||= ::Rails.application.secrets.tap do |_|
26
- # Reset secrets state if the app hasn't been initialized
27
- # See https://github.com/palkan/runger_config/issues/14
28
- next if ::Rails.application.initialized?
29
- ::Rails.application.remove_instance_variable(:@secrets)
30
- end
31
- end
28
+ ::Rails.application.remove_instance_variable(:@secrets)
32
29
  end
33
- end
34
30
  end
35
31
  end
@@ -1,9 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Runger
4
- module Rails
5
- module Loaders
6
- class YAML < Runger::Loaders::YAML; end
7
- end
8
- end
3
+ class Runger::Rails::Loaders::YAML < Runger::Loaders::YAML
9
4
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "runger/rails/loaders/yaml"
4
- require "runger/rails/loaders/secrets"
5
- require "runger/rails/loaders/credentials"
3
+ require 'runger/rails/loaders/credentials'
4
+ require 'runger/rails/loaders/secrets'
5
+ require 'runger/rails/loaders/yaml'
@@ -2,33 +2,33 @@
2
2
 
3
3
  # Try to require zeitwerk
4
4
  begin
5
- require "zeitwerk"
6
- require "active_support/dependencies/zeitwerk_integration"
5
+ require 'active_support/dependencies/zeitwerk_integration'
6
+ require 'zeitwerk'
7
7
  rescue LoadError
8
8
  end
9
9
 
10
- module Runger
11
- class Settings
12
- class << self
13
- attr_reader :autoload_static_config_path, :autoloader
14
- attr_writer :autoload_via_zeitwerk
10
+ class Runger::Settings
11
+ class << self
12
+ attr_reader :autoload_static_config_path, :autoloader
13
+ attr_writer :autoload_via_zeitwerk
15
14
 
16
- def autoload_static_config_path=(val)
17
- if autoload_via_zeitwerk
18
- raise "Cannot setup autoloader after application has been initialized" if ::Rails.application.initialized?
15
+ def autoload_static_config_path=(val)
16
+ if autoload_via_zeitwerk
17
+ raise('Cannot setup autoloader after application has been initialized') if ::Rails.application.initialized?
19
18
 
20
- return unless ::Rails.root.join(val).exist?
19
+ return unless ::Rails.root.join(val).exist?
21
20
 
22
- return if val == autoload_static_config_path
21
+ return if val == autoload_static_config_path
23
22
 
24
- autoloader&.unload
23
+ autoloader&.unload
25
24
 
26
- @autoload_static_config_path = val
25
+ @autoload_static_config_path = val
27
26
 
28
- # See Rails 6 https://github.com/rails/rails/blob/8ab4fd12f18203b83d0f252db96d10731485ff6a/railties/lib/rails/autoloaders.rb#L10
29
- # and Rails 7 https://github.com/rails/rails/blob/5462fbd5de1900c1b1ce1c9dc11c1a2d8cdcd809/railties/lib/rails/autoloaders.rb#L15
30
- @autoloader = Zeitwerk::Loader.new.tap do |loader|
31
- loader.tag = "runger.config"
27
+ # See Rails 6 https://github.com/rails/rails/blob/8ab4fd12f18203b83d0f252db96d10731485ff6a/railties/lib/rails/autoloaders.rb#L10
28
+ # and Rails 7 https://github.com/rails/rails/blob/5462fbd5de1900c1b1ce1c9dc11c1a2d8cdcd809/railties/lib/rails/autoloaders.rb#L15
29
+ @autoloader =
30
+ Zeitwerk::Loader.new.tap do |loader|
31
+ loader.tag = 'runger.config'
32
32
 
33
33
  if defined?(ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector)
34
34
  loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector
@@ -38,46 +38,46 @@ module Runger
38
38
  loader.push_dir(::Rails.root.join(val))
39
39
  loader.setup
40
40
  end
41
- else
42
- if autoload_static_config_path
43
- old_path = ::Rails.root.join(autoload_static_config_path).to_s
44
- ActiveSupport::Dependencies.autoload_paths.delete(old_path)
45
- ::Rails.application.config.eager_load_paths.delete(old_path)
46
- end
47
-
48
- @autoload_static_config_path = val
49
- new_path = ::Rails.root.join(val).to_s
50
- ActiveSupport::Dependencies.autoload_paths << new_path
51
- ::Rails.application.config.eager_load_paths << new_path
41
+ else
42
+ if autoload_static_config_path
43
+ old_path = ::Rails.root.join(autoload_static_config_path).to_s
44
+ ActiveSupport::Dependencies.autoload_paths.delete(old_path)
45
+ ::Rails.application.config.eager_load_paths.delete(old_path)
52
46
  end
47
+
48
+ @autoload_static_config_path = val
49
+ new_path = ::Rails.root.join(val).to_s
50
+ ActiveSupport::Dependencies.autoload_paths << new_path
51
+ ::Rails.application.config.eager_load_paths << new_path
53
52
  end
53
+ end
54
54
 
55
- def cleanup_autoload_paths
56
- return unless autoload_via_zeitwerk
55
+ def cleanup_autoload_paths
56
+ return unless autoload_via_zeitwerk
57
57
 
58
- return unless autoload_static_config_path
59
- ActiveSupport::Dependencies.autoload_paths.delete(::Rails.root.join(autoload_static_config_path).to_s)
60
- end
58
+ return unless autoload_static_config_path
61
59
 
62
- def autoload_via_zeitwerk
63
- return @autoload_via_zeitwerk if instance_variable_defined?(:@autoload_via_zeitwerk)
60
+ ActiveSupport::Dependencies.autoload_paths.delete(::Rails.root.join(autoload_static_config_path).to_s)
61
+ end
64
62
 
65
- @autoload_via_zeitwerk = defined?(::Zeitwerk)
66
- end
63
+ def autoload_via_zeitwerk
64
+ return @autoload_via_zeitwerk if instance_variable_defined?(:@autoload_via_zeitwerk)
67
65
 
68
- def current_environment
69
- @current_environment || ::Rails.env.to_s
70
- end
66
+ @autoload_via_zeitwerk = defined?(::Zeitwerk)
67
+ end
71
68
 
72
- def app_root
73
- ::Rails.root
74
- end
69
+ def current_environment
70
+ @current_environment || ::Rails.env.to_s
75
71
  end
76
72
 
77
- self.default_config_path = ->(name) { ::Rails.root.join("config", "#{name}.yml") }
78
- self.known_environments = %w[test development production]
79
- self.use_local_files ||= ::Rails.env.development?
80
- # Don't try read defaults when no key defined
81
- self.default_environmental_key = nil
73
+ def app_root
74
+ ::Rails.root
75
+ end
82
76
  end
77
+
78
+ self.default_config_path = ->(name) { ::Rails.root.join('config', "#{name}.yml") }
79
+ self.known_environments = %w[test development production]
80
+ self.use_local_files ||= ::Rails.env.development?
81
+ # Don't try read defaults when no key defined
82
+ self.default_environmental_key = nil
83
83
  end
data/lib/runger/rails.rb CHANGED
@@ -1,24 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Runger
4
- module Rails
5
- end
3
+ module Runger::Rails
6
4
  end
7
5
 
8
- require "runger/rails/settings"
9
- require "runger/rails/config"
10
- require "runger/rails/loaders"
6
+ require 'runger/rails/config'
7
+ require 'runger/rails/loaders'
8
+ require 'runger/rails/settings'
11
9
 
12
10
  # Configure Rails loaders
13
- Runger.loaders.override :yml, Runger::Rails::Loaders::YAML
11
+ Runger.loaders.override(:yml, Runger::Rails::Loaders::YAML)
14
12
 
15
13
  if Rails::VERSION::MAJOR >= 7 && Rails::VERSION::MINOR >= 1
16
- Runger.loaders.insert_after :yml, :credentials, Runger::Rails::Loaders::Credentials
14
+ Runger.loaders.insert_after(:yml, :credentials, Runger::Rails::Loaders::Credentials)
17
15
  else
18
- Runger.loaders.insert_after :yml, :secrets, Runger::Rails::Loaders::Secrets
19
- Runger.loaders.insert_after :secrets, :credentials, Runger::Rails::Loaders::Credentials
16
+ Runger.loaders.insert_after(:yml, :secrets, Runger::Rails::Loaders::Secrets)
17
+ Runger.loaders.insert_after(:secrets, :credentials, Runger::Rails::Loaders::Credentials)
20
18
  end
21
19
 
22
20
  # Load Railties after configuring loaders.
23
21
  # The application could be already initialized, and that would make `Runger.loaders` frozen
24
- require "runger/railtie"
22
+ require 'runger/railtie'
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Runger # :nodoc:
4
- DEFAULT_CONFIGS_PATH = "config/configs"
4
+ DEFAULT_CONFIGS_PATH = 'config/configs'
5
5
 
6
6
  class Railtie < ::Rails::Railtie # :nodoc:
7
7
  # Add settings to Rails config
@@ -9,6 +9,7 @@ module Runger # :nodoc:
9
9
 
10
10
  config.before_configuration do
11
11
  next if ::Rails.application.initialized?
12
+
12
13
  config.runger_config.autoload_static_config_path = DEFAULT_CONFIGS_PATH
13
14
  end
14
15
 
@@ -18,7 +19,7 @@ module Runger # :nodoc:
18
19
 
19
20
  # Remove `autoload_static_config_path` from Rails `autoload_paths`
20
21
  # since we use our own autoloading mechanism
21
- initializer "runger_config.cleanup_autoload" do
22
+ initializer 'runger_config.cleanup_autoload' do
22
23
  Runger::Settings.cleanup_autoload_paths
23
24
  end
24
25
 
data/lib/runger/rbs.rb CHANGED
@@ -3,33 +3,33 @@
3
3
  module Runger
4
4
  module RBSGenerator
5
5
  TYPE_TO_CLASS = {
6
- string: "String",
7
- integer: "Integer",
8
- float: "Float",
9
- date: "Date",
10
- datetime: "DateTime",
11
- uri: "URI",
12
- boolean: "bool"
6
+ string: 'String',
7
+ integer: 'Integer',
8
+ float: 'Float',
9
+ date: 'Date',
10
+ datetime: 'DateTime',
11
+ uri: 'URI',
12
+ boolean: 'bool',
13
13
  }.freeze
14
14
 
15
15
  # Generate RBS signature from a config class
16
16
  def to_rbs
17
- *namespace, class_name = name.split("::")
17
+ *namespace, class_name = name.split('::')
18
18
 
19
19
  buf = []
20
20
  indent = 0
21
- interface_name = "_Config"
21
+ interface_name = '_Config'
22
22
 
23
23
  if namespace.empty?
24
24
  interface_name = "_#{class_name}"
25
25
  else
26
- buf << "module #{namespace.join("::")}"
26
+ buf << "module #{namespace.join('::')}"
27
27
  indent += 1
28
28
  end
29
29
 
30
30
  # Using interface emulates a module we include to provide getters and setters
31
31
  # (thus making `super` possible)
32
- buf << "#{" " * indent}interface #{interface_name}"
32
+ buf << "#{' ' * indent}interface #{interface_name}"
33
33
  indent += 1
34
34
 
35
35
  # Generating setters and getters for config attributes
@@ -39,17 +39,17 @@ module Runger
39
39
  type =
40
40
  case type
41
41
  in NilClass
42
- "untyped"
42
+ 'untyped'
43
43
  in Symbol
44
- TYPE_TO_CLASS.fetch(type) { defaults[param] ? "Symbol" : "untyped" }
44
+ TYPE_TO_CLASS.fetch(type) { defaults[param] ? 'Symbol' : 'untyped' }
45
45
  in Array
46
- "Array[untyped]"
46
+ 'Array[untyped]'
47
47
  in array: _, type:, **nil
48
- "Array[#{TYPE_TO_CLASS.fetch(type, "untyped")}]"
48
+ "Array[#{TYPE_TO_CLASS.fetch(type, 'untyped')}]"
49
49
  in Hash
50
- "Hash[string,untyped]"
50
+ 'Hash[string,untyped]'
51
51
  in TrueClass | FalseClass
52
- "bool"
52
+ 'bool'
53
53
  else
54
54
  type.class.to_s
55
55
  end
@@ -57,36 +57,36 @@ module Runger
57
57
  getter_type = type
58
58
  getter_type = "#{type}?" unless required_attributes.include?(param)
59
59
 
60
- buf << "#{" " * indent}def #{param}: () -> #{getter_type}"
61
- buf << "#{" " * indent}def #{param}=: (#{type}) -> void"
60
+ buf << "#{' ' * indent}def #{param}: () -> #{getter_type}"
61
+ buf << "#{' ' * indent}def #{param}=: (#{type}) -> void"
62
62
 
63
- if type == "bool" || type == "bool?"
64
- buf << "#{" " * indent}def #{param}?: () -> #{getter_type}"
63
+ if ['bool', 'bool?'].include?(type)
64
+ buf << "#{' ' * indent}def #{param}?: () -> #{getter_type}"
65
65
  end
66
66
  end
67
67
 
68
68
  indent -= 1
69
- buf << "#{" " * indent}end"
69
+ buf << "#{' ' * indent}end"
70
70
 
71
- buf << ""
71
+ buf << ''
72
72
 
73
- buf << "#{" " * indent}class #{class_name} < #{superclass.name}"
73
+ buf << "#{' ' * indent}class #{class_name} < #{superclass.name}"
74
74
  indent += 1
75
75
 
76
- buf << "#{" " * indent}include #{interface_name}"
76
+ buf << "#{' ' * indent}include #{interface_name}"
77
77
 
78
78
  indent -= 1
79
- buf << "#{" " * indent}end"
79
+ buf << "#{' ' * indent}end"
80
80
 
81
81
  unless namespace.empty?
82
- buf << "end"
82
+ buf << 'end'
83
83
  end
84
84
 
85
- buf << ""
85
+ buf << ''
86
86
 
87
87
  buf.join("\n")
88
88
  end
89
89
  end
90
90
 
91
- Config.extend RBSGenerator
91
+ Config.extend(RBSGenerator)
92
92
  end
@@ -1,111 +1,109 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "pathname"
4
-
5
- module Runger
6
- # Use Settings name to not confuse with Config.
7
- #
8
- # Settings contain the library-wide configuration.
9
- class Settings
10
- # Future encapsulates settings that will be introduced in the upcoming version
11
- # with the default values, which could break compatibility
12
- class Future
13
- class << self
14
- def setting(name, default_value)
15
- settings[name] = default_value
16
-
17
- define_method(name) do
18
- store[name]
19
- end
20
-
21
- define_method(:"#{name}=") do |val|
22
- store[name] = val
23
- end
24
- end
3
+ require 'pathname'
4
+
5
+ # Use Settings name to not confuse with Config.
6
+ #
7
+ # Settings contain the library-wide configuration.
8
+ class Runger::Settings
9
+ # Future encapsulates settings that will be introduced in the upcoming version
10
+ # with the default values, which could break compatibility
11
+ class Future
12
+ class << self
13
+ def setting(name, default_value)
14
+ settings[name] = default_value
25
15
 
26
- def settings
27
- @settings ||= {}
16
+ define_method(name) do
17
+ store[name]
28
18
  end
29
- end
30
19
 
31
- def initialize
32
- @store = {}
20
+ define_method(:"#{name}=") do |val|
21
+ store[name] = val
22
+ end
33
23
  end
34
24
 
35
- def use(*names)
36
- store.clear
37
- names.each { store[_1] = self.class.settings[_1] }
25
+ def settings
26
+ @settings ||= {}
38
27
  end
28
+ end
39
29
 
40
- setting :unwrap_known_environments, true
41
-
42
- private
30
+ def initialize
31
+ @store = {}
32
+ end
43
33
 
44
- attr_reader :store
34
+ def use(*names)
35
+ store.clear
36
+ names.each { store[_1] = self.class.settings[_1] }
45
37
  end
46
38
 
47
- class << self
48
- # Define whether to load data from
49
- # *.yml.local (or credentials/local.yml.enc)
50
- attr_accessor :use_local_files,
51
- :current_environment,
52
- :default_environmental_key,
53
- :known_environments
54
-
55
- # A proc returning a path to YML config file given the config name
56
- attr_reader :default_config_path
57
-
58
- def default_config_path=(val)
59
- if val.is_a?(Proc)
60
- @default_config_path = val
61
- return
62
- end
39
+ setting :unwrap_known_environments, true
63
40
 
64
- val = val.to_s
41
+ private
65
42
 
66
- @default_config_path = ->(name) { File.join(val, "#{name}.yml") }
43
+ attr_reader :store
44
+ end
45
+
46
+ class << self
47
+ # Define whether to load data from
48
+ # *.yml.local (or credentials/local.yml.enc)
49
+ attr_accessor :use_local_files,
50
+ :current_environment,
51
+ :default_environmental_key,
52
+ :known_environments
53
+
54
+ # A proc returning a path to YML config file given the config name
55
+ attr_reader :default_config_path
56
+
57
+ def default_config_path=(val)
58
+ if val.is_a?(Proc)
59
+ @default_config_path = val
60
+ return
67
61
  end
68
62
 
69
- # Enable source tracing
70
- attr_accessor :tracing_enabled
63
+ val = val.to_s
71
64
 
72
- def future
73
- @future ||= Future.new
74
- end
65
+ @default_config_path = ->(name) { File.join(val, "#{name}.yml") }
66
+ end
75
67
 
76
- def app_root
77
- Pathname.new(Dir.pwd)
78
- end
68
+ # Enable source tracing
69
+ attr_accessor :tracing_enabled
79
70
 
80
- def default_environmental_key?
81
- !default_environmental_key.nil?
82
- end
71
+ def future
72
+ @future ||= Future.new
73
+ end
83
74
 
84
- def matching_env?(env)
85
- return true if env.nil? || env.to_s == current_environment
86
-
87
- if env.is_a?(::Hash)
88
- envs = env[:except]
89
- excluded_envs = [envs].flat_map(&:to_s)
90
- excluded_envs.none?(current_environment)
91
- elsif env.is_a?(::Array)
92
- env.flat_map(&:to_s).include?(current_environment)
93
- else
94
- false
95
- end
75
+ def app_root
76
+ Pathname.new(Dir.pwd)
77
+ end
78
+
79
+ def default_environmental_key?
80
+ !default_environmental_key.nil?
81
+ end
82
+
83
+ def matching_env?(env)
84
+ return true if env.nil? || env.to_s == current_environment
85
+
86
+ if env.is_a?(::Hash)
87
+ envs = env[:except]
88
+ excluded_envs = [envs].flat_map(&:to_s)
89
+ excluded_envs.none?(current_environment)
90
+ elsif env.is_a?(::Array)
91
+ env.flat_map(&:to_s).include?(current_environment)
92
+ else
93
+ false
96
94
  end
97
95
  end
96
+ end
98
97
 
99
- # By default, use RUNGER_ENV
100
- self.current_environment = ENV["RUNGER_ENV"]
98
+ # By default, use RUNGER_ENV
99
+ self.current_environment = ENV.fetch('RUNGER_ENV', nil)
101
100
 
102
- # By default, use local files only in development (that's the purpose if the local files)
103
- self.use_local_files = (ENV["RUNGER_ENV"] == "development" || ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development" || (defined?(Rails) && Rails.env.development?))
101
+ # By default, use local files only in development (that's the purpose if the local files)
102
+ self.use_local_files = (ENV['RUNGER_ENV'] == 'development' || ENV['RACK_ENV'] == 'development' || ENV['RAILS_ENV'] == 'development' || (defined?(Rails) && Rails.env.development?))
104
103
 
105
- # By default, consider configs are stored in the ./config folder
106
- self.default_config_path = ->(name) { "./config/#{name}.yml" }
104
+ # By default, consider configs are stored in the ./config folder
105
+ self.default_config_path = ->(name) { "./config/#{name}.yml" }
107
106
 
108
- # Tracing is enabled by default
109
- self.tracing_enabled = true
110
- end
107
+ # Tracing is enabled by default
108
+ self.tracing_enabled = true
111
109
  end
@@ -1,36 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Runger
4
- module Testing
5
- module Helpers
6
- # Sets the ENV variables to the provided
7
- # values and restore outside the block
8
- #
9
- # Also resets Runger.env before and after calling the block
10
- # to make sure that the values are not cached.
11
- #
12
- # NOTE: to remove the env value, pass `nil` as the value
13
- def with_env(data)
14
- was_values = []
3
+ module Runger::Testing::Helpers
4
+ # Sets the ENV variables to the provided
5
+ # values and restore outside the block
6
+ #
7
+ # Also resets Runger.env before and after calling the block
8
+ # to make sure that the values are not cached.
9
+ #
10
+ # NOTE: to remove the env value, pass `nil` as the value
11
+ def with_env(data)
12
+ was_values = []
15
13
 
16
- data.each do |key, val|
17
- was_values << [key, ENV[key]]
18
- next ENV.delete(key) if val.nil?
19
- ENV[key] = val
20
- end
14
+ data.each do |key, val|
15
+ was_values << [key, ENV.fetch(key, nil)]
16
+ next ENV.delete(key) if val.nil?
21
17
 
22
- # clear cached env values
23
- Runger.env.clear
24
- yield
25
- ensure
26
- was_values.each do |(key, val)|
27
- next ENV.delete(key) if val.nil?
28
- ENV[key] = val
29
- end
18
+ ENV[key] = val
19
+ end
20
+
21
+ # clear cached env values
22
+ Runger.env.clear
23
+ yield
24
+ ensure
25
+ was_values.each do |(key, val)|
26
+ next ENV.delete(key) if val.nil?
30
27
 
31
- # clear cache again
32
- Runger.env.clear
33
- end
28
+ ENV[key] = val
34
29
  end
30
+
31
+ # clear cache again
32
+ Runger.env.clear
35
33
  end
36
34
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "runger/testing/helpers"
3
+ require 'runger/testing/helpers'
4
4
 
5
5
  if defined?(RSpec::Core) && RSpec.respond_to?(:configure)
6
6
  RSpec.configure do |config|
7
7
  config.include(
8
8
  Runger::Testing::Helpers,
9
9
  type: :config,
10
- file_path: %r{spec/configs}
10
+ file_path: %r{spec/configs},
11
11
  )
12
12
  end
13
13
  end