Skip to content

Commit

Permalink
Merge pull request #4 from appbot/namespace-and-readme
Browse files Browse the repository at this point in the history
Correct namespacing for a SimpleCov formatter
  • Loading branch information
gondalez authored Aug 23, 2024
2 parents f6c3cdb + a71bb09 commit c299ef6
Show file tree
Hide file tree
Showing 20 changed files with 189 additions and 154 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## [0.1.0] - 2024-07-19

- Initial release

## [1.0.0] - 2024-07-19

- Fix namespacing
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
simplecov-inline (0.1.0)
simplecov-inline (1.0.0)
rainbow (~> 3.1)
simplecov (~> 0.22)

Expand All @@ -11,7 +11,7 @@ GEM
ast (2.4.2)
byebug (11.1.3)
diff-lcs (1.5.1)
docile (1.4.0)
docile (1.4.1)
json (2.7.2)
language_server-protocol (3.17.0.3)
parallel (1.26.3)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2024 tris
Copyright (c) 2024 Appbot

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
55 changes: 45 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
# SimpleCov::Inline
# SimpleCov::Formatter::Inline

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simplecov/inline`. To experiment with that code, run `bin/console` for an interactive prompt.
A SimpleCov formatter that outputs missing line and branch coverage inline.

TODO: Delete this and the text above, and describe your gem
Can be configured to filter to certain files and skip output as needed.

Comes with a rails+rspec integration that:

* filters to only the rspec file and the file of the module being tested.
* skips output if any examples fail
* skips output if no examples were run
* skips output if not all examples within a spec file are run

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add simplecov-inline
$ bundle add simplecov-inline --github="appbot/simplecov-inline"

## Usage

If bundler is not being used to manage dependencies, install the gem by executing:
### Plug the formatter in

$ gem install simplecov-inline
``` ruby
SimpleCov.formatter = SimpleCov::Formatter::Inline
```

## Usage
### Configure rails and rspec

``` ruby
SimpleCov::Inline::Integration.configure_rspec_rails
```

TODO: Write usage instructions here
### Manually Filter Files

If you are not using rspec and rails you can manually filter to a set of files.

``` ruby
SimpleCov::Formatter::Inline.config do |config|
config.files = ['/path/to/your/file.rb']
end
```

### Manually Supress Output

If you are not using rspec and rails you can turn off the formatter with a reason.

``` ruby
SimpleCov::Formatter::Inline.config do |config|
config.no_output!(reason: 'your reason here')
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
After checking out the repo, run `rake` to run the tests and rubocop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

If would like to use docker, you can run `docker/rake` to save yourself having to install ruby locally.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

Expand All @@ -34,4 +69,4 @@ The gem is available as open source under the terms of the [MIT License](https:/

## Code of Conduct

Everyone interacting in the SimpleCov::Inline project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/appbot/simplecov-inline/blob/main/CODE_OF_CONDUCT.md).
Everyone interacting in the SimpleCov::Formatter::Inline project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/appbot/simplecov-inline/blob/main/CODE_OF_CONDUCT.md).
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

require "bundler/setup"
require "simplecov/inline"
require "simplecov/formatter/inline"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'rainbow'

module SimpleCov
module Inline
class Formatter
module Formatter
class Inline
Result = Struct.new(:file, :start_line, :end_line, :type) do
def to_s
lines = [start_line, end_line].uniq.join('-')
Expand Down
66 changes: 66 additions & 0 deletions lib/simplecov/formatter/inline/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module SimpleCov
module Formatter
class Inline
class Integration
class << self
def configure_rspec_rails(rspec: RSpec, rails: Rails)
RSpec::Core::Formatters.register SimpleCov::Formatter::Inline::RSpecFormatterSkipOnFailure, :dump_failures

rspec.configure do |rspec_config|
rspec_config.add_formatter SimpleCov::Formatter::Inline::RSpecFormatterSkipOnFailure
rspec_config.before(:suite) do
SimpleCov::Formatter::Inline::Integration
.configure_formatter(rspec_config:, rails_root: rails.root.to_s)
end
end
end

def configure_formatter(rspec_config:, rails_root:)
# Restrict coverage reporting to spec files and the file they are testing.
# Note files_to_run contains all spec files when running rspec with no args,
# so in that case do not filter.
# To do so would exclude files that are covered but not directly tested.
return if running_all_specs?(rspec_config:, rails_root:)

SimpleCov::Formatter::Inline.config do |coverage_config|
if rspec_config.inclusion_filter.rules.key?(:locations)
# Skip coverage output if running rspec against a single line.
# e.g. spec/x_spec.rb:123
coverage_config.no_output!(reason: 'filtered to line of code')
else
coverage_config.files = rspec_config.files_to_run.flat_map do |spec_path|
[spec_path, rails_path_under_test(spec_path:, rails_root:)]
end.compact
end
end
end

private

def running_all_specs?(rspec_config:, rails_root:)
Dir.glob("#{rails_root}#{rspec_config.pattern}").to_set == rspec_config.files_to_run.to_set
end

def rails_path_under_test(spec_path:, rails_root:)
# Mappings
# /app/spec/lib/x_spec.rb -> /app/lib/x.rb
# /app/spec/controller/y_spec.rb -> /app/app/controllers/y.rb
raise 'Spec file must be in rails root.' unless spec_path.start_with?(rails_root)

_spec_root, spec_type, *other_directories, filename = spec_path[(rails_root.length + 1)..].split('/')

return if filename.nil?

filename_under_test = filename.gsub(/_spec.rb$/, '.rb')

if spec_type == 'lib'
[rails_root, spec_type, *other_directories, filename_under_test]
else
[rails_root, 'app', spec_type, *other_directories, filename_under_test]
end.compact.join('/')
end
end
end
end
end
end
28 changes: 28 additions & 0 deletions lib/simplecov/formatter/inline/rspec_formatter_skip_on_failure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module SimpleCov
module Formatter
class Inline
class RSpecFormatterSkipOnFailure
def initialize(output)
@output = output
end

def dump_failures(notification)
return unless skip_reason(notification:)

SimpleCov::Formatter::Inline.config do |coverage_config|
coverage_config.no_output!(reason: skip_reason(notification:))
end
end

private

def skip_reason(notification:)
return 'no examples were run' if notification.examples.none?
return 'some specs failed' if notification.failed_examples.any?

nil
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/simplecov/formatter/inline/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module SimpleCov
module Formatter
class Inline
VERSION = '1.0.0'.freeze
end
end
end
15 changes: 4 additions & 11 deletions lib/simplecov/inline.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
require_relative 'inline/version'
require_relative 'inline/formatter'
require_relative 'inline/rspec_formatter_skip_on_failure'
require_relative 'inline/integration'

module SimpleCov
module Inline
class Error < StandardError; end
# Your code goes here...
end
end
require_relative 'formatter/inline'
require_relative 'formatter/inline/integration'
require_relative 'formatter/inline/rspec_formatter_skip_on_failure'
require_relative 'formatter/inline/version'
63 changes: 0 additions & 63 deletions lib/simplecov/inline/integration.rb

This file was deleted.

26 changes: 0 additions & 26 deletions lib/simplecov/inline/rspec_formatter_skip_on_failure.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/simplecov/inline/version.rb

This file was deleted.

6 changes: 0 additions & 6 deletions sig/simplecov/inline.rbs

This file was deleted.

4 changes: 2 additions & 2 deletions simplecov-inline.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require_relative 'lib/simplecov/inline/version'
require_relative 'lib/simplecov/formatter/inline/version'

Gem::Specification.new do |spec|
spec.name = 'simplecov-inline'
spec.version = SimpleCov::Inline::VERSION
spec.version = SimpleCov::Formatter::Inline::VERSION
spec.authors = ['tris']
spec.email = ['[email protected]']

Expand Down
Loading

0 comments on commit c299ef6

Please sign in to comment.