simplecov-single_file_reporter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ coverage
2
+ spec/fixtures/*/coverage
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - ree
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+ gemspec
3
+
4
+ gem "bump", :github => "grosser/bump", :branch => "anylib"
5
+ gem "rake"
6
+ gem "rspec", "~>2"
7
+ gem "test-unit"
8
+ gem "minitest"
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ GIT
2
+ remote: git://github.com/grosser/bump.git
3
+ revision: fc5cc418963e0a1257bb6d12dd44382abea05dd7
4
+ branch: anylib
5
+ specs:
6
+ bump (0.3.8)
7
+
8
+ PATH
9
+ remote: .
10
+ specs:
11
+ simplecov-single_file_reporter (0.0.1)
12
+ simplecov
13
+
14
+ GEM
15
+ remote: http://rubygems.org/
16
+ specs:
17
+ diff-lcs (1.1.3)
18
+ minitest (4.2.0)
19
+ multi_json (1.5.0)
20
+ rake (0.9.2)
21
+ rspec (2.6.0)
22
+ rspec-core (~> 2.6.0)
23
+ rspec-expectations (~> 2.6.0)
24
+ rspec-mocks (~> 2.6.0)
25
+ rspec-core (2.6.4)
26
+ rspec-expectations (2.6.0)
27
+ diff-lcs (~> 1.1.2)
28
+ rspec-mocks (2.6.0)
29
+ simplecov (0.7.1)
30
+ multi_json (~> 1.0)
31
+ simplecov-html (~> 0.7.1)
32
+ simplecov-html (0.7.1)
33
+ test-unit (2.5.3)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bump!
40
+ minitest
41
+ rake
42
+ rspec (~> 2)
43
+ simplecov-single_file_reporter!
44
+ test-unit
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "bump/tasks"
3
+
4
+ task :default do
5
+ sh "rspec spec/"
6
+ end
data/Readme.md ADDED
@@ -0,0 +1,58 @@
1
+ print coverage for 1 test/spec file
2
+
3
+ Install
4
+ =======
5
+
6
+ gem install simplecov-single_file_reporter
7
+
8
+ Usage
9
+ =====
10
+
11
+ ### Simple setup
12
+
13
+ Print coverage percent when you run a single file
14
+
15
+ ```Ruby
16
+ require "simplecov/single_file_reporter"
17
+ SimpleCov.start
18
+ SimpleCov::SingleFileReporter.print
19
+ ```
20
+
21
+ ```Bash
22
+ ruby test/a_test.rb
23
+ 1 tests, 1 assertions
24
+ lib/a.rb coverage: 80.0
25
+ ```
26
+
27
+ ### Customizing file-finding rules
28
+
29
+ SingleFileReporter needs to find the file that is tested.
30
+ ```
31
+ test/test_xxx.rb -> lib/xxx.rb or app/xxx.rb
32
+ ```
33
+
34
+ If it's something common, make a pull request.<br/>
35
+ If your app has special rules you can add them:
36
+
37
+ ```Ruby
38
+ class MyReporter < SimpleCov::SingleFileReporter
39
+ remove :file_under_test
40
+ def self.file_under_test(test_file)
41
+ super(test_file) || test_file.split("test/").last.sub("foo", "bar").sub("_test.rb", ".rb")
42
+ end
43
+ end
44
+
45
+ SimpleCov.start
46
+ MyReporter.print
47
+ ```
48
+
49
+ Authors
50
+ ======
51
+
52
+ ### [Contributors](https://github.com/grosser/simplecov-single_file_reporter/contributors)
53
+ - [Eirik Dentz Sinclair](https://github.com/edsinclair)
54
+
55
+ [Michael Grosser](http://grosser.it)<br/>
56
+ michael@grosser.it<br/>
57
+ License: MIT<br/>
58
+ [![Build Status](https://travis-ci.org/grosser/simplecov-single_file_reporter.png)](https://travis-ci.org/grosser/simplecov-single_file_reporter)
@@ -0,0 +1,86 @@
1
+ require "simplecov"
2
+
3
+ class SimpleCov::SingleFileReporter
4
+ VERSION = "0.0.1"
5
+
6
+ ANSI_COLOR_CODE = {
7
+ :red => "\e[31m",
8
+ :green => "\e[32m",
9
+ :yellow => "\e[33m"
10
+ }
11
+
12
+ ANSI_COLOR_CODE_TERMINATOR = "\e[0m"
13
+
14
+ class << self
15
+ def print
16
+ return unless test_file = (called_with_single_test || called_with_single_spec)
17
+ SimpleCov.at_exit do
18
+ SimpleCov.result.format! # keep generating default report so people can see why the coverage is not 100%
19
+ puts coverage_for(test_file)
20
+ end
21
+ end
22
+
23
+ def coverage_for(test_file)
24
+ if file = file_under_test(test_file)
25
+ if percent = covered_percent(file)
26
+ message(file, percent)
27
+ else
28
+ "Could not find coverage for file #{file} in Simplecov report"
29
+ end
30
+ else
31
+ "Could not find tested file for #{test_file}"
32
+ end
33
+ end
34
+
35
+ def covered_percent(file)
36
+ data = SimpleCov.result.files.detect { |f| f.filename =~ /#{Regexp.escape file}$/ }
37
+ data.covered_percent.round(2) if data
38
+ end
39
+
40
+ private
41
+
42
+ def called_with_single_test
43
+ ARGV.empty? && File.exist?($0) && $0
44
+ end
45
+
46
+ def called_with_single_spec
47
+ $0 =~ %r{/r?spec$} && ARGV.size == 1 && File.exist?(ARGV[0]) && ARGV[0]
48
+ end
49
+
50
+ # TODO: Make this work for models, helpers, lib, etc
51
+ def file_under_test(test_file)
52
+ file = test_file.split(%r{(test|spec)/}).last.
53
+ sub(%r{^functional/}, "controllers/").
54
+ sub(%r{^unit/}, "").
55
+ sub(%r{^lib/}, "").
56
+ sub(%r{_(test|spec)\.rb}, ".rb").
57
+ sub(%r{(^|/)test_([^/]+\.rb)}, "\\1\\2")
58
+
59
+ possibilities = ["app", "lib"].map { |f| "#{f}/#{file}" }
60
+ possibilities.detect { |f| File.exist?(f) }
61
+ end
62
+
63
+ def color?
64
+ STDOUT.tty?
65
+ end
66
+
67
+ def message(file, percent)
68
+ message = "#{file} coverage: #{percent}"
69
+ if color?
70
+ "#{ANSI_COLOR_CODE.fetch(color(percent))}#{message}#{ANSI_COLOR_CODE_TERMINATOR}"
71
+ else
72
+ message
73
+ end
74
+ end
75
+
76
+ def color(percent)
77
+ if percent == 100.0
78
+ :green
79
+ elsif percent > 90
80
+ :yellow
81
+ else
82
+ :red
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+ name = "simplecov-single_file_reporter"
3
+ require name.sub("-", "/")
4
+
5
+ Gem::Specification.new name, SimpleCov::SingleFileReporter::VERSION do |s|
6
+ s.summary = "print coverage per test file"
7
+ s.authors = ["Michael Grosser"]
8
+ s.email = "michael@grosser.it"
9
+ s.homepage = "http://github.com/grosser/#{name}"
10
+ s.files = `git ls-files`.split("\n")
11
+ s.license = "MIT"
12
+ s.add_runtime_dependency "simplecov"
13
+ end
@@ -0,0 +1,2 @@
1
+ require "rake/testtask"
2
+ Rake::TestTask.new(:default)
@@ -0,0 +1,9 @@
1
+ def a
2
+ 1
3
+ 2
4
+ 3
5
+ end
6
+
7
+ 1
8
+ 2
9
+ 3
@@ -0,0 +1,13 @@
1
+ require "minitest/autorun"
2
+ require File.expand_path("../../../setup_simple_print.rb", __FILE__)
3
+
4
+ # Load file under test
5
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
6
+ require "a"
7
+
8
+ class ATest < MiniTest::Unit::TestCase
9
+ def test_a
10
+ assert true
11
+ end
12
+ end
13
+
@@ -0,0 +1,2 @@
1
+ require "rspec/core/rake_task"
2
+ RSpec::Core::RakeTask.new(:default)
@@ -0,0 +1,9 @@
1
+ def a
2
+ 1
3
+ 2
4
+ 3
5
+ end
6
+
7
+ 1
8
+ 2
9
+ 3
@@ -0,0 +1,12 @@
1
+ require File.expand_path("../../../setup_simple_print.rb", __FILE__)
2
+
3
+ # Load file under test
4
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
+ require "a"
6
+
7
+ describe "A" do
8
+ it "is true" do
9
+ true.should == true
10
+ end
11
+ end
12
+
@@ -0,0 +1,12 @@
1
+ require File.expand_path("../../../setup_simple_print.rb", __FILE__)
2
+
3
+ # Load file under test
4
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
+ require "a"
6
+
7
+ describe "B" do
8
+ it "is true" do
9
+ true.should == true
10
+ end
11
+ end
12
+
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../../../lib", __FILE__)
2
+ require 'simplecov/single_file_reporter'
3
+ SimpleCov.root(File.expand_path("../../", __FILE__))
4
+ SimpleCov.start
5
+ SimpleCov::SingleFileReporter.print
@@ -0,0 +1,2 @@
1
+ require "rake/testtask"
2
+ Rake::TestTask.new(:default)
@@ -0,0 +1,9 @@
1
+ def a
2
+ 1
3
+ 2
4
+ 3
5
+ end
6
+
7
+ 1
8
+ 2
9
+ 3
@@ -0,0 +1,13 @@
1
+ require "test/unit"
2
+ require File.expand_path("../../../setup_simple_print.rb", __FILE__)
3
+
4
+ # Load file under test
5
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
6
+ require "a"
7
+
8
+ class ATest < Test::Unit::TestCase
9
+ def test_a
10
+ assert true
11
+ end
12
+ end
13
+
@@ -0,0 +1,150 @@
1
+ require "spec_helper"
2
+
3
+ describe SimpleCov::SingleFileReporter do
4
+ def self.in_folder(folder)
5
+ around do |example|
6
+ Dir.chdir fixtures.join(folder) do
7
+ example.call
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.it_shows_correct_coverage_reports(options={})
13
+ it "shows percentage after single file is run" do
14
+ result = run(options[:run] || "ruby test/test_a.rb")
15
+ result.should include("Coverage report generated")
16
+ result.should include(options[:result_run] || "1 tests, 1 assertions")
17
+ result.should include("lib/a.rb coverage: 80.0")
18
+ end
19
+
20
+ it "does not show percentage for rake" do
21
+ result = run("rake")
22
+ result.should include("Coverage report generated")
23
+ result.should include(options[:result_rake] || "1 tests, 1 assertions")
24
+ result.should_not include("lib/a.rb coverage")
25
+ end
26
+ end
27
+
28
+ let(:fixtures){ Bundler.root.join("spec", "fixtures") }
29
+
30
+ it "has a VERSION" do
31
+ SimpleCov::SingleFileReporter::VERSION.should =~ /^[\.\da-z]+$/
32
+ end
33
+
34
+ describe ".color" do
35
+ [[100, :green], [99, :yellow], [91, :yellow], [90, :red], [0, :red]].each do |percent, color|
36
+ it "shows #{color} for #{percent}" do
37
+ call(:color, percent).should == color
38
+ end
39
+ end
40
+ end
41
+
42
+ describe ".message" do
43
+ it "is plain for none-tty" do
44
+ STDOUT.stub(:tty?).and_return(false)
45
+ call(:message, "xxx.rb", 10).should == "xxx.rb coverage: 10"
46
+ end
47
+
48
+ it "is colored for tty" do
49
+ STDOUT.stub(:tty?).and_return(true)
50
+ call(:message, "xxx.rb", 10).should == "\e[31mxxx.rb coverage: 10\e[0m"
51
+ end
52
+ end
53
+
54
+ describe ".file_under_test" do
55
+ around do |example|
56
+ tmp = fixtures.join("tmp")
57
+ `rm -rf #{tmp}`
58
+ `mkdir -p #{tmp}`
59
+ begin
60
+ Dir.chdir tmp do
61
+ example.call
62
+ end
63
+ ensure
64
+ `rm -rf #{tmp}`
65
+ end
66
+ end
67
+
68
+ it "does not find unfindable" do
69
+ touch "lib/xxx.rb"
70
+ call(:file_under_test, "yyy.rb").should == nil
71
+ end
72
+
73
+ it "finds files in lib via _test.rb" do
74
+ test_find "test/xxx_test.rb", "lib/xxx.rb"
75
+ end
76
+
77
+ it "finds files in lib via test_*.rb" do
78
+ test_find "test/test_xxx.rb", "lib/xxx.rb"
79
+ end
80
+
81
+ it "finds files in app" do
82
+ test_find "test/xxx_test.rb", "app/xxx.rb"
83
+ end
84
+
85
+ it "finds nested files" do
86
+ test_find "test/foo/bar/xxx_test.rb", "lib/foo/bar/xxx.rb"
87
+ end
88
+
89
+ it "finds functional as app/controllers" do
90
+ test_find "test/functional/xxx_controller_test.rb", "app/controllers/xxx_controller.rb"
91
+ end
92
+
93
+ it "finds test/unit/xxx as app/xxx" do
94
+ test_find "test/unit/xxx_test.rb", "app/xxx.rb"
95
+ end
96
+
97
+ it "finds test/unit/xxx as lib/xxx" do
98
+ test_find "test/unit/xxx_test.rb", "lib/xxx.rb"
99
+ end
100
+
101
+ it "finds test/unit/lib/xxx as lib/xxx" do
102
+ test_find "test/unit/lib/xxx_test.rb", "lib/xxx.rb"
103
+ end
104
+
105
+ it "finds spec" do
106
+ test_find "spec/xxx_spec.rb", "lib/xxx.rb"
107
+ end
108
+ end
109
+
110
+ context "in a test-unit project" do
111
+ in_folder "test-unit-project"
112
+ it_shows_correct_coverage_reports
113
+ end
114
+
115
+ context "in a minitest project" do
116
+ in_folder "minitest-project"
117
+ it_shows_correct_coverage_reports
118
+ end
119
+
120
+ context "in a rspec project" do
121
+ in_folder "rspec-project"
122
+ it_shows_correct_coverage_reports(
123
+ :run => "rspec spec/a_spec.rb",
124
+ :result_rake => "2 examples, 0 failures",
125
+ :result_run => "1 example, 0 failures",
126
+ )
127
+ end
128
+
129
+ private
130
+
131
+ def run(command)
132
+ result = `#{command} 2>&1`
133
+ ensure
134
+ raise "FAILED: #{result}" unless $?.success?
135
+ end
136
+
137
+ def call(name, *args)
138
+ SimpleCov::SingleFileReporter.send(name, *args)
139
+ end
140
+
141
+ def touch(file)
142
+ run "mkdir -p #{File.dirname(file)} && touch #{file}"
143
+ end
144
+
145
+ def test_find(test, file)
146
+ touch file
147
+ touch test
148
+ call(:file_under_test, test).should == file
149
+ end
150
+ end
@@ -0,0 +1 @@
1
+ require "simplecov/single_file_reporter"
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplecov-single_file_reporter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Michael Grosser
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ none: false
21
+ prerelease: false
22
+ name: simplecov
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ none: false
29
+ type: :runtime
30
+ description:
31
+ email: michael@grosser.it
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .gitignore
37
+ - .travis.yml
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - Rakefile
41
+ - Readme.md
42
+ - lib/simplecov/single_file_reporter.rb
43
+ - simplecov-single_file_reporter.gemspec
44
+ - spec/fixtures/minitest-project/Rakefile
45
+ - spec/fixtures/minitest-project/lib/a.rb
46
+ - spec/fixtures/minitest-project/test/test_a.rb
47
+ - spec/fixtures/rspec-project/Rakefile
48
+ - spec/fixtures/rspec-project/lib/a.rb
49
+ - spec/fixtures/rspec-project/spec/a_spec.rb
50
+ - spec/fixtures/rspec-project/spec/b_spec.rb
51
+ - spec/fixtures/setup_simple_print.rb
52
+ - spec/fixtures/test-unit-project/Rakefile
53
+ - spec/fixtures/test-unit-project/lib/a.rb
54
+ - spec/fixtures/test-unit-project/test/test_a.rb
55
+ - spec/simplecov/single_file_reporter_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: http://github.com/grosser/simplecov-single_file_reporter
58
+ licenses:
59
+ - MIT
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: 756792922926595275
72
+ none: false
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: 756792922926595275
81
+ none: false
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: print coverage per test file
88
+ test_files: []