Class: Pinspec::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/pinspec/config.rb

Constant Summary collapse

FILENAME =
".pinspec.yml"
KEYS =
%w[cases boots sample redact compare-sql verify-level snapshot test-command env].freeze
EMPTY =
{ "env" => {} }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_root, data, path) ⇒ Config

Returns a new instance of Config.



34
35
36
37
38
# File 'lib/pinspec/config.rb', line 34

def initialize(app_root, data, path)
  @app_root = app_root
  @data = data || {}
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



32
33
34
# File 'lib/pinspec/config.rb', line 32

def path
  @path
end

Class Method Details

.load(app_root) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pinspec/config.rb', line 13

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)

  unknown = parsed.keys.map(&:to_s) - 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.message}"
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
# File 'lib/pinspec/config.rb', line 51

def [](key)
  @data[key.to_s]
end

#envObject

Raises:



44
45
46
47
48
49
# File 'lib/pinspec/config.rb', line 44

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/pinspec/config.rb', line 40

def exist?
  !@path.nil?
end

#to_yaml_documentObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pinspec/config.rb', line 65

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.



58
59
60
61
62
63
# File 'lib/pinspec/config.rb', line 58

def value(key, given, default)
  return given unless given == default || given.nil?

  fetched = @data[key.to_s]
  fetched.nil? ? default : fetched
end