diff --git a/config/flay.yml b/config/flay.yml index 3c2258be..c8a9cc0b 100644 --- a/config/flay.yml +++ b/config/flay.yml @@ -1,3 +1,3 @@ --- threshold: 18 -total_score: 1208 +total_score: 1213 diff --git a/config/reek.yml b/config/reek.yml index 76628b8a..3e437d9a 100644 --- a/config/reek.yml +++ b/config/reek.yml @@ -131,6 +131,7 @@ UtilityFunction: - Mutant::Integration::Rspec#parse_example - Mutant::Meta::Example::Verification#format_mutation - Mutant::Repository::Diff#tracks? # intentional, private + - Mutant::Repository::Diff#within_working_directory? # intentional, private - Mutant::Reporter::CLI::Format::Progressive#new_buffer - Mutant::Reporter::CLI::Printer::StatusProgressive#object # False positive calls super - Mutant::Integration::Rspec#parse_expression # intentional, private diff --git a/lib/mutant/repository.rb b/lib/mutant/repository.rb index 6b230e68..16647424 100644 --- a/lib/mutant/repository.rb +++ b/lib/mutant/repository.rb @@ -48,7 +48,7 @@ module Mutant # # @api private def touches?(path, line_range) - return false unless tracks?(path) + return false unless within_working_directory?(path) && tracks?(path) command = %W[ git log @@ -83,6 +83,18 @@ module Mutant ) end + # Test if the path is within the current working directory + # + # @param [Pathname] path + # + # @return [TrueClass, nil] + # + # @api private + def within_working_directory?(path) + working_directory = Pathname.pwd + path.ascend { |parent| return true if working_directory.eql?(parent) } + end + end # Diff end # Repository end # Mutant diff --git a/spec/unit/mutant/repository/diff_spec.rb b/spec/unit/mutant/repository/diff_spec.rb index 92b53d40..e394b694 100644 --- a/spec/unit/mutant/repository/diff_spec.rb +++ b/spec/unit/mutant/repository/diff_spec.rb @@ -8,25 +8,39 @@ describe Mutant::Repository::Diff do end describe '#touches?' do - let(:object) { described_class.new('from_rev', 'to_rev') } - let(:path) { Pathname.new('foo.rb') } - let(:line_range) { 1..2 } + let(:object) { described_class.new('from_rev', 'to_rev') } + let(:path) { Pathname.pwd.join('foo.rb') } + let(:line_range) { 1..2 } subject { object.touches?(path, line_range) } - before do - expect(Kernel).to receive(:system) - .ordered - .with( - *%w[git ls-files --error-unmatch -- foo.rb], - out: File::NULL, - err: File::NULL - ).and_return(git_ls_success?) + shared_context 'test if git tracks the file' do + before do + expect(Kernel).to receive(:system) + .ordered + .with( + *%W[git ls-files --error-unmatch -- #{path}], + out: File::NULL, + err: File::NULL + ).and_return(git_ls_success?) + end + end + + context 'when file is in a different subdirectory' do + let(:path) { Pathname.new('/foo.rb') } + + before do + expect(Kernel).to_not receive(:system) + end + + it { should be(false) } end context 'when file is NOT tracked in repository' do let(:git_ls_success?) { false } + include_context 'test if git tracks the file' + it { should be(false) } end @@ -36,6 +50,8 @@ describe Mutant::Repository::Diff do let(:stdout) { double('Stdout', empty?: stdout_empty?) } let(:stdout_empty?) { false } + include_context 'test if git tracks the file' + before do expect(Open3).to receive(:capture2) .ordered @@ -44,9 +60,7 @@ describe Mutant::Repository::Diff do end let(:expected_git_log_command) do - %w[ - git log from_rev...to_rev -L 1,2:foo.rb - ] + %W[git log from_rev...to_rev -L 1,2:#{path}] end context 'on failure of git log command' do