Class: Hashira::CI::Baseline

Inherits:
Object
  • Object
show all
Defined in:
lib/hashira/ci/baseline.rb

Constant Summary collapse

SCHEMA_VERSION =
4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, recorded, scope = Hashira::CI::Scope.none) ⇒ Baseline

Returns a new instance of Baseline.



24
25
26
27
28
# File 'lib/hashira/ci/baseline.rb', line 24

def initialize(path, recorded, scope = Hashira::CI::Scope.none)
  @path = path
  @recorded = recorded
  @scope = scope
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/hashira/ci/baseline.rb', line 30

def path
  @path
end

Class Method Details

.load(path, analyzers: [], targets: []) ⇒ Object



8
9
10
# File 'lib/hashira/ci/baseline.rb', line 8

def self.load(path, analyzers: [], targets: [])
  new(path, read(path), Hashira::CI::Scope.new(analyzers:, targets:))
end

.read(path) ⇒ Object



12
13
14
15
16
# File 'lib/hashira/ci/baseline.rb', line 12

def self.read(path)
  return {} unless path && File.exist?(path)
  recorded = JSON.parse(File.read(path))
  recorded.is_a?(Hash) ? recorded : raise(JSON::ParserError, "its top level is a list, not an object")
end

.scored(recorded) ⇒ Object



38
# File 'lib/hashira/ci/baseline.rb', line 38

def self.scored(recorded) = recorded.is_a?(Array) ? recorded.to_h { [it, nil] } : recorded

.trouble(path) ⇒ Object



18
19
20
21
22
# File 'lib/hashira/ci/baseline.rb', line 18

def self.trouble(path)
  read(path) && nil
rescue JSON::ParserError, SystemCallError => error
  "#{path} is not a usable baseline — #{error.message.lines.first.strip}. Re-record it with --update-baseline"
end

Instance Method Details

#analyzersObject



44
# File 'lib/hashira/ci/baseline.rb', line 44

def analyzers = @recorded.fetch("analyzers", wanted[:analyzers])

#edgesObject



34
# File 'lib/hashira/ci/baseline.rb', line 34

def edges = @recorded.fetch("edges", [])

#exist?Boolean

Returns:

  • (Boolean)


32
# File 'lib/hashira/ci/baseline.rb', line 32

def exist? = File.exist?(@path)

#findingsObject



36
# File 'lib/hashira/ci/baseline.rb', line 36

def findings = self.class.scored(@recorded.fetch("findings", {}))

#findings?Boolean

Returns:

  • (Boolean)


40
# File 'lib/hashira/ci/baseline.rb', line 40

def findings? = @recorded.key?("findings")

#packagingObject



42
# File 'lib/hashira/ci/baseline.rb', line 42

def packaging = @recorded.fetch("packaging", "folder")

#targetsObject



46
# File 'lib/hashira/ci/baseline.rb', line 46

def targets = @recorded.fetch("targets", wanted[:targets])

#wantedObject



48
# File 'lib/hashira/ci/baseline.rb', line 48

def wanted = @scope.to_h

#write(edges, findings, packaging:) ⇒ Object



50
51
52
# File 'lib/hashira/ci/baseline.rb', line 50

def write(edges, findings, packaging:)
  File.write(@path, JSON.pretty_generate(payload(edges, findings, packaging)) << "\n")
end