Skip to content

Commit

Permalink
Add Map concept so we can customize the way how & when we dump the ma…
Browse files Browse the repository at this point in the history
…p to our storage
  • Loading branch information
pluff committed Apr 15, 2017
1 parent 3754ca9 commit a64d6fa
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.3
Style/GuardClause:
Enabled: false
2 changes: 2 additions & 0 deletions crystalball.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Gem::Specification.new do |spec|

spec.add_dependency 'git'

spec.required_ruby_version = '> 2.3.0'

spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
Expand Down
2 changes: 2 additions & 0 deletions lib/crystalball.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'crystalball/predictor'
require 'crystalball/execution_detector'
require 'crystalball/case_map'
require 'crystalball/map_generator/simple_map'
require 'crystalball/map_generator/standard_map'
require 'crystalball/map_generator'
require 'crystalball/version'

Expand Down
30 changes: 13 additions & 17 deletions lib/crystalball/map_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def start!(config = default_config)
generator = build(config)

RSpec.configure do |c|
c.before(:suite) { generator.load_map }
c.before(:suite) { generator.start! }

c.around(:each) { |e| generator.refresh_for_case(e) }

c.after(:suite) { generator.dump_map }
c.after(:suite) { generator.finalize! }
end
end

Expand All @@ -25,40 +25,36 @@ def build(config)
def default_config
{
execution_detector: ExecutionDetector.new(Dir.pwd),
map_class: StandardMap,
map_storage: MapStorage::YAMLStorage.new('execution_map.yml')
}
end
end


def initialize(execution_detector:, map_storage:)
def initialize(execution_detector:, map_class:, map_storage:)
@execution_detector = execution_detector
@map_storage = map_storage
@map = map_class.new(map_storage)
end

def start!
map_storage.clear!
end

def refresh_for_case(example)
before = Coverage.peek_result
example.run
after = Coverage.peek_result

stash(CaseMap.new(example, execution_detector.detect(before, after)))
map.stash(CaseMap.new(example, execution_detector.detect(before, after)))
end

def dump_map
map_storage.dump @stash
end

def load_map
@stash = map_storage.load
def finalize!
map.dump
end

private

attr_reader :execution_detector, :map_storage

def stash(case_map)
@stash ||= {}
@stash[case_map.case_uid] = case_map.coverage
end
attr_reader :execution_detector, :map, :map_storage
end
end
30 changes: 30 additions & 0 deletions lib/crystalball/map_generator/simple_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Crystalball
class MapGenerator
class SimpleMap
def initialize(storage)
@storage = storage
@raw_map = {}
end

def load
self.raw_map = storage.load
end

def stash(case_map)
raw_map[case_map.case_uid] = case_map.coverage
end

def dump
storage.dump raw_map
end

def clear!
self.raw_map = {}
end

private

attr_accessor :raw_map, :storage
end
end
end
22 changes: 22 additions & 0 deletions lib/crystalball/map_generator/standard_map.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Crystalball
class MapGenerator
class StandardMap < SimpleMap
def initialize(storage, dump_threshold: 100)
super(storage)
@dump_threshold = dump_threshold
end

def stash(case_map)
super
if raw_map.size >= dump_threshold
dump
clear!
end
end

private

attr_reader :dump_threshold
end
end
end
8 changes: 6 additions & 2 deletions lib/crystalball/map_storage/yaml_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ def initialize(path)
@path = path
end

def clear!
File.delete(path) if File.exists?(path)
end

def load
YAML.load(File.read(path)) if File.exists?(path)
YAML.safe_load(File.read(path)) if File.exists?(path)
end

def dump(map)
File.open(path, 'w') { |f| f.write YAML.dump(map) }
File.open(path, 'a') { |f| f.write YAML.dump(map) }
end
end
end
Expand Down

0 comments on commit a64d6fa

Please sign in to comment.