Module: SimpleCov::ResultMerger::ResultsetStore

Defined in:
lib/simplecov/result_merger/resultset_store.rb

Overview

Reads and writes the persistent .resultset.json cache, including file-lock synchronization between processes and atomic temp-file renames so concurrent readers don't observe a truncated file.

Class Method Summary collapse

Class Method Details

.resultset_pathObject



19
20
21
# File 'lib/simplecov/result_merger/resultset_store.rb', line 19

def resultset_path
  File.join(SimpleCov.coverage_path, ".resultset.json")
end

.synchronizeObject

Serialize threads before taking the process-wide file lock. Nested calls by the owning thread bypass flock so they cannot deadlock on a second descriptor for the same lock file.



38
39
40
41
42
# File 'lib/simplecov/result_merger/resultset_store.rb', line 38

def synchronize(&)
  return yield if LOCK_MONITOR.mon_owned?

  LOCK_MONITOR.synchronize { with_flock(&) }
end

.with_flockObject



44
45
46
47
48
49
50
# File 'lib/simplecov/result_merger/resultset_store.rb', line 44

def with_flock
  FileUtils.mkdir_p(SimpleCov.coverage_path)
  File.open(writelock_path, "w+") do |f|
    f.flock(File::LOCK_EX)
    yield
  end
end

.write(resultset) ⇒ Object

Compact JSON, not pretty-printed: this is a machine-read cache that every parallel worker rewrites wholesale, and on a large project pretty printing nearly doubles the bytes written, read back, and parsed on each of those store-merge round trips.



31
32
33
# File 'lib/simplecov/result_merger/resultset_store.rb', line 31

def write(resultset)
  AtomicFile.write(resultset_path, "#{JSON.generate(resultset)}\n")
end

.writelock_pathObject



23
24
25
# File 'lib/simplecov/result_merger/resultset_store.rb', line 23

def writelock_path
  File.join(SimpleCov.coverage_path, ".resultset.json.lock")
end