forked from mojombo/grit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_git.rb
143 lines (119 loc) · 4.13 KB
/
test_git.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require File.dirname(__FILE__) + '/helper'
class TestGit < Test::Unit::TestCase
def setup
@git = Git.new(File.join(File.dirname(__FILE__), *%w[..]))
end
def teardown
Grit.debug = false
end
def test_method_missing
assert_match(/^git version [\w\.]*$/, @git.version)
end
def test_logs_stderr
Grit.debug = true
Grit.stubs(:log)
Grit.expects(:log).with(includes("git: 'bad' is not a git command"))
@git.bad
end
def test_logs_stderr_when_skipping_timeout
Grit.debug = true
Grit.stubs(:log)
Grit.expects(:log).with(includes("git: 'bad' is not a git command"))
@git.bad :timeout => false
end
def test_transform_options
assert_equal ["-s"], @git.transform_options({:s => true})
assert_equal [], @git.transform_options({:s => false})
assert_equal ["-s '5'"], @git.transform_options({:s => 5})
assert_equal ["--max-count"], @git.transform_options({:max_count => true})
assert_equal ["--max-count='5'"], @git.transform_options({:max_count => 5})
assert_equal ["-s", "-t"], @git.transform_options({:s => true, :t => true}).sort
end
def test_uses_native_command_execution
@git.expects(:native)
@git.something
end
def test_can_skip_timeout
Timeout.expects(:timeout).never
@git.something(:timeout => false)
end
def test_raises_if_too_many_bytes
fail if jruby?
assert_raises Grit::Git::GitTimeout do
@git.sh "yes | head -#{Grit::Git.git_max_size + 1}"
end
end
def test_raises_on_slow_shell
Grit::Git.git_timeout = 0.0000001
assert_raises Grit::Git::GitTimeout do
@git.version
end
ensure
Grit::Git.git_timeout = 5.0
end
def test_with_timeout_default_parameter
assert_nothing_raised do
Git::Git.with_timeout do
@git.version
end
end
end
def test_it_really_shell_escapes_arguments_to_the_git_shell
@git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' foo --bar='bazz\\'er'")
@git.run('', :foo, '', {:bar => "bazz'er"}, [])
@git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' bar -x 'quu\\'x'")
@git.run('', :bar, '', {:x => "quu'x"}, [])
end
def test_it_shell_escapes_the_standalone_argument
@git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' foo 'bar\\'s'")
@git.run('', :foo, '', {}, ["bar's"])
@git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' foo 'bar' '\\; echo \\'noooo\\''")
@git.run('', :foo, '', {}, ["bar", "; echo 'noooo'"])
end
def test_piping_should_work_on_1_9
@git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' archive 'master' | gzip")
@git.archive({}, "master", "| gzip")
end
def test_fs_read
f = stub
File.expects(:read).with(File.join(@git.git_dir, 'foo')).returns('bar')
assert_equal 'bar', @git.fs_read('foo')
end
def test_fs_write
f = stub
f.expects(:write).with('baz')
FileUtils.expects(:mkdir_p).with(File.join(@git.git_dir, 'foo'))
File.expects(:open).with(File.join(@git.git_dir, 'foo/bar'), 'w').yields(f)
@git.fs_write('foo/bar', 'baz')
end
def test_fs_delete
FileUtils.expects(:rm_rf).with(File.join(@git.git_dir, 'foo'))
@git.fs_delete('foo')
end
def test_passing_env_to_native
args = [
{ 'A' => 'B' },
Grit::Git.git_binary, "--git-dir=#{@git.git_dir}", "help", "-a",
{:input => nil, :chdir => nil, :timeout => Grit::Git.git_timeout, :max => Grit::Git.git_max_size}
]
p = Grit::Git::Child.new(*args)
Grit::Git::Child.expects(:new).with(*args).returns(p)
@git.native(:help, {:a => true, :env => { 'A' => 'B' }})
end
def test_native_process_info_option_on_failure
exitstatus, out, err = @git.no_such_command({:process_info => true})
assert_equal 1, exitstatus
assert !err.empty?
end
def test_native_process_info_option_on_success
exitstatus, out, err = @git.help({:process_info => true})
assert_equal 0, exitstatus
assert !out.empty?
assert err.empty?
end
def test_raising_exceptions_when_native_git_commands_fail
assert_raise Grit::Git::CommandFailed do
@git.native(:bad, {:raise => true})
end
end
end