diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be508903..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 }} @@ -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 diff --git a/.simplecov b/.simplecov new file mode 100644 index 00000000..febc6938 --- /dev/null +++ b/.simplecov @@ -0,0 +1,46 @@ +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' + + 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'] + require 'simplecov-console' + require 'codecov' + + SimpleCov.formatters = [ + SimpleCov::Formatter::Console, + SimpleCov::Formatter::Codecov, + ] +end + +# vim: filetype=ruby 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 diff --git a/bin/msync b/bin/msync index 7afddef8..ae7d7124 100755 --- a/bin/msync +++ b/bin/msync @@ -1,5 +1,21 @@ #!/usr/bin/env ruby +if ENV['COVERAGE'] + # This block allow us to grab code coverage when running this script. + # + # 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})" +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 de44b9d4..730de3d6 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' @@ -6,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 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' 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' 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