Class: Undercover::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/undercover/changeset.rb

Overview

Base class for different kinds of input

Constant Summary collapse

T_ZERO =
Time.strptime('0', '%s').freeze

Instance Method Summary collapse

Constructor Details

#initialize(dir, compare_base = nil, filter_set = nil) ⇒ Changeset

Returns a new instance of Changeset.



11
12
13
14
15
16
17
# File 'lib/undercover/changeset.rb', line 11

def initialize(dir, compare_base = nil, filter_set = nil)
  @dir = dir
  @repo = Rugged::Repository.new(dir)
  @repo.workdir = Pathname.new(dir).dirname.to_s # TODO: can replace?
  @compare_base = compare_base
  @filter_set = filter_set
end

Instance Method Details

#each_changed_lineObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/undercover/changeset.rb', line 33

def each_changed_line
  full_diff.each_patch do |patch|
    filepath = patch.delta.new_file[:path]
    next if filter_set && !filter_set.include?(filepath)

    patch.each_hunk do |hunk|
      hunk.lines.select(&:addition?).each do |line|
        yield filepath, line.new_lineno
      end
    end
  end
end

#file_pathsObject



29
30
31
# File 'lib/undercover/changeset.rb', line 29

def file_paths
  full_diff.deltas.map { |d| d.new_file[:path] }.sort
end

#filter_with(filter_set) ⇒ Object



52
53
54
# File 'lib/undercover/changeset.rb', line 52

def filter_with(filter_set)
  @filter_set = filter_set
end

#last_modifiedObject



19
20
21
22
23
24
25
26
27
# File 'lib/undercover/changeset.rb', line 19

def last_modified
  mod = file_paths.map do |f|
    path = File.join(repo.workdir, f)
    next T_ZERO unless File.exist?(path)

    File.mtime(path)
  end.max
  mod || T_ZERO
end

#validate(lcov_report_path) ⇒ Object



46
47
48
49
50
# File 'lib/undercover/changeset.rb', line 46

def validate(lcov_report_path)
  return :no_changes if full_diff.deltas.empty?

  :stale_coverage if last_modified > File.mtime(lcov_report_path)
end