-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathtest_helper.rb
171 lines (140 loc) · 5.05 KB
/
test_helper.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require 'tempfile'
require 'tmpdir'
require 'minitest/autorun'
require 'rugged'
require 'pp'
module Rugged
class TestCase < Minitest::Test
# Automatically clean up created fixture repos after each test run
def after_teardown
Rugged::TestCase::FixtureRepo.teardown
super
end
module FixtureRepo
# Create a new, empty repository.
def self.empty(*args)
path = Dir.mktmpdir("rugged-empty")
ensure_cleanup(path)
Rugged::Repository.init_at(path, *args)
end
# Create a repository based on a rugged fixture repo.
def self.from_rugged(name, *args)
path = Dir.mktmpdir("rugged-#{name}")
ensure_cleanup(path)
FileUtils.cp_r(File.join(TestCase::TEST_DIR, "fixtures", name, "."), path)
prepare(path)
Rugged::Repository.new(path, *args).tap do |repo|
rewrite_gitmodules(repo) unless repo.bare?
end
end
# Create a repository based on a libgit2 fixture repo.
def self.from_libgit2(name, *args)
path = Dir.mktmpdir("rugged-libgit2-#{name}")
ensure_cleanup(path)
FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, name, "."), path)
prepare(path)
Rugged::Repository.new(path, *args).tap do |repo|
rewrite_gitmodules(repo) unless repo.bare?
end
end
# Create a repository cloned from another Rugged::Repository instance.
def self.clone(repository)
path = Dir.mktmpdir("rugged")
ensure_cleanup(path)
`git clone --quiet -- #{repository.path} #{path}`
Rugged::Repository.new(path)
end
def self.prepare(path)
Dir.chdir(path) do
File.rename(".gitted", ".git") if File.exist?(".gitted")
File.rename("gitattributes", ".gitattributes") if File.exist?("gitattributes")
File.rename("gitignore", ".gitignore") if File.exist?("gitignore")
end
end
# Rugged reuses libgit2 fixtures and needs the same setup code.
#
# This should be the same as the libgit2 fixture
# setup in vendor/libgit2/tests/submodule/submodule_helpers.c
def self.rewrite_gitmodules(repo)
workdir = repo.workdir
return unless File.exist?(File.join(workdir, 'gitmodules'))
input_path = File.join(workdir, 'gitmodules')
output_path = File.join(workdir, '.gitmodules')
submodules = []
File.open(input_path, 'r') do |input|
File.open(output_path, 'w') do |output|
input.each_line do |line|
if %r{path = (?<submodule>.+$)} =~ line
submodules << submodule.strip
elsif %r{url = \.\.\/(?<url>.+$)} =~ line
# Copy repositories pointed to by relative urls
# and replace the relative url by the absolute path to the
# copied repo.
url.strip!
path = Dir.mktmpdir(url)
ensure_cleanup(path)
FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, url, "."), path)
line = "url = #{path}\n"
end
output.write(line)
end
end
end
FileUtils.remove_entry_secure(input_path)
# rename .gitted -> .git in submodule dirs
submodules.each do |submodule|
submodule_path = File.join(workdir, submodule)
if File.exist?(File.join(submodule_path, '.gitted'))
Dir.chdir(submodule_path) do
File.rename('.gitted', '.git')
end
end
end
end
# Delete temp directories that got created
def self.teardown
self.directories.each { |path| FileUtils.remove_entry_secure(path) }
self.directories.clear
end
def self.directories
@directories ||= []
end
# Registers the given +path+ to be deleted when #teardown is called.
def self.ensure_cleanup(path)
self.directories << path
end
end
TEST_DIR = File.dirname(File.expand_path(__FILE__))
LIBGIT2_FIXTURE_DIR = File.expand_path("../../vendor/libgit2/tests/resources", __FILE__)
end
class OnlineTestCase < TestCase
if ENV['GITTEST_REMOTE_REPO_PATH']
def before_setup
remote_repo = Rugged::Repository.new(ENV['GITTEST_REMOTE_REPO_PATH'])
remote_repo.references.each do |ref|
remote_repo.references.delete(ref)
end
super
end
end
def self.ssh_creds?
%w{URL USER KEY PUBKEY PASSPHRASE}.all? { |key| ENV["GITTEST_REMOTE_SSH_#{key}"] }
end
def self.git_creds?
ENV['GITTEST_REMOTE_GIT_URL']
end
def ssh_key_credential
Rugged::Credentials::SshKey.new({
username: ENV["GITTEST_REMOTE_SSH_USER"],
publickey: ENV["GITTEST_REMOTE_SSH_PUBKEY"],
privatekey: ENV["GITTEST_REMOTE_SSH_KEY"],
passphrase: ENV["GITTEST_REMOTE_SSH_PASSPHRASE"],
})
end
def ssh_key_credential_from_agent
Rugged::Credentials::SshKeyFromAgent.new({
username: ENV["GITTEST_REMOTE_SSH_USER"]
})
end
end
end