Class: Necropsy::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/necropsy/configuration.rb

Constant Summary collapse

DEFAULT_FAIL_ON =
:high
DEFAULT_BASELINE =
'.necropsy_baseline.yml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, path:, data:) ⇒ Configuration

Returns a new instance of Configuration.



18
19
20
21
22
# File 'lib/necropsy/configuration.rb', line 18

def initialize(root:, path:, data:)
  @root = File.expand_path(root)
  @path = path
  @data = stringify_keys(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/necropsy/configuration.rb', line 10

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/necropsy/configuration.rb', line 10

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



10
11
12
# File 'lib/necropsy/configuration.rb', line 10

def root
  @root
end

Class Method Details

.load(root:, path: nil) ⇒ Object



12
13
14
15
16
# File 'lib/necropsy/configuration.rb', line 12

def self.load(root:, path: nil)
  config_path = path ? File.expand_path(path, root) : File.join(root, '.necropsy.yml')
  data = File.exist?(config_path) ? YAML.load_file(config_path) : {}
  new(root: root, path: config_path, data: data || {})
end

Instance Method Details

#baseline_pathObject



51
52
53
# File 'lib/necropsy/configuration.rb', line 51

def baseline_path
  fetch('ci', 'baseline') || DEFAULT_BASELINE
end

#bench_precision_thresholdObject



69
70
71
# File 'lib/necropsy/configuration.rb', line 69

def bench_precision_threshold
  (fetch('bench', 'precision_threshold') || 0.85).to_f
end

#bench_recall_thresholdObject



73
74
75
76
# File 'lib/necropsy/configuration.rb', line 73

def bench_recall_threshold
  value = fetch('bench', 'recall_threshold')
  value&.to_f
end

#cache_enabled?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/necropsy/configuration.rb', line 78

def cache_enabled?
  value = fetch('cache', 'enabled')
  value.nil? || value != false
end

#cache_pathObject



83
84
85
# File 'lib/necropsy/configuration.rb', line 83

def cache_path
  fetch('cache', 'path') || '.necropsy_cache/scan.yml'
end

#custom_analyzersObject



32
33
34
# File 'lib/necropsy/configuration.rb', line 32

def custom_analyzers
  Array(fetch('analyzers', 'custom')).compact
end

#dynamic_config(name) ⇒ Object



28
29
30
# File 'lib/necropsy/configuration.rb', line 28

def dynamic_config(name)
  fetch('analyzers', 'dynamic', name.to_s) || {}
end

#entry_point_patternsObject



36
37
38
# File 'lib/necropsy/configuration.rb', line 36

def entry_point_patterns
  Array(fetch('entry_points', 'extra')).compact.map(&:to_s)
end

#factory_methodsObject



91
92
93
# File 'lib/necropsy/configuration.rb', line 91

def factory_methods
  Array(fetch('rta', 'factory_methods') || %w[build create build_stubbed]).map(&:to_s)
end

#fail_onObject



55
56
57
# File 'lib/necropsy/configuration.rb', line 55

def fail_on
  (fetch('ci', 'fail_on') || DEFAULT_FAIL_ON).to_sym
end

#frameworksObject



40
41
42
# File 'lib/necropsy/configuration.rb', line 40

def frameworks
  Array(data['frameworks']).map(&:to_s)
end

#min_observation_daysObject



59
60
61
62
63
# File 'lib/necropsy/configuration.rb', line 59

def min_observation_days
  coverage_days = fetch('analyzers', 'dynamic', 'coverage', 'min_observation_days')
  coverband_days = fetch('analyzers', 'dynamic', 'coverband', 'min_observation_days')
  (coverage_days || coverband_days || 30).to_i
end

#quarantine_daysObject



65
66
67
# File 'lib/necropsy/configuration.rb', line 65

def quarantine_days
  (fetch('quarantine', 'days') || 30).to_i
end

#rails_enabled?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/necropsy/configuration.rb', line 44

def rails_enabled?
  return true if frameworks.include?('rails')

  gemfiles = [File.join(root, 'Gemfile.lock'), File.join(root, 'Gemfile')]
  gemfiles.any? { |file| File.exist?(file) && File.read(file).match?(/(?:^|\s)rails(?:\s|\z|,)/) }
end

#scan_cache_keyObject



87
88
89
# File 'lib/necropsy/configuration.rb', line 87

def scan_cache_key
  data.except('cache')
end

#static_analyzersObject



24
25
26
# File 'lib/necropsy/configuration.rb', line 24

def static_analyzers
  Array(fetch('analyzers', 'static') || %w[name_resolution cha rta]).map(&:to_s)
end