Skip to content

Commit 5121b60

Browse files
committed
📝 Document approach to accurate code coverage for version.rb
1 parent 3ed8b99 commit 5121b60

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,24 @@ MyLib::Version.to_a # => [0, 1, 0]
217217
MyLib::Version.to_h # => { major: 0, minor: 1, patch: 0, pre: "" }
218218
```
219219

220-
### Side benefit
220+
### Side benefit #1
221+
222+
You can reference the DRY version from your gemspec, *and* still get accurate code coverage!
223+
224+
```ruby
225+
# Get the GEMFILE_VERSION without *require* "my_gem/version", for code coverage accuracy
226+
# See: https://github.com/simplecov-ruby/simplecov/issues/557#issuecomment-825171399
227+
load 'lib/my_gem/version.rb'
228+
gem_version = MyGem::Version::VERSION
229+
MyGem::Version.send(:remove_const, :VERSION)
230+
231+
Gem::Specification.new do |spec|
232+
# ...
233+
spec.version = gem_version
234+
end
235+
```
236+
237+
### Side benefit #2
221238

222239
Your `version.rb` file now abides the Ruby convention of directory / path matching the namespace / class!
223240

0 commit comments

Comments
 (0)