Class: Undercover::Changeset

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, compare_base = nil) ⇒ Changeset

Returns a new instance of Changeset.



17
18
19
20
21
22
23
# File 'lib/undercover/changeset.rb', line 17

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

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



14
15
16
# File 'lib/undercover/changeset.rb', line 14

def files
  @files
end

Instance Method Details

#each_changed_lineObject



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

def each_changed_line
  files.each do |filepath, line_numbers|
    line_numbers.each { |ln| yield filepath, ln }
  end
end

#file_pathsObject



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

def file_paths
  files.keys.sort
end

#last_modifiedObject



37
38
39
40
41
42
43
44
45
# File 'lib/undercover/changeset.rb', line 37

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

#updateObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/undercover/changeset.rb', line 25

def update
  full_diff.each_patch do |patch|
    filepath = patch.delta.new_file[:path]
    line_nums = patch.each_hunk.map do |hunk|
      # TODO: optimise this to use line ranges!
      hunk.lines.select(&:addition?).map(&:new_lineno)
    end.flatten
    @files[filepath] = line_nums if line_nums.any?
  end
  self
end

#validate(lcov_report_path) ⇒ Object

TODO: refactor to a standalone validator (depending on changeset AND lcov) TODO: add specs



59
60
61
62
# File 'lib/undercover/changeset.rb', line 59

def validate(lcov_report_path)
  return :no_changes if files.empty?
  return :stale_coverage if last_modified > File.mtime(lcov_report_path)
end