Permalink
Browse files

Added a rake task to ensure a certain locode-to-lospec ratio.

  • Loading branch information...
ELLIOTTCABLE committed Apr 13, 2008
1 parent 17b49e7 commit ede8387efd3f617fda72000947a31b0d97e704ea
Showing with 121 additions and 14 deletions.
  1. +17 −14 Rakefile.rb
  2. +26 −0 bugs/issue-c5e7e38b18dfcc5f9259b133d924acac4f819648.yaml
  3. +78 −0 lib/spec/rake/verify_spec_ratio.rb
@@ -6,6 +6,7 @@
require 'rake/rdoctask'
require 'spec/rake/spectask'
require 'spec/rake/verify_rcov'
require 'lib/spec/rake/verify_spec_ratio'
require 'fileutils'
require 'merb-core'
require 'rubigen'
@@ -48,11 +49,14 @@
t.rcov_dir = :meta / :coverage
end

desc "Enforces coverage maintenance"
RCov::VerifyTask.new(:verify) do |t|
t.threshold = 100
t.index_html = :meta / :coverage / 'index.html'
end

Spec::Rake::VerifySpecRatioTask.new(:ratio) do |t|
t.ratio = 0.50
end

task :open do
system 'open ' + :meta / :coverage / 'index.html' if PLATFORM['darwin']
@@ -61,10 +65,11 @@

namespace :ditz do

desc "Current issues summary"
desc "Show current issue status overview"
task :status do
system 'ditz status'
end
desc "Show currently open issues"
task :todo do
system 'ditz todo'
end
@@ -73,32 +78,29 @@
system 'ditz log'
end

desc "Generate issues to meta/issues"
# desc "Generate issues to meta/issues"
task :html do
# `'d instead of system'd, because I don't want that output cluttering shit
`ditz html meta/issues`
end
# desc "Opens meta/issues in your main browser, if you are using a Macintosh"
task :'html:open' do
system 'open ' + :meta / :issues / 'index.html' if PLATFORM['darwin']
end

desc "Stage all issues to git before commiting"
desc "Stage all issues to git (to be run before commiting, or just use aok)"
task :stage do
system 'git-add bugs/'
end
end

# Well that was a waste of time...
# Rake::RDocTask.new do |d|
# d.main = "meta/docs/index.rdoc"
# d.rdoc_files.include("app/**/*.rb", "lib/**/*.rb")
# d.rdoc_dir = :meta / :documentation
# end

desc 'Check everything over before commiting'
task :aok => [:check_config, :'rcov:run', :'rcov:verify', :'ditz:stage', :'ditz:html', :'ditz:todo', :'ditz:status', :'rcov:open', :'ditz:html:open']
task :aok => [:check_config,
:'rcov:run', :'rcov:verify', :'rcov:ratio', :'rcov:open',
:'ditz:stage', :'ditz:html', :'ditz:todo', :'ditz:status', :'ditz:html:open']

# desc 'Task run during continuous integration'
task :cruise => [:check_config, :'rcov:plain', :'ditz:html', :'rcov:verify']
task :cruise => [:check_config, :'rcov:plain', :'ditz:html', :'rcov:verify', :'rcov:ratio']

# Tasks for systems
Dir[Merb.root / "systems" / "*"].each do |system|
@@ -118,7 +120,8 @@

desc "Enforces coverage maintenance"
RCov::VerifyTask.new(:verify) do |t|
t.threshold = 100
t.threshold = 95
t.require_exact_threshold = false
t.index_html = :systems / system / :meta / :coverage / 'index.html'
end

@@ -0,0 +1,26 @@
--- !ditz.rubyforge.org,2008-03-06/issue
title: Enforce LoC / LoS ratio
desc: Specs should fail if there are less lines of specs than lines of code. Just another tool, like the rcov verification task, too attempt to help enforce spec coverage
type: :feature
component: RDie
release: 0 (stable API)
reporter: elliottcable <git@elliottcable.name>
status: :closed
disposition: :fixed
creation_time: 2008-04-13 06:16:28.900008 Z
references: []

id: c5e7e38b18dfcc5f9259b133d924acac4f819648
log_events:
- - 2008-04-13 06:16:30.283790 Z
- elliottcable <git@elliottcable.name>
- created
- ""
- - 2008-04-13 06:16:46.202566 Z
- elliottcable <git@elliottcable.name>
- changed status from unstarted to in_progress
- Going to write a raketask for this
- - 2008-04-13 12:17:44.402653 Z
- elliottcable <git@elliottcable.name>
- closed issue with disposition fixed
- Added a rake task that will fail if the ratio is below a certain level.
@@ -0,0 +1,78 @@
module Spec
module Rake

# A task that can verify that the ratio of (lines of spec)
# over (lines of code) doesn't drop below a certain threshold.
# It should be run after running Spec::Rake::SpecTask.
class VerifySpecRatioTask < ::Rake::TaskLib
# Name of the task. Defaults to :verify_spec_ratio
attr_accessor :name

# Glob pattern to match code files. (default is '**/*.rb')
# Automatically excludes everything matching +spec_pattern+
attr_accessor :pattern

# Glob pattern to match spec files. (default is 'spec/**/*_spec.rb')
attr_accessor :spec_pattern

# Whether or not to output details. Defaults to true.
attr_accessor :verbose

# The threshold value (in decimal value) for ratio. If the
# actual ratio is not above this value, the task will raise an
# exception. The ratio is devised as LoS/LoC, so values above 1
# mean expressive specs, while values below one mean weak specs.
attr_accessor :ratio

def initialize(name=:verify_spec_ratio)
@name = name
@pattern = '**/*.rb'
@spec_pattern = 'spec/**/*_spec.rb'
@verbose = true
yield self if block_given?
raise "Ratio must be set" if @ratio.nil?
define_task
end

def define_task
desc "Verify that spec-ratio is at least #{@ratio}%"
task @name do
loc = 0
los = 0

code_files = Dir[@pattern]
spec_files = Dir[@spec_pattern]

code_files.each do |code_file|
next if spec_files.include? code_file
code = File.open(code_file)

code.each_line do |line|
line.gsub! /#(.*)/, '' # Get rid of all comments
line.gsub! /\s/, '' # Get rid of all whitespace
loc += 1 unless line.empty?
end
end

spec_files.each do |spec_file|
spec = File.open(spec_file)

spec.each_line do |line|
line.gsub! /#(.*)/, '' # Get rid of all comments
line.gsub! /\s/, '' # Get rid of all whitespace
los += 1 unless line.empty?
end
end

ratio = (los.to_f / loc.to_f)

puts "Spec-ratio: #{los}/#{loc} (#{ratio.to_s[0..3]}, expected >=: #{@ratio})" if @verbose
raise "Spec-ratio must be above #{@ratio} but was #{ratio.to_s[0..3]}" if ratio < @ratio
raise "Your spec-ratio file pattern matched no files" if loc == 0
raise "Your spec-ratio specfile pattern matched no files" if los == 0
end
end
end

end
end

0 comments on commit ede8387

Please sign in to comment.