Module: SimpleCov::CLI::Status::Facts

Extended by:
Facts
Included in:
Facts
Defined in:
lib/simplecov/cli/status/facts.rb

Instance Method Summary collapse

Instance Method Details

#age_of(timestamp) ⇒ Object



30
31
32
# File 'lib/simplecov/cli/status/facts.rb', line 30

def age_of(timestamp)
  whole_seconds(Process.clock_gettime(Process::CLOCK_REALTIME) - timestamp)
end

#behind(commit) ⇒ Object

Commits between the report's commit and HEAD: 0 at HEAD, nil when unanswerable, meaning no commit recorded, no git, or a commit this repository has never seen.



47
48
49
50
51
52
# File 'lib/simplecov/cli/status/facts.rb', line 47

def behind(commit)
  return nil unless commit.instance_of?(String)

  stdout, _detail, success = Git.capture("rev-list", "--count", "#{commit}..HEAD")
  commit_count(_ = stdout) if success
end

#commit_count(output) ⇒ Object

Integer ignores surrounding whitespace on its own, so git's trailing newline needs no trimming. The base is spelled out because bare Integer reads a leading zero as octal.



57
58
59
# File 'lib/simplecov/cli/status/facts.rb', line 57

def commit_count(output)
  Integer(output, 10)
end

#gather(document, resultset_path) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/simplecov/cli/status/facts.rb', line 9

def gather(document, resultset_path)
  contexts = document["contexts"]
  meta_facts(document).merge(
    totals: totals(document["total"]),
    contexts: (contexts.size if contexts.instance_of?(Array)),
    resultset_path: resultset_path, resultset: readable_resultset(resultset_path)
  )
end

#meta_facts(document) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simplecov/cli/status/facts.rb', line 18

def meta_facts(document)
  none = {} #: Hash[String, untyped]
  recorded = document["meta"]
  meta = recorded.instance_of?(Hash) ? recorded : none
  generated = parse_time(meta["timestamp"])
  {
    generated_at: meta["timestamp"], age: generated && whole_seconds(Time.now - generated),
    version: meta["simplecov_version"], command_name: meta["command_name"],
    commit: meta["commit"], behind: behind(meta["commit"])
  }
end

#parse_time(value) ⇒ Object



38
39
40
41
42
# File 'lib/simplecov/cli/status/facts.rb', line 38

def parse_time(value)
  Time.iso8601(value) if value.instance_of?(String)
rescue ArgumentError
  nil
end

#readable_resultset(path) ⇒ Object

An unreadable resultset answers nothing rather than raising: its absence is a fact, not an error.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/simplecov/cli/status/facts.rb', line 73

def readable_resultset(path)
  parsed = JSON.parse(File.read(path))
  return nil unless parsed.instance_of?(Hash)

  parsed.map do |command, data|
    timestamp = data["timestamp"] if data.instance_of?(Hash)
    {command: command, age: (age_of(timestamp) if timestamp.is_a?(Numeric))}
  end
rescue SystemCallError, JSON::ParserError
  nil
end

#totals(total) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/simplecov/cli/status/facts.rb', line 61

def totals(total)
  return {} unless total.instance_of?(Hash)

  rows = {"line" => "lines", "branch" => "branches", "method" => "methods"}.filter_map do |name, key|
    percent = total.dig(key, "percent")
    [name, percent] if percent.is_a?(Numeric)
  end
  rows.to_h
end

#whole_seconds(elapsed) ⇒ Object



34
35
36
# File 'lib/simplecov/cli/status/facts.rb', line 34

def whole_seconds(elapsed)
  elapsed.truncate
end