simplecov_text_formatter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock ADDED
@@ -0,0 +1,123 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simplecov_text_formatter (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.1.4.1)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ zeitwerk (~> 2.3)
15
+ ast (2.4.2)
16
+ coderay (1.1.3)
17
+ concurrent-ruby (1.1.9)
18
+ diff-lcs (1.4.4)
19
+ docile (1.4.0)
20
+ ffi (1.15.4)
21
+ formatador (0.3.0)
22
+ guard (2.18.0)
23
+ formatador (>= 0.2.4)
24
+ listen (>= 2.7, < 4.0)
25
+ lumberjack (>= 1.0.12, < 2.0)
26
+ nenv (~> 0.1)
27
+ notiffany (~> 0.0)
28
+ pry (>= 0.13.0)
29
+ shellany (~> 0.0)
30
+ thor (>= 0.18.1)
31
+ guard-compat (1.2.1)
32
+ guard-rspec (4.7.3)
33
+ guard (~> 2.1)
34
+ guard-compat (~> 1.1)
35
+ rspec (>= 2.99.0, < 4.0)
36
+ i18n (1.8.10)
37
+ concurrent-ruby (~> 1.0)
38
+ listen (3.7.0)
39
+ rb-fsevent (~> 0.10, >= 0.10.3)
40
+ rb-inotify (~> 0.9, >= 0.9.10)
41
+ lumberjack (1.2.8)
42
+ method_source (1.0.0)
43
+ minitest (5.14.4)
44
+ nenv (0.3.0)
45
+ notiffany (0.1.3)
46
+ nenv (~> 0.1)
47
+ shellany (~> 0.0)
48
+ parallel (1.21.0)
49
+ parser (3.0.2.0)
50
+ ast (~> 2.4.1)
51
+ pry (0.14.1)
52
+ coderay (~> 1.1)
53
+ method_source (~> 1.0)
54
+ rack (2.2.3)
55
+ rainbow (3.0.0)
56
+ rake (12.3.3)
57
+ rb-fsevent (0.11.0)
58
+ rb-inotify (0.10.1)
59
+ ffi (~> 1.0)
60
+ regexp_parser (2.1.1)
61
+ rexml (3.2.5)
62
+ rspec (3.10.0)
63
+ rspec-core (~> 3.10.0)
64
+ rspec-expectations (~> 3.10.0)
65
+ rspec-mocks (~> 3.10.0)
66
+ rspec-core (3.10.1)
67
+ rspec-support (~> 3.10.0)
68
+ rspec-expectations (3.10.1)
69
+ diff-lcs (>= 1.2.0, < 2.0)
70
+ rspec-support (~> 3.10.0)
71
+ rspec-mocks (3.10.2)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.10.0)
74
+ rspec-support (3.10.2)
75
+ rspec_junit_formatter (0.4.1)
76
+ rspec-core (>= 2, < 4, != 2.12.0)
77
+ rubocop (1.21.0)
78
+ parallel (~> 1.10)
79
+ parser (>= 3.0.0.0)
80
+ rainbow (>= 2.2.2, < 4.0)
81
+ regexp_parser (>= 1.8, < 3.0)
82
+ rexml
83
+ rubocop-ast (>= 1.9.1, < 2.0)
84
+ ruby-progressbar (~> 1.7)
85
+ unicode-display_width (>= 1.4.0, < 3.0)
86
+ rubocop-ast (1.11.0)
87
+ parser (>= 3.0.1.1)
88
+ rubocop-rails (2.12.2)
89
+ activesupport (>= 4.2.0)
90
+ rack (>= 1.1)
91
+ rubocop (>= 1.7.0, < 2.0)
92
+ ruby-progressbar (1.11.0)
93
+ shellany (0.0.1)
94
+ simplecov (0.21.2)
95
+ docile (~> 1.1)
96
+ simplecov-html (~> 0.11)
97
+ simplecov_json_formatter (~> 0.1)
98
+ simplecov-html (0.12.3)
99
+ simplecov_json_formatter (0.1.3)
100
+ thor (0.18.1)
101
+ tzinfo (2.0.4)
102
+ concurrent-ruby (~> 1.0)
103
+ unicode-display_width (2.1.0)
104
+ zeitwerk (2.4.2)
105
+
106
+ PLATFORMS
107
+ x86_64-darwin-20
108
+ x86_64-linux
109
+
110
+ DEPENDENCIES
111
+ bundler (~> 2.2.15)
112
+ guard-rspec
113
+ pry
114
+ rake (~> 12.0)
115
+ rspec (~> 3.0)
116
+ rspec_junit_formatter
117
+ rubocop (~> 1.9)
118
+ rubocop-rails
119
+ simplecov (~> 0.21)
120
+ simplecov_text_formatter!
121
+
122
+ BUNDLED WITH
123
+ 2.2.15
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright 2021 Platanus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # SimpleCov Text Formatter
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/simplecov_text_formatter.svg)](https://badge.fury.io/rb/simplecov_text_formatter)
4
+ [![CircleCI](https://circleci.com/gh/platanus/simplecov_text_formatter.svg?style=shield)](https://app.circleci.com/pipelines/github/platanus/simplecov_text_formatter)
5
+
6
+ Text formatter for SimpleCov code coverage tool
7
+
8
+ ***Note: To learn more about SimpleCov, check out the main repo at [https://github.com/simplecov-ruby/simplecov](https://github.com/colszowka/simplecov)***
9
+
10
+ ## Installation
11
+
12
+ Add to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'simplecov'
16
+ gem 'simplecov_text_formatter'
17
+ ```
18
+
19
+ ```bash
20
+ bundle install
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ Add the formatter to your `spec/spec_helper.rb`.
26
+
27
+ ```ruby
28
+ require 'simplecov'
29
+ require 'simplecov_text_formatter'
30
+
31
+ SimpleCov.start 'rails' do
32
+ # ...
33
+
34
+ formatter SimpleCov::Formatter::MultiFormatter.new(
35
+ [
36
+ SimpleCov::Formatter::TextFormatter,
37
+ SimpleCov::Formatter::HTMLFormatter
38
+ ]
39
+ )
40
+ end
41
+ ```
42
+
43
+ Then run your test and check the file `coverage/coverage.txt`. You'll see something like this:
44
+
45
+ ```
46
+ /app/channels/application_cable/channel.rb:1:1: Not covered lines: 1 to 4
47
+ /app/channels/application_cable/connection.rb:1:1: Not covered lines: 1 to 4
48
+ /app/controllers/api/v1/users_controller.rb:39:1: Not covered lines: 39
49
+ /app/controllers/application_controller.rb:17:1: Not covered lines: 17
50
+ /app/controllers/home_controller.rb:1:1: Not covered lines: 1 to 5
51
+ /app/controllers/organizations_controller.rb:1:1: Not covered lines: 1 and 2
52
+ /app/controllers/organizations_controller.rb:4:1: Not covered lines: 4 to 6
53
+ /app/controllers/organizations_controller.rb:8:1: Not covered lines: 8 to 13
54
+ /app/controllers/organizations_controller.rb:15:1: Not covered lines: 15
55
+ /app/controllers/organizations_controller.rb:17:1: Not covered lines: 17 to 20
56
+ /app/controllers/teams_controller.rb:1:1: Not covered lines: 1 and 2
57
+ /app/controllers/teams_controller.rb:4:1: Not covered lines: 4 to 6
58
+ /app/controllers/teams_controller.rb:8:1: Not covered lines: 8 to 11
59
+ /app/controllers/teams_controller.rb:13:1: Not covered lines: 13
60
+ /app/controllers/teams_controller.rb:15:1: Not covered lines: 15 to 18
61
+ /app/controllers/users_controller.rb:13:1: Not covered lines: 13
62
+ /app/controllers/users_controller.rb:17:1: Not covered lines: 17 and 18
63
+ /app/controllers/users_controller.rb:28:1: Not covered lines: 28
64
+ /app/jobs/application_job.rb:1:1: Not covered lines: 1
65
+ /app/jobs/application_job.rb:7:1: Not covered lines: 7
66
+ /app/models/user.rb:17:1: Not covered lines: 17
67
+ /app/policies/active_admin/comment_policy.rb:1:1: Not covered lines: 1 and 2
68
+ /app/policies/active_admin/page_policy.rb:1:1: Not covered lines: 1 to 5
69
+ /app/policies/admin_user_policy.rb:1:1: Not covered lines: 1 and 2
70
+ /app/policies/application_policy.rb:1:1: Not covered lines: 1 and 2
71
+ /app/policies/application_policy.rb:4:1: Not covered lines: 4 to 7
72
+ /app/policies/application_policy.rb:9:1: Not covered lines: 9 to 11
73
+ /app/policies/application_policy.rb:13:1: Not covered lines: 13 to 15
74
+ /app/policies/application_policy.rb:17:1: Not covered lines: 17 to 19
75
+ /app/policies/application_policy.rb:21:1: Not covered lines: 21 to 23
76
+ /app/policies/application_policy.rb:25:1: Not covered lines: 25 to 27
77
+ /app/policies/application_policy.rb:29:1: Not covered lines: 29 to 31
78
+ /app/policies/application_policy.rb:33:1: Not covered lines: 33 to 35
79
+ /app/policies/application_policy.rb:37:1: Not covered lines: 37 and 38
80
+ /app/policies/application_policy.rb:40:1: Not covered lines: 40 to 43
81
+ /app/policies/application_policy.rb:45:1: Not covered lines: 45 to 47
82
+ /app/policies/application_policy.rb:49:1: Not covered lines: 49 to 52
83
+ /app/policies/application_policy.rb:54:1: Not covered lines: 54 to 57
84
+ /app/policies/organization_policy.rb:1:1: Not covered lines: 1 and 2
85
+ /app/policies/team_member_policy.rb:1:1: Not covered lines: 1 and 2
86
+ /app/policies/team_policy.rb:1:1: Not covered lines: 1 and 2
87
+ /app/policies/user_policy.rb:1:1: Not covered lines: 1 and 2
88
+ /app/uploaders/base_uploader.rb:9:1: Not covered lines: 9
89
+ /engines/dynattributes/app/commands/dynattributes/validate_date_attribute.rb:4:1: Not covered lines: 4
90
+ /engines/dynattributes/app/commands/dynattributes/validate_date_time_attribute.rb:4:1: Not covered lines: 4
91
+ /engines/dynattributes/app/commands/dynattributes/validate_select_definition.rb:4:1: Not covered lines: 4
92
+ /engines/dynattributes/app/models/dynattributes/instance.rb:13:1: Not covered lines: 13 and 14
93
+ /engines/dynattributes/app/models/dynattributes/instance.rb:16:1: Not covered lines: 16
94
+ /engines/dynattributes/lib/dynattributes.rb:21:1: Not covered lines: 21
95
+ /engines/tasks/lib/tasks.rb:20:1: Not covered lines: 20
96
+ ```
97
+
98
+ ## Testing
99
+
100
+ To run the specs you need to execute, **in the root path of the gem**, the following command:
101
+
102
+ ```bash
103
+ bundle exec guard
104
+ ```
105
+
106
+ You need to put **all your tests** in the `/my_gem/spec/` directory.
107
+
108
+ ## Publishing
109
+
110
+ On master/main branch...
111
+
112
+ 1. Change `VERSION` in `lib/simplecov_text_formatter/version.rb`.
113
+ 2. Change `Unreleased` title to current version in `CHANGELOG.md`.
114
+ 3. Run `bundle install`.
115
+ 4. Commit new release. For example: `Releasing v0.1.0`.
116
+ 5. Create tag. For example: `git tag v0.1.0`.
117
+ 6. Push tag. For example: `git push origin v0.1.0`.
118
+
119
+ ## Contributing
120
+
121
+ 1. Fork it
122
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
123
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
124
+ 4. Push to the branch (`git push origin my-new-feature`)
125
+ 5. Create new Pull Request
126
+
127
+ ## Credits
128
+
129
+ Thank you [contributors](https://github.com/platanus/simplecov_text_formatter/graphs/contributors)!
130
+
131
+ <img src="http://platan.us/gravatar_with_text.png" alt="Platanus" width="250"/>
132
+
133
+ SimpleCov Text Formatter is maintained by [platanus](http://platan.us).
134
+
135
+ ## License
136
+
137
+ SimpleCov Text Formatter is © 2021 platanus, spa. It is free software and may be redistributed under the terms specified in the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simplecov_text_formatter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ module SimpleCovTextFormatter
2
+ class ResultExporter
3
+ FILENAME = 'coverage.txt'
4
+
5
+ def initialize(errors_list)
6
+ @result = errors_list
7
+ end
8
+
9
+ def export
10
+ File.open(export_path, 'w') do |file|
11
+ file << @result.join("\n")
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def export_path
18
+ File.join(SimpleCov.coverage_path, FILENAME)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module SimpleCovTextFormatter
2
+ class ResultFormatter
3
+ def initialize(result)
4
+ @result = result
5
+ end
6
+
7
+ def format
8
+ formatted_result = []
9
+
10
+ @result.files.each do |source_file|
11
+ messages = format_source_file(source_file)
12
+ formatted_result += messages if messages
13
+ end
14
+
15
+ formatted_result
16
+ end
17
+
18
+ private
19
+
20
+ def format_source_file(source_file)
21
+ source_file_formatter = SourceFileFormatter.new(source_file)
22
+ source_file_formatter.format
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,92 @@
1
+ module SimpleCovTextFormatter
2
+ class SourceFileFormatter
3
+ NOT_COVERED_LINE_CODE = 0
4
+ VALID_MSG_PARTS_COUNT = 4
5
+ FILE_COLUMN = 1
6
+
7
+ def initialize(source_file)
8
+ @source_file = source_file
9
+ end
10
+
11
+ def format
12
+ not_covered_lines_in_batches.map do |batch|
13
+ build_message(batch)
14
+ end.reject(&:nil?)
15
+ end
16
+
17
+ private
18
+
19
+ def build_message(batch)
20
+ message_parts = build_message_parts(batch)
21
+ return unless message_parts.count == VALID_MSG_PARTS_COUNT
22
+ return if full_coverage?
23
+
24
+ message_parts.join(":")
25
+ end
26
+
27
+ def build_message_parts(batch)
28
+ [
29
+ @source_file.filename,
30
+ batch.first.to_s,
31
+ FILE_COLUMN.to_s,
32
+ " Not covered lines: #{build_error_text(batch)}"
33
+ ].reject(&:nil?)
34
+ end
35
+
36
+ def build_error_text(batch)
37
+ case batch.count
38
+ when 1
39
+ batch.first
40
+ when 2
41
+ "#{batch.first} and #{batch.last}"
42
+ else
43
+ "#{batch.first} to #{batch.last}"
44
+ end
45
+ end
46
+
47
+ def not_covered_lines_in_batches
48
+ line_numbers = not_covered_lines.map(&:line_number).sort
49
+ batches = []
50
+ current_batch = []
51
+
52
+ line_numbers.each_with_index do |line_number, index|
53
+ current_batch << line_number
54
+
55
+ if !near_lines?(line_number, line_numbers[index + 1])
56
+ batches << current_batch
57
+ current_batch = []
58
+ end
59
+ end
60
+
61
+ batches
62
+ end
63
+
64
+ def near_lines?(line, next_line)
65
+ [*line..line + 2].include?(next_line)
66
+ end
67
+
68
+ def full_coverage?
69
+ covered_percentage.to_i == 100
70
+ end
71
+
72
+ def covered_percentage
73
+ @covered_percentage ||= (covered_lines.count.to_f / valid_lines.count * 100).round(2)
74
+ end
75
+
76
+ def covered_lines
77
+ @covered_lines ||= lines.select { |l| l.coverage && l.coverage > NOT_COVERED_LINE_CODE }
78
+ end
79
+
80
+ def not_covered_lines
81
+ @not_covered_lines ||= lines.select { |l| l.coverage == NOT_COVERED_LINE_CODE }
82
+ end
83
+
84
+ def valid_lines
85
+ @valid_lines ||= lines.reject { |l| l.coverage.nil? }
86
+ end
87
+
88
+ def lines
89
+ @lines ||= @source_file.lines
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module SimplecovTextFormatter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ require 'simplecov_text_formatter/version'
2
+ require 'simplecov_text_formatter/source_file_formatter'
3
+ require 'simplecov_text_formatter/result_formatter'
4
+ require 'simplecov_text_formatter/result_exporter'
5
+
6
+ module SimpleCov
7
+ module Formatter
8
+ class TextFormatter
9
+ def format(result)
10
+ formatted_result = format_result(result)
11
+ export_formatted_result(formatted_result)
12
+ puts(output_message(result))
13
+ end
14
+
15
+ private
16
+
17
+ def format_result(result)
18
+ SimpleCovTextFormatter::ResultFormatter.new(result).format
19
+ end
20
+
21
+ def export_formatted_result(formatted_result)
22
+ SimpleCovTextFormatter::ResultExporter.new(formatted_result).export
23
+ end
24
+
25
+ def output_message(result)
26
+ <<~MSG
27
+ Text Coverage report generated for #{result.command_name} to #{SimpleCov.coverage_path}.
28
+ #{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered.
29
+ MSG
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "simplecov_text_formatter/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "simplecov_text_formatter"
7
+ spec.version = SimplecovTextFormatter::VERSION
8
+ spec.authors = ["Platanus", "Leandro Segovia"]
9
+ spec.email = ["rubygems@platan.us", "leandro@platan.us"]
10
+ spec.homepage = "https://github.com/platanus/simplecov_text_formatter"
11
+ spec.summary = "Text formatter for SimpleCov"
12
+ spec.description = "Text formatter for SimpleCov code coverage tool"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 2.2.15"
21
+ spec.add_development_dependency "guard-rspec"
22
+ spec.add_development_dependency "pry"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec_junit_formatter"
26
+ spec.add_development_dependency "rubocop", "~> 1.9"
27
+ spec.add_development_dependency "rubocop-rails"
28
+ spec.add_development_dependency "simplecov", "~> 0.21"
29
+ end