format-staged/spec/git.rb

89 lines
1.6 KiB
Ruby
Raw Permalink Normal View History

2022-05-29 12:17:28 +02:00
# frozen_string_literal: true
require 'tmpdir'
require 'fileutils'
require 'English'
##
# Test helpers for managing a git repository
module Git
##
# A git repository
class Repo
attr_reader :path
def initialize
@path = Dir.mktmpdir
2022-06-05 13:19:08 +02:00
git 'init', '-b', 'main'
2022-06-05 13:03:51 +02:00
git 'config', 'user.name', 'Test User'
git 'config', 'user.email', 'test@example.com'
2022-05-29 12:17:28 +02:00
end
def file_in_tree(name, content)
set_content name, content
stage name
commit "Add #{name}"
end
def set_content(name, content)
absolute = Pathname.new(path) + name
FileUtils.mkdir_p absolute.dirname
File.write absolute, content.end_with?("\n") ? content : "#{content}\n"
end
2022-06-05 12:40:48 +02:00
def get_content(name)
File.read(Pathname.new(path) + name).chomp
end
def get_staged(name)
git 'show', ":#{name}"
end
2022-05-29 12:17:28 +02:00
def stage(file)
git 'add', file
end
def commit(message)
git 'commit', '-m', message
end
def content(file)
git 'show', ":#{file}"
end
def cleanup
FileUtils.remove_entry path
end
def git(*cmd)
in_repo do
output = IO.popen(['git'] + cmd) do |io|
io.read.chomp
end
raise 'Failed to run git' unless $CHILD_STATUS.success?
output
end
end
def in_repo(&block)
Dir.chdir path, &block
end
2022-06-05 12:40:48 +02:00
def run_formatter(**arguments)
2022-06-05 12:40:48 +02:00
in_repo do
FormatStaged.run(**arguments, formatter: "#{__dir__}/test_hook.rb {}", patterns: ['*.test'])
2022-06-05 12:40:48 +02:00
end
end
2022-05-29 12:17:28 +02:00
end
def self.new_repo
repo = Repo.new
yield repo if block_given?
repo
end
end