From 114936fbc607b8a7c760199b1dd13672d3bfd6f3 Mon Sep 17 00:00:00 2001 From: Shawn Balestracci Date: Thu, 18 Dec 2008 19:05:41 -0800 Subject: [PATCH] Implementation of plugin --- MIT-LICENSE | 20 ++++++++++++++++++++ README | 23 ++++++++++++++++++++--- Rakefile | 22 ++++++++++++++++++++++ init.rb | 1 + install.rb | 1 + lib/coveralls.rb | 21 +++++++++++++++++++++ tasks/coveralls_tasks.rake | 4 ++++ test/coveralls_test.rb | 28 ++++++++++++++++++++++++++++ uninstall.rb | 1 + 9 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 MIT-LICENSE create mode 100644 Rakefile create mode 100644 init.rb create mode 100644 install.rb create mode 100644 lib/coveralls.rb create mode 100644 tasks/coveralls_tasks.rake create mode 100644 test/coveralls_test.rb create mode 100644 uninstall.rb diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..8eaf6db --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2008 [name of plugin creator] + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README index 817cf2f..dd56d8f 100644 --- a/README +++ b/README @@ -1,13 +1,30 @@ Coveralls ========= -Introduction goes here. + A rails plugin to make sure all files are required and show up in the code coverage report. (Untouched files are excluded and can thus inflate the overall code coverage.) If you forget to test a new library or module, it may not show up on the report, and thus you won't see that the coverage for that file is low. + + Example ======= -Example goes here. +After installing the plugin create a test or spec that contains Coverall.require_all_ruby_files + +This will require all ruby files in /lib and /app if they haven't already been required. + +You can also pass in an array of directories to require. + +Coverall.require_all_ruby_files ["/app/models","/app/lib"] + + + + +Thanks +======= + +Special thanks go to Stephen Anderson for the name and for encouraging me to create this plugin. + +Copyright (c) 2008 Shawn Balestracci, released under the MIT license -Copyright (c) 2008 [name of plugin creator], released under the MIT license diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..6b0a469 --- /dev/null +++ b/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the coveralls plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the coveralls plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'Coveralls' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..bd0b660 --- /dev/null +++ b/init.rb @@ -0,0 +1 @@ +require 'coveralls' diff --git a/install.rb b/install.rb new file mode 100644 index 0000000..f7732d3 --- /dev/null +++ b/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/lib/coveralls.rb b/lib/coveralls.rb new file mode 100644 index 0000000..443dc0a --- /dev/null +++ b/lib/coveralls.rb @@ -0,0 +1,21 @@ +class Coveralls + # Method to require all ruby classes when calculating code coverage. + # Call this to not leave untested files out of the code coverage percentages. + def self.require_all_ruby_files(target_dirs=["/lib", "/app"]) + Array(target_dirs).collect do |target_dir| + dir = "#{RAILS_ROOT}#{target_dir}/**/*.rb" + Dir.glob(dir).each do |ruby_file| + obj = ruby_file.split("#{target_dir}/",2).last.split(".rb").first + obj = obj.split("/",2).last if target_dir == "/app" + begin + # trigger the normal Rails mechanism to require files + obj.classify.constantize + rescue NameError, LoadError + require obj + end + end + end + + end + +end diff --git a/tasks/coveralls_tasks.rake b/tasks/coveralls_tasks.rake new file mode 100644 index 0000000..076cf19 --- /dev/null +++ b/tasks/coveralls_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :coveralls do +# # Task goes here +# end diff --git a/test/coveralls_test.rb b/test/coveralls_test.rb new file mode 100644 index 0000000..3e0e5b6 --- /dev/null +++ b/test/coveralls_test.rb @@ -0,0 +1,28 @@ +require 'test/unit' +ENV['RAILS_ENV'] = 'test' +ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..' +require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb')) + +class CoverallsTest < Test::Unit::TestCase + def test_default_require_all + all_files = Coveralls.require_all_ruby_files + controllers = (Dir.glob(File.join(RAILS_ROOT,"/app","controllers","**","*.rb"))) + libs = (Dir.glob(File.join(RAILS_ROOT,"/lib","*.rb"))) + + assert_equal libs,all_files.flatten & libs + assert_equal controllers,all_files.flatten & controllers + end + + def test_invalid_require + all_files = Coveralls.require_all_ruby_files "/dog_not_here_zzz" + assert_equal [],all_files.flatten + end + + def test_specify_directory + all_files = Coveralls.require_all_ruby_files "/app/controllers" + controllers = (Dir.glob(File.join(RAILS_ROOT,"/app","controllers","**","*.rb"))) + + assert_equal controllers, all_files.flatten + end + +end diff --git a/uninstall.rb b/uninstall.rb new file mode 100644 index 0000000..9738333 --- /dev/null +++ b/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here