Merge branch 'master' into 5675-project-subprojects-in-anonymous-view
[arvados.git] / services / api / test / helpers / git_test_helper.rb
1 require 'fileutils'
2 require 'tmpdir'
3
4 # Commit log for "foo" repository in test.git.tar
5 # master is the main branch
6 # b1 is a branch off of master
7 # tag1 is a tag
8 #
9 # 1de84a8 * b1
10 # 077ba2a * master
11 # 4fe459a * tag1
12 # 31ce37f * foo
13
14 module GitTestHelper
15   def self.included base
16     base.setup do
17       @tmpdir = Dir.mktmpdir()
18       system("tar", "-xC", @tmpdir, "-f", "test/test.git.tar")
19       Rails.configuration.git_repositories_dir = "#{@tmpdir}/test"
20       intdir = Rails.configuration.git_internal_dir
21       if not File.exist? intdir
22         FileUtils.mkdir_p intdir
23         IO.read("|git --git-dir #{intdir.to_s.shellescape} init")
24         assert $?.success?
25       end
26     end
27
28     base.teardown do
29       FileUtils.remove_entry @tmpdir, true
30       FileUtils.remove_entry Commit.cache_dir_base, true
31     end
32   end
33
34   def internal_tag tag
35     IO.read "|git --git-dir #{Rails.configuration.git_internal_dir.shellescape} log --format=format:%H -n1 #{tag.shellescape}"
36   end
37
38   # Intercept fetch_remote_repository and fetch from a specified url
39   # or local fixture instead of the remote url requested. fakeurl can
40   # be a url (probably starting with file:///) or the name of a
41   # fixture (as a symbol)
42   def fetch_remote_from_local_repo url, fakeurl
43     if fakeurl.is_a? Symbol
44       fakeurl = 'file://' + repositories(fakeurl).server_path
45     end
46     Commit.expects(:fetch_remote_repository).once.with do |gitdir, giturl|
47       if giturl == url
48         Commit.unstub(:fetch_remote_repository)
49         Commit.fetch_remote_repository gitdir, fakeurl
50         true
51       end
52     end
53   end
54 end