e40174a8d06337c7915abce556db29a74277d9c7
[rubygit.git] / tests / test_helper.rb
blobe40174a8d06337c7915abce556db29a74277d9c7
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   
32   def with_temp_bare
33     in_temp_dir do |path|
34       g = Git.clone(@wbare, 'new')
35       Dir.chdir('new') do
36         yield g
37       end
38     end
39   end
40   
41   def create_temp_repo(clone_path)
42     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
43     @tmp_path = File.join("/tmp/", filename)
44     FileUtils.mkdir_p(@tmp_path)
45     FileUtils.cp_r(clone_path, @tmp_path)
46     tmp_path = File.join(@tmp_path, 'working')
47     Dir.chdir(tmp_path) do
48       FileUtils.mv('dot_git', '.git')
49     end
50     tmp_path
51   end
52   
53   def in_temp_dir(remove_after = true)
54     filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
55     tmp_path = File.join("/tmp/", filename)
56     FileUtils.mkdir(tmp_path)
57     Dir.chdir tmp_path do
58       yield tmp_path
59     end
60     FileUtils.rm_r(tmp_path) if remove_after
61   end
62   
63   
64   def new_file(name, contents)
65     File.open(name, 'w') do |f|
66       f.puts contents
67     end
68   end
70   def append_file(name, contents)
71     File.open(name, 'a') do |f|
72       f.puts contents
73     end
74   end
75   
76 end