diff --git a/lib/code_climate/test_reporter/shorten_filename.rb b/lib/code_climate/test_reporter/shorten_filename.rb index 782b073..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(/^#{::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 70affd7..773fb65 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[1..-1]}/file1") end end end