Skip to content

Commit

Permalink
Refactoring and cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Jun 21, 2009
1 parent aa144ba commit d1cf385
Show file tree
Hide file tree
Showing 16 changed files with 277 additions and 297 deletions.
49 changes: 45 additions & 4 deletions lib/cucover.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
$:.unshift(File.dirname(__FILE__))
require 'dependencies'
require 'cucover/logging_config'
require 'cucover/cli_commands/coverage_of'
require 'cucover/cli_commands/cucumber'
require 'cucover/cli_commands/show_recordings'
require 'cucover/logging_config'
require 'cucover/controller'
require 'cucover/cli'
require 'cucover/monkey'
require 'cucover/rails'
require 'cucover/recording'
require 'cucover/controller'
require 'cucover/cli'
require 'cucover/recorder'
require 'cucover/store'

module Cucover
class << self
def logger
Logging::Logger['Cucover']
end
end

def should_execute?(scenario_or_table_row)
controller(scenario_or_table_row).should_execute?
end

def start_recording!(scenario_or_table_row)
raise("Already recording. Please call stop first.") if recording?

@current_recorder = Recorder.new(scenario_or_table_row)
@current_recorder.start!
record_file(scenario_or_table_row.file_colon_line.split(':').first) # TODO: clean this by extending the feature element
end

def stop_recording!
return unless recording?

@current_recorder.stop!
store.keep!(@current_recorder.recording)
@current_recorder = nil
end

def record_file(source_file)
Cucover.logger.debug("Recording extra source file #{source_file}")
@current_recorder.record_file!(source_file)
end

private

def controller(scenario_or_table_row)
Controller.new(scenario_or_table_row.file_colon_line, store)
end

def recording?
!!@current_recorder
end

def store
@store ||= Store.new
end
end
end
2 changes: 1 addition & 1 deletion lib/cucover/cli_commands/coverage_of.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CoverageOf

def initialize(cli_args)
@filespec = cli_args[1]
@store = ::Cucover::Recording::Store.new
@store = Store.new
end

def execute
Expand Down
2 changes: 1 addition & 1 deletion lib/cucover/cli_commands/show_recordings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Cucover
module CliCommands
class ShowRecordings
def initialize(cli_args)
@store = Cucover::Recording::Store.new
@store = Store.new
end

def execute
Expand Down
10 changes: 2 additions & 8 deletions lib/cucover/controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
module Cucover
class Controller
class << self
def [](scenario_or_table_row)
new(scenario_or_table_row.file_colon_line)
end
end

def initialize(file_colon_line)
def initialize(file_colon_line, store)
@file_colon_line = file_colon_line
@store = Recording::Store.new
@store = store
end

def should_execute?
Expand Down
6 changes: 3 additions & 3 deletions lib/cucover/cucumber_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def file
Cucover.logger.info("Starting #{scenario_or_table_row.class} #{scenario_or_table_row.file_colon_line}")
Cucover::Rails.patch_if_necessary

if Cucover::Controller[scenario_or_table_row].should_execute?
Cucover::Recording.start(scenario_or_table_row)
if Cucover.should_execute?(scenario_or_table_row)
Cucover.start_recording!(scenario_or_table_row)
else
announce "[ Cucover - Skipping clean scenario ]"
scenario_or_table_row.skip_invoke!
end
end

After do
Cucover::Recording.stop
Cucover.stop_recording!
end
5 changes: 2 additions & 3 deletions lib/cucover/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ def patch_if_necessary
return if @patched
return unless defined?(ActionView)

Monkey.extend_every ActionView::Template => Cucover::Rails::RecordsRenders

Monkey.extend_every ActionView::Template => Cucover::Rails::RecordsRenders
@patched = true
end
end

module RecordsRenders
def render
Cucover::Recording.record_file(@filename)
Cucover.record_file(@filename)
super
end
end
Expand Down
37 changes: 37 additions & 0 deletions lib/cucover/recorder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Cucover
class Recorder
def initialize(scenario_or_table_row)
@scenario_or_table_row = scenario_or_table_row
@analyzer = Rcov::CodeCoverageAnalyzer.new
@additional_covered_files = []
end

def record_file!(source_file)
unless @additional_covered_files.include?(source_file)
@additional_covered_files << source_file
end
end

def start!
@start_time = Time.now
@analyzer.install_hook
end

def stop!
@end_time = Time.now
@analyzer.remove_hook
Cucover.logger.info("Finished recording #{@scenario_or_table_row.file_colon_line}.")
Cucover.logger.debug("Covered files: #{@analyzer.analyzed_files.join(',')}")
Cucover.logger.debug("Additional Covered files: #{@additional_covered_files.join(',')}")
end

