From 8c007d3cd63c5e3cda5ce8e89c9101763d665f1f Mon Sep 17 00:00:00 2001 From: Max Jacobson Date: Mon, 7 Nov 2016 19:47:42 -0500 Subject: [PATCH 1/2] Escape SimpleCov.root to allow folders with parens Without escaping the root, the absolute paths remain absolute and then our validation rejects the test file reports and the user is told their report is invalid. This can occur when run in a local environment or on a service such as Jenkins. --- .../test_reporter/shorten_filename.rb | 2 +- spec/lib/shorten_filename_spec.rb | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/code_climate/test_reporter/shorten_filename.rb b/lib/code_climate/test_reporter/shorten_filename.rb index 782b073..93e67db 100644 --- a/lib/code_climate/test_reporter/shorten_filename.rb +++ b/lib/code_climate/test_reporter/shorten_filename.rb @@ -7,7 +7,7 @@ def initialize(filename) def short_filename return @filename unless ::SimpleCov.root - apply_prefix @filename.gsub(/^#{::SimpleCov.root}/, ".").gsub(%r{^\./}, "") + apply_prefix @filename.gsub(/^#{Regexp.escape(::SimpleCov.root)}/, ".").gsub(%r{^\./}, "") end private diff --git a/spec/lib/shorten_filename_spec.rb b/spec/lib/shorten_filename_spec.rb index 70affd7..db2be81 100644 --- a/spec/lib/shorten_filename_spec.rb +++ b/spec/lib/shorten_filename_spec.rb @@ -6,11 +6,25 @@ module CodeClimate::TestReporter let(:shorten_filename){ ShortenFilename.new('file1') } let(:shorten_filename_with_simplecov_root) { ShortenFilename.new("#{::SimpleCov.root}/file1") } let(:shorten_filename_with_double_simplecov_root) { ShortenFilename.new("#{::SimpleCov.root}/#{::SimpleCov.root}/file1") } + let(:root) { "/Users/oink/my-great-project" } + + before do + allow(::SimpleCov).to receive(:root).and_return(root) + end describe '#short_filename' do it 'should return the filename of the file relative to the SimpleCov root' do - expect(shorten_filename.send(:short_filename)).to eq('file1') - expect(shorten_filename_with_simplecov_root.send(:short_filename)).to eq('file1') + expect(shorten_filename.short_filename).to eq('file1') + expect(shorten_filename_with_simplecov_root.short_filename).to eq('file1') + end + + context "when the root has parentheses in it" do + let(:root) { "/Users/oink/my-great-project/hello world (ok)" } + + it 'should return the filename of the file relative to the SimpleCov root' do + expect(shorten_filename.short_filename).to eq('file1') + expect(shorten_filename_with_simplecov_root.short_filename).to eq('file1') + end end context "with path prefix" do @@ -27,13 +41,13 @@ module CodeClimate::TestReporter end it 'should include the path prefix if set' do - expect(shorten_filename.send(:short_filename)).to eq('custom/file1') - expect(shorten_filename_with_simplecov_root.send(:short_filename)).to eq('custom/file1') + expect(shorten_filename.short_filename).to eq('custom/file1') + expect(shorten_filename_with_simplecov_root.short_filename).to eq('custom/file1') end end it "should not strip the subdirectory if it has the same name as the root" do - expect(shorten_filename_with_double_simplecov_root.send(:short_filename)).to eq("#{::SimpleCov.root}/file1") + expect(shorten_filename_with_double_simplecov_root.short_filename).to eq("#{::SimpleCov.root}/file1") end end end From 32a0f48bac269b730c8339148ba4b544eccf61c3 Mon Sep 17 00:00:00 2001 From: Max Jacobson Date: Mon, 7 Nov 2016 19:56:17 -0500 Subject: [PATCH 2/2] Refactor to use pathname --- .../test_reporter/shorten_filename.rb | 26 +++++++++++++------ spec/lib/shorten_filename_spec.rb | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/code_climate/test_reporter/shorten_filename.rb b/lib/code_climate/test_reporter/shorten_filename.rb index 93e67db..c4c93b6 100644 --- a/lib/code_climate/test_reporter/shorten_filename.rb +++ b/lib/code_climate/test_reporter/shorten_filename.rb @@ -1,24 +1,34 @@ +require "pathname" + module CodeClimate module TestReporter class ShortenFilename def initialize(filename) @filename = filename + root = ::SimpleCov.root + @root = Pathname.new(root) if root end def short_filename - return @filename unless ::SimpleCov.root - apply_prefix @filename.gsub(/^#{Regexp.escape(::SimpleCov.root)}/, ".").gsub(%r{^\./}, "") - end - - private + return @filename unless root + path = Pathname.new(@filename) + shorter = + if path.relative? + path + else + path.relative_path_from(root) + end - def apply_prefix(filename) if (prefix = CodeClimate::TestReporter.configuration.path_prefix) - File.join(prefix, filename) + Pathname.new(prefix).join(shorter).to_s else - filename + shorter.to_s end end + + private + + attr_reader :root end end end diff --git a/spec/lib/shorten_filename_spec.rb b/spec/lib/shorten_filename_spec.rb index db2be81..773fb65 100644 --- a/spec/lib/shorten_filename_spec.rb +++ b/spec/lib/shorten_filename_spec.rb @@ -47,7 +47,7 @@ module CodeClimate::TestReporter end it "should not strip the subdirectory if it has the same name as the root" do - expect(shorten_filename_with_double_simplecov_root.short_filename).to eq("#{::SimpleCov.root}/file1") + expect(shorten_filename_with_double_simplecov_root.short_filename).to eq("#{::SimpleCov.root[1..-1]}/file1") end end end