forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_spec_helpers.rb
63 lines (53 loc) · 1.88 KB
/
git_spec_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
require 'tmpdir'
# Helpers for creating temporary repositories and directories for testing.
module GitSpecHelpers
module_function
# Creates an empty git repository, allowing you to execute a block where
# the current working directory is set to that repository's root directory.
#
# @param options [Hash]
# @return [String] path of the repository
def repo(options = {})
directory('some-repo') do
create_cmd = %w[git init]
create_cmd += ['--template', options[:template_dir]] if options[:template_dir]
create_cmd += ['--separate-git-dir', options[:git_dir]] if options[:git_dir]
result = Overcommit::Utils.execute(create_cmd)
raise "Unable to create repo: #{result.stderr}" unless result.success?
# Need to define user info since some CI contexts don't have defaults set
`git config --local user.name "Overcommit Tester"`
`git config --local user.email "overcommit@example.com"`
`git config --local rerere.enabled 0` # Don't record resolutions in tests
`git config --local commit.gpgsign false`
yield if block_given?
end
end
# Retrieve sha1 based on git ref
#
# @param ref [String] git ref
# @return [String] ref's sha1
def get_sha1(ref)
`git rev-parse #{ref}`.chomp
end
# Creates a directory (with an optional specific name) in a temporary
# directory which will automatically be destroyed.
#
# @param name [String] base name of the directory
# @return [String] path of the directory that was created
def directory(name = 'some-dir', &block)
tmpdir = Dir.mktmpdir.tap do |path|
Dir.chdir(path) do
Dir.mkdir(name)
Dir.chdir(name, &block) if block_given?
end
end
File.join(tmpdir, name)
end
# Returns a random git object hash.
#
# @return [String]
def random_hash
Array.new(40) { rand(65..90).chr }.join
end
end