Class: Pinspec::Config
- Inherits:
-
Object
- Object
- Pinspec::Config
- Defined in:
- lib/pinspec/config.rb
Constant Summary collapse
- FILENAME =
".pinspec.yml"- KEYS =
%w[cases boots sample redact compare-sql verify-level test-command env].freeze
- RETIRED_KEYS =
Keys that used to be valid. A config written for an older pinspec is ignored with a note rather than failing the run - an upgrade should not stop a build.
%w[snapshot].freeze
- EMPTY =
{ "env" => {} }.freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #env ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(app_root, data, path) ⇒ Config
constructor
A new instance of Config.
- #to_yaml_document ⇒ Object
-
#value(key, given, default) ⇒ Object
CLI flag beats file beats default.
Constructor Details
#initialize(app_root, data, path) ⇒ Config
Returns a new instance of Config.
45 46 47 48 49 |
# File 'lib/pinspec/config.rb', line 45 def initialize(app_root, data, path) @app_root = app_root @data = data || {} @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
43 44 45 |
# File 'lib/pinspec/config.rb', line 43 def path @path end |
Class Method Details
.load(app_root) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pinspec/config.rb', line 17 def self.load(app_root) path = File.join(app_root.to_s, FILENAME) return new(app_root, EMPTY, nil) unless File.file?(path) parsed = YAML.safe_load(Analyzer::Source.read(path), permitted_classes: [], aliases: false) || {} raise ConfigInvalid, "#{path} must contain a mapping, got #{parsed.class}" unless parsed.is_a?(Hash) retired = parsed.keys.map(&:to_s) & RETIRED_KEYS unless retired.empty? warn "pinspec: #{path} sets #{retired.map(&:inspect).join(', ')}, which " \ "#{retired.size == 1 ? 'was' : 'were'} removed. Ignoring #{retired.size == 1 ? 'it' : 'them'}; " \ "delete the line to silence this." end unknown = parsed.keys.map(&:to_s) - KEYS - RETIRED_KEYS unless unknown.empty? raise ConfigInvalid, "#{path} has unknown #{unknown.size == 1 ? 'key' : 'keys'} " \ "#{unknown.map(&:inspect).join(', ')}. Known keys: #{KEYS.join(', ')}." end new(app_root, parsed, path) rescue Psych::SyntaxError => e raise ConfigInvalid, "#{path} is not valid YAML: #{e.}" end |
Instance Method Details
#[](key) ⇒ Object
62 63 64 |
# File 'lib/pinspec/config.rb', line 62 def [](key) @data[key.to_s] end |
#env ⇒ Object
55 56 57 58 59 60 |
# File 'lib/pinspec/config.rb', line 55 def env raw = @data["env"] || {} raise ConfigInvalid, "#{@path}: `env` must be a mapping of KEY: VALUE" unless raw.is_a?(Hash) raw.transform_keys(&:to_s).transform_values(&:to_s) end |
#exist? ⇒ Boolean
51 52 53 |
# File 'lib/pinspec/config.rb', line 51 def exist? !@path.nil? end |
#to_yaml_document ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pinspec/config.rb', line 76 def to_yaml_document <<~YAML # pinspec settings. Every key here is a CLI flag you would otherwise repeat; # a flag on the command line still wins. # # The app's Ruby is detected on each run from .ruby-version or .tool-versions, # so it is deliberately not recorded here. Use `env` only for what pinspec # cannot work out for itself: # # env: # DATABASE_USERNAME: myapp # #{YAML.dump(@data).sub(/\A---\n/, "").chomp} YAML end |
#value(key, given, default) ⇒ Object
CLI flag beats file beats default. Thor cannot tell a flag that was typed from one that defaulted, so the caller passes the default separately and an option equal to it is treated as untyped.
69 70 71 72 73 74 |
# File 'lib/pinspec/config.rb', line 69 def value(key, given, default) return given unless given == default || given.nil? fetched = @data[key.to_s] fetched.nil? ? default : fetched end |