9907ffebe59b7727e9c41f9d50ce05deb7d26891
[rubygit.git] / tests / test_helper.rb
blob9907ffebe59b7727e9c41f9d50ce05deb7d26891
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 @tmp_path
26       #puts "teardown #{@tmp_path}"
27       FileUtils.rm_r(@tmp_path)
28     end
29   end
30   
31   def create_temp_repo(clone_path)
32     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
33     @tmp_path = File.join("/tmp/", filename)
34     FileUtils.mkdir_p(@tmp_path)
35     FileUtils.cp_r(clone_path, @tmp_path)
36     tmp_path = File.join(@tmp_path, 'working')
37     Dir.chdir(tmp_path) do
38       FileUtils.mv('dot_git', '.git')
39     end
40     tmp_path
41   end
42   
43   def in_temp_dir(remove_after = true)
44     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
45     tmp_path = File.join("/tmp/", filename)
46     FileUtils.mkdir(tmp_path)
47     Dir.chdir tmp_path do
48       yield tmp_path
49     end
50     FileUtils.rm_r(tmp_path) if remove_after
51   end
52   
53   
54   def new_file(name, contents)
55     File.open(name, 'w') do |f|
56       f.puts contents
57     end
58   end
60   def append_file(name, contents)
61     File.open(name, 'a') do |f|
62       f.puts contents
63     end
64   end
65   
66 end