8784: Fix test for latest firefox.
[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       # Extract the test repository data into the default test
18       # environment's Rails.configuration.git_repositories_dir. (We
19       # don't use that config setting here, though: it doesn't seem
20       # worth the risk of stepping on a real git repo root.)
21       @tmpdir = Rails.root.join 'tmp', 'git'
22       FileUtils.mkdir_p @tmpdir
23       system("tar", "-xC", @tmpdir.to_s, "-f", "test/test.git.tar")
24       Rails.configuration.git_repositories_dir = "#{@tmpdir}/test"
25
26       intdir = Rails.configuration.git_internal_dir
27       if not File.exist? intdir
28         FileUtils.mkdir_p intdir
29         IO.read("|git --git-dir #{intdir.to_s.shellescape} init")
30         assert $?.success?
31       end
32     end
33
34     base.teardown do
35       FileUtils.remove_entry @tmpdir, true
36       FileUtils.remove_entry Commit.cache_dir_base, true
37     end
38   end
39
40   def internal_tag tag
41     IO.read "|git --git-dir #{Rails.configuration.git_internal_dir.shellescape} log --format=format:%H -n1 #{tag.shellescape}"
42   end
43
44   # Intercept fetch_remote_repository and fetch from a specified url
45   # or local fixture instead of the remote url requested. fakeurl can
46   # be a url (probably starting with file:///) or the name of a
47   # fixture (as a symbol)
48   def fetch_remote_from_local_repo url, fakeurl
49     if fakeurl.is_a? Symbol
50       fakeurl = 'file://' + repositories(fakeurl).server_path
51     end
52     Commit.expects(:fetch_remote_repository).once.with do |gitdir, giturl|
53       if giturl == url
54         Commit.unstub(:fetch_remote_repository)
55         Commit.fetch_remote_repository gitdir, fakeurl
56         true
57       end
58     end
59   end
60 end