def recording
Recording.new(
@scenario_or_table_row.file_colon_line,
@scenario_or_table_row.exception,
@additional_covered_files,
@analyzer,
@start_time, @end_time)
end
end
end
88 changes: 54 additions & 34 deletions lib/cucover/recording.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
require 'cucover/recording/recorder'
require 'cucover/recording/data'
require 'cucover/recording/covered_file'
require 'cucover/recording/store'

module Cucover
module Recording
class << self
def start(scenario_or_table_row)
raise("Already recording. Please call stop first.") if recording?

@current_recorder = Recording::Recorder.new(scenario_or_table_row)
@current_recorder.start
record_file(scenario_or_table_row.file_colon_line.split(':').first) # TODO: clean this by extending the feature element
end
class Recording < Struct.new(
:file_colon_line,
:exception,
:additional_covered_files,
:analyzer,
:start_time, :end_time)

def stop
return unless recording?
@current_recorder.stop
store.keep(@current_recorder.to_data)
@current_recorder = nil
end

def record_file(source_file)
Cucover.logger.debug("Recording extra source file #{source_file}")
@current_recorder.record_file(source_file)
end
def feature_filename
file_colon_line.split(':').first
end

def covers_file?(source_file)
covered_files.include?(source_file)
end

def covers_line?(source_file, line_number)
covered_files.detect{ |f| f.file == source_file }.covers_line?(line_number)
end

def covered_files
@covered_files ||= analyzed_covered_files + additional_covered_files
end

def record_exception(exception)
@current_recorder.fail!(exception)
def failed?
!!exception
end

private

def additional_covered_files
super.map do |filename|
CoveredFile.new(filename, nil, self)
end

private
end

def recording?
!!@current_recorder
def analyzed_covered_files
filtered_analyzed_files.map do |filename|
lines, marked_info, count_info = analyzer.data(filename)
CoveredFile.new(filename, marked_info, self)
end
end

def store
store ||= Recording::Store.new
def boring?(file)
[
/gem/,
/vendor/,
/lib\/ruby/,
/cucover/
].any? do |expression|
file.match expression
end
end

def filtered_analyzed_files
analyzer.analyzed_files.reject{ |f| boring?(f) }
end

def normalized_files
cleaned_analyzed_files + additional_covered_files
end
end
end
end
require 'cucover/recording/covered_file'
97 changes: 47 additions & 50 deletions lib/cucover/recording/covered_file.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
module Cucover
module Recording
class CoveredFile

module HasLineNumberDetail
def covered_lines
return @covered_lines if @covered_lines
@covered_lines = []
@marked_info.each_with_index do |covered, index|
line_number = index + 1
@covered_lines << line_number if covered
end
@covered_lines
end
end

attr_reader :file

def initialize(full_filename, marked_info, recording)
@recording = recording
@full_filename = full_filename
@marked_info = marked_info
@file = File.expand_path(full_filename).gsub(/^#{Dir.pwd}\//, '')

extend HasLineNumberDetail if @marked_info
end

def dirty?
Cucover.logger.debug("#{file} last modified at #{File.mtime(@full_filename)}")
Cucover.logger.debug("#{file} recording started at #{@recording.start_time}")
result = File.mtime(@full_filename).to_i >= @recording.start_time.to_i
Cucover.logger.debug(result ? "dirty" : "not dirty")
result
end

def covers_line?(line_number)
covered_lines.include?(line_number)
end

def ==(other)
other == file
end

def to_s
"#{file}:#{covered_lines.join(':')}"
end

private

class Recording::CoveredFile
attr_reader :file

def initialize(full_filename, rcov_marked_info, recording)
@full_filename = full_filename
@rcov_marked_info = rcov_marked_info
@recording = recording
@file = File.expand_path(full_filename).gsub(/^#{Dir.pwd}\//, '')

extend HasLineNumberDetail if @rcov_marked_info
end

def dirty?
Cucover.logger.debug("#{file} last modified at #{File.mtime(@full_filename)}")
Cucover.logger.debug("#{file} recording started at #{@recording.start_time}")
result = File.mtime(@full_filename).to_i >= @recording.start_time.to_i
Cucover.logger.debug("verdict: #{(result ? "dirty" : "not dirty")}")
result
end

def covers_line?(line_number)
covered_lines.include?(line_number)
end

def ==(other)
other == file
end

def to_s
"#{file}:#{covered_lines.join(':')}"
end

private

def covered_lines
['<unknown lines>']
end

module HasLineNumberDetail
def covered_lines
['<unknown lines>']
return @covered_lines if @covered_lines

@covered_lines = []
@rcov_marked_info.each_with_index do |covered, index|
line_number = index + 1
@covered_lines << line_number if covered
end
@covered_lines
end

end
end
end
Loading

0 comments on commit d1cf385

Please sign in to comment.