Class: Covered::Persist

Inherits:
Wrapper show all
Defined in:
lib/covered/persist.rb

Constant Summary collapse

DEFAULT_PATH =
".covered.db"

Instance Attribute Summary

Attributes inherited from Wrapper

#output

Instance Method Summary collapse

Methods inherited from Wrapper

#accept?, #each, #expand_path, #relative_path, #to_h

Methods inherited from Base

#accept?, #each, #expand_path, #relative_path

Constructor Details

#initialize(output, path = DEFAULT_PATH) ⇒ Persist

Returns a new instance of Persist.


31
32
33
34
35
36
37
# File 'lib/covered/persist.rb', line 31

def initialize(output, path = DEFAULT_PATH)
	super(output)
	
	@path = path
	
	@touched = Set.new
end

Instance Method Details

#apply(record) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/covered/persist.rb', line 39

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

#disableObject


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/covered/persist.rb', line 100

def disable
	super
	
	# @touched.each do |path|
	# 	if @output.accept?(path)
	# 		puts "Updated #{path} coverage."
	# 	end
	# end
	
	save!
end

#enableObject


94
95
96
97
98
# File 'lib/covered/persist.rb', line 94

def enable
	super
	
	load!
end

#load!(path = @path) ⇒ Object


62
63
64
65
66
67
68
69
70
71
# File 'lib/covered/persist.rb', line 62

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

#make_packer(io) ⇒ Object

def each super do |coverage| if @touched.include?(coverage.path) yield coverage end end end


120
121
122
123
124
125
126
# File 'lib/covered/persist.rb', line 120

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

#make_unpacker(io) ⇒ Object


128
129
130
131
132
133
134
# File 'lib/covered/persist.rb', line 128

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

#mark(file, line, count) ⇒ Object


88
89
90
91
92
# File 'lib/covered/persist.rb', line 88

def mark(file, line, count)
	@touched << file

	super
end

#save!(path = @path) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/covered/persist.rb', line 73

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

#serialize(coverage) ⇒ Object


53
54
55
56
57
58
59
60
# File 'lib/covered/persist.rb', line 53

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