Module: SimpleCov::ResultMerger::ResultsetStore

Extended by:
ResultsetStore
Included in:
ResultsetStore
Defined in:
lib/simplecov/result_merger/resultset_store.rb

Instance Method Summary collapse

Instance Method Details

#holding_writelockObject



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

def holding_writelock
  open_file(writelock_path, "w+") do |file|
    file.flock(File::LOCK_EX)
    yield
  end
end

#open_file(name, mode) ⇒ Object

mutant:disable — Ruby 4.0 removed Kernel#open's leading-pipe command mode, so no test can tell File.open from open here any more. The explicit receiver is kept: on older rubies it is what refuses to run a lock path as a command.



57
58
59
# File 'lib/simplecov/result_merger/resultset_store.rb', line 57

def open_file(name, mode, &)
  File.open(name, mode, &)
end

#resultset_pathObject



16
17
18
# File 'lib/simplecov/result_merger/resultset_store.rb', line 16

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

#synchronizeObject

Threads are serialized before the process-wide file lock is taken. Nested calls by the owning thread bypass flock so they cannot deadlock on a second descriptor for the same lock file.



35
36
37
38
39
# File 'lib/simplecov/result_merger/resultset_store.rb', line 35

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

  LOCK_MONITOR.synchronize { with_flock(&) }
end

#with_flockObject



41
42
43
44
# File 'lib/simplecov/result_merger/resultset_store.rb', line 41

def with_flock(&)
  FileUtils.mkdir_p(SimpleCov.coverage_path)
  holding_writelock(&)
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.



28
29
30
# File 'lib/simplecov/result_merger/resultset_store.rb', line 28

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

#writelock_pathObject



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

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