process_settings 0.13.3 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/bin/combine_process_settings +31 -8
- data/bin/diff_process_settings +1 -4
- data/lib/process_settings/abstract_monitor.rb +29 -5
- data/lib/process_settings/testing/helpers.rb +51 -32
- data/lib/process_settings/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe4e35a75d3c8a06396dbc7094874cf5e2509847f66c442ff5297e9c8011cebd
|
4
|
+
data.tar.gz: a7b18e595bde05fd56037570a80008bf3f0cc5d679810dce724a63977fec9a02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f972dcfa9f6e7f988866ac290e71231f0a005842105a284e6a688123e74f9e970ec60749675d0855b47d13d344462169f29cd70b7251b799ae6a6ef4f82b6a6
|
7
|
+
data.tar.gz: e30ad37d7c7508c70471c75322ffcf98c981fb54bdf0e339e6ab512d95c04a8bcd2dad5917381c0a2fbd3ce468e1f04cc18bc0c7919bc9848be3f8b934155853
|
data/README.md
CHANGED
@@ -169,8 +169,8 @@ The settings YAML files are always combined in alphabetical order by file path.
|
|
169
169
|
|
170
170
|
### Testing
|
171
171
|
For testing, it is often necessary to set a specific override hash for the process_settings values to use in
|
172
|
-
that use case. The `ProcessSettings::Testing::Helpers`
|
173
|
-
override a specific hash of process settings, while leaving the rest intact, and resetting back to the defaults
|
172
|
+
that use case. The `ProcessSettings::Testing::RSpec::Helpers` and `ProcessSettings::Testing::Minitest::Helpers` modules are provided for this purpose.
|
173
|
+
They can be used to override a specific hash of process settings, while leaving the rest intact, and resetting back to the defaults
|
174
174
|
after the test case is over. Here are some examples using various testing frameworks:
|
175
175
|
|
176
176
|
#### RSpec
|
@@ -181,7 +181,7 @@ require 'process_settings/testing/helpers'
|
|
181
181
|
RSpec.configure do |config|
|
182
182
|
# ...
|
183
183
|
|
184
|
-
include ProcessSettings::Testing::Helpers
|
184
|
+
config.include ProcessSettings::Testing::RSpec::Helpers
|
185
185
|
|
186
186
|
# Note: the include above will automatically register a global after block that will reset process_settings to their initial values.
|
187
187
|
# ...
|
@@ -206,7 +206,7 @@ end
|
|
206
206
|
require 'process_settings/testing/helpers'
|
207
207
|
|
208
208
|
context SomeClass do
|
209
|
-
include ProcessSettings::Testing::Helpers
|
209
|
+
include ProcessSettings::Testing::Minitest::Helpers
|
210
210
|
|
211
211
|
# Note: the include above will automatically register a teardown block that will reset process_settings to their initial values.
|
212
212
|
|
@@ -92,6 +92,26 @@ def warn_if_old_libyaml_version
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
+
def settings_files_match?(filename_1, filename_2)
|
96
|
+
filename_1 && filename_2 &&
|
97
|
+
File.exist?(filename_1) && File.exist?(filename_2) &&
|
98
|
+
diff_process_settings(filename_1, filename_2)
|
99
|
+
end
|
100
|
+
|
101
|
+
def diff_process_settings(filename_1, filename_2)
|
102
|
+
system(<<~EOS)
|
103
|
+
bundle exec diff_process_settings --silent "#{filename_1}" "#{filename_2}"
|
104
|
+
EOS
|
105
|
+
status_code = $?.exitstatus
|
106
|
+
case status_code
|
107
|
+
when 0
|
108
|
+
true
|
109
|
+
when 1
|
110
|
+
false
|
111
|
+
else
|
112
|
+
raise "diff_process_settings failed with code #{status_code}"
|
113
|
+
end
|
114
|
+
end
|
95
115
|
|
96
116
|
#
|
97
117
|
# main
|
@@ -115,14 +135,17 @@ tmp_output_filename = "#{output_filename}.tmp"
|
|
115
135
|
system("rm -f #{tmp_output_filename}")
|
116
136
|
File.write(tmp_output_filename, yaml_with_warning_comment)
|
117
137
|
|
118
|
-
|
119
|
-
if
|
120
|
-
|
121
|
-
|
138
|
+
if settings_files_match?(options.initial_filename, tmp_output_filename)
|
139
|
+
if settings_files_match?(output_filename, tmp_output_filename)
|
140
|
+
puts "#{options.root_folder}: unchanged" if options.verbose
|
141
|
+
FileUtils.rm_f(tmp_output_filename)
|
122
142
|
else
|
123
|
-
|
124
|
-
mv
|
125
|
-
|
126
|
-
|
143
|
+
puts "#{options.root_folder}: UPDATING (changed now)" if options.verbose
|
144
|
+
FileUtils.mv(tmp_output_filename, output_filename)
|
145
|
+
end
|
146
|
+
else
|
147
|
+
puts "#{options.root_folder}: UPDATING (unchanged now, but changed from initial)" if options.verbose
|
148
|
+
FileUtils.mv(tmp_output_filename, output_filename)
|
149
|
+
end
|
127
150
|
|
128
151
|
exit(0)
|
data/bin/diff_process_settings
CHANGED
@@ -11,14 +11,16 @@ module ProcessSettings
|
|
11
11
|
OnChangeDeprecation = ActiveSupport::Deprecation.new('1.0', 'ProcessSettings::Monitor')
|
12
12
|
|
13
13
|
class AbstractMonitor
|
14
|
+
|
14
15
|
attr_reader :min_polling_seconds, :logger
|
15
|
-
attr_reader :static_context, :statically_targeted_settings
|
16
|
+
attr_reader :static_context, :statically_targeted_settings, :full_context_cache
|
16
17
|
|
17
18
|
def initialize(logger:)
|
18
19
|
@logger = logger or raise ArgumentError, "logger must be not be nil"
|
19
20
|
@on_change_callbacks = []
|
20
21
|
@when_updated_blocks = Set.new
|
21
22
|
@static_context = {}
|
23
|
+
@full_context_cache = {}
|
22
24
|
end
|
23
25
|
|
24
26
|
# This is the main entry point for looking up settings on the Monitor instance.
|
@@ -94,10 +96,20 @@ module ProcessSettings
|
|
94
96
|
def targeted_value(*path, dynamic_context:, required: true)
|
95
97
|
# Merging the static context in is necessary to make sure that the static context isn't shifting
|
96
98
|
# this can be rather costly to do every time if the dynamic context is not changing
|
97
|
-
|
98
|
-
#
|
99
|
-
|
100
|
-
|
99
|
+
|
100
|
+
# Warn in the case where dynamic context was attempting to change a static value
|
101
|
+
changes = dynamic_context.each_with_object({}) do |(key, dynamic_value), result|
|
102
|
+
if static_context.has_key?(key)
|
103
|
+
static_value = static_context[key]
|
104
|
+
if static_value != dynamic_value
|
105
|
+
result[key] = [static_value, dynamic_value]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
changes.empty? or warn("WARNING: static context overwritten by dynamic!\n#{changes.inspect}")
|
111
|
+
|
112
|
+
full_context = full_context_from_cache(dynamic_context)
|
101
113
|
result = statically_targeted_settings.reduce(:not_found) do |latest_result, target_and_settings|
|
102
114
|
# find last value from matching targets
|
103
115
|
if target_and_settings.target.target_key_matches?(full_context)
|
@@ -119,6 +131,18 @@ module ProcessSettings
|
|
119
131
|
end
|
120
132
|
end
|
121
133
|
|
134
|
+
def full_context_from_cache(dynamic_context)
|
135
|
+
if (full_context = full_context_cache[dynamic_context])
|
136
|
+
full_context
|
137
|
+
else
|
138
|
+
dynamic_context.deep_merge(static_context).tap do |full_context|
|
139
|
+
if full_context_cache.size <= 1000
|
140
|
+
full_context_cache[dynamic_context] = full_context
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
122
146
|
private
|
123
147
|
|
124
148
|
class << self
|
@@ -8,47 +8,66 @@ require 'process_settings/testing/monitor'
|
|
8
8
|
|
9
9
|
module ProcessSettings
|
10
10
|
module Testing
|
11
|
-
module
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
module Base
|
12
|
+
module Helpers
|
13
|
+
# Adds the given settings_hash as an override at the end of the process_settings array, with default targeting (true).
|
14
|
+
# Therefore this will override these settings while leaving others alone.
|
15
|
+
#
|
16
|
+
# @param [Hash] settings_hash
|
17
|
+
#
|
18
|
+
# @return none
|
19
|
+
def stub_process_settings(settings_hash)
|
20
|
+
new_target_and_settings = ProcessSettings::TargetAndSettings.new(
|
21
|
+
'<test_override>',
|
22
|
+
Target::true_target,
|
23
|
+
ProcessSettings::Settings.new(settings_hash.deep_stringify_keys)
|
24
|
+
)
|
25
|
+
|
26
|
+
new_process_settings = [
|
27
|
+
*initial_instance.statically_targeted_settings,
|
28
|
+
new_target_and_settings
|
29
|
+
]
|
30
|
+
|
31
|
+
ProcessSettings.instance = ProcessSettings::Testing::Monitor.new(
|
32
|
+
new_process_settings,
|
33
|
+
logger: initial_instance.logger
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
def initial_instance
|
38
|
+
@initial_instance ||= ProcessSettings.instance
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module RSpec
|
44
|
+
module Helpers
|
45
|
+
include Base::Helpers
|
46
|
+
|
47
|
+
class << self
|
48
|
+
def included(including_klass)
|
15
49
|
including_klass.after do
|
16
50
|
ProcessSettings.instance = initial_instance
|
17
51
|
end
|
18
|
-
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module Minitest
|
58
|
+
module Helpers
|
59
|
+
include Base::Helpers
|
60
|
+
|
61
|
+
class << self
|
62
|
+
def included(including_klass)
|
19
63
|
including_klass.define_method(:teardown) do
|
20
64
|
ProcessSettings.instance = initial_instance
|
21
65
|
end
|
22
66
|
end
|
23
67
|
end
|
24
68
|
end
|
25
|
-
# Adds the given settings_hash as an override at the end of the process_settings array, with default targeting (true).
|
26
|
-
# Therefore this will override these settings while leaving others alone.
|
27
|
-
#
|
28
|
-
# @param [Hash] settings_hash
|
29
|
-
#
|
30
|
-
# @return none
|
31
|
-
def stub_process_settings(settings_hash)
|
32
|
-
new_target_and_settings = ProcessSettings::TargetAndSettings.new(
|
33
|
-
'<test_override>',
|
34
|
-
Target::true_target,
|
35
|
-
ProcessSettings::Settings.new(settings_hash.deep_stringify_keys)
|
36
|
-
)
|
37
|
-
|
38
|
-
new_process_settings = [
|
39
|
-
*initial_instance.statically_targeted_settings,
|
40
|
-
new_target_and_settings
|
41
|
-
]
|
42
|
-
|
43
|
-
ProcessSettings.instance = ProcessSettings::Testing::Monitor.new(
|
44
|
-
new_process_settings,
|
45
|
-
logger: initial_instance.logger
|
46
|
-
)
|
47
|
-
end
|
48
|
-
|
49
|
-
def initial_instance
|
50
|
-
@initial_instance ||= ProcessSettings.instance
|
51
|
-
end
|
52
69
|
end
|
70
|
+
|
71
|
+
Helpers = RSpec::Helpers # for backward-compatibility
|
53
72
|
end
|
54
73
|
end
|