Test basic functionality

This commit is contained in:
Sven Weidauer 2022-06-05 12:40:48 +02:00
parent 736f394281
commit 3711acbec3
4 changed files with 60 additions and 7 deletions

View file

@ -10,4 +10,4 @@ Metrics/MethodLength:
Metrics/BlockLength:
Exclude:
- bin/git-format-staged # Long block for option parser is ok
- spec/**/*.rb

View file

@ -31,6 +31,14 @@ module Git
File.write absolute, content.end_with?("\n") ? content : "#{content}\n"
end
def get_content(name)
File.read(Pathname.new(path) + name).chomp
end
def get_staged(name)
git 'show', ":#{name}"
end
def stage(file)
git 'add', file
end
@ -62,6 +70,12 @@ module Git
def in_repo(&block)
Dir.chdir path, &block
end
def run_formatter
in_repo do
FormatStaged.run formatter: "#{__dir__}/test_hook.rb {}", patterns: ['*.test']
end
end
end
def self.new_repo

6
spec/test_hook.rb Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
$stdin.readlines.each do |line|
puts line.gsub(/([^\s]*)\s*=\s*(.*)/, '\1 = \2')
end

View file

@ -6,12 +6,8 @@ require 'format_staged'
describe FormatStaged do
def repo
@repo ||= Git.new_repo do |r|
r.file_in_tree 'index.js', <<~CONTENT
function foo () {
return 'foo'
}
CONTENT
@repo ||= Git.new_repo do |repo|
repo.file_in_tree 'origin.test', 'x = y'
end
end
@ -19,4 +15,41 @@ describe FormatStaged do
@repo&.cleanup
@repo = nil
end
it 'updates staged file and working copy' do
repo.set_content 'test.test', 'a=b'
repo.stage 'test.test'
repo.run_formatter
expect(repo.get_staged('test.test')).to eq('a = b')
expect(repo.get_content('test.test')).to eq('a = b')
end
it 'leaves other changes in working copy' do
repo.set_content 'test.test', "x=y\na=b\n"
repo.stage 'test.test'
repo.set_content 'test.test', 'abc'
repo.run_formatter
expect(repo.get_content('test.test')).to eq('abc')
expect(repo.get_staged('test.test')).to eq("x = y\na = b")
end
it 'merges update to working copy' 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
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
it 'only touches files matching the given pattern' do
repo.set_content 'test.other', 'x=y'
repo.stage 'test.other'
repo.run_formatter
expect(repo.get_content('test.other')).to eq('x=y')
end
end