simplecov-rspec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6f72895c7f0c30898b8b0359e0b4f6f8cafa838b7f9deb59fb220cef5e8a92c4
4
+ data.tar.gz: a383b340c09720a2a9440d714a5d289271c1174577f6cd9a8afecb645f7ba78e
5
+ SHA512:
6
+ metadata.gz: 58626ae17aff8c6645255092e1ec565d8b85f26824915b532471367ede6f900732c168fffbd659c946604ffc7e7b8ea53aa931aadffeede2a96b3350d820d0fe
7
+ data.tar.gz: c5dfe1463c2edd3bd72ad4cc215ad2371d53d51254e4ebd4fa33ee25aeacb6f6ee9b3ff880214ae42ff12482330c934676aa487e937c09d7ec85e577866db64a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format Fuubar
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ SuggestExtensions: false
4
+ DefaultFormatter: fuubar
5
+
6
+ # Output extra information for each offense to make it easier to diagnose:
7
+ DisplayCopNames: true
8
+ DisplayStyleGuide: true
9
+ ExtraDetails: true
10
+
11
+ # RuboCop enforces rules depending on the oldest version of Ruby which
12
+ # your project supports:
13
+ TargetRubyVersion: 3.0
14
+
15
+ Gemspec/DevelopmentDependencies:
16
+ Enabled: false
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ # The DSL for RSpec and the gemspec file make it very hard to limit block length
22
+ Metrics/BlockLength:
23
+ Exclude:
24
+ - '**/*_spec.rb'
25
+ - '*.gemspec'
26
+
27
+ Metrics/ModuleLength:
28
+ CountAsOne: ['hash']
29
+
30
+ Metrics/ClassLength:
31
+ CountAsOne: ['hash']
32
+
33
+ Style/AsciiComments:
34
+ Enabled: false
35
+
36
+ Style/StderrPuts:
37
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Change Log
2
+
3
+ Changes for each release are listed in this file.
4
+
5
+ This project adheres to [Semantic Versioning](https://semver.org/) for its releases.
6
+
7
+ ## v0.1.0 (2024-09-07)
8
+
9
+ [Full Changelog](https://github.com/main-branch/simplecov-rspec/compare/9fe828c..v0.1.0)
10
+
11
+ Changes:
12
+
13
+ * 93d7004 Add RSpec HTML formatter for CI build
14
+ * 94f5a80 Configure RSpec to run turnip tests when run from rake
15
+ * b3bf845 Use SimpleCov LCov formatter in the CI build
16
+ * 9fe828c Initial revision
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 James Couball
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,174 @@
1
+ # The `simplecov-rspec` Gem
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/simplecov-rspec.svg)](https://badge.fury.io/rb/simplecov-rspec)
4
+ [![Documentation](https://img.shields.io/badge/Documentation-Latest-green)](https://rubydoc.info/gems/simplecov-rspec/)
5
+ [![Change Log](https://img.shields.io/badge/CHANGELOG-Latest-green)](https://rubydoc.info/gems/simplecov-rspec/file/CHANGELOG.md)
6
+ [![Build Status](https://github.com/main-branch/simplecov-rspec/actions/workflows/continuous_integration.yml/badge.svg)](https://github.com/main-branch/simplecov-rspec/actions/workflows/continuous_integration.yml)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/9a58b51d18910db724c7/maintainability)](https://codeclimate.com/github/main-branch/simplecov-rspec/maintainability)
8
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/9a58b51d18910db724c7/test_coverage)](https://codeclimate.com/github/main-branch/simplecov-rspec/test_coverage)
9
+
10
+ `simplecov-rspec` is a Ruby gem that integrates SimpleCov with RSpec to ensure your
11
+ tests meet a minimum coverage threshold. It enhances your test suite by automatically
12
+ failing tests when coverage falls below a specified threshold and, optionally,
13
+ listing uncovered lines to help you improve coverage.
14
+
15
+ When `simplecov-rspec` is used, RSpec will report an error if the percent of test
16
+ coverage falls below a defined threshold:
17
+
18
+ ```text
19
+ Coverage report generated for RSpec to /Projects/example_project/coverage. 284 / 286 LOC (99.3%) covered.
20
+
21
+ FAIL: RSpec Test coverage fell below 100%
22
+ ```
23
+
24
+ If configured to list the lines that were not covered by tests, RSpec will additionally output:
25
+
26
+ ```text
27
+ The following lines were not covered by tests:
28
+ ./lib/example_project.rb:74
29
+ ./lib/example_project.rb:75
30
+ ```
31
+
32
+ * [Installation](#installation)
33
+ * [Getting Started](#getting-started)
34
+ * [Basic Setup](#basic-setup)
35
+ * [Configuration from Environment Variables](#configuration-from-environment-variables)
36
+ * [Development](#development)
37
+ * [Contributing](#contributing)
38
+ * [License](#license)
39
+ * [Code of Conduct](#code-of-conduct)
40
+
41
+ ## Installation
42
+
43
+ Install the gem and add to the application's Gemfile by executing:
44
+
45
+ ```bash
46
+ bundle add simplecov-rspec
47
+ ```
48
+
49
+ If bundler is not being used to manage dependencies, install the gem by executing:
50
+
51
+ ```bash
52
+ gem install simplecov-rspec
53
+ ```
54
+
55
+ ## Getting Started
56
+
57
+ To use `simplecov-rspec`, follow these steps:
58
+
59
+ 1. Add `require 'simplecov/rspec'` to your `spec_helper.rb`.
60
+ 2. Replace `SimpleCov.start` with `SimpleCov::RSpec.start` in your `spec_helper.rb`,
61
+ ensuring this line appears before requiring your project files.
62
+
63
+ Here is an example `spec_helper.rb`. Your spec helper may include
64
+ other code in addition to these:
65
+
66
+ ```ruby
67
+ require 'simplecov/rspec'
68
+
69
+ SimpleCov::RSpec.start
70
+
71
+ require 'my_project'
72
+ ```
73
+
74
+ This will configure RSpec to fail when test coverage falls below 100%.
75
+
76
+ That is it!
77
+
78
+ ### Basic Setup
79
+
80
+ To initialize simplecov-rspec with defaults, add the following to your `spec_helper.rb`:
81
+
82
+ ```ruby
83
+ require 'simplecov/rspec'
84
+
85
+ SimpleCov::RSpec.start
86
+ ```
87
+
88
+ This is equivalent to starting with the following options:
89
+
90
+ ```ruby
91
+ SimpleCov::RSpec.start(
92
+ coverage_threshold: 100,
93
+ fail_on_low_coverage: true,
94
+ list_lines_not_covered: false
95
+ )
96
+ ```
97
+
98
+ The test coverage threshold is the minimum percent of lines covered by tests as
99
+ tracked by SimpleCov.
100
+
101
+ To initialize SimpleCov with a test coverage threshold less than 100%:
102
+
103
+ ```ruby
104
+ SimpleCov::RSpec.start(coverage_threshold: 90)
105
+ ```
106
+
107
+ A configuration block can be given to the `start` method to further configure
108
+ SimpleCov:
109
+
110
+ ```ruby
111
+ # Initialize SimpleCov with a specific formatter
112
+ SimpleCov::RSpec.start { formatter = SimpleCov::Formatter::LcovFormatter }
113
+ ```
114
+
115
+ This block is passed on to `SimpleCov::RSpec.start`. See [Configuring
116
+ SimpleCov](https://github.com/simplecov-ruby/simplecov?tab=readme-ov-file#configuring-simplecov)
117
+ for details.
118
+
119
+ ### Configuration from Environment Variables
120
+
121
+ Environment variables can be used to set these options as follows:
122
+
123
+ You can configure `simplecov-rspec` using the following environment variables:
124
+
125
+ * **`COVERAGE_THRESHOLD`**: Sets the minimum coverage threshold (0-100). Overrides
126
+ `coverage_threshold`.
127
+ * **`FAIL_ON_LOW_COVERAGE`**: Controls whether tests fail if coverage is below the threshold.
128
+ Set to 'true', 'yes', 'on', or '1' (case insensitive) to enable.
129
+ * **`LIST_UNCOVERED_LINES`**: Determines if uncovered lines are listed. Set to 'true',
130
+ 'yes', 'on', or '1' (case insensitive) to enable.
131
+
132
+ For example, here is a bash script to run tests in an infinite loop while writing
133
+ test output to `fail.txt`:
134
+
135
+ ```bash
136
+ while true; do COV_NO_FAIL=TRUE rspec >> fail.txt; done
137
+ ```
138
+
139
+ In a CI system, you might want to set `LIST_UNCOVERED_LINES=yes` in order to list
140
+ uncovered lines on different platforms than the one you run for local development.
141
+
142
+ ## Development
143
+
144
+ If you want to contribute or experiment with the gem, follow these steps to set up
145
+ your development environment:
146
+
147
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake
148
+ spec` to run the tests. You can also run `bin/console` for an interactive prompt that
149
+ will allow you to experiment.
150
+
151
+ To install this gem onto your local machine, run `bundle exec rake install`. To
152
+ release a new version, update the version number in `version.rb`, and then run
153
+ `bundle exec rake release`, which will create a git tag for the version, push git
154
+ commits and the created tag, and push the `.gem` file to
155
+ [rubygems.org](https://rubygems.org).
156
+
157
+ ## Contributing
158
+
159
+ Bug reports and pull requests are welcome on GitHub at
160
+ https://github.com/main-branch/simplecov-rspec. This project is intended to be a safe,
161
+ welcoming space for collaboration, and contributors are expected to adhere to the
162
+ [code of
163
+ conduct](https://github.com/main-branch/simplecov-rspec/blob/main/CODE_OF_CONDUCT.md).
164
+
165
+ ## License
166
+
167
+ The gem is available as open source under the terms of the [MIT
168
+ License](https://opensource.org/licenses/MIT).
169
+
170
+ ## Code of Conduct
171
+
172
+ Everyone interacting in the Simplecov::Rspec project's codebases, issue trackers,
173
+ chat rooms and mailing lists is expected to follow the [code of
174
+ conduct](https://github.com/main-branch/simplecov-rspec/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ desc 'Run the same tasks that the CI build will run'
4
+ if RUBY_PLATFORM == 'java'
5
+ task default: %w[spec rubocop bundle:audit build]
6
+ else
7
+ task default: %w[spec rubocop yard bundle:audit build]
8
+ end
9
+
10
+ # Bundler Audit
11
+
12
+ require 'bundler/audit/task'
13
+ Bundler::Audit::Task.new
14
+
15
+ # Bundler Gem Build
16
+
17
+ require 'bundler'
18
+ require 'bundler/gem_tasks'
19
+
20
+ begin
21
+ Bundler.setup(:default, :development)
22
+ rescue Bundler::BundlerError => e
23
+ warn e.message
24
+ warn 'Run `bundle install` to install missing gems'
25
+ exit e.status_code
26
+ end
27
+
28
+ CLEAN << 'pkg'
29
+ CLOBBER << 'Gemfile.lock'
30
+
31
+ # RSpec
32
+
33
+ require 'rspec/core/rake_task'
34
+
35
+ RSpec::Core::RakeTask.new do |t|
36
+ t.pattern = 'spec/**/*{_spec.rb,.feature}'
37
+ end
38
+
39
+ CLEAN << 'coverage'
40
+ CLEAN << '.rspec_status'
41
+ CLEAN << 'rspec-report.xml'
42
+
43
+ # Rubocop
44
+
45
+ require 'rubocop/rake_task'
46
+
47
+ RuboCop::RakeTask.new do |t|
48
+ t.options = %w[
49
+ --format progress
50
+ --format json --out rubocop-report.json
51
+ ]
52
+ end
53
+
54
+ CLEAN << 'rubocop-report.json'
55
+
56
+ unless RUBY_PLATFORM == 'java'
57
+ # yard:build
58
+
59
+ require 'yard'
60
+
61
+ YARD::Rake::YardocTask.new('yard:build') do |t|
62
+ t.files = %w[lib/**/*.rb examples/**/*]
63
+ t.stats_options = ['--list-undoc']
64
+ end
65
+
66
+ CLEAN << '.yardoc'
67
+ CLEAN << 'doc'
68
+
69
+ # yard:audit
70
+
71
+ desc 'Run yardstick to show missing YARD doc elements'
72
+ task :'yard:audit' do
73
+ sh "yardstick 'lib/**/*.rb'"
74
+ end
75
+
76
+ # yard:coverage
77
+
78
+ require 'yardstick/rake/verify'
79
+
80
+ Yardstick::Rake::Verify.new(:'yard:coverage') do |verify|
81
+ verify.threshold = 100
82
+ end
83
+
84
+ # yard
85
+
86
+ desc 'Run all YARD tasks'
87
+ task yard: %i[yard:build yard:audit yard:coverage]
88
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # SimpleCov module
4
+ module Simplecov
5
+ # SimpleCov::RSpec module
6
+ module Rspec
7
+ # This gem's version
8
+ VERSION = '0.1.0'
9
+ end
10
+ end
@@ -0,0 +1,328 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+
5
+ # SimpleCov namespace
6
+ module SimpleCov
7
+ # Configure SimpleCov to fail RSpec if the test coverage falls below a given threshold
8
+ #
9
+ # Configures RSpec to:
10
+ #
11
+ # 1. Fail (and exit with with a non-zero exitcode) if the test
12
+ # coverage is below the configured threshold and
13
+ # 2. (optionally) list the lines of code not covered by tests.
14
+ #
15
+ # Simply add the line `SimpleCov::RSpec.start` in place of `SimpleCov::Start` in
16
+ # the project's `spec_helper.rb`. This line must appear before the project is
17
+ # required.
18
+ #
19
+ # @example Initialize SimpleCov with defaults
20
+ # SimpleCov::RSpec.start
21
+ #
22
+ # @example Initialize SimpleCov with a test coverage threshold other than 100%
23
+ # SimpleCov::RSpec.start(coverage_threshold: 90, fail_on_low_coverage: true, list_uncovered_lines: false)
24
+ #
25
+ # @example Pass a configuration block to SimpleCov::RSpec.start
26
+ # SimpleCov::RSpec.start { formatter = SimpleCov::Formatter::LcovFormatter }
27
+ #
28
+ # @example Bash script to run tests in an infinite loop writing failures to `fail.txt`:
29
+ # while true; do FAIL_ON_LOW_COVERAGE=TRUE rspec >> fail.txt; done
30
+ #
31
+ # @!attribute [r] env
32
+ # Command line environment variables (default: ENV)
33
+ # @return [Hash]
34
+ # @api private
35
+ #
36
+ # @!attribute [r] simplecov_module
37
+ # The SimpleCov module (default: ::SimpleCov)
38
+ # @return [Module]
39
+ # @api private
40
+ #
41
+ # @!attribute [r] start_config_block
42
+ # A configuration block to pass to `SimpleCov.start`
43
+ # @return [Proc]
44
+ # @api private
45
+ #
46
+ # @api public
47
+ #
48
+ class RSpec
49
+ # rubocop:disable Layout/LineLength
50
+
51
+ # Configure and start SimpleCov for RSpec
52
+ #
53
+ # @example Initialize SimpleCov with defaults
54
+ # SimpleCov::RSpec.start
55
+ #
56
+ # @return [Void]
57
+ #
58
+ # @api public
59
+ #
60
+ # @overload start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block)
61
+ #
62
+ # @param coverage_threshold [Integer] the test coverage threshold (default: 100)
63
+ #
64
+ # Coverage below this threshold will cause the rspec to fail if
65
+ # fail_on_low_coverage is true.
66
+ #
67
+ # @param fail_on_low_coverage [Boolean] whether to fail if the coverage is below the threshold (default: true)
68
+ #
69
+ # This setting will be read from the environment variable FAIL_ON_LOW_COVERAGE if
70
+ # it is NOT given in the `.start` method. Setting that environment variable
71
+ # to 'true', 'yes', 'on', or '1' will cause this setting to be `false`
72
+ # (the logic is inverted). Any other value will cause this seetting to
73
+ # be `true`.
74
+ #
75
+ # @param list_uncovered_lines [Boolean] whether to list the lines not covered by tests (default: false)
76
+ #
77
+ # All lines not covered by tests will be listed if the coverage is below the threshold.
78
+ # Probably only makes sense to use if the threshold is 100%.
79
+ #
80
+ # This setting will be read from the environment variable LIST_UNCOVERED_LINES
81
+ # if it is NOT given in the `.start` method. Setting that environment variable
82
+ # to 'true', 'yes', 'on', or '1' will cause this setting to be `true`. Any
83
+ # other value will cause this setting to be `false`.
84
+ #
85
+ # @param start_config_block [Proc] a configuration block to pass to `SimpleCov.start` (default: nil)
86
+ #
87
+ # @param rspec_dry_run [Boolean] whether the rspec run is a dry run
88
+ #
89
+ # Typically not set by the user. Used for this gem's unit testing.
90
+ #
91
+ # If RSpec is being run in dry run mode, test coverage under the threshold will not fail the build.
92
+ #
93
+ # The purpose of this is to allow the test coverage to be run in a dry run by an IDE
94
+ # so it can report failed tests and coverage without reporting that the entire RSpec
95
+ # run has failed.
96
+ #
97
+ # @param simplecov_module [Module] the SimpleCov module (default: ::SimpleCov)
98
+ #
99
+ # Typically not set by the user. Used for this gem's unit testing.
100
+ #
101
+ # Allows the SimpleCov module to be mocked.
102
+ #
103
+ # @param env [Hash] the environment variables (default: ENV)
104
+ #
105
+ # Typically not set by the user. Used for this gem's unit testing.
106
+ #
107
+ # @example Initialize SimpleCov with a test coverage threshold other than 100%
108
+ # SimpleCov::RSpec.start(coverage_threshold: 90)
109
+ #
110
+ # @example Initialize SimpleCov to not fail the test run if the coverage is below the threshold
111
+ # SimpleCov::RSpec.start(fail_on_low_coverage: false)
112
+ #
113
+ # # OR use an environment variable to override the default
114
+ # ENV['FAIL_ON_LOW_COVERAGE'] = 'true'
115
+ # SimpleCov::RSpec.start
116
+ #
117
+ # # OR use an environment variable to override the default from the rspec command line
118
+ # FAIL_ON_LOW_COVERAGE=TRUE rspec
119
+ #
120
+ # @example Initialize SimpleCov to list the lines not covered by tests
121
+ # SimpleCov::RSpec.start(list_uncovered_lines: true)
122
+ #
123
+ # # OR use an environment variable to override the default
124
+ # ENV['LIST_UNCOVERED_LINES'] = 'true'
125
+ # SimpleCov::RSpec.start
126
+ #
127
+ # # OR use an environment variable to override the default from the rspec command line
128
+ # LIST_UNCOVERED_LINES=TRUE rspec
129
+ #
130
+ def self.start(...) = new(...).send(:start)
131
+
132
+ # rubocop:enable Layout/LineLength
133
+
134
+ # Environment variable to override coverage_threshold
135
+ COVERAGE_THRESHOLD = 'COVERAGE_THRESHOLD'
136
+
137
+ # Environment variable to override fail_on_low_coverage
138
+ FAIL_ON_LOW_COVERAGE = 'FAIL_ON_LOW_COVERAGE'
139
+
140
+ # Environment variable to override list_uncovered_lines
141
+ LIST_UNCOVERED_LINES = 'LIST_UNCOVERED_LINES'
142
+
143
+ # Default value for coverage_threshold
144
+ DEFAULT_TEST_COVERAGE_THRESHOLD = 100
145
+
146
+ # Default value for fail_on_low_coverage
147
+ DEFAULT_FAIL_ON_LOW_COVERAGEERAGE = true
148
+
149
+ # Default value for list_uncovered_lines
150
+ DEFAULT_LIST_UNCOVERED_LINES = false
151
+
152
+ attr_reader :env, :simplecov_module, :start_config_block
153
+
154
+ # The coverage threshold
155
+ #
156
+ # Searches the ENV, the value given in the `.start` method, and the default value
157
+ # and returns the first value found.
158
+ #
159
+ # @return [Integer]
160
+ #
161
+ # @api private
162
+ #
163
+ def coverage_threshold
164
+ return env.fetch(COVERAGE_THRESHOLD).to_i if env.key?(COVERAGE_THRESHOLD)
165
+
166
+ return @coverage_threshold.to_i unless @coverage_threshold.nil?
167
+
168
+ DEFAULT_TEST_COVERAGE_THRESHOLD
169
+ end
170
+
171
+ # Whether to fail if the coverage is below the threshold
172
+ #
173
+ # Searches the ENV, the value given in the `.start` method, and the default value
174
+ # and returns the first value found.
175
+ #
176
+ # @return [Boolean]
177
+ #
178
+ # @api private
179
+ #
180
+ def fail_on_low_coverage?
181
+ return false if rspec_dry_run?
182
+
183
+ return env_true?(FAIL_ON_LOW_COVERAGE) if env.key?(FAIL_ON_LOW_COVERAGE)
184
+
185
+ return @fail_on_low_coverage unless @fail_on_low_coverage.nil?
186
+
187
+ DEFAULT_FAIL_ON_LOW_COVERAGEERAGE
188
+ end
189
+
190
+ # Whether to list the lines not covered by tests
191
+ #
192
+ # Searches the ENV, the value given in the `.start` method, and the default value
193
+ # and returns the first value found.
194
+ #
195
+ # @return [Boolean]
196
+ #
197
+ # @api private
198
+ #
199
+ def list_uncovered_lines?
200
+ return env_true?(LIST_UNCOVERED_LINES) if env.key?(LIST_UNCOVERED_LINES)
201
+
202
+ return @list_uncovered_lines unless @list_uncovered_lines.nil?
203
+
204
+ DEFAULT_LIST_UNCOVERED_LINES
205
+ end
206
+
207
+ # Whether the rspec run is a dry run
208
+ #
209
+ # @return [Boolean]
210
+ #
211
+ # @api private
212
+ #
213
+ def rspec_dry_run? = @rspec_dry_run
214
+
215
+ private
216
+
217
+ # rubocop:disable Metrics/ParameterLists
218
+
219
+ # Create a new SimpleCov::RSpec instance
220
+ # @see SimpleCov::RSpec.start
221
+ # @api private
222
+ def initialize(
223
+ coverage_threshold: nil,
224
+ fail_on_low_coverage: nil,
225
+ list_uncovered_lines: nil,
226
+ rspec_dry_run: ::RSpec.configuration.dry_run?,
227
+ env: ENV,
228
+ simplecov_module: ::SimpleCov,
229
+ &start_config_block
230
+ )
231
+ @coverage_threshold = coverage_threshold
232
+ @fail_on_low_coverage = fail_on_low_coverage
233
+ @list_uncovered_lines = list_uncovered_lines
234
+ @start_config_block = start_config_block
235
+ @rspec_dry_run = rspec_dry_run
236
+ @env = env
237
+ @simplecov_module = simplecov_module
238
+ end
239
+
240
+ # rubocop:enable Metrics/ParameterLists
241
+
242
+ # Set the at_exit hook and then configure and start SimpleCov
243
+ # @return [Void]
244
+ # @api private
245
+ def start
246
+ simplecov_module.at_exit(&at_exit_hook)
247
+ simplecov_module.start(&start_config_block)
248
+ end
249
+
250
+ # Called by SimpleCov.at_exit
251
+ # @return [Proc]
252
+ # @api private
253
+ def at_exit_hook
254
+ lambda do
255
+ simplecov_module.result.format!
256
+ output_at_exit_report
257
+ exit 1 if coverage_below_threshold? && fail_on_low_coverage?
258
+ end
259
+ end
260
+
261
+ # Output the at_exit report
262
+ # @return [Void]
263
+ # @api private
264
+ def output_at_exit_report
265
+ low_coverage_report if show_low_coverage_report?
266
+ lines_not_covered_report if show_lines_not_covered_report?
267
+ $stderr.puts if show_low_coverage_report? || show_lines_not_covered_report?
268
+ end
269
+
270
+ # Whether to show the low coverage report
271
+ # @return [Boolean]
272
+ # @api private
273
+ def show_low_coverage_report? = coverage_below_threshold?
274
+
275
+ # Whether the test coverage is below the threshold
276
+ # @return [Boolean]
277
+ # @api private
278
+ def coverage_below_threshold? = simplecov_module.result.covered_percent < coverage_threshold
279
+
280
+ # Output the low coverage part of the at_exit report
281
+ # @return [Void]
282
+ # @api private
283
+ def low_coverage_report
284
+ $stderr.puts
285
+ $stderr.print 'FAIL: ' if fail_on_low_coverage?
286
+ $stderr.puts "Test coverage is below the low coverage threshold of #{coverage_threshold}%"
287
+ end
288
+
289
+ # Whether there are uncovered lines
290
+ # @return [Boolean]
291
+ # @api private
292
+ def uncovered_lines_found? = simplecov_module.result.files.any? { |source_file| source_file.missed_lines.any? }
293
+
294
+ # Whether to show lines not covered report
295
+ # @return [Boolean]
296
+ # @api private
297
+ def show_lines_not_covered_report? = list_uncovered_lines? && uncovered_lines_found?
298
+
299
+ # Output the lines not covered part of the at_exit report
300
+ # @return [Void]
301
+ # @api private
302
+ def lines_not_covered_report
303
+ $stderr.puts
304
+ $stderr.puts "The following lines were not covered by tests:\n"
305
+ simplecov_module.result.files.each do |source_file| # SimpleCov::SourceFile
306
+ source_file.missed_lines.each do |line| # SimpleCov::SourceFile::Line
307
+ $stderr.puts " ./#{source_file.project_filename}:#{line.number}"
308
+ end
309
+ end
310
+ end
311
+
312
+ # Return `true` if the environment variable is set to a truthy value
313
+ #
314
+ # @example
315
+ # env_true?('LIST_UNCOVERED_LINES')
316
+ #
317
+ # @param name [String] the name of the environment variable
318
+ # @return [Boolean]
319
+ # @api private
320
+ #
321
+ def env_true?(name)
322
+ value = env.fetch(name, '').downcase
323
+ %w[yes on true 1].include?(value)
324
+ end
325
+ end
326
+ end
327
+
328
+ require_relative 'rspec/version'
metadata ADDED
@@ -0,0 +1,235 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplecov-rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Couball
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler-audit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: create_github_release
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fuubar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.64'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.64'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.22'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.22'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov-lcov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.8'
125
+ - !ruby/object:Gem::Dependency
126
+ name: turnip
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '4.4'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '4.4'
139
+ - !ruby/object:Gem::Dependency
140
+ name: redcarpet
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.6'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.6'
153
+ - !ruby/object:Gem::Dependency
154
+ name: yard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '0.9'
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 0.9.28
163
+ type: :development
164
+ prerelease: false
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - "~>"
168
+ - !ruby/object:Gem::Version
169
+ version: '0.9'
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: 0.9.28
173
+ - !ruby/object:Gem::Dependency
174
+ name: yardstick
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: '0.9'
180
+ type: :development
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - "~>"
185
+ - !ruby/object:Gem::Version
186
+ version: '0.9'
187
+ description: |
188
+ Configures RSpec to fail (and exit with with a non-zero exitcode) if the
189
+ test coverage is below the configured threshold and (optionally) list the
190
+ lines of code not covered by tests.
191
+ email:
192
+ - jcouball@yahoo.com
193
+ executables: []
194
+ extensions: []
195
+ extra_rdoc_files: []
196
+ files:
197
+ - ".rspec"
198
+ - ".rubocop.yml"
199
+ - CHANGELOG.md
200
+ - CODE_OF_CONDUCT.md
201
+ - LICENSE.txt
202
+ - README.md
203
+ - Rakefile
204
+ - lib/simplecov/rspec.rb
205
+ - lib/simplecov/rspec/version.rb
206
+ homepage: https://github.com/main-branch/simplecov-rspec
207
+ licenses:
208
+ - MIT
209
+ metadata:
210
+ allowed_push_host: https://rubygems.org
211
+ homepage_uri: https://github.com/main-branch/simplecov-rspec
212
+ changelog_uri: https://rubydoc.info/gems/simplecov-rspec/0.1.0/file/CHANGELOG.md
213
+ documentation_uri: https://rubydoc.info/gems/simplecov-rspec/0.1.0
214
+ rubygems_mfa_required: 'true'
215
+ post_install_message:
216
+ rdoc_options: []
217
+ require_paths:
218
+ - lib
219
+ required_ruby_version: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: 3.0.0
224
+ required_rubygems_version: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - ">="
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
229
+ requirements: []
230
+ rubygems_version: 3.5.16
231
+ signing_key:
232
+ specification_version: 4
233
+ summary: Configure SimpleCov to fail RSpec if the test coverage falls below a given
234
+ threshold
235
+ test_files: []