class Covered::Persist
def apply(record)
def apply(record) # The file must still exist: return unless path = expand_path(record[:path]) return unless File.exist? path # If the file has been modified since... we can't use the coverage. return unless mtime = record[:mtime] return if File.mtime(path).to_f > record[:mtime] record[:coverage].each_with_index do |count, index| @output.mark(path, index, count) if count end end
def disable
def disable super # @touched.each do |path| # if @output.accept?(path) # puts "Updated #{path} coverage." # end # end save! end
def enable
def enable super load! end
def initialize(output, path = DEFAULT_PATH)
def initialize(output, path = DEFAULT_PATH) super(output) @path = path @touched = Set.new end
def load!(path = @path)
def load!(path = @path) return unless File.exist?(@path) # Load existing coverage information and mark all files: File.open(@path, "r") do |file| file.flock(File::LOCK_SH) make_unpacker(file).each(&self.method(:apply)) end end
def make_packer(io)
def make_packer(io) packer = MessagePack::Packer.new(io) packer.register_type(0x00, Symbol, :to_msgpack_ext) packer.register_type(0x01, Time) {|object| object.to_s} return packer end
def make_unpacker(io)
def make_unpacker(io) unpacker = MessagePack::Unpacker.new(io) unpacker.register_type(0x00, Symbol, :from_msgpack_ext) unpacker.register_type(0x01, Time, :parse) return unpacker end
def mark(file, line, count)
def mark(file, line, count) @touched << file super end
def save!(path = @path)
def save!(path = @path) # Dump all coverage: File.open(@path, "w") do |file| file.flock(File::LOCK_EX) packer = make_packer(file) self.each do |coverage| packer.write(serialize(coverage)) end packer.flush end end
def serialize(coverage)
def serialize(coverage) { # We want to use relative paths so that moving the repo won't break everything: path: relative_path(coverage.path), coverage: coverage.counts, mtime: File.mtime(coverage.path).to_f, } end