From d555fecd440164c2f45f7c31b787c051290140b1 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Tue, 16 Jul 2019 14:27:50 +0200 Subject: [PATCH] Backport the changes of Coveralls-Community fork Coveralls-Community must be released as a new gem. Meanwhile this commit allow to release a new version of coveralls_reborn with SimpleCov 0.17.0 support Ref: https://github.com/Coveralls-Community/coveralls-ruby --- .rubocop.yml | 23 +- .travis.yml | 14 +- CHANGELOG.md | 18 - Gemfile | 17 +- coveralls-ruby.gemspec | 10 +- lib/coveralls.rb | 7 +- lib/coveralls/api.rb | 64 +-- lib/coveralls/command.rb | 13 +- lib/coveralls/configuration.rb | 415 ++++++++++-------- lib/coveralls/output.rb | 12 +- lib/coveralls/rake/task.rb | 1 + lib/coveralls/simplecov.rb | 26 +- lib/coveralls/version.rb | 2 +- spec/coveralls/configuration_spec.rb | 195 ++++---- spec/coveralls/coveralls_spec.rb | 1 - spec/coveralls/fixtures/app/models/dog.rb | 4 +- spec/coveralls/fixtures/app/models/house.rb | 4 +- spec/coveralls/fixtures/app/models/robot.rb | 4 +- spec/coveralls/fixtures/app/models/user.rb | 4 +- .../fixtures/app/vendor/vendored_gem.rb | 2 +- spec/coveralls/output_spec.rb | 10 +- spec/coveralls/simple_cov/formatter_spec.rb | 22 + spec/spec_helper.rb | 28 +- 23 files changed, 469 insertions(+), 427 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0f2e06e4..1c1df3b1 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,22 +1,23 @@ require: rubocop-rspec AllCops: - TargetRubyVersion: 2.2 + TargetRubyVersion: 2.3 Exclude: - 'spec/coveralls/fixtures/**/*' - 'vendor/bundle/**/*' -Lint/HandleExceptions: - Enabled: false +Layout/AlignHash: + EnforcedColonStyle: table + EnforcedHashRocketStyle: table Metrics/AbcSize: - Max: 37 + Max: 39.29 Metrics/CyclomaticComplexity: - Max: 18 + Max: 19 Metrics/PerceivedComplexity: - Max: 19 + Max: 20 Metrics/BlockLength: Exclude: @@ -33,10 +34,6 @@ Metrics/ModuleLength: - 'lib/coveralls/configuration.rb' - 'spec/**/*' -Naming/AccessorMethodName: - Exclude: - - 'lib/coveralls/configuration.rb' - RSpec/ExampleLength: Max: 8 @@ -52,7 +49,5 @@ RSpec/NestedGroups: Style/Documentation: Enabled: false -# TODO: Remove when targeting Ruby 2.3 because it is already enabled. -Style/FrozenStringLiteralComment: - Enabled: true - EnforcedStyle: always +Style/IfUnlessModifier: + Enabled: false diff --git a/.travis.yml b/.travis.yml index 44b28b3e..835c8ce0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,6 @@ language: ruby cache: bundler -sudo: false - before_install: - gem update --system - gem install bundler @@ -16,15 +14,15 @@ env: - JRUBY_OPTS="--dev --debug" rvm: - - 2.2.10 - - 2.3.7 - - 2.4.4 - - 2.5.1 + - 2.3.8 + - 2.4.6 + - 2.5.5 + - 2.6.3 - ruby-head - - jruby-9.2.0.0 + - jruby-9.2.7.0 matrix: allow_failures: - rvm: ruby-head - - rvm: jruby-9.2.0.0 + - rvm: jruby-9.2.7.0 fast_finish: true diff --git a/CHANGELOG.md b/CHANGELOG.md index 85d9a6c1..7fb30cff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,21 +1,3 @@ # Changelog ### Please see Github Releases section for current releases. - -## 0.7.0 (September 18, 2013) - -[Full Changelog](https://github.com/lemurheavy/coveralls-ruby/compare/v0.6.4...v0.7.0) - -Added: -* output silencing (Thanks @elizabrock) -* ruby warning fixes (Thanks @steveklabnik and @Nucc) - -## 0.6.4 (April 2, 2013) - -[Full Changelog](https://github.com/lemurheavy/coveralls-ruby/compare/v0.6.3...v0.6.4) - -Enhancements: - -* Support [Jenkins CI](http://jenkins-ci.org/) -* Support VCR versions <= 2 -* Add SimpleCov filter 'vendor' by default. diff --git a/Gemfile b/Gemfile index 40e2bdd5..3e87eb59 100644 --- a/Gemfile +++ b/Gemfile @@ -9,21 +9,16 @@ platforms :jruby do gem 'jruby-openssl', '~> 0.10.1' end -platforms :rbx do - gem 'rubinius-developer_tools', '~> 2.0' - gem 'rubysl', '~> 2.2' -end - group :development do gem 'rake', '~> 12.3' - gem 'rspec', '~> 3.7' - gem 'rubocop', '~> 0.60.0' - gem 'rubocop-rspec', '~> 1.30' + gem 'rspec', '~> 3.8' + gem 'rubocop', '~> 0.73.0' + gem 'rubocop-rspec', '~> 1.33' gem 'truthy', '~> 1.0' - gem 'vcr', '~> 4.0' - gem 'webmock', '~> 3.4' + gem 'vcr', '~> 5.0' + gem 'webmock', '~> 3.5' end group :test do - gem 'pry', '~> 0.11.3' + gem 'pry', '~> 0.12.2' end diff --git a/coveralls-ruby.gemspec b/coveralls-ruby.gemspec index 37ccdf7c..4c436aa9 100644 --- a/coveralls-ruby.gemspec +++ b/coveralls-ruby.gemspec @@ -18,14 +18,16 @@ Gem::Specification.new do |gem| gem.name = 'coveralls_reborn' gem.require_paths = ['lib'] gem.version = Coveralls::VERSION - - gem.required_ruby_version = '>= 2.2' + gem.metadata = { + 'source_code_uri' => 'https://github.com/tagliala/coveralls-ruby-reborn' + } + gem.required_ruby_version = '>= 2.3' gem.add_dependency 'json', '~> 2.1' - gem.add_dependency 'simplecov', '~> 0.16.1' + gem.add_dependency 'simplecov', '~> 0.17.0' gem.add_dependency 'term-ansicolor', '~> 1.6' gem.add_dependency 'thor', '~> 0.20.0' gem.add_dependency 'tins', '~> 1.16' - gem.add_development_dependency 'bundler', '~> 1.16' + gem.add_development_dependency 'bundler', '>= 1.16', '< 3' end diff --git a/lib/coveralls.rb b/lib/coveralls.rb index 1a0d86d5..94e13707 100644 --- a/lib/coveralls.rb +++ b/lib/coveralls.rb @@ -44,7 +44,9 @@ def setup! begin require 'simplecov' @adapter = :simplecov if defined?(::SimpleCov) - rescue StandardError + rescue StandardError => e + # TODO: Add error action + puts e.message end end @@ -58,7 +60,7 @@ def setup! end def start!(simplecov_setting = 'test_frameworks', &block) - return if @adapter != :simplecov + return unless @adapter == :simplecov ::SimpleCov.add_filter 'vendor' @@ -82,6 +84,7 @@ def should_run? # Fail early if we're not on a CI unless will_run? Coveralls::Output.puts('[Coveralls] Outside the CI environment, not sending data.', color: 'yellow') + return false end diff --git a/lib/coveralls/api.rb b/lib/coveralls/api.rb index a6845e68..c5cf0803 100644 --- a/lib/coveralls/api.rb +++ b/lib/coveralls/api.rb @@ -12,43 +12,41 @@ class API else API_HOST = ENV['COVERALLS_DEVELOPMENT'] ? 'localhost:3000' : 'coveralls.io' API_PROTOCOL = ENV['COVERALLS_DEVELOPMENT'] ? 'http' : 'https' - API_DOMAIN = "#{API_PROTOCOL}://#{API_HOST}".freeze + API_DOMAIN = "#{API_PROTOCOL}://#{API_HOST}" end - API_BASE = "#{API_DOMAIN}/api/v1".freeze + API_BASE = "#{API_DOMAIN}/api/v1" - def self.post_json(endpoint, hash) - disable_net_blockers! - - uri = endpoint_to_uri(endpoint) - - Coveralls::Output.puts(JSON.pretty_generate(hash).to_s, color: 'green') if ENV['COVERALLS_DEBUG'] - Coveralls::Output.puts("[Coveralls] Submitting to #{API_BASE}", color: 'cyan') + class << self + def post_json(endpoint, hash) + disable_net_blockers! - client = build_client(uri) - request = build_request(uri.path, hash) + uri = endpoint_to_uri(endpoint) - response = client.request(request) + Coveralls::Output.puts(JSON.pretty_generate(hash).to_s, color: 'green') if ENV['COVERALLS_DEBUG'] + Coveralls::Output.puts("[Coveralls] Submitting to #{API_BASE}", color: 'cyan') - response_hash = JSON.parse(response.body.to_str) + client = build_client(uri) + request = build_request(uri.path, hash) + response = client.request(request) + response_hash = JSON.parse(response.body.to_str) - if response_hash['message'] - Coveralls::Output.puts("[Coveralls] #{response_hash['message']}", color: 'cyan') - end + if response_hash['message'] + Coveralls::Output.puts("[Coveralls] #{response_hash['message']}", color: 'cyan') + end - if response_hash['url'] - Coveralls::Output.puts("[Coveralls] #{Coveralls::Output.format(response_hash['url'], color: 'underline')}", color: 'cyan') - end + if response_hash['url'] + Coveralls::Output.puts("[Coveralls] #{Coveralls::Output.format(response_hash['url'], color: 'underline')}", color: 'cyan') + end - case response - when Net::HTTPServiceUnavailable - Coveralls::Output.puts('[Coveralls] API timeout occured, but data should still be processed', color: 'red') - when Net::HTTPInternalServerError - Coveralls::Output.puts("[Coveralls] API internal error occured, we're on it!", color: 'red') + case response + when Net::HTTPServiceUnavailable + Coveralls::Output.puts('[Coveralls] API timeout occured, but data should still be processed', color: 'red') + when Net::HTTPInternalServerError + Coveralls::Output.puts("[Coveralls] API internal error occured, we're on it!", color: 'red') + end end - end - class << self private def disable_net_blockers! @@ -57,7 +55,9 @@ def disable_net_blockers! allow = WebMock::Config.instance.allow || [] WebMock::Config.instance.allow = [*allow].push API_HOST - rescue LoadError + rescue StandardError => e + # TODO: Add error action + puts e.message end begin @@ -66,7 +66,9 @@ def disable_net_blockers! VCR.send(VCR.version.major < 2 ? :config : :configure) do |c| c.ignore_hosts API_HOST end - rescue LoadError + rescue StandardError + # TODO: Add error action + puts error.message end end @@ -111,20 +113,24 @@ def build_request_body(hash, boundary) def hash_to_file(hash) file = nil - Tempfile.open(['coveralls-upload', 'json']) do |f| + + Tempfile.open(%w[coveralls-upload json]) do |f| f.write(JSON.dump(hash)) file = f end + File.new(file.path, 'rb') end def apified_hash(hash) config = Coveralls::Configuration.configuration + if ENV['COVERALLS_DEBUG'] || Coveralls.testing Coveralls::Output.puts '[Coveralls] Submitting with config:', color: 'yellow' output = JSON.pretty_generate(config).gsub(/"repo_token": ?"(.*?)"/, '"repo_token": "[secure]"') Coveralls::Output.puts output, color: 'yellow' end + hash.merge(config) end end diff --git a/lib/coveralls/command.rb b/lib/coveralls/command.rb index 9500b210..926b3bd6 100644 --- a/lib/coveralls/command.rb +++ b/lib/coveralls/command.rb @@ -7,8 +7,10 @@ class CommandLine < Thor desc 'push', 'Runs your test suite and pushes the coverage results to Coveralls.' def push return unless ensure_can_run_locally! + ENV['COVERALLS_RUN_LOCALLY'] = 'true' cmds = ['bundle exec rake'] + if File.exist?('.travis.yml') cmds = begin YAML.load_file('.travis.yml')['script'] || cmds @@ -16,14 +18,18 @@ def push cmds end end + cmds.each { |cmd| system cmd } + ENV['COVERALLS_RUN_LOCALLY'] = nil end desc 'report', 'Runs your test suite locally and displays coverage statistics.' def report ENV['COVERALLS_NOISY'] = 'true' + exec 'bundle exec rake' + ENV['COVERALLS_NOISY'] = nil end @@ -49,8 +55,11 @@ def version private + def config + Coveralls::Configuration.configuration + end + def open_token_based_url(url) - config = Coveralls::Configuration.configuration if config[:repo_token] url = url.gsub('%@', config[:repo_token]) `open #{url}` @@ -60,13 +69,13 @@ def open_token_based_url(url) end def ensure_can_run_locally! - config = Coveralls::Configuration.configuration if config[:repo_token].nil? Coveralls::Output.puts 'Coveralls cannot run locally because no repo_secret_token is set in .coveralls.yml', color: 'red' Coveralls::Output.puts 'Please try again when you get your act together.', color: 'red' return false end + true end end diff --git a/lib/coveralls/configuration.rb b/lib/coveralls/configuration.rb index d7e50e4c..83ce1a00 100644 --- a/lib/coveralls/configuration.rb +++ b/lib/coveralls/configuration.rb @@ -5,220 +5,257 @@ module Coveralls module Configuration - def self.configuration - config = { - environment: relevant_env, - git: git - } - yml = yaml_config - if yml - config[:configuration] = yml - config[:repo_token] = yml['repo_token'] || yml['repo_secret_token'] - end - if ENV['COVERALLS_REPO_TOKEN'] - config[:repo_token] = ENV['COVERALLS_REPO_TOKEN'] - end - if ENV['COVERALLS_PARALLEL'] && ENV['COVERALLS_PARALLEL'] != 'false' - config[:parallel] = true - end - if ENV['TRAVIS'] - set_service_params_for_travis(config, yml ? yml['service_name'] : nil) - elsif ENV['CIRCLECI'] - set_service_params_for_circleci(config) - elsif ENV['SEMAPHORE'] - set_service_params_for_semaphore(config) - elsif ENV['JENKINS_URL'] || ENV['JENKINS_HOME'] - set_service_params_for_jenkins(config) - elsif ENV['APPVEYOR'] - set_service_params_for_appveyor(config) - elsif ENV['TDDIUM'] - set_service_params_for_tddium(config) - elsif ENV['GITLAB_CI'] - set_service_params_for_gitlab(config) - elsif ENV['COVERALLS_RUN_LOCALLY'] || Coveralls.testing - set_service_params_for_coveralls_local(config) - end - - # standardized env vars - set_standard_service_params_for_generic_ci(config) - - if ENV['COVERALLS_SERVICE_NAME'] - config[:service_name] = ENV['COVERALLS_SERVICE_NAME'] - end - - config - end + class << self + def configuration + config = { + environment: relevant_env, + git: git + } - def self.set_service_params_for_travis(config, service_name) - config[:service_job_id] = ENV['TRAVIS_JOB_ID'] - config[:service_pull_request] = ENV['TRAVIS_PULL_REQUEST'] unless ENV['TRAVIS_PULL_REQUEST'] == 'false' - config[:service_name] = service_name || 'travis-ci' - config[:service_branch] = ENV['TRAVIS_BRANCH'] - end + yml = yaml_config - def self.set_service_params_for_circleci(config) - config[:service_name] = 'circleci' - config[:service_number] = ENV['CIRCLE_BUILD_NUM'] - config[:service_pull_request] = (ENV['CI_PULL_REQUEST'] || '')[/(\d+)$/, 1] - config[:parallel] = ENV['CIRCLE_NODE_TOTAL'].to_i > 1 - config[:service_job_number] = ENV['CIRCLE_NODE_INDEX'] - end + if yml + config[:configuration] = yml + config[:repo_token] = yml['repo_token'] || yml['repo_secret_token'] + end - def self.set_service_params_for_semaphore(config) - config[:service_name] = 'semaphore' - config[:service_number] = ENV['SEMAPHORE_BUILD_NUMBER'] - config[:service_pull_request] = ENV['PULL_REQUEST_NUMBER'] - end + if ENV['COVERALLS_REPO_TOKEN'] + config[:repo_token] = ENV['COVERALLS_REPO_TOKEN'] + end - def self.set_service_params_for_jenkins(config) - config[:service_name] = 'jenkins' - config[:service_number] = ENV['BUILD_NUMBER'] - config[:service_branch] = ENV['BRANCH_NAME'] - config[:service_pull_request] = ENV['ghprbPullId'] - end + if ENV['COVERALLS_PARALLEL'] && ENV['COVERALLS_PARALLEL'] != 'false' + config[:parallel] = true + end - def self.set_service_params_for_appveyor(config) - config[:service_name] = 'appveyor' - config[:service_number] = ENV['APPVEYOR_BUILD_VERSION'] - config[:service_branch] = ENV['APPVEYOR_REPO_BRANCH'] - config[:commit_sha] = ENV['APPVEYOR_REPO_COMMIT'] - repo_name = ENV['APPVEYOR_REPO_NAME'] - config[:service_build_url] = format('https://ci.appveyor.com/project/%s/build/%s', repo_name: repo_name, service_number: config[:service_number]) - end + if ENV['COVERALLS_FLAG_NAME'] + config[:flag_name] = ENV['COVERALLS_FLAG_NAME'] + end - def self.set_service_params_for_tddium(config) - config[:service_name] = 'tddium' - config[:service_number] = ENV['TDDIUM_SESSION_ID'] - config[:service_job_number] = ENV['TDDIUM_TID'] - config[:service_pull_request] = ENV['TDDIUM_PR_ID'] - config[:service_branch] = ENV['TDDIUM_CURRENT_BRANCH'] - config[:service_build_url] = "https://ci.solanolabs.com/reports/#{ENV['TDDIUM_SESSION_ID']}" - end + if ENV['TRAVIS'] + define_service_params_for_travis(config, yml ? yml['service_name'] : nil) + elsif ENV['CIRCLECI'] + define_service_params_for_circleci(config) + elsif ENV['SEMAPHORE'] + define_service_params_for_semaphore(config) + elsif ENV['JENKINS_URL'] || ENV['JENKINS_HOME'] + define_service_params_for_jenkins(config) + elsif ENV['APPVEYOR'] + define_service_params_for_appveyor(config) + elsif ENV['TDDIUM'] + define_service_params_for_tddium(config) + elsif ENV['GITLAB_CI'] + define_service_params_for_gitlab(config) + elsif ENV['COVERALLS_RUN_LOCALLY'] || Coveralls.testing + define_service_params_for_coveralls_local(config) + end - def self.set_service_params_for_gitlab(config) - config[:service_name] = 'gitlab-ci' - config[:service_job_number] = ENV['CI_BUILD_NAME'] - config[:service_job_id] = ENV['CI_BUILD_ID'] - config[:service_branch] = ENV['CI_BUILD_REF_NAME'] - config[:commit_sha] = ENV['CI_BUILD_REF'] - end + # standardized env vars + define_standard_service_params_for_generic_ci(config) - def self.set_service_params_for_coveralls_local(config) - config[:service_job_id] = nil - config[:service_name] = 'coveralls-ruby' - config[:service_event_type] = 'manual' - end + if ENV['COVERALLS_SERVICE_NAME'] + config[:service_name] = ENV['COVERALLS_SERVICE_NAME'] + end - def self.set_standard_service_params_for_generic_ci(config) - config[:service_name] ||= ENV['CI_NAME'] - config[:service_number] ||= ENV['CI_BUILD_NUMBER'] - config[:service_job_id] ||= ENV['CI_JOB_ID'] - config[:service_build_url] ||= ENV['CI_BUILD_URL'] - config[:service_branch] ||= ENV['CI_BRANCH'] - config[:service_pull_request] ||= (ENV['CI_PULL_REQUEST'] || '')[/(\d+)$/, 1] - end + config + end - def self.yaml_config - return unless configuration_path && File.exist?(configuration_path) - YAML.load_file(configuration_path) - end + def define_service_params_for_travis(config, service_name) + config[:service_job_id] = ENV['TRAVIS_JOB_ID'] + config[:service_pull_request] = ENV['TRAVIS_PULL_REQUEST'] unless ENV['TRAVIS_PULL_REQUEST'] == 'false' + config[:service_name] = service_name || 'travis-ci' + config[:service_branch] = ENV['TRAVIS_BRANCH'] + end - def self.configuration_path - File.expand_path(File.join(root, '.coveralls.yml')) if root - end + def define_service_params_for_circleci(config) + config[:service_name] = 'circleci' + config[:service_number] = ENV['CIRCLE_BUILD_NUM'] + config[:service_pull_request] = (ENV['CI_PULL_REQUEST'] || '')[/(\d+)$/, 1] + config[:parallel] = ENV['CIRCLE_NODE_TOTAL'].to_i > 1 + config[:service_job_number] = ENV['CIRCLE_NODE_INDEX'] + end - def self.root - pwd - end + def define_service_params_for_semaphore(config) + config[:service_name] = 'semaphore' + config[:service_number] = ENV['SEMAPHORE_BUILD_NUMBER'] + config[:service_pull_request] = ENV['PULL_REQUEST_NUMBER'] + end - def self.pwd - Dir.pwd - end + def define_service_params_for_jenkins(config) + config[:service_name] = 'jenkins' + config[:service_number] = ENV['BUILD_NUMBER'] + config[:service_branch] = ENV['BRANCH_NAME'] + config[:service_pull_request] = ENV['ghprbPullId'] + end - def self.simplecov_root - ::SimpleCov.root if defined?(::SimpleCov) - end + def define_service_params_for_appveyor(config) + config[:service_name] = 'appveyor' + config[:service_number] = ENV['APPVEYOR_BUILD_VERSION'] + config[:service_branch] = ENV['APPVEYOR_REPO_BRANCH'] + config[:commit_sha] = ENV['APPVEYOR_REPO_COMMIT'] + repo_name = ENV['APPVEYOR_REPO_NAME'] + config[:service_build_url] = format('https://ci.appveyor.com/project/%s/build/%s', repo_name: repo_name, service_number: config[:service_number]) + end - def self.rails_root - Rails.root.to_s - rescue StandardError - nil - end + def define_service_params_for_tddium(config) + config[:service_name] = 'tddium' + config[:service_number] = ENV['TDDIUM_SESSION_ID'] + config[:service_job_number] = ENV['TDDIUM_TID'] + config[:service_pull_request] = ENV['TDDIUM_PR_ID'] + config[:service_branch] = ENV['TDDIUM_CURRENT_BRANCH'] + config[:service_build_url] = "https://ci.solanolabs.com/reports/#{ENV['TDDIUM_SESSION_ID']}" + end - def self.git - hash = {} - - Dir.chdir(root) do - hash[:head] = { - id: ENV.fetch('GIT_ID', `git log -1 --pretty=format:'%H'`), - author_name: ENV.fetch('GIT_AUTHOR_NAME', `git log -1 --pretty=format:'%aN'`), - author_email: ENV.fetch('GIT_AUTHOR_EMAIL', `git log -1 --pretty=format:'%ae'`), - committer_name: ENV.fetch('GIT_COMMITTER_NAME', `git log -1 --pretty=format:'%cN'`), - committer_email: ENV.fetch('GIT_COMMITTER_EMAIL', `git log -1 --pretty=format:'%ce'`), - message: ENV.fetch('GIT_MESSAGE', `git log -1 --pretty=format:'%s'`) - } + def define_service_params_for_gitlab(config) + config[:service_name] = 'gitlab-ci' + config[:service_job_number] = ENV['CI_BUILD_NAME'] + config[:service_job_id] = ENV['CI_BUILD_ID'] + config[:service_branch] = ENV['CI_BUILD_REF_NAME'] + config[:commit_sha] = ENV['CI_BUILD_REF'] + end - # Branch - hash[:branch] = ENV.fetch('GIT_BRANCH', `git rev-parse --abbrev-ref HEAD`) - - # Remotes - remotes = nil - begin - remotes = `git remote -v`.split(/\n/).map do |remote| - splits = remote.split(' ').compact - { name: splits[0], url: splits[1] } - end.uniq - rescue StandardError - end - hash[:remotes] = remotes + def define_service_params_for_coveralls_local(config) + config[:service_job_id] = nil + config[:service_name] = 'coveralls-ruby' + config[:service_event_type] = 'manual' end - hash - rescue StandardError => e - Coveralls::Output.puts 'Coveralls git error:', color: 'red' - Coveralls::Output.puts e.to_s, color: 'red' - nil - end + def define_standard_service_params_for_generic_ci(config) + config[:service_name] ||= ENV['CI_NAME'] + config[:service_number] ||= ENV['CI_BUILD_NUMBER'] + config[:service_job_id] ||= ENV['CI_JOB_ID'] + config[:service_build_url] ||= ENV['CI_BUILD_URL'] + config[:service_branch] ||= ENV['CI_BRANCH'] + config[:service_pull_request] ||= (ENV['CI_PULL_REQUEST'] || '')[/(\d+)$/, 1] + end - def self.relevant_env - hash = { - pwd: pwd, - rails_root: rails_root, - simplecov_root: simplecov_root, - gem_version: VERSION - } + def yaml_config + return unless configuration_path && File.exist?(configuration_path) - hash.merge! begin - if ENV['TRAVIS'] - { - travis_job_id: ENV['TRAVIS_JOB_ID'], - travis_pull_request: ENV['TRAVIS_PULL_REQUEST'], - branch: ENV['TRAVIS_BRANCH'] - } - elsif ENV['CIRCLECI'] - { - circleci_build_num: ENV['CIRCLE_BUILD_NUM'], - branch: ENV['CIRCLE_BRANCH'], - commit_sha: ENV['CIRCLE_SHA1'] - } - elsif ENV['JENKINS_URL'] - { - jenkins_build_num: ENV['BUILD_NUMBER'], - jenkins_build_url: ENV['BUILD_URL'], - branch: ENV['GIT_BRANCH'], - commit_sha: ENV['GIT_COMMIT'] - } - elsif ENV['SEMAPHORE'] - { - branch: ENV['BRANCH_NAME'], - commit_sha: ENV['REVISION'] + YAML.load_file(configuration_path) + end + + def configuration_path + return unless root + + File.expand_path(File.join(root, '.coveralls.yml')) + end + + def root + pwd + end + + def pwd + Dir.pwd + end + + def simplecov_root + return unless defined?(::SimpleCov) + + ::SimpleCov.root + end + + def rails_root + Rails.root.to_s + rescue StandardError + nil + end + + def git + hash = {} + + Dir.chdir(root) do + hash[:head] = { + id: ENV.fetch('GIT_ID', `git log -1 --pretty=format:'%H'`), + author_name: ENV.fetch('GIT_AUTHOR_NAME', `git log -1 --pretty=format:'%aN'`), + author_email: ENV.fetch('GIT_AUTHOR_EMAIL', `git log -1 --pretty=format:'%ae'`), + committer_name: ENV.fetch('GIT_COMMITTER_NAME', `git log -1 --pretty=format:'%cN'`), + committer_email: ENV.fetch('GIT_COMMITTER_EMAIL', `git log -1 --pretty=format:'%ce'`), + message: ENV.fetch('GIT_MESSAGE', `git log -1 --pretty=format:'%s'`) } - else - {} + + # Branch + hash[:branch] = ENV.fetch('GIT_BRANCH', `git rev-parse --abbrev-ref HEAD`) + + # Remotes + remotes = nil + begin + remotes = `git remote -v`.split(/\n/).map do |remote| + splits = remote.split(' ').compact + { name: splits[0], url: splits[1] } + end.uniq + rescue StandardError => e + # TODO: Add error action + puts e.message + end + + hash[:remotes] = remotes + end + + hash + rescue StandardError => e + Coveralls::Output.puts 'Coveralls git error:', color: 'red' + Coveralls::Output.puts e.to_s, color: 'red' + nil + end + + def relevant_env + hash = { + pwd: pwd, + rails_root: rails_root, + simplecov_root: simplecov_root, + gem_version: VERSION + } + + hash.merge! begin + if ENV['TRAVIS'] + travis_env_hash + elsif ENV['CIRCLECI'] + circleci_env_hash + elsif ENV['JENKINS_URL'] + jenkins_env_hash + elsif ENV['SEMAPHORE'] + semaphore_env_hash + else + {} + end end + + hash + end + + private + + def circleci_env_hash + { + circleci_build_num: ENV['CIRCLE_BUILD_NUM'], + branch: ENV['CIRCLE_BRANCH'], + commit_sha: ENV['CIRCLE_SHA1'] + } + end + + def jenkins_env_hash + { + jenkins_build_num: ENV['BUILD_NUMBER'], + jenkins_build_url: ENV['BUILD_URL'], + branch: ENV['GIT_BRANCH'], + commit_sha: ENV['GIT_COMMIT'] + } end - hash + def semaphore_env_hash + { + branch: ENV['BRANCH_NAME'], + commit_sha: ENV['REVISION'] + } + end + + def travis_env_hash + { + travis_job_id: ENV['TRAVIS_JOB_ID'], + travis_pull_request: ENV['TRAVIS_PULL_REQUEST'], + branch: ENV['TRAVIS_BRANCH'] + } + end end end end diff --git a/lib/coveralls/output.rb b/lib/coveralls/output.rb index 4fc50552..66c2f6d3 100644 --- a/lib/coveralls/output.rb +++ b/lib/coveralls/output.rb @@ -66,12 +66,10 @@ def no_color? def format(string, options = {}) unless no_color? require 'term/ansicolor' - if options[:color] - options[:color].split(/\s/).reverse_each do |color| - if Term::ANSIColor.respond_to?(color.to_sym) - string = Term::ANSIColor.send(color.to_sym, string) - end - end + options[:color]&.split(/\s/)&.reverse_each do |color| + next unless Term::ANSIColor.respond_to?(color.to_sym) + + string = Term::ANSIColor.send(color.to_sym, string) end end string @@ -92,6 +90,7 @@ def format(string, options = {}) # Returns nil. def puts(string, options = {}) return if silent? + (options[:output] || output).puts format(string, options) end @@ -109,6 +108,7 @@ def puts(string, options = {}) # Returns nil. def print(string, options = {}) return if silent? + (options[:output] || output).print format(string, options) end diff --git a/lib/coveralls/rake/task.rb b/lib/coveralls/rake/task.rb index 3fac921e..a4219ac7 100644 --- a/lib/coveralls/rake/task.rb +++ b/lib/coveralls/rake/task.rb @@ -12,6 +12,7 @@ def initialize(*_args) desc 'Push latest coverage results to Coveralls.io' task :push do require 'coveralls' + Coveralls.push! end end diff --git a/lib/coveralls/simplecov.rb b/lib/coveralls/simplecov.rb index 25770922..d0865362 100644 --- a/lib/coveralls/simplecov.rb +++ b/lib/coveralls/simplecov.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'pathname' + module Coveralls module SimpleCov class Formatter @@ -10,6 +12,7 @@ def display_result(result) else Coveralls::Output.puts '[Coveralls] There are no covered files.', color: 'yellow' end + result.files.each do |f| Coveralls::Output.print ' * ' Coveralls::Output.print short_filename(f.filename).to_s, color: 'cyan' @@ -24,6 +27,7 @@ def display_result(result) end Coveralls::Output.puts '' end + true end @@ -49,20 +53,22 @@ def get_source_files(result) source_files << properties end + source_files end def format(result) unless Coveralls.should_run? display_result result if Coveralls.noisy? + return end # Post to Coveralls. API.post_json 'jobs', - source_files: get_source_files(result), + source_files: get_source_files(result), test_framework: result.command_name.downcase, - run_at: result.created_at + run_at: result.created_at Coveralls::Output.puts output_message result @@ -75,14 +81,15 @@ def display_error(error) Coveralls::Output.puts 'Coveralls encountered an exception:', color: 'red' Coveralls::Output.puts error.class.to_s, color: 'red' Coveralls::Output.puts error.message, color: 'red' - if error.backtrace - error.backtrace.each do |line| - Coveralls::Output.puts line, color: 'red' - end + + error.backtrace&.each do |line| + Coveralls::Output.puts line, color: 'red' end + if error.respond_to?(:response) && error.response Coveralls::Output.puts error.response.to_s, color: 'red' end + false end @@ -95,8 +102,11 @@ def output_message(result) end def short_filename(filename) - filename = filename.gsub(::SimpleCov.root, '.').gsub(%r{^\./}, '') if ::SimpleCov.root - filename + return filename unless ::SimpleCov.root + + filename = Pathname.new(filename) + root = Pathname.new(::SimpleCov.root) + filename.relative_path_from(root).to_s end end end diff --git a/lib/coveralls/version.rb b/lib/coveralls/version.rb index f1141ed1..ff01592e 100644 --- a/lib/coveralls/version.rb +++ b/lib/coveralls/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Coveralls - VERSION = '0.12.0'.freeze + VERSION = '0.13.0' end diff --git a/spec/coveralls/configuration_spec.rb b/spec/coveralls/configuration_spec.rb index faed1620..1ed0217c 100644 --- a/spec/coveralls/configuration_spec.rb +++ b/spec/coveralls/configuration_spec.rb @@ -20,13 +20,14 @@ let(:repo_secret_token) { SecureRandom.hex(4) } let(:yaml_config) do { - 'repo_token' => repo_token, + 'repo_token' => repo_token, 'repo_secret_token' => repo_secret_token } end before do - allow(described_class).to receive(:yaml_config).and_return(yaml_config) + allow(File).to receive(:exist?).with(described_class.configuration_path).and_return(true) + allow(YAML).to receive(:load_file).with(described_class.configuration_path).and_return(yaml_config) end it 'sets the Yaml config and associated variables if present' do @@ -67,14 +68,54 @@ end end + context 'when flag_name is in environment' do + before do + allow(ENV).to receive(:[]).with('COVERALLS_FLAG_NAME').and_return(true) + end + + it 'sets flag_name to true if present' do + config = described_class.configuration + expect(config[:flag_name]).to be true + end + end + context 'with services' do + SERVICES = { + appveyor: 'APPVEYOR', + circleci: 'CIRCLECI', + gitlab: 'GITLAB_CI', + jenkins: 'JENKINS_URL', + semaphore: 'SEMAPHORE', + tddium: 'TDDIUM', + travis: 'TRAVIS', + coveralls_local: 'COVERALLS_RUN_LOCALLY', + generic: 'CI_NAME' + }.freeze + + shared_examples 'a service' do |service_name| + let(:service_variable) { options[:service_variable] } + + before do + allow(ENV).to receive(:[]).with(SERVICES[service_name]).and_return('1') + described_class.configuration + end + + it 'sets service parameters for this service and no other' do + SERVICES.each_key.reject { |service| service == service_name }.each do |service| + expect(described_class).not_to have_received(:"define_service_params_for_#{service}") + end + + expect(described_class).to have_received(:"define_service_params_for_#{service_name}") unless service_name == :generic + expect(described_class).to have_received(:define_standard_service_params_for_generic_ci) + end + end + before do - allow(described_class).to receive(:set_service_params_for_travis) - allow(described_class).to receive(:set_service_params_for_circleci) - allow(described_class).to receive(:set_service_params_for_semaphore) - allow(described_class).to receive(:set_service_params_for_jenkins) - allow(described_class).to receive(:set_service_params_for_coveralls_local) - allow(described_class).to receive(:set_standard_service_params_for_generic_ci) + SERVICES.each_key do |service| + allow(described_class).to receive(:"define_service_params_for_#{service}") + end + + allow(described_class).to receive(:define_standard_service_params_for_generic_ci) end context 'with env based service name' do @@ -91,105 +132,45 @@ end end - context 'when using Travis' do - before do - allow(ENV).to receive(:[]).with('TRAVIS').and_return('1') - described_class.configuration - end - - it 'sets service parameters for this service and no other' do - expect(described_class).to have_received(:set_service_params_for_travis).with(anything, anything) - expect(described_class).not_to have_received(:set_service_params_for_circleci) - expect(described_class).not_to have_received(:set_service_params_for_semaphore) - expect(described_class).not_to have_received(:set_service_params_for_jenkins) - expect(described_class).not_to have_received(:set_service_params_for_coveralls_local) - expect(described_class).to have_received(:set_standard_service_params_for_generic_ci) - end + context 'when using AppVeyor' do + it_behaves_like 'a service', :appveyor end context 'when using CircleCI' do - before do - allow(ENV).to receive(:[]).with('CIRCLECI').and_return('1') - described_class.configuration - end + it_behaves_like 'a service', :circleci + end - it 'sets service parameters for this service and no other' do - expect(described_class).not_to have_received(:set_service_params_for_travis) - expect(described_class).to have_received(:set_service_params_for_circleci) - expect(described_class).not_to have_received(:set_service_params_for_semaphore) - expect(described_class).not_to have_received(:set_service_params_for_jenkins) - expect(described_class).not_to have_received(:set_service_params_for_coveralls_local) - expect(described_class).to have_received(:set_standard_service_params_for_generic_ci) - end + context 'when using GitLab CI' do + it_behaves_like 'a service', :gitlab end - context 'when using Semaphore' do - before do - allow(ENV).to receive(:[]).with('SEMAPHORE').and_return('1') - described_class.configuration - end + context 'when using Jenkins' do + it_behaves_like 'a service', :jenkins + end - it 'sets service parameters for this service and no other' do - expect(described_class).not_to have_received(:set_service_params_for_travis) - expect(described_class).not_to have_received(:set_service_params_for_circleci) - expect(described_class).to have_received(:set_service_params_for_semaphore) - expect(described_class).not_to have_received(:set_service_params_for_jenkins) - expect(described_class).not_to have_received(:set_service_params_for_coveralls_local) - expect(described_class).to have_received(:set_standard_service_params_for_generic_ci) - end + context 'when using Semaphore' do + it_behaves_like 'a service', :semaphore end - context 'when using Jenkins' do - before do - allow(ENV).to receive(:[]).with('JENKINS_URL').and_return('1') - described_class.configuration - end + context 'when using Tddium' do + it_behaves_like 'a service', :tddium + end - it 'sets service parameters for this service and no other' do - expect(described_class).not_to have_received(:set_service_params_for_travis) - expect(described_class).not_to have_received(:set_service_params_for_circleci) - expect(described_class).not_to have_received(:set_service_params_for_semaphore) - expect(described_class).to have_received(:set_service_params_for_jenkins) - expect(described_class).not_to have_received(:set_service_params_for_coveralls_local) - expect(described_class).to have_received(:set_standard_service_params_for_generic_ci) - end + context 'when using Travis' do + it_behaves_like 'a service', :travis end context 'when running Coveralls locally' do - before do - allow(ENV).to receive(:[]).with('COVERALLS_RUN_LOCALLY').and_return('1') - described_class.configuration - end - - it 'sets service parameters for this service and no other' do - expect(described_class).not_to have_received(:set_service_params_for_travis) - expect(described_class).not_to have_received(:set_service_params_for_circleci) - expect(described_class).not_to have_received(:set_service_params_for_semaphore) - expect(described_class).not_to have_received(:set_service_params_for_jenkins) - expect(described_class).to have_received(:set_service_params_for_coveralls_local) - expect(described_class).to have_received(:set_standard_service_params_for_generic_ci) - end + it_behaves_like 'a service', :coveralls_local end context 'when using a generic CI' do - before do - allow(ENV).to receive(:[]).with('CI_NAME').and_return('1') - described_class.configuration - end - - it 'sets service parameters for this service and no other' do - expect(described_class).not_to have_received(:set_service_params_for_travis) - expect(described_class).not_to have_received(:set_service_params_for_circleci) - expect(described_class).not_to have_received(:set_service_params_for_semaphore) - expect(described_class).not_to have_received(:set_service_params_for_jenkins) - expect(described_class).not_to have_received(:set_service_params_for_coveralls_local) - expect(described_class).to have_received(:set_standard_service_params_for_generic_ci).with(anything) - end + it_behaves_like 'a service', :generic end end end - describe '.set_service_params_for_travis' do + describe '.define_service_params_for_travis' do let(:travis_job_id) { SecureRandom.hex(4) } before do @@ -198,25 +179,25 @@ it 'sets the service_job_id' do config = {} - described_class.set_service_params_for_travis(config, nil) + described_class.define_service_params_for_travis(config, nil) expect(config[:service_job_id]).to eq(travis_job_id) end it 'sets the service_name to travis-ci by default' do config = {} - described_class.set_service_params_for_travis(config, nil) + described_class.define_service_params_for_travis(config, nil) expect(config[:service_name]).to eq('travis-ci') end it 'sets the service_name to a value if one is passed in' do config = {} random_name = SecureRandom.hex(4) - described_class.set_service_params_for_travis(config, random_name) + described_class.define_service_params_for_travis(config, random_name) expect(config[:service_name]).to eq(random_name) end end - describe '.set_service_params_for_circleci' do + describe '.define_service_params_for_circleci' do let(:circle_build_num) { SecureRandom.hex(4) } before do @@ -225,13 +206,13 @@ it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_circleci(config) + described_class.define_service_params_for_circleci(config) expect(config[:service_name]).to eq('circleci') expect(config[:service_number]).to eq(circle_build_num) end end - describe '.set_service_params_for_gitlab' do + describe '.define_service_params_for_gitlab' do let(:commit_sha) { SecureRandom.hex(32) } let(:service_job_number) { 'spec:one' } let(:service_job_id) { 1234 } @@ -246,7 +227,7 @@ it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_gitlab(config) + described_class.define_service_params_for_gitlab(config) expect(config[:service_name]).to eq('gitlab-ci') expect(config[:service_job_number]).to eq(service_job_number) expect(config[:service_job_id]).to eq(service_job_id) @@ -255,7 +236,7 @@ end end - describe '.set_service_params_for_semaphore' do + describe '.define_service_params_for_semaphore' do let(:semaphore_build_num) { SecureRandom.hex(4) } before do @@ -264,13 +245,13 @@ it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_semaphore(config) + described_class.define_service_params_for_semaphore(config) expect(config[:service_name]).to eq('semaphore') expect(config[:service_number]).to eq(semaphore_build_num) end end - describe '.set_service_params_for_jenkins' do + describe '.define_service_params_for_jenkins' do let(:service_pull_request) { '1234' } let(:build_num) { SecureRandom.hex(4) } @@ -281,25 +262,25 @@ it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_jenkins(config) - described_class.set_standard_service_params_for_generic_ci(config) + described_class.define_service_params_for_jenkins(config) + described_class.define_standard_service_params_for_generic_ci(config) expect(config[:service_name]).to eq('jenkins') expect(config[:service_number]).to eq(build_num) expect(config[:service_pull_request]).to eq(service_pull_request) end end - describe '.set_service_params_for_coveralls_local' do + describe '.define_service_params_for_coveralls_local' do it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_coveralls_local(config) + described_class.define_service_params_for_coveralls_local(config) expect(config[:service_name]).to eq('coveralls-ruby') expect(config[:service_job_id]).to be_nil expect(config[:service_event_type]).to eq('manual') end end - describe '.set_service_params_for_generic_ci' do + describe '.define_service_params_for_generic_ci' do let(:service_name) { SecureRandom.hex(4) } let(:service_number) { SecureRandom.hex(4) } let(:service_build_url) { SecureRandom.hex(4) } @@ -316,7 +297,7 @@ it 'sets the expected parameters' do config = {} - described_class.set_standard_service_params_for_generic_ci(config) + described_class.define_standard_service_params_for_generic_ci(config) expect(config[:service_name]).to eq(service_name) expect(config[:service_number]).to eq(service_number) expect(config[:service_build_url]).to eq(service_build_url) @@ -325,7 +306,7 @@ end end - describe '.set_service_params_for_appveyor' do + describe '.define_service_params_for_appveyor' do let(:service_number) { SecureRandom.hex(4) } let(:service_branch) { SecureRandom.hex(4) } let(:commit_sha) { SecureRandom.hex(4) } @@ -340,7 +321,7 @@ it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_appveyor(config) + described_class.define_service_params_for_appveyor(config) expect(config[:service_name]).to eq('appveyor') expect(config[:service_number]).to eq(service_number) expect(config[:service_branch]).to eq(service_branch) @@ -349,7 +330,7 @@ end end - describe '.set_service_params_for_tddium' do + describe '.define_service_params_for_tddium' do let(:service_number) { SecureRandom.hex(4) } let(:service_job_number) { SecureRandom.hex(4) } let(:service_pull_request) { SecureRandom.hex(4) } @@ -364,7 +345,7 @@ it 'sets the expected parameters' do config = {} - described_class.set_service_params_for_tddium(config) + described_class.define_service_params_for_tddium(config) expect(config[:service_name]).to eq('tddium') expect(config[:service_number]).to eq(service_number) expect(config[:service_job_number]).to eq(service_job_number) diff --git a/spec/coveralls/coveralls_spec.rb b/spec/coveralls/coveralls_spec.rb index c426504a..87449444 100644 --- a/spec/coveralls/coveralls_spec.rb +++ b/spec/coveralls/coveralls_spec.rb @@ -75,7 +75,6 @@ end end - expect(::SimpleCov).to have_received(:start).with 'rails' expect(::SimpleCov.formatter).to be Coveralls::NilFormatter end end diff --git a/spec/coveralls/fixtures/app/models/dog.rb b/spec/coveralls/fixtures/app/models/dog.rb index 2a846aa0..28b9ae72 100755 --- a/spec/coveralls/fixtures/app/models/dog.rb +++ b/spec/coveralls/fixtures/app/models/dog.rb @@ -3,8 +3,8 @@ class Foo def initialize @foo = 'baz' end - + def bar @foo end -end \ No newline at end of file +end diff --git a/spec/coveralls/fixtures/app/models/house.rb b/spec/coveralls/fixtures/app/models/house.rb index 2a846aa0..28b9ae72 100755 --- a/spec/coveralls/fixtures/app/models/house.rb +++ b/spec/coveralls/fixtures/app/models/house.rb @@ -3,8 +3,8 @@ class Foo def initialize @foo = 'baz' end - + def bar @foo end -end \ No newline at end of file +end diff --git a/spec/coveralls/fixtures/app/models/robot.rb b/spec/coveralls/fixtures/app/models/robot.rb index 2a846aa0..28b9ae72 100755 --- a/spec/coveralls/fixtures/app/models/robot.rb +++ b/spec/coveralls/fixtures/app/models/robot.rb @@ -3,8 +3,8 @@ class Foo def initialize @foo = 'baz' end - + def bar @foo end -end \ No newline at end of file +end diff --git a/spec/coveralls/fixtures/app/models/user.rb b/spec/coveralls/fixtures/app/models/user.rb index 2a846aa0..28b9ae72 100755 --- a/spec/coveralls/fixtures/app/models/user.rb +++ b/spec/coveralls/fixtures/app/models/user.rb @@ -3,8 +3,8 @@ class Foo def initialize @foo = 'baz' end - + def bar @foo end -end \ No newline at end of file +end diff --git a/spec/coveralls/fixtures/app/vendor/vendored_gem.rb b/spec/coveralls/fixtures/app/vendor/vendored_gem.rb index 6a98939f..21af39e0 100644 --- a/spec/coveralls/fixtures/app/vendor/vendored_gem.rb +++ b/spec/coveralls/fixtures/app/vendor/vendored_gem.rb @@ -1 +1 @@ -# this file should not be covered \ No newline at end of file +# this file should not be covered diff --git a/spec/coveralls/output_spec.rb b/spec/coveralls/output_spec.rb index 7a3a8de0..369d8060 100644 --- a/spec/coveralls/output_spec.rb +++ b/spec/coveralls/output_spec.rb @@ -11,6 +11,7 @@ out = StringIO.new allow(described_class).to receive(:output).and_return(out) described_class.puts 'this is a test' + expect(out.string).to eq "this is a test\n" end @@ -18,6 +19,7 @@ it 'accepts an IO injection' do out = StringIO.new described_class.puts 'this is a test', output: out + expect(out.string).to eq "this is a test\n" end end @@ -26,6 +28,7 @@ it 'accepts an IO injection' do out = StringIO.new described_class.print 'this is a test', output: out + expect(out.string).to eq 'this is a test' end end @@ -33,7 +36,7 @@ describe 'when silenced' do before { described_class.silent = true } - it 'does not puts' do + it 'does not put' do expect { described_class.puts 'foo' }.not_to output("foo\n").to_stdout end @@ -50,7 +53,7 @@ expect(described_class.format(string, color: 'red')).to eq(ansi_color_string) end - it 'also accepts no color arguments' do + it 'accepts no color arguments' do unformatted_string = 'Hi Doggie!' expect(described_class.format(unformatted_string)).to eq(unformatted_string) end @@ -71,8 +74,7 @@ it 'does not add color to string' do unformatted_string = 'Hi Doggie!' - expect(described_class.format(unformatted_string, color: 'red')) - .to eq(unformatted_string) + expect(described_class.format(unformatted_string, color: 'red')).to eq(unformatted_string) end end end diff --git a/spec/coveralls/simple_cov/formatter_spec.rb b/spec/coveralls/simple_cov/formatter_spec.rb index 3a26b55f..20de8dbb 100644 --- a/spec/coveralls/simple_cov/formatter_spec.rb +++ b/spec/coveralls/simple_cov/formatter_spec.rb @@ -78,5 +78,27 @@ def source_fixture(filename) expect(source_file[:coverage]).to eq [nil, 1, 1, 1, nil, 0, 1, 1, nil, nil, nil, nil, nil] end end + + describe '#short_filename' do + subject { described_class.new.short_filename(filename) } + + let(:filename) { '/app/app/controllers/application_controller.rb' } + + before do + allow(SimpleCov).to receive(:root).and_return(root_path) + end + + context 'with nil root path' do + let(:root_path) { nil } + + it { is_expected.to eql filename } + end + + context 'with multiple matches of root path' do + let(:root_path) { '/app' } + + it { is_expected.to eql 'app/controllers/application_controller.rb' } + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4b681d30..f2124e92 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,11 +19,11 @@ def setup_formatter SimpleCov::Formatter::HTMLFormatter end - # SimpleCov.start 'test_frameworks' SimpleCov.start do add_filter do |source_file| source_file.filename =~ /spec/ && source_file.filename !~ /fixture/ end + add_filter %r{/.bundle/} end end @@ -40,6 +40,7 @@ def setup_formatter config.run_all_when_everything_filtered = true config.filter_run :focus config.include WebMock::API + config.after(:suite) do setup_formatter WebMock.disable! @@ -50,11 +51,11 @@ def stub_api_post body = '{"message":"","url":""}' stub_request(:post, Coveralls::API::API_BASE + '/jobs').with( headers: { - 'Accept' => '*/*; q=0.5, application/xml', + 'Accept' => '*/*; q=0.5, application/xml', 'Accept-Encoding' => 'gzip, deflate', - 'Content-Length' => /.+/, - 'Content-Type' => /.+/, - 'User-Agent' => 'Ruby' + 'Content-Length' => /.+/, + 'Content-Type' => /.+/, + 'User-Agent' => 'Ruby' } ).to_return(status: 200, body: body, headers: {}) end @@ -67,13 +68,12 @@ def silence end end -module Kernel - def silence_stream(stream) - old_stream = stream.dup - stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') - stream.sync = true - yield - ensure - stream.reopen(old_stream) - end +def silence_stream(stream) + old_stream = stream.dup + stream.reopen(IO::NULL) + stream.sync = true + yield +ensure + stream.reopen(old_stream) + old_stream.close end