Add test to make sure the code is within a working directory

This commit is contained in:
Dan Kubb 2015-07-28 12:34:01 -07:00
parent 5dba6da7bb
commit a1e8dfc869
4 changed files with 43 additions and 16 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 18
total_score: 1208
total_score: 1213

View file

@ -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

View file

@ -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

View file

@ -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