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?, #add, #clear, #expand_path, #mark, #relative_path, #start, #to_h

Methods inherited from Base

#accept?, #add, #clear, #expand_path, #mark, #relative_path, #start

Constructor Details

#initialize(output, path = DEFAULT_PATH) ⇒ Persist

Returns a new instance of Persist.



16
17
18
19
20
# File 'lib/covered/persist.rb', line 16

def initialize(output, path = DEFAULT_PATH)
  super(output)
  
  @path = self.expand_path(path)
end

Instance Method Details

#apply(record, ignore_mtime: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/covered/persist.rb', line 22

def apply(record, ignore_mtime: false)
  if coverage = record[:coverage]
    if path = record[:path]
      path = self.expand_path(path)
      coverage.path = path
    end
    
    if ignore_mtime || coverage.fresh?
      add(coverage)
      return true
    end
  end
  
  return false
end

#each(&block) ⇒ Object



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

def each(&block)
  return to_enum unless block_given?
  
  @output.clear
  self.load!
  
  super
end

#finishObject



80
81
82
83
84
# File 'lib/covered/persist.rb', line 80

def finish
  super
  
  self.save!
end

#load!(**options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/covered/persist.rb', line 48

def load!(**options)
  return unless File.exist?(@path)
  
  # Load existing coverage information and mark all files:
  File.open(@path, "rb") do |file|
    file.flock(File::LOCK_SH)
    
    make_unpacker(file).each do |record|
      # pp load: record
      self.apply(record, **options)
    end
  end
rescue
  raise LoadError, "Failed to load coverage from #{@path}, maybe old format or corrupt!"
end

#make_factoryObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/covered/persist.rb', line 95

def make_factory
  factory = MessagePack::Factory.new
  
  factory.register_type(0x00, Symbol)
  
  factory.register_type(0x01, Time,
    packer: MessagePack::Time::Packer,
    unpacker: MessagePack::Time::Unpacker
  )
  
  factory.register_type(0x20, Source,
    recursive: true,
    packer: :serialize,
    unpacker: :deserialize,
  )
  
  factory.register_type(0x21, Coverage,
    recursive: true,
    packer: :serialize,
    unpacker: :deserialize,
  )
  
  return factory
end

#make_packer(io) ⇒ Object



120
121
122
# File 'lib/covered/persist.rb', line 120

def make_packer(io)
  return make_factory.packer(io)
end

#make_unpacker(io) ⇒ Object



124
125
126
# File 'lib/covered/persist.rb', line 124

def make_unpacker(io)
  return make_factory.unpacker(io)
end

#save!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/covered/persist.rb', line 64

def save!
  # Dump all coverage:
  File.open(@path, "ab") do |file|
    file.flock(File::LOCK_EX)
    
    packer = make_packer(file)
    
    @output.each do |coverage|
      # pp save: coverage
      packer.write(serialize(coverage))
    end
    
    packer.flush
  end
end

#serialize(coverage) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/covered/persist.rb', line 38

def serialize(coverage)
  {
    # We want to use relative paths so that moving the repo won't break everything:
    pid: Process.pid,
    path: relative_path(coverage.path),
    # relative_path: relative_path(coverage.path),
    coverage: coverage,
  }
end