From 0a27a7f44f2b4531c0c9cc2b6636ab1f46c08551 Mon Sep 17 00:00:00 2001 From: rafayet-monon Date: Thu, 15 Oct 2020 19:54:58 +0600 Subject: [PATCH 1/6] Created undercover-report cli --- README.md | 14 +++++++---- bin/undercover-report | 10 ++++++++ lib/undercover/cli.rb | 50 ++++++++++++++++++++++++++++++++++++++++ lib/undercover/plugin.rb | 10 ++++---- spec/cli_spec.rb | 46 ++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 9 deletions(-) create mode 100755 bin/undercover-report create mode 100644 lib/undercover/cli.rb create mode 100644 spec/cli_spec.rb diff --git a/README.md b/README.md index 319e9aa..2d5de74 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/bin/undercover-report b/bin/undercover-report new file mode 100755 index 0000000..77ceb57 --- /dev/null +++ b/bin/undercover-report @@ -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) diff --git a/lib/undercover/cli.rb b/lib/undercover/cli.rb new file mode 100644 index 0000000..153ae27 --- /dev/null +++ b/lib/undercover/cli.rb @@ -0,0 +1,50 @@ +# 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}` + 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 diff --git a/lib/undercover/plugin.rb b/lib/undercover/plugin.rb index 436c64b..2802ece 100644 --- a/lib/undercover/plugin.rb +++ b/lib/undercover/plugin.rb @@ -23,15 +23,15 @@ class DangerUndercover < Plugin # 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 = 'coverage/undercover.txt', 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 diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb new file mode 100644 index 0000000..7360111 --- /dev/null +++ b/spec/cli_spec.rb @@ -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) + 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 From 903cf89acfdde8baa71b0cc9211851abda979993 Mon Sep 17 00:00:00 2001 From: rafayet-monon Date: Fri, 16 Oct 2020 09:54:17 +0600 Subject: [PATCH 2/6] Fix undercover argument passing. --- lib/undercover/cli.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/undercover/cli.rb b/lib/undercover/cli.rb index 153ae27..fb90e9c 100644 --- a/lib/undercover/cli.rb +++ b/lib/undercover/cli.rb @@ -11,7 +11,8 @@ class << self # @return [String] # def run(args = nil) - undercover_output = `undercover #{args}` + undercover_output = `undercover #{args.join(' ')}` + File.open(output_file, 'w') do |f| f.write(undercover_output) end From 309f3470c4524a8e4aa4ace060a8ad364238572b Mon Sep 17 00:00:00 2001 From: rafayet-monon Date: Fri, 16 Oct 2020 10:14:13 +0600 Subject: [PATCH 3/6] Fix undercover argument passing bug on nil. --- lib/undercover/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/undercover/cli.rb b/lib/undercover/cli.rb index fb90e9c..18a6997 100644 --- a/lib/undercover/cli.rb +++ b/lib/undercover/cli.rb @@ -11,7 +11,7 @@ class << self # @return [String] # def run(args = nil) - undercover_output = `undercover #{args.join(' ')}` + undercover_output = `undercover #{args&.join(' ')}` File.open(output_file, 'w') do |f| f.write(undercover_output) From 54b93874d14959e8f3fd592c3ff13911d24de4e0 Mon Sep 17 00:00:00 2001 From: rafayet-monon Date: Fri, 16 Oct 2020 13:16:59 +0600 Subject: [PATCH 4/6] Update gem version to 1.1.0 --- lib/undercover/gem_version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/undercover/gem_version.rb b/lib/undercover/gem_version.rb index de3d654..cdd3d5a 100644 --- a/lib/undercover/gem_version.rb +++ b/lib/undercover/gem_version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Undercover - VERSION = '1.0.0' + VERSION = '1.1.0' end From 8bd498350b37b703057501752ef36f8f6413bf0a Mon Sep 17 00:00:00 2001 From: rafayet-monon Date: Mon, 19 Oct 2020 11:36:10 +0600 Subject: [PATCH 5/6] Add default path as constant. --- lib/undercover/plugin.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/undercover/plugin.rb b/lib/undercover/plugin.rb index 2802ece..d069899 100644 --- a/lib/undercover/plugin.rb +++ b/lib/undercover/plugin.rb @@ -16,6 +16,7 @@ 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, @@ -23,7 +24,7 @@ class DangerUndercover < Plugin # If there are reports then it shows the report as a warning in danger. # @return [void] # - def report(undercover_path = 'coverage/undercover.txt', 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.force_encoding('UTF-8') From 4fd259d2ba33632a9d1b4b262c93f713e6b24eae Mon Sep 17 00:00:00 2001 From: rafayet-monon Date: Tue, 20 Oct 2020 12:46:54 +0600 Subject: [PATCH 6/6] Add comments in tests. --- Gemfile.lock | 8 +++++--- spec/cli_spec.rb | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index dd26c32..609f9a7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - danger-undercover (1.0.0) + danger-undercover (1.1.0) danger-plugin-api (~> 1.0) GEM @@ -19,7 +19,7 @@ GEM colored2 (3.1.2) cork (0.3.0) colored2 (~> 3.1) - danger (8.0.6) + danger (8.1.0) claide (~> 1.0) claide-plugins (>= 0.9.2) colored2 (~> 3.1) @@ -35,8 +35,9 @@ GEM danger-plugin-api (1.0.0) danger (> 2.0) diff-lcs (1.4.4) - faraday (1.0.1) + faraday (1.1.0) multipart-post (>= 1.2, < 3) + ruby2_keywords faraday-http-cache (2.2.0) faraday (>= 0.8) ffi (1.13.1) @@ -117,6 +118,7 @@ GEM rubocop-ast (0.7.1) parser (>= 2.7.1.5) ruby-progressbar (1.10.1) + ruby2_keywords (0.0.2) sawyer (0.8.2) addressable (>= 2.3.5) faraday (> 0.8, < 2.0) diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 7360111..0b621dc 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -7,14 +7,16 @@ 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') } + let!(:directory) { File.join(Dir.getwd, 'coverage') } # default directory + let!(:file) { File.join(Dir.getwd, 'coverage/undercover.txt') } # default file before(:each) do + # mocks the undercover #{args&.join(' ')} CLI command output. allow(described_class).to receive(:`).and_return(mock_message) end after(:all) do + # removes the folder after tests pass. FileUtils.rm_rf(File.join(Dir.getwd, 'coverage')) end