adding my testing git directory
[rubygit.git] / tests / test_helper.rb
blob78328c9c859ea6b778fbcd61370339d341a084b0
1 require 'test/unit'
2 require 'fileutils'
3 require File.dirname(__FILE__) + '/../lib/git'
5 class Test::Unit::TestCase
6   
7   def set_file_paths
8     cwd = `pwd`.chomp
9     if File.directory?(File.join(cwd, 'files'))
10       @test_dir = File.join(cwd, 'files')
11     elsif File.directory?(File.join(cwd, '..', 'files'))
12       @test_dir = File.join(cwd, '..', 'files')
13     elsif File.directory?(File.join(cwd, 'tests', 'files'))
14       @test_dir = File.join(cwd, 'tests', 'files')
15     end
16     
17     @wdir_dot = File.expand_path(File.join(@test_dir, 'working'))
18     @wbare = File.expand_path(File.join(@test_dir, 'working.git'))
19     @index = File.expand_path(File.join(@test_dir, 'index'))
20     
21     @wdir = create_temp_repo(@wdir_dot)
22   end
23   
24   def teardown
25     if @wdir
26       FileUtils.rm_r(@wdir)
27     end
28   end
29   
30   def create_temp_repo(clone_path)
31     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s
32     tmp_path = File.join("/tmp/", filename)
33     FileUtils.mkdir_p(tmp_path)
34     FileUtils.cp_r(clone_path, tmp_path)
35     tmp_path = File.join(tmp_path, 'working')
36     Dir.chdir(tmp_path) do
37       FileUtils.mv('dot_git', '.git')
38     end
39     tmp_path
40   end
41   
42   def in_temp_dir(remove_after = true)
43     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s
44     tmp_path = File.join("/tmp/", filename)
45     FileUtils.mkdir(tmp_path)
46     Dir.chdir tmp_path do
47       yield tmp_path
48     end
49     FileUtils.rm_r(tmp_path) if remove_after
50   end
51   
52   
53   def new_file(name, contents)
54     File.open(name, 'w') do |f|
55       f.puts contents
56     end
57   end
59   def append_file(name, contents)
60     File.open(name, 'a') do |f|
61       f.puts contents
62     end
63   end
64   
65 end