Class: Spoom::Cli::Coverage

Inherits:
Thor
  • Object
show all
Includes:
Helper
Defined in:
lib/spoom/cli/coverage.rb

Constant Summary collapse

DATA_DIR =
"spoom_data"

Instance Method Summary collapse

Methods included from Helper

#color?, #colorize, #exec_path, #in_sorbet_project!, #in_sorbet_project?, #say_error, #sorbet_config

Instance Method Details

#open(file = "spoom_report.html") ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/spoom/cli/coverage.rb', line 143

def open(file = "spoom_report.html")
  unless File.exist?(file)
    say_error("No report file to open #{colorize(file, :blue)}")
    $stderr.puts <<~OUT

      If you already generated a report under another name use #{colorize('spoom coverage open PATH', :blue)}.

      To generate a report run #{colorize('spoom coverage report', :blue)}.
    OUT
    exit(1)
  end

  exec("open #{file}")
end

#reportObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/spoom/cli/coverage.rb', line 112

def report
  in_sorbet_project!

  data_dir = options[:data]
  files = Dir.glob("#{data_dir}/*.json")
  if files.empty?
    message_no_data(data_dir)
    exit(1)
  end

  snapshots = files.sort.map do |file|
    json = File.read(file)
    Spoom::Coverage::Snapshot.from_json(json)
  end.filter(&:commit_timestamp).sort_by!(&:commit_timestamp)

  palette = Spoom::Coverage::D3::ColorPalette.new(
    ignore: options[:color_ignore],
    false: options[:color_false],
    true: options[:color_true],
    strict: options[:color_strict],
    strong: options[:color_strong]
  )

  report = Spoom::Coverage.report(snapshots, palette: palette, path: exec_path)
  file = options[:file]
  File.write(file, report.html)
  puts "Report generated under #{file}"
  puts "\nUse #{colorize('spoom coverage open', :blue)} to open it."
end

#snapshotObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spoom/cli/coverage.rb', line 18

def snapshot
  in_sorbet_project!

  path = exec_path
  snapshot = Spoom::Coverage.snapshot(path: path)
  snapshot.print

  save_dir = options[:save]
  return unless save_dir
  FileUtils.mkdir_p(save_dir)
  file = "#{save_dir}/#{snapshot.commit_sha || snapshot.timestamp}.json"
  File.write(file, snapshot.to_json)
  puts "\nSnapshot data saved under #{file}"
end

#timelineObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/spoom/cli/coverage.rb', line 38

def timeline
  in_sorbet_project!
  path = exec_path

  sha_before = Spoom::Git.last_commit(path: path)
  unless sha_before
    say_error("Not in a git repository")
    $stderr.puts "\nSpoom needs to checkout into your previous commits to build the timeline."
    exit(1)
  end

  unless Spoom::Git.workdir_clean?(path: path)
    say_error("Uncommited changes")
    $stderr.puts "\nSpoom needs to checkout into your previous commits to build the timeline."
    $stderr.puts "\nPlease git commit or git stash your changes then try again."
    exit(1)
  end

  save_dir = options[:save]
  FileUtils.mkdir_p(save_dir) if save_dir

  from = parse_time(options[:from], "--from")
  to = parse_time(options[:to], "--to")

  unless from
    intro_sha = Spoom::Git.sorbet_intro_commit(path: path)
    intro_sha = T.must(intro_sha) # we know it's in there since in_sorbet_project!
    from = Spoom::Git.commit_time(intro_sha, path: path)
  end

  timeline = Spoom::Timeline.new(from, to, path: path)
  ticks = timeline.ticks

  if ticks.empty?
    say_error("No commits to replay, try different --from and --to options")
    exit(1)
  end

  ticks.each_with_index do |sha, i|
    date = Spoom::Git.commit_time(sha, path: path)
    puts "Analyzing commit #{sha} - #{date&.strftime('%F')} (#{i + 1} / #{ticks.size})"

    Spoom::Git.checkout(sha, path: path)

    snapshot = T.let(nil, T.nilable(Spoom::Coverage::Snapshot))
    if options[:bundle_install]
      Bundler.with_clean_env do
        next unless bundle_install(path, sha)
        snapshot = Spoom::Coverage.snapshot(path: path)
      end
    else
      snapshot = Spoom::Coverage.snapshot(path: path)
    end
    next unless snapshot

    snapshot.print(indent_level: 2)
    puts "\n"

    next unless save_dir
    file = "#{save_dir}/#{sha}.json"
    File.write(file, snapshot.to_json)
    puts "  Snapshot data saved under #{file}\n\n"
  end
  Spoom::Git.checkout(sha_before, path: path)
end