[#1] Fail if all files are reset to committed version.

This commit is contained in:
Sven Weidauer 2022-06-06 10:18:05 +02:00
parent d00760732d
commit ba54c407cb
4 changed files with 40 additions and 9 deletions

View file

@ -6,5 +6,5 @@ gemspec
group :ci do
gem 'code-scanning-rubocop'
gem "rspec_junit_formatter"
gem 'rspec_junit_formatter'
end

View file

@ -73,4 +73,5 @@ if !parameters[:formatter] || parameters[:patterns].empty?
exit
end
FormatStaged.run(**parameters)
success = FormatStaged.run(**parameters)
exit success ? 0 : 1

View file

@ -28,9 +28,15 @@ module FormatStaged
end
def run
matching_files(repo_root).each do |file|
format_file(file)
files = matching_files(repo_root)
if files.empty?
info 'No staged file matching pattern. Done'
return true
end
formatted = files.filter { |file| format_file file }
!formatted.empty?
end
def repo_root
@ -57,7 +63,7 @@ module FormatStaged
if new_hash == file.dst_hash
info "Unchanged #{file.src_path}"
return false
return true
end
if object_is_empty new_hash
@ -68,6 +74,11 @@ module FormatStaged
replace_file_in_index file, new_hash
update_working_copy file, new_hash
if new_hash == file.src_hash
info "File #{file.src_path} equal to comitted"
return false
end
true
end

View file

@ -19,8 +19,9 @@ describe FormatStaged do
it 'updates staged file and working copy' do
repo.set_content 'test.test', 'a=b'
repo.stage 'test.test'
repo.run_formatter
success = repo.run_formatter
expect(success).to be_truthy
expect(repo.get_staged('test.test')).to eq('a = b')
expect(repo.get_content('test.test')).to eq('a = b')
end
@ -29,8 +30,9 @@ describe FormatStaged do
repo.set_content 'test.test', "x=y\na=b\n"
repo.stage 'test.test'
repo.set_content 'test.test', 'abc'
repo.run_formatter
success = repo.run_formatter
expect(success).to be_truthy
expect(repo.get_content('test.test')).to eq('abc')
expect(repo.get_staged('test.test')).to eq("x = y\na = b")
end
@ -39,8 +41,9 @@ describe FormatStaged do
repo.set_content 'test.test', "x=y\n#stuff\n"
repo.stage 'test.test'
repo.set_content 'test.test', "x=y\n#stuff\nmore stuff\n"
repo.run_formatter
success = repo.run_formatter
expect(success).to be_truthy
expect(repo.get_content('test.test')).to eq("x = y\n#stuff\nmore stuff")
expect(repo.get_staged('test.test')).to eq("x = y\n#stuff")
end
@ -48,8 +51,24 @@ describe FormatStaged do
it 'only touches files matching the given pattern' do
repo.set_content 'test.other', 'x=y'
repo.stage 'test.other'
repo.run_formatter
success = repo.run_formatter
expect(success).to be_truthy
expect(repo.get_content('test.other')).to eq('x=y')
end
it 'fails if files are changed to already comitted version' do
repo.file_in_tree 'test.test', 'x = y'
repo.set_content 'test.test', 'x=y'
repo.stage 'test.test'
success = repo.run_formatter
expect(success).to be_falsy
expect(repo.get_content('test.test')).to eq('x = y')
end
it 'succeeds if there are no staged files' do
expect(repo.run_formatter).to be_truthy
end
end