applied a patch by mateusz jedruch <mateusz.jedruch@gmail.com> for iterating through...
[rubygit.git] / tests / test_helper.rb
blob1ee8dbfb98753f69beba7ba5b8cb8d00c4d4c6c6
1 require 'test/unit'
2 require 'fileutils'
3 require 'logger'
4 require File.dirname(__FILE__) + '/../lib/git'
6 class Test::Unit::TestCase
7   
8   def set_file_paths
9     cwd = `pwd`.chomp
10     if File.directory?(File.join(cwd, 'files'))
11       @test_dir = File.join(cwd, 'files')
12     elsif File.directory?(File.join(cwd, '..', 'files'))
13       @test_dir = File.join(cwd, '..', 'files')
14     elsif File.directory?(File.join(cwd, 'tests', 'files'))
15       @test_dir = File.join(cwd, 'tests', 'files')
16     end
17     
18     @wdir_dot = File.expand_path(File.join(@test_dir, 'working'))
19     @wbare = File.expand_path(File.join(@test_dir, 'working.git'))
20     @index = File.expand_path(File.join(@test_dir, 'index'))
21     
22     @wdir = create_temp_repo(@wdir_dot)
23   end
24   
25   def teardown
26     if @tmp_path
27       #puts "teardown #{@tmp_path}"
28       FileUtils.rm_r(@tmp_path)
29     end
30   end
31   
32   
33   def with_temp_bare
34     in_temp_dir do |path|
35       g = Git.clone(@wbare, 'new')
36       Dir.chdir('new') do
37         yield g
38       end
39     end
40   end
41   
42   def create_temp_repo(clone_path)
43     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
44     @tmp_path = File.join("/tmp/", filename)
45     FileUtils.mkdir_p(@tmp_path)
46     FileUtils.cp_r(clone_path, @tmp_path)
47     tmp_path = File.join(@tmp_path, 'working')
48     Dir.chdir(tmp_path) do
49       FileUtils.mv('dot_git', '.git')
50     end
51     tmp_path
52   end
53   
54   def in_temp_dir(remove_after = true)
55     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
56     tmp_path = File.join("/tmp/", filename)
57     FileUtils.mkdir(tmp_path)
58     Dir.chdir tmp_path do
59       yield tmp_path
60     end
61     FileUtils.rm_r(tmp_path) if remove_after
62   end
63   
64   
65   def new_file(name, contents)
66     File.open(name, 'w') do |f|
67       f.puts contents
68     end
69   end
71   def append_file(name, contents)
72     File.open(name, 'a') do |f|
73       f.puts contents
74     end
75   end
76   
77 end