Prepare for testing

This commit is contained in:
Sven Weidauer 2022-05-29 12:17:28 +02:00
parent 29f580b431
commit d52e6ea10e
4 changed files with 99 additions and 1 deletions

View file

@ -45,6 +45,8 @@ GEM
parser (>= 3.1.1.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.11.1)
rubocop (~> 1.19)
ruby-progressbar (1.11.0)
unicode-display_width (2.1.0)
@ -58,6 +60,7 @@ DEPENDENCIES
rspec
rubocop (~> 1.29)
rubocop-rake (~> 0.6)
rubocop-rspec
BUNDLED WITH
2.1.4

View file

@ -20,9 +20,10 @@ Gem::Specification.new do |s|
s.add_dependency 'colorize'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rubocop', '~> 1.29'
s.add_development_dependency 'rubocop-rake', '~> 0.6'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rubocop-rspec'
s.metadata = {
'rubygems_mfa_required' => 'true'

72
spec/git.rb Normal file
View file

@ -0,0 +1,72 @@
# 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
git 'init'
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
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
end
def self.new_repo
repo = Repo.new
yield repo if block_given?
repo
end
end

22
spec/test_spec.rb Normal file
View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require_relative 'git'
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
end
end
after :each do
@repo&.cleanup
@repo = nil
end
end