Class: RSpec::Covers::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/covers/configuration.rb,
sig/rspec/covers.rbs

Constant Summary collapse

VALID_MODES =
%i[executed checked].freeze
VALID_UNDECLARED_POLICIES =
%i[ignore warn risky].freeze
VALID_RISKY_POLICIES =
%i[fail report].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rspec/covers/configuration.rb', line 17

def initialize
  @strict = false
  @mode = :executed
  @undeclared = :ignore
  @allowlist = []
  @suggest = false
  @root = Dir.pwd
  @production_paths = %w[app lib]
  @suggestion_weights = {
    nc: 1.0,
    ncc: 1.0,
    lcba: 1.0,
    dynamic: 1.0
  }
  @suggestion_limit = 3
  @report_path = nil
  @validate_declarations = true
  @validation_threshold = 0.2
  @risky = :fail
  @json_events = false
end

Instance Attribute Details

#allowlistArray[String]

Returns the value of attribute allowlist.

Returns:

  • (Array[String])


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def allowlist
  @allowlist
end

#json_eventsBoolean

Returns the value of attribute json_events.

Returns:

  • (Boolean)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def json_events
  @json_events
end

#modeSymbol

Returns the value of attribute mode.

Returns:

  • (Symbol)


39
40
41
# File 'lib/rspec/covers/configuration.rb', line 39

def mode
  @mode
end

#production_pathsArray[String]

Returns the value of attribute production_paths.

Returns:

  • (Array[String])


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def production_paths
  @production_paths
end

#report_pathString?

Returns the value of attribute report_path.

Returns:

  • (String, nil)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def report_path
  @report_path
end

#riskySymbol

Returns the value of attribute risky.

Returns:

  • (Symbol)


39
40
41
# File 'lib/rspec/covers/configuration.rb', line 39

def risky
  @risky
end

#rootString

Returns the value of attribute root.

Returns:

  • (String)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def root
  @root
end

#strictBoolean

Returns the value of attribute strict.

Returns:

  • (Boolean)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def strict
  @strict
end

#suggestBoolean

Returns the value of attribute suggest.

Returns:

  • (Boolean)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def suggest
  @suggest
end

#suggestion_limitObject

Returns the value of attribute suggestion_limit.



12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def suggestion_limit
  @suggestion_limit
end

#suggestion_weightsObject

Returns the value of attribute suggestion_weights.



12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def suggestion_weights
  @suggestion_weights
end

#undeclaredSymbol

Returns the value of attribute undeclared.

Returns:

  • (Symbol)


39
40
41
# File 'lib/rspec/covers/configuration.rb', line 39

def undeclared
  @undeclared
end

#validate_declarationsBoolean

Returns the value of attribute validate_declarations.

Returns:

  • (Boolean)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def validate_declarations
  @validate_declarations
end

#validation_thresholdFloat

Returns the value of attribute validation_threshold.

Returns:

  • (Float)


12
13
14
# File 'lib/rspec/covers/configuration.rb', line 12

def validation_threshold
  @validation_threshold
end

Instance Method Details

#allowlisted?(path) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rspec/covers/configuration.rb', line 87

def allowlisted?(path)
  absolute = File.expand_path(path)
  relative = relative_path(absolute)

  Array(allowlist).any? do |pattern|
    expanded = File.expand_path(pattern.to_s, expanded_root)

    File.fnmatch?(pattern.to_s, relative, File::FNM_PATHNAME | File::FNM_EXTGLOB) ||
      File.fnmatch?(expanded, absolute, File::FNM_PATHNAME | File::FNM_EXTGLOB)
  end
end

#production_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/covers/configuration.rb', line 68

def production_file?(path)
  absolute = File.expand_path(path)
  return false unless absolute.start_with?("#{expanded_root}#{File::SEPARATOR}")
  return false unless File.extname(absolute) == ".rb"

  relative = relative_path(absolute)
  return false if relative.start_with?("spec/", "test/", "tmp/", ".")
  return false if allowlisted?(absolute)

  production_paths.any? { |pattern| production_path_match?(relative, pattern) }
end

#production_filesObject



80
81
82
83
84
85
# File 'lib/rspec/covers/configuration.rb', line 80

def production_files
  production_paths.flat_map { |path| files_for_production_path(path) }
                  .map { |path| File.expand_path(path) }
                  .select { |path| production_file?(path) }
                  .uniq
end

#relative_path(path) ⇒ Object



99
100
101
102
103
# File 'lib/rspec/covers/configuration.rb', line 99

def relative_path(path)
  Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(expanded_root)).to_s
rescue ArgumentError
  File.expand_path(path)
end