square-circle-triangle-grit 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,207 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestCommit < Test::Unit::TestCase
4
+ def setup
5
+ @r = Repo.new(File.join(File.dirname(__FILE__), *%w[dot_git]), :is_bare => true)
6
+ end
7
+
8
+ # __bake__
9
+
10
+ def test_bake
11
+ Git.any_instance.expects(:rev_list).returns(fixture('rev_list_single'))
12
+ @c = Commit.create(@r, :id => '4c8124ffcf4039d292442eeccabdeca5af5c5017')
13
+ @c.author # bake
14
+
15
+ assert_equal "Tom Preston-Werner", @c.author.name
16
+ assert_equal "tom@mojombo.com", @c.author.email
17
+ end
18
+
19
+ # short_name
20
+
21
+ def test_id_abbrev
22
+ assert_equal '80f136f', @r.commit('80f136f500dfdb8c3e8abf4ae716f875f0a1b57f').id_abbrev
23
+ end
24
+
25
+ # count
26
+
27
+ def test_count
28
+ assert_equal 107, Commit.count(@r, 'master')
29
+ end
30
+
31
+ # diff
32
+
33
+ def test_diff
34
+ # git diff --full-index 91169e1f5fa4de2eaea3f176461f5dc784796769 > test/fixtures/diff_p
35
+
36
+ Git.any_instance.expects(:diff).with({:full_index => true}, 'master').returns(fixture('diff_p'))
37
+ diffs = Commit.diff(@r, 'master')
38
+
39
+ assert_equal 15, diffs.size
40
+
41
+ assert_equal '.gitignore', diffs.first.a_path
42
+ assert_equal '.gitignore', diffs.first.b_path
43
+ assert_equal '4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs.first.a_blob.id
44
+ assert_equal '2dd02534615434d88c51307beb0f0092f21fd103', diffs.first.b_blob.id
45
+ assert_equal '100644', diffs.first.b_mode
46
+ assert_equal false, diffs.first.new_file
47
+ assert_equal false, diffs.first.deleted_file
48
+ assert_equal "--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diffs.first.diff
49
+
50
+ assert_equal 'lib/grit/actor.rb', diffs[5].a_path
51
+ assert_equal nil, diffs[5].a_blob
52
+ assert_equal 'f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_blob.id
53
+ assert_equal true, diffs[5].new_file
54
+ end
55
+
56
+ def test_diff_with_two_commits
57
+ # git diff --full-index 59ddc32 13d27d5 > test/fixtures/diff_2
58
+ Git.any_instance.expects(:diff).with({:full_index => true}, '59ddc32', '13d27d5').returns(fixture('diff_2'))
59
+ diffs = Commit.diff(@r, '59ddc32', '13d27d5')
60
+
61
+ assert_equal 3, diffs.size
62
+ assert_equal %w(lib/grit/commit.rb test/fixtures/show_empty_commit test/test_commit.rb), diffs.collect { |d| d.a_path }
63
+ end
64
+
65
+ def test_diff_with_files
66
+ # git diff --full-index 59ddc32 -- lib > test/fixtures/diff_f
67
+ Git.any_instance.expects(:diff).with({:full_index => true}, '59ddc32', '--', 'lib').returns(fixture('diff_f'))
68
+ diffs = Commit.diff(@r, '59ddc32', %w(lib))
69
+
70
+ assert_equal 1, diffs.size
71
+ assert_equal 'lib/grit/diff.rb', diffs.first.a_path
72
+ end
73
+
74
+ def test_diff_with_two_commits_and_files
75
+ # git diff --full-index 59ddc32 13d27d5 -- lib > test/fixtures/diff_2f
76
+ Git.any_instance.expects(:diff).with({:full_index => true}, '59ddc32', '13d27d5', '--', 'lib').returns(fixture('diff_2f'))
77
+ diffs = Commit.diff(@r, '59ddc32', '13d27d5', %w(lib))
78
+
79
+ assert_equal 1, diffs.size
80
+ assert_equal 'lib/grit/commit.rb', diffs.first.a_path
81
+ end
82
+
83
+ # diffs
84
+ def test_diffs
85
+ # git diff --full-index 91169e1f5fa4de2eaea3f176461f5dc784796769 > test/fixtures/diff_p
86
+
87
+ Git.any_instance.expects(:diff).returns(fixture('diff_p'))
88
+ @c = Commit.create(@r, :id => '91169e1f5fa4de2eaea3f176461f5dc784796769')
89
+ diffs = @c.diffs
90
+
91
+ assert_equal 15, diffs.size
92
+
93
+ assert_equal '.gitignore', diffs.first.a_path
94
+ assert_equal '.gitignore', diffs.first.b_path
95
+ assert_equal '4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs.first.a_blob.id
96
+ assert_equal '2dd02534615434d88c51307beb0f0092f21fd103', diffs.first.b_blob.id
97
+ assert_equal '100644', diffs.first.b_mode
98
+ assert_equal false, diffs.first.new_file
99
+ assert_equal false, diffs.first.deleted_file
100
+ assert_equal "--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diffs.first.diff
101
+
102
+ assert_equal 'lib/grit/actor.rb', diffs[5].a_path
103
+ assert_equal nil, diffs[5].a_blob
104
+ assert_equal 'f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_blob.id
105
+ assert_equal true, diffs[5].new_file
106
+ end
107
+
108
+ def test_diffs_on_initial_import
109
+ # git show --full-index 634396b2f541a9f2d58b00be1a07f0c358b999b3 > test/fixtures/diff_i
110
+
111
+ Git.any_instance.expects(:show).with({:full_index => true, :pretty => 'raw'}, '634396b2f541a9f2d58b00be1a07f0c358b999b3').returns(fixture('diff_i'))
112
+ @c = Commit.create(@r, :id => '634396b2f541a9f2d58b00be1a07f0c358b999b3')
113
+ diffs = @c.diffs
114
+
115
+ assert_equal 10, diffs.size
116
+
117
+ assert_equal 'History.txt', diffs.first.a_path
118
+ assert_equal 'History.txt', diffs.first.b_path
119
+ assert_equal nil, diffs.first.a_blob
120
+ assert_equal nil, diffs.first.b_mode
121
+ assert_equal '81d2c27608b352814cbe979a6acd678d30219678', diffs.first.b_blob.id
122
+ assert_equal true, diffs.first.new_file
123
+ assert_equal false, diffs.first.deleted_file
124
+ assert_equal "--- /dev/null\n+++ b/History.txt\n@@ -0,0 +1,5 @@\n+== 1.0.0 / 2007-10-09\n+\n+* 1 major enhancement\n+ * Birthday!\n+", diffs.first.diff
125
+
126
+
127
+ assert_equal 'lib/grit.rb', diffs[5].a_path
128
+ assert_equal nil, diffs[5].a_blob
129
+ assert_equal '32cec87d1e78946a827ddf6a8776be4d81dcf1d1', diffs[5].b_blob.id
130
+ assert_equal true, diffs[5].new_file
131
+ end
132
+
133
+ def test_diffs_on_initial_import_with_empty_commit
134
+ Git.any_instance.expects(:show).with(
135
+ {:full_index => true, :pretty => 'raw'},
136
+ '634396b2f541a9f2d58b00be1a07f0c358b999b3'
137
+ ).returns(fixture('show_empty_commit'))
138
+
139
+ @c = Commit.create(@r, :id => '634396b2f541a9f2d58b00be1a07f0c358b999b3')
140
+ diffs = @c.diffs
141
+
142
+ assert_equal [], diffs
143
+ end
144
+
145
+ def test_diffs_with_mode_only_change
146
+ Git.any_instance.expects(:diff).returns(fixture('diff_mode_only'))
147
+ @c = Commit.create(@r, :id => '91169e1f5fa4de2eaea3f176461f5dc784796769')
148
+ diffs = @c.diffs
149
+
150
+ assert_equal 23, diffs.size
151
+ assert_equal '100644', diffs[0].a_mode
152
+ assert_equal '100755', diffs[0].b_mode
153
+ end
154
+
155
+ # to_s
156
+
157
+ def test_to_s
158
+ @c = Commit.create(@r, :id => 'abc')
159
+ assert_equal "abc", @c.to_s
160
+ end
161
+
162
+ # to_patch
163
+
164
+ def test_to_patch
165
+ @c = Commit.create(@r, :id => '80f136f500dfdb8c3e8abf4ae716f875f0a1b57f')
166
+
167
+ patch = @c.to_patch
168
+
169
+ assert patch.include?('From 80f136f500dfdb8c3e8abf4ae716f875f0a1b57f Mon Sep 17 00:00:00 2001')
170
+ assert patch.include?('From: tom <tom@taco.(none)>')
171
+ assert patch.include?('Date: Tue, 20 Nov 2007 17:27:42 -0800')
172
+ assert patch.include?('Subject: [PATCH] fix tests on other machines')
173
+ assert patch.include?('test/test_reality.rb | 30 +++++++++++++++---------------')
174
+ assert patch.include?('@@ -1,17 +1,17 @@')
175
+ assert patch.include?('+# recurse(t)')
176
+ assert patch.include?("1.6.")
177
+ end
178
+
179
+ # inspect
180
+
181
+ def test_inspect
182
+ @c = Commit.create(@r, :id => 'abc')
183
+ assert_equal %Q{#<Grit::Commit "abc">}, @c.inspect
184
+ end
185
+
186
+ # to_hash
187
+
188
+ def test_to_hash
189
+ old_tz, ENV["TZ"] = ENV["TZ"], "US/Pacific"
190
+ @c = Commit.create(@r, :id => '4c8124ffcf4039d292442eeccabdeca5af5c5017')
191
+ date = Time.parse('Wed Oct 10 03:06:12 -0400 2007')
192
+ expected = {
193
+ 'parents' => ['id' => "634396b2f541a9f2d58b00be1a07f0c358b999b3"],
194
+ 'committed_date' => date.xmlschema,
195
+ 'tree' => "672eca9b7f9e09c22dcb128c283e8c3c8d7697a4",
196
+ 'authored_date' => date.xmlschema,
197
+ 'committer' => {'email' => "tom@mojombo.com", 'name' => "Tom Preston-Werner"},
198
+ 'message' => "implement Grit#heads",
199
+ 'author' => {'email' => "tom@mojombo.com", 'name' => "Tom Preston-Werner"},
200
+ 'id' => "4c8124ffcf4039d292442eeccabdeca5af5c5017"
201
+ }
202
+
203
+ assert_equal expected, @c.to_hash
204
+ ensure
205
+ ENV["TZ"] = old_tz
206
+ end
207
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestCommitStats < Test::Unit::TestCase
4
+
5
+ def setup
6
+ File.expects(:exist?).returns(true)
7
+ @r = Repo.new(GRIT_REPO)
8
+
9
+ Git.any_instance.expects(:log).returns(fixture('log'))
10
+ @stats = @r.commit_stats
11
+ end
12
+
13
+ def test_commit_stats
14
+ assert_equal 3, @stats.size
15
+ end
16
+
17
+ # to_hash
18
+
19
+ def test_to_hash
20
+ expected = {
21
+ "files"=>
22
+ [["examples/ex_add_commit.rb", 13, 0, 13],
23
+ ["examples/ex_index.rb", 1, 1, 2]],
24
+ "total"=>15,
25
+ "additions"=>14,
26
+ "id"=>"a49b96b339c525d7fd455e0ad4f6fe7b550c9543",
27
+ "deletions"=>1
28
+ }
29
+
30
+ assert_equal expected, @stats.assoc('a49b96b339c525d7fd455e0ad4f6fe7b550c9543')[1].to_hash
31
+ end
32
+
33
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestCommitWrite < Test::Unit::TestCase
4
+ def setup
5
+ @r = Repo.new(GRIT_REPO)
6
+ end
7
+
8
+ def test_commit
9
+ Git.any_instance.expects(:commit).returns(fixture('commit'))
10
+ results = @r.commit_index('my message')
11
+ assert_match /Created commit/, results
12
+ end
13
+
14
+ def test_commit_all
15
+ Git.any_instance.expects(:commit).returns(fixture('commit'))
16
+ results = @r.commit_all('my message')
17
+ assert_match /Created commit/, results
18
+ end
19
+
20
+ end
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestConfig < Test::Unit::TestCase
4
+ def setup
5
+ @r = Repo.new(GRIT_REPO)
6
+ end
7
+
8
+ # data
9
+
10
+ def test_bracketed_fetch
11
+ Git.any_instance.expects(:config).returns(fixture('simple_config'))
12
+
13
+ config = @r.config
14
+
15
+ assert_equal "git://github.com/mojombo/grit.git", config["remote.origin.url"]
16
+ end
17
+
18
+ def test_bracketed_fetch_returns_nil
19
+ Git.any_instance.expects(:config).returns(fixture('simple_config'))
20
+
21
+ config = @r.config
22
+
23
+ assert_equal nil, config["unknown"]
24
+ end
25
+
26
+ def test_fetch
27
+ Git.any_instance.expects(:config).returns(fixture('simple_config'))
28
+
29
+ config = @r.config
30
+
31
+ assert_equal "false", config.fetch("core.bare")
32
+ end
33
+
34
+ def test_fetch_with_default
35
+ Git.any_instance.expects(:config).returns(fixture('simple_config'))
36
+
37
+ config = @r.config
38
+
39
+ assert_equal "default", config.fetch("unknown", "default")
40
+ end
41
+
42
+ def test_fetch_without_default_raises
43
+ Git.any_instance.expects(:config).returns(fixture('simple_config'))
44
+
45
+ config = @r.config
46
+
47
+ assert_raise(IndexError) do
48
+ config.fetch("unknown")
49
+ end
50
+ end
51
+
52
+ def test_set_value
53
+ Git.any_instance.expects(:config).with({}, 'unknown', 'default')
54
+
55
+ config = @r.config
56
+ config["unknown"] = "default"
57
+ end
58
+ end
data/test/test_diff.rb ADDED
@@ -0,0 +1,18 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestDiff < Test::Unit::TestCase
4
+ def setup
5
+ @r = Repo.new(GRIT_REPO)
6
+ end
7
+
8
+ # list_from_string
9
+
10
+ def test_list_from_string_new_mode
11
+ output = fixture('diff_new_mode')
12
+
13
+ diffs = Grit::Diff.list_from_string(@r, output)
14
+ assert_equal 2, diffs.size
15
+ assert_equal 10, diffs.first.diff.split("\n").size
16
+ assert_equal nil, diffs.last.diff
17
+ end
18
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestFileIndex < Test::Unit::TestCase
4
+
5
+ def setup_a
6
+ @findex = Grit::GitRuby::FileIndex.new(File.join(File.dirname(__FILE__), *%w[dot_git]))
7
+ @commit = 'c12f398c2f3c4068ca5e01d736b1c9ae994b2138'
8
+ end
9
+
10
+ def test_count_all
11
+ setup_a
12
+ assert_equal 107, @findex.count_all
13
+ end
14
+
15
+ def test_count
16
+ setup_a
17
+ assert_equal 20, @findex.count(@commit)
18
+ end
19
+
20
+ def test_files
21
+ setup_a
22
+ files = @findex.files(@commit)
23
+ assert_equal 4, files.size
24
+ assert_equal "lib/grit/blob.rb", files.first
25
+ end
26
+
27
+ def test_commits_for
28
+ setup_a
29
+ commits = @findex.commits_for('lib/grit/blob.rb')
30
+ assert commits.include?('3e0955045cb189a7112015c26132152a94f637bf')
31
+ assert_equal 8, commits.size
32
+ end
33
+
34
+ def test_last_commits_array
35
+ setup_a
36
+ arr = @findex.last_commits(@commit, ['lib/grit/git.rb', 'lib/grit/actor.rb', 'lib/grit/commit.rb'])
37
+ assert_equal '74fd66519e983a0f29e16a342a6059dbffe36020', arr['lib/grit/git.rb']
38
+ assert_equal @commit, arr['lib/grit/commit.rb']
39
+ assert_equal nil, arr['lib/grit/actor.rb']
40
+ end
41
+
42
+ def test_last_commits_pattern
43
+ setup_a
44
+ arr = @findex.last_commits(@commit, /lib\/grit\/[^\/]*$/)
45
+ assert_equal 10, arr.size
46
+ assert_equal @commit, arr['lib/grit/commit.rb']
47
+ assert_equal nil, arr['lib/grit/actor.rb']
48
+ end
49
+
50
+ def test_last_commits_array
51
+ setup_a
52
+ arr = @findex.last_commits(@commit, ['lib/grit.rb', 'lib/grit/'])
53
+ assert_equal @commit, arr['lib/grit/']
54
+ end
55
+
56
+ end
data/test/test_git.rb ADDED
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class TestGit < Test::Unit::TestCase
4
+ def setup
5
+ @git = Git.new(File.join(File.dirname(__FILE__), *%w[..]))
6
+ end
7
+
8
+ def teardown
9
+ Grit.debug = false
10
+ end
11
+
12
+ def test_method_missing
13
+ assert_match(/^git version [\w\.]*$/, @git.version)
14
+ end
15
+
16
+ def test_logs_stderr
17
+ Grit.debug = true
18
+ Grit.stubs(:log)
19
+ Grit.expects(:log).with(includes("git: 'bad' is not a git-command"))
20
+ @git.bad
21
+ end
22
+
23
+ def testl_logs_stderr_when_skipping_timeout
24
+ Grit.debug = true
25
+ Grit.stubs(:log)
26
+ Grit.expects(:log).with(includes("git: 'bad' is not a git-command"))
27
+ @git.bad :timeout => false
28
+ end
29
+
30
+ def test_transform_options
31
+ assert_equal ["-s"], @git.transform_options({:s => true})
32
+ assert_equal ["-s '5'"], @git.transform_options({:s => 5})
33
+
34
+ assert_equal ["--max-count"], @git.transform_options({:max_count => true})
35
+ assert_equal ["--max-count='5'"], @git.transform_options({:max_count => 5})
36
+
37
+ assert_equal ["-s", "-t"], @git.transform_options({:s => true, :t => true}).sort
38
+ end
39
+
40
+ def test_uses_custom_sh_method
41
+ @git.expects(:sh)
42
+ @git.something
43
+ end
44
+
45
+ def test_can_skip_timeout
46
+ @git.expects(:wild_sh)
47
+ @git.something(:timeout => false)
48
+ end
49
+
50
+ def test_raises_if_too_many_bytes
51
+ @git.instance_variable_set(:@bytes_read, 6000000)
52
+ assert_raises Grit::Git::GitTimeout do
53
+ @git.version
54
+ end
55
+ end
56
+
57
+ def test_raises_on_slow_shell
58
+ Grit::Git.git_timeout = 0.0000001
59
+ assert_raises Grit::Git::GitTimeout do
60
+ @git.version
61
+ end
62
+ Grit::Git.git_timeout = 5.0
63
+ end
64
+
65
+ def test_it_really_shell_escapes_arguments_to_the_git_shell
66
+ @git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' foo --bar='bazz\\'er'")
67
+ @git.foo(:bar => "bazz'er")
68
+ @git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' bar -x 'quu\\'x'")
69
+ @git.bar(:x => "quu'x")
70
+ end
71
+
72
+ def test_it_shell_escapes_the_standalone_argument
73
+ @git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' foo 'bar\\'s'")
74
+ @git.foo({}, "bar's")
75
+
76
+ @git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' foo 'bar' '\\; echo \\'noooo\\''")
77
+ @git.foo({}, "bar", "; echo 'noooo'")
78
+ end
79
+
80
+ def test_piping_should_work_on_1_9
81
+ @git.expects(:sh).with("#{Git.git_binary} --git-dir='#{@git.git_dir}' archive 'master' | gzip")
82
+ @git.archive({}, "master", "| gzip")
83
+ end
84
+ end