Add test to make sure the code is within a working directory
This commit is contained in:
parent
5dba6da7bb
commit
a1e8dfc869
4 changed files with 43 additions and 16 deletions
|
@ -1,3 +1,3 @@
|
||||||
---
|
---
|
||||||
threshold: 18
|
threshold: 18
|
||||||
total_score: 1208
|
total_score: 1213
|
||||||
|
|
|
@ -131,6 +131,7 @@ UtilityFunction:
|
||||||
- Mutant::Integration::Rspec#parse_example
|
- Mutant::Integration::Rspec#parse_example
|
||||||
- Mutant::Meta::Example::Verification#format_mutation
|
- Mutant::Meta::Example::Verification#format_mutation
|
||||||
- Mutant::Repository::Diff#tracks? # intentional, private
|
- Mutant::Repository::Diff#tracks? # intentional, private
|
||||||
|
- Mutant::Repository::Diff#within_working_directory? # intentional, private
|
||||||
- Mutant::Reporter::CLI::Format::Progressive#new_buffer
|
- Mutant::Reporter::CLI::Format::Progressive#new_buffer
|
||||||
- Mutant::Reporter::CLI::Printer::StatusProgressive#object # False positive calls super
|
- Mutant::Reporter::CLI::Printer::StatusProgressive#object # False positive calls super
|
||||||
- Mutant::Integration::Rspec#parse_expression # intentional, private
|
- Mutant::Integration::Rspec#parse_expression # intentional, private
|
||||||
|
|
|
@ -48,7 +48,7 @@ module Mutant
|
||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
def touches?(path, line_range)
|
def touches?(path, line_range)
|
||||||
return false unless tracks?(path)
|
return false unless within_working_directory?(path) && tracks?(path)
|
||||||
|
|
||||||
command = %W[
|
command = %W[
|
||||||
git log
|
git log
|
||||||
|
@ -83,6 +83,18 @@ module Mutant
|
||||||
)
|
)
|
||||||
end
|
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 # Diff
|
||||||
end # Repository
|
end # Repository
|
||||||
end # Mutant
|
end # Mutant
|
||||||
|
|
|
@ -8,25 +8,39 @@ describe Mutant::Repository::Diff do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#touches?' do
|
describe '#touches?' do
|
||||||
let(:object) { described_class.new('from_rev', 'to_rev') }
|
let(:object) { described_class.new('from_rev', 'to_rev') }
|
||||||
let(:path) { Pathname.new('foo.rb') }
|
let(:path) { Pathname.pwd.join('foo.rb') }
|
||||||
let(:line_range) { 1..2 }
|
let(:line_range) { 1..2 }
|
||||||
|
|
||||||
subject { object.touches?(path, line_range) }
|
subject { object.touches?(path, line_range) }
|
||||||
|
|
||||||
before do
|
shared_context 'test if git tracks the file' do
|
||||||
expect(Kernel).to receive(:system)
|
before do
|
||||||
.ordered
|
expect(Kernel).to receive(:system)
|
||||||
.with(
|
.ordered
|
||||||
*%w[git ls-files --error-unmatch -- foo.rb],
|
.with(
|
||||||
out: File::NULL,
|
*%W[git ls-files --error-unmatch -- #{path}],
|
||||||
err: File::NULL
|
out: File::NULL,
|
||||||
).and_return(git_ls_success?)
|
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
|
end
|
||||||
|
|
||||||
context 'when file is NOT tracked in repository' do
|
context 'when file is NOT tracked in repository' do
|
||||||
let(:git_ls_success?) { false }
|
let(:git_ls_success?) { false }
|
||||||
|
|
||||||
|
include_context 'test if git tracks the file'
|
||||||
|
|
||||||
it { should be(false) }
|
it { should be(false) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -36,6 +50,8 @@ describe Mutant::Repository::Diff do
|
||||||
let(:stdout) { double('Stdout', empty?: stdout_empty?) }
|
let(:stdout) { double('Stdout', empty?: stdout_empty?) }
|
||||||
let(:stdout_empty?) { false }
|
let(:stdout_empty?) { false }
|
||||||
|
|
||||||
|
include_context 'test if git tracks the file'
|
||||||
|
|
||||||
before do
|
before do
|
||||||
expect(Open3).to receive(:capture2)
|
expect(Open3).to receive(:capture2)
|
||||||
.ordered
|
.ordered
|
||||||
|
@ -44,9 +60,7 @@ describe Mutant::Repository::Diff do
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:expected_git_log_command) do
|
let(:expected_git_log_command) do
|
||||||
%w[
|
%W[git log from_rev...to_rev -L 1,2:#{path}]
|
||||||
git log from_rev...to_rev -L 1,2:foo.rb
|
|
||||||
]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'on failure of git log command' do
|
context 'on failure of git log command' do
|
||||||
|
|
Loading…
Reference in a new issue