Module: EasyCov

Includes:
Filters
Defined in:
lib/easycov.rb,
lib/easycov/filters.rb

Defined Under Namespace

Modules: Filters

Constant Summary collapse

TOP_PID =

self

Process.pid

Constants included from Filters

Filters::IGNORE_GEMS, Filters::IGNORE_OUTSIDE_ROOT, Filters::IGNORE_STDLIB, Filters::IGNORE_TESTS

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Filters

stdlib_paths

Class Attribute Details

.pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/easycov.rb', line 15

def path
  @path
end

Returns the value of attribute resolve_symlinks.



15
16
17
# File 'lib/easycov.rb', line 15

def resolve_symlinks
  @resolve_symlinks
end

.rootObject

Returns the value of attribute root.



15
16
17
# File 'lib/easycov.rb', line 15

def root
  @root
end

Class Method Details

.checkpointObject

Write coverage to disk and restart



50
51
52
53
# File 'lib/easycov.rb', line 50

def checkpoint
  dump()
  start()
end

.detect_path!Object

Detect the coverage path

Uses the first of Dir.pwd/.coverage or Dir.pwd/coverage



73
74
75
76
77
78
79
80
81
# File 'lib/easycov.rb', line 73

def detect_path!
  %w{.coverage coverage}.each { |c|
    cov_dir = File.join(Dir.pwd, c)
    if File.directory?(cov_dir) then
      EasyCov.path = cov_dir
      return
    end
  }
end

.dumpObject

Dump coverage to disk in a thread-safe way



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/easycov.rb', line 28

def dump
  return if ENV["DISABLE_EASYCOV"] == "1"
  Coverage.start # always make sure we are started

  FileUtils.mkdir_p(@path)

  if ENV["PARALLEL_EASYCOV"] == "1" then
    # in parallel mode, write output to separate files for each process
    # to be merged later, via #merge
    write_json(File.join(@path, ".tmp.#{$$}.resultset.json"))
    return
  end

  # default is to lock the output file
  output = File.join(@path, ".resultset.json")
  EasyCov.lock(output) do
    write_json(output)
  end

end

.filter(&block) ⇒ Object

Add filter block



61
62
63
# File 'lib/easycov.rb', line 61

def filter(&block)
  filters << block
end

.filtersObject

List of filters



56
57
58
# File 'lib/easycov.rb', line 56

def filters
  @filters ||= []
end

.install_exit_hookObject



83
84
85
86
87
88
# File 'lib/easycov.rb', line 83

def install_exit_hook
  return if ENV["DISABLE_EASYCOV"] == "1"
  Kernel.at_exit do
    EasyCov.checkpoint
  end
end

.lock(lockfile, &block) ⇒ Object

Create a flock on the given file

Parameters:

  • lockfile (String)

    to lock on

  • (Block)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/easycov.rb', line 114

def lock(lockfile, &block)
  lockfile = "#{lockfile}.lock"

  FileUtils.touch(lockfile)
  lock = File.new(lockfile)
  lock.flock(File::LOCK_EX)

  block.call()

  begin
    lock.flock(File::LOCK_UN)
  rescue
  end

  begin
    File.delete(lockfile)
  rescue
  end
end

.merge!Object

Merge all temporary coverage files into main file



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/easycov.rb', line 91

def merge!

  output = File.join(@path, ".resultset.json")
  files = [ output ] + Dir.glob(File.join(@path, ".tmp.*.resultset.json"))

  data = {}
  files.each do |f|
    next if !File.exists? f
    data.merge!(MultiJson.load(File.read(f)))
    File.delete(f)
  end

  prune(data)

  # write to final dest
  File.open(output+".tmp", 'w'){ |f| f.write(MultiJson.dump(data)) }
  File.rename(output+".tmp", output)
end

.startObject

Start coverage engine Can be run multiple times without side-effect.



19
20
21
22
23
24
25
# File 'lib/easycov.rb', line 19

def start
  return if ENV["DISABLE_EASYCOV"] == "1"
  @resolve_symlinks = true if @resolve_symlinks.nil?
  @path ||= File.expand_path("coverage")
  @root ||= Dir.pwd # only set first time
  Coverage.start
end