Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create undercover-report command #3

Merged
merged 6 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ To know more about running undercover [visit here](https://github.com/grodowski/
> Use the `-c --compare ref` flag to specify a git ref (commit hash, branch name, tag) to compare against.
**This is a recommended usage for CI/CD build environments**, as `undercover` will `exit 1` if there are any warnings.

Run the below command to output undercover report to a `txt` file which this plugin will use to geneate PR comments.
To use it on a CI server, run this command before running `Danger` so that the file is created beforehand.
This plugin provides a command `undercover-report` that uses `undercover` command from
[Undercover](https://github.com/grodowski/undercover) gem. It takes all the options that `undercover` command takes.

$ undercover -c $compare_git_ref > coverage/undercover.txt
They both works in the same way but what `undercover-report` extra does is it prints `undercover` report to a default
file in `coverage/undercover.txt`. This makes using `undercover` in CI server much easier.

To use it on a CI server, run the below command before running `Danger` so that the report file is created beforehand
which `Danger` will use..

$ undercover-report -c $compare_git_ref

>Here $compare_git_ref as per undercover documentation, can be a commit hash, branch name, or tag. i.e. origin/master
, origin/development
Expand All @@ -27,7 +33,7 @@ To use it on a CI server, run this command before running `Danger` so that the f
Then in your `Dangerfile` add the following line with the output file

```ruby
undercover.report 'coverage/undercover.txt'
undercover.report
```

## Development
Expand Down
10 changes: 10 additions & 0 deletions bin/undercover-report
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# bin/undercover_report

#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift("#{__dir__}/../lib")

require 'undercover/cli'

puts DangerUndercover::CLI.run(ARGV)
51 changes: 51 additions & 0 deletions lib/undercover/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'fileutils'

module DangerUndercover
# module for undercover-report
module CLI
class << self
# Runs the undercover command with provided arguments
# and writes the output to a file
# @return [String]
#
def run(args = nil)
undercover_output = `undercover #{args&.join(' ')}`

File.open(output_file, 'w') do |f|
f.write(undercover_output)
end

undercover_output
end

private

# Returns the file to write report to
# @return [String]
#
def output_file
create_directory!

File.join(output_directory, 'undercover.txt')
end

# Creates directory if doesn't exists
# @return [String]
#
def create_directory!
return if Dir.exist?(output_directory)

FileUtils.mkdir_p(output_directory)
end

# Output directory
# @return [String]
#
def output_directory
File.join(Dir.getwd, 'coverage')
end
end
end
end
2 changes: 1 addition & 1 deletion lib/undercover/gem_version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Undercover
VERSION = '1.0.0'
VERSION = '1.1.0'
end
11 changes: 6 additions & 5 deletions lib/undercover/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ module Danger
#
class DangerUndercover < Plugin
VALID_FILE_FORMAT = '.txt'
DEFAULT_PATH = 'coverage/undercover.txt'

# Checks the file validity and warns if no file is found
# if a valid file is found then if there are no changes,
# shows the report as a message in Danger.
# If there are reports then it shows the report as a warning in danger.
# @return [void]
#
def report(undercover_path, sticky: true)
def report(undercover_path = DEFAULT_PATH, sticky: true)
return fail('Undercover: coverage report cannot be found.') unless valid_file? undercover_path

report = File.open(undercover_path).read
report = File.open(undercover_path).read.force_encoding('UTF-8')

if report.match(/No coverage is missing in latest changes/)
message(report, sticky: sticky)
else
if report.match(/some methods have no test coverage/)
warn(report, sticky: sticky)
else
message(report, sticky: sticky)
end
end

Expand Down
46 changes: 46 additions & 0 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require File.expand_path('spec_helper', __dir__)
require 'undercover/cli'

# rubocop:disable Metrics/BlockLength
module DangerUndercover
describe DangerUndercover::CLI do
let!(:mock_message) { 'Test Passed' }
let!(:directory) { File.join(Dir.getwd, 'coverage') }
let!(:file) { File.join(Dir.getwd, 'coverage/undercover.txt') }

before(:each) do
allow(described_class).to receive(:`).and_return(mock_message)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is receive(:)` for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

receive(:)` is to mock the -

undercover #{args&.join(' ')} CLI command output.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I would not have guessed. So a comment would help future devs, yourself included.

end

after(:all) do
FileUtils.rm_rf(File.join(Dir.getwd, 'coverage'))
end

it 'prints the undercover output' do
expect(described_class.run).to eql(mock_message)
end

it "creates a default folder if doesn't exists" do
FileUtils.rm_rf(directory)
described_class.run

expect(Dir.exist?(directory)).to be true
end

it 'creates default file undercover.txt' do
described_class.run

expect(File.exist?(file)).to be true
end

it 'writes undercover report to default file' do
described_class.run
report = File.open(file).read

expect(report).to eql(mock_message)
end
end
end
# rubocop:enable Metrics/BlockLength