From a7de9fd248dbebe228b5a94a1a614719b87c2285 Mon Sep 17 00:00:00 2001 From: Adam Phillips Date: Thu, 14 Nov 2013 18:48:32 +0000 Subject: [PATCH] Added path_prefix to configuration Added path_prefix configuration option. This allows prefixing of all file paths and is used when the Rails application is not in the root folder of the repository. --- .../test_reporter/configuration.rb | 2 +- lib/code_climate/test_reporter/formatter.rb | 9 ++++++++- spec/lib/configuration_spec.rb | 14 ++++++++++++++ spec/lib/formatter_spec.rb | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/code_climate/test_reporter/configuration.rb b/lib/code_climate/test_reporter/configuration.rb index 1af61e0..0f05ec8 100644 --- a/lib/code_climate/test_reporter/configuration.rb +++ b/lib/code_climate/test_reporter/configuration.rb @@ -19,7 +19,7 @@ def self.configuration end class Configuration - attr_accessor :branch, :logger, :profile + attr_accessor :branch, :logger, :profile, :path_prefix def logger @logger ||= default_logger diff --git a/lib/code_climate/test_reporter/formatter.rb b/lib/code_climate/test_reporter/formatter.rb index 0b69284..ee0a0a5 100644 --- a/lib/code_climate/test_reporter/formatter.rb +++ b/lib/code_climate/test_reporter/formatter.rb @@ -84,7 +84,8 @@ def calculate_blob_id(path) def short_filename(filename) return filename unless ::SimpleCov.root - filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '') + filename = filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '') + apply_prefix filename end def tddium? @@ -99,6 +100,12 @@ def round(numeric, precision) private + def apply_prefix filename + prefix = CodeClimate::TestReporter.configuration.path_prefix + return filename if prefix.nil? + "#{prefix}/#{filename}" + end + def ci_service_data @ci_service_data ||= Ci.service_data end diff --git a/spec/lib/configuration_spec.rb b/spec/lib/configuration_spec.rb index 5f4a379..081cbbc 100644 --- a/spec/lib/configuration_spec.rb +++ b/spec/lib/configuration_spec.rb @@ -13,6 +13,7 @@ module CodeClimate::TestReporter expect(CodeClimate::TestReporter.configuration.logger).to be_instance_of Logger expect(CodeClimate::TestReporter.configuration.logger.level).to eq Logger::INFO expect(CodeClimate::TestReporter.configuration.profile).to eq('test_frameworks') + expect(CodeClimate::TestReporter.configuration.path_prefix).to be_nil end end @@ -47,6 +48,19 @@ module CodeClimate::TestReporter expect(CodeClimate::TestReporter.configuration.profile).to eq('custom') end + + it 'stores path prefix' do + CodeClimate::TestReporter.configure do |config| + config.path_prefix = 'custom' + end + + expect(CodeClimate::TestReporter.configuration.path_prefix).to eq('custom') + + CodeClimate::TestReporter.configure do |config| + config.path_prefix = nil + end + + end end end end diff --git a/spec/lib/formatter_spec.rb b/spec/lib/formatter_spec.rb index 708634f..e133565 100644 --- a/spec/lib/formatter_spec.rb +++ b/spec/lib/formatter_spec.rb @@ -98,4 +98,22 @@ module CodeClimate::TestReporter app.http_user_agent.should include("v#{CodeClimate::TestReporter::VERSION}") end end + + describe '#short_filename' do + it 'should return the filename of the file relative to the SimpleCov root' do + CodeClimate::TestReporter::Formatter.new.short_filename('file1').should == 'file1' + CodeClimate::TestReporter::Formatter.new.short_filename("#{::SimpleCov.root}/file1").should == 'file1' + end + + it 'should include the path prefix if set' do + CodeClimate::TestReporter.configure do |config| + config.path_prefix = 'custom' + end + CodeClimate::TestReporter::Formatter.new.short_filename('file1').should == 'custom/file1' + CodeClimate::TestReporter::Formatter.new.short_filename("#{::SimpleCov.root}/file1").should == 'custom/file1' + CodeClimate::TestReporter.configure do |config| + config.path_prefix = nil + end + end + end end