From 92c496427c259834fc66f75acdbb1875d555c501 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 21:16:46 +0200 Subject: [PATCH 1/9] Spec: Show nil value as nil, not as an empty string --- spec/unit/modulesync/git_service_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/modulesync/git_service_spec.rb b/spec/unit/modulesync/git_service_spec.rb index e8016c23..d20f0767 100644 --- a/spec/unit/modulesync/git_service_spec.rb +++ b/spec/unit/modulesync/git_service_spec.rb @@ -163,7 +163,7 @@ RSpec.shared_examples 'hostname_extractor' do |url, hostname| context "with '#{url}' URL" do subject { ModuleSync::GitService.extract_hostname(url) } - it "should extract '#{hostname}' as hostname" do + it "should extract #{hostname.nil? ? 'nil' : "'#{hostname}'"} as hostname" do expect(subject).to eq(hostname) end end From da01249aef332b1caf5f9c3ba434610dd178f7f7 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 14:54:26 +0200 Subject: [PATCH 2/9] CI: Run unit and behavior tests --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be508903..4db11632 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,9 @@ jobs: with: ruby-version: ${{ matrix.ruby }} bundler-cache: true - - name: Run tests + - name: Run unit tests run: bundle exec rake spec + - name: Run behavior tests + run: bundle exec cucumber - name: Build gem run: gem build *.gemspec From a4b2bf8bd75a80c65eef951e970a1246f36ab708 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 20:52:21 +0200 Subject: [PATCH 3/9] Simplecov: Move configuration to `.simplecov` file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to simplecov's documentation¹, to merge multiple results its better to only maintain the `.simplecov` file and let `require 'simplecov'` loads this file. [1] https://github.com/simplecov-ruby/simplecov#using-simplecov-for-centralized-config --- .simplecov | 25 +++++++++++++++++++++++++ spec/spec_helper.rb | 24 +----------------------- 2 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 .simplecov diff --git a/.simplecov b/.simplecov new file mode 100644 index 00000000..2b40ebb9 --- /dev/null +++ b/.simplecov @@ -0,0 +1,25 @@ +begin + require 'simplecov' + require 'simplecov-console' + require 'codecov' +rescue LoadError +else + SimpleCov.start do + track_files 'lib/**/*.rb' + + add_filter '/spec' + + enable_coverage :branch + + # do not track vendored files + add_filter '/vendor' + add_filter '/.vendor' + end + + SimpleCov.formatters = [ + SimpleCov::Formatter::Console, + SimpleCov::Formatter::Codecov, + ] +end + +# vim: filetype=ruby diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2740180c..f094b0d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,25 +1,3 @@ -begin - require 'simplecov' - require 'simplecov-console' - require 'codecov' -rescue LoadError -else - SimpleCov.start do - track_files 'lib/**/*.rb' - - add_filter '/spec' - - enable_coverage :branch - - # do not track vendored files - add_filter '/vendor' - add_filter '/.vendor' - end - - SimpleCov.formatters = [ - SimpleCov::Formatter::Console, - SimpleCov::Formatter::Codecov, - ] -end +require 'simplecov' require 'modulesync' From ec4e0646fd1b0cc892c311aee61615cd98bc2298 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 20:56:31 +0200 Subject: [PATCH 4/9] Gemfile: Add simplecov to development deps --- modulesync.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/modulesync.gemspec b/modulesync.gemspec index 1d5b7d35..804d811b 100644 --- a/modulesync.gemspec +++ b/modulesync.gemspec @@ -23,6 +23,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rspec' spec.add_development_dependency 'rubocop', '~> 0.50.0' spec.add_development_dependency 'cucumber' + spec.add_development_dependency 'simplecov' spec.add_runtime_dependency 'git', '~>1.7' spec.add_runtime_dependency 'gitlab', '~>4.0' From f4c666971626325f8d8610ea7ab17433255d846f Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 20:57:49 +0200 Subject: [PATCH 5/9] Cucumber: Use simplecov --- features/support/env.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/features/support/env.rb b/features/support/env.rb index de44b9d4..698d63b1 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,3 +1,7 @@ +require 'simplecov' + +SimpleCov.command_name 'Cucumber' + require 'aruba/cucumber' require_relative '../../spec/helpers/faker' From 069604e1fb425685e39dd53dcb495da6c3109951 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 21:10:59 +0200 Subject: [PATCH 6/9] Code coverage: Grab results when using Aruba To run behavior tests, `aruba` executes `bin/msync` directly. In order to allow simplecov to track files, etc. we need to setup it at application start. --- bin/msync | 32 ++++++++++++++++++++++++++++++++ features/support/env.rb | 3 +++ 2 files changed, 35 insertions(+) diff --git a/bin/msync b/bin/msync index 7afddef8..650f1e2a 100755 --- a/bin/msync +++ b/bin/msync @@ -1,5 +1,37 @@ #!/usr/bin/env ruby +if ENV['COVERAGE'] + # This block allow us to grab code coverage when running this script. + # Note: This environment variable is set in Cucumber/Aruba configuration to collect reports' + require 'simplecov' + + # https://github.com/simplecov-ruby/simplecov/issues/234 + # As described in the issue, every process must have an unique name: + SimpleCov.command_name "#{File.basename $PROGRAM_NAME} (pid: #{Process.pid})" + + # When running with aruba simplecov was using /tmp/aruba as the root folder. + # This is to force using the project folder + SimpleCov.root(File.join(File.expand_path(File.dirname(__FILE__)), '..')) + + SimpleCov.start do + filters.clear # This will remove the :root_filter and :bundler_filter that come via simplecov's defaults + + # Because simplecov filters everything outside of the SimpleCov.root + # This should be added, cf. + # https://github.com/colszowka/simplecov#default-root-filter-and-coverage-for-things-outside-of-it + add_filter do |src| + !(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /project/ + end + + # Ignoring test folders and tmp for Aruba + add_filter '/spec/' + add_filter '/test/' + add_filter '/features/' + add_filter '/tmp/' + add_filter '/vendor/' + end +end + lib = File.expand_path('../../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) diff --git a/features/support/env.rb b/features/support/env.rb index 698d63b1..730de3d6 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -10,4 +10,7 @@ Before do @aruba_timeout_seconds = 5 + + # This enables coverage when aruba runs `msync` executable (cf. `bin/msync`) + set_environment_variable('COVERAGE', '1') end From 7e5f404d42d4e9b6095026eea9c39cfd9812307f Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 21:15:25 +0200 Subject: [PATCH 7/9] Code coverage: Allow developer to use simplecov and CI to use codecov --- .github/workflows/ci.yml | 4 ++-- .simplecov | 26 ++++++++++++-------------- Gemfile | 2 +- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4db11632..4ab66428 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,9 +18,9 @@ jobs: - ruby: "2.6" - ruby: "2.7" - ruby: "3.0" - coverage: "yes" + codecov: "yes" env: - COVERAGE: ${{ matrix.coverage }} + CODECOV: ${{ matrix.codecov }} steps: - uses: actions/checkout@v2 - name: Install Ruby ${{ matrix.ruby }} diff --git a/.simplecov b/.simplecov index 2b40ebb9..e65ca011 100644 --- a/.simplecov +++ b/.simplecov @@ -1,20 +1,18 @@ -begin - require 'simplecov' - require 'simplecov-console' - require 'codecov' -rescue LoadError -else - SimpleCov.start do - track_files 'lib/**/*.rb' +SimpleCov.start do + track_files 'lib/**/*.rb' + + add_filter '/spec' - add_filter '/spec' + enable_coverage :branch - enable_coverage :branch + # do not track vendored files + add_filter '/vendor' + add_filter '/.vendor' +end - # do not track vendored files - add_filter '/vendor' - add_filter '/.vendor' - end +if ENV['CODECOV'] + require 'simplecov-console' + require 'codecov' SimpleCov.formatters = [ SimpleCov::Formatter::Console, diff --git a/Gemfile b/Gemfile index 42a7f317..c2c70247 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ group :release do gem 'github_changelog_generator', :require => false end -group :coverage, optional: ENV['COVERAGE']!='yes' do +group :coverage, optional: ENV['CODECOV']!='yes' do gem 'simplecov-console', :require => false gem 'codecov', :require => false end From 541d1406ae6120b90d6bea64bcd23cb833a60374 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Oct 2021 23:03:29 +0200 Subject: [PATCH 8/9] Simplecov: Track more files and categorize them using groups --- .simplecov | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.simplecov b/.simplecov index e65ca011..3654b3f9 100644 --- a/.simplecov +++ b/.simplecov @@ -1,13 +1,23 @@ SimpleCov.start do - track_files 'lib/**/*.rb' + add_group 'Source code', 'lib' - add_filter '/spec' + add_group 'Unit tests', 'spec' + + add_group 'Behavior tests', 'features' + add_filter '/features/support/env.rb' enable_coverage :branch # do not track vendored files add_filter '/vendor' add_filter '/.vendor' + + # exclude anything that is not in lib, spec or features directories + add_filter do |src| + !(src.filename =~ /^#{SimpleCov.root}\/(lib|spec|features)/) + end + + track_files '**/*.rb' end if ENV['CODECOV'] From 68b7578a9b9fc3d102765851a552846dfccf6db0 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 8 Oct 2021 16:44:11 +0200 Subject: [PATCH 9/9] Simplecov: Centralize filters setup --- .simplecov | 13 +++++++++++++ bin/msync | 30 +++++++----------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/.simplecov b/.simplecov index 3654b3f9..febc6938 100644 --- a/.simplecov +++ b/.simplecov @@ -1,4 +1,17 @@ SimpleCov.start do + if ENV['SIMPLECOV_ROOT'] + SimpleCov.root(ENV['SIMPLECOV_ROOT']) + + filters.clear # This will remove the :root_filter and :bundler_filter that come via simplecov's defaults + + # Because simplecov filters everything outside of the SimpleCov.root + # This should be added, cf. + # https://github.com/colszowka/simplecov#default-root-filter-and-coverage-for-things-outside-of-it + add_filter do |src| + !(src.filename =~ /^#{SimpleCov.root}/) + end + end + add_group 'Source code', 'lib' add_group 'Unit tests', 'spec' diff --git a/bin/msync b/bin/msync index 650f1e2a..ae7d7124 100755 --- a/bin/msync +++ b/bin/msync @@ -2,34 +2,18 @@ if ENV['COVERAGE'] # This block allow us to grab code coverage when running this script. - # Note: This environment variable is set in Cucumber/Aruba configuration to collect reports' + # + # Note: This environment variable (ie. COVERAGE) is set in Cucumber/Aruba configuration to collect reports + simplecov_root = File.expand_path File.join(File.dirname(__FILE__), '..') + + # When running with aruba simplecov was using /tmp/aruba as the root folder. + # This is to force using the project folder + ENV['SIMPLECOV_ROOT'] = simplecov_root require 'simplecov' # https://github.com/simplecov-ruby/simplecov/issues/234 # As described in the issue, every process must have an unique name: SimpleCov.command_name "#{File.basename $PROGRAM_NAME} (pid: #{Process.pid})" - - # When running with aruba simplecov was using /tmp/aruba as the root folder. - # This is to force using the project folder - SimpleCov.root(File.join(File.expand_path(File.dirname(__FILE__)), '..')) - - SimpleCov.start do - filters.clear # This will remove the :root_filter and :bundler_filter that come via simplecov's defaults - - # Because simplecov filters everything outside of the SimpleCov.root - # This should be added, cf. - # https://github.com/colszowka/simplecov#default-root-filter-and-coverage-for-things-outside-of-it - add_filter do |src| - !(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /project/ - end - - # Ignoring test folders and tmp for Aruba - add_filter '/spec/' - add_filter '/test/' - add_filter '/features/' - add_filter '/tmp/' - add_filter '/vendor/' - end end lib = File.expand_path('../../lib', __FILE__)