git_statistics 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/commits_spec.rb CHANGED
@@ -2,237 +2,288 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
  include GitStatistics
3
3
 
4
4
  describe Commits do
5
- collector = Collector.new(false)
5
+ let(:verbose) {false}
6
+ let(:limit) {100}
7
+ let(:fresh) {true}
8
+ let(:pretty) {false}
9
+ let(:collector) {Collector.new(verbose, limit, fresh, pretty)}
10
+
11
+ let(:commits) {collector.commits}
12
+
13
+ let(:fixture_file) {"multiple_authors.json"}
14
+ let(:save_file) {collector.commits_path + "0.json"}
15
+ let(:email) {false}
16
+ let(:merge) {false}
17
+ let(:sort) {:commits}
18
+ let(:stats) do
19
+ setup_commits(commits, fixture_file, save_file, pretty)
20
+ commits.calculate_statistics(email, merge)
21
+ commits.author_top_n_type(sort)
22
+ end
23
+
24
+ describe "#flush_commits" do
25
+ let(:commits) {collector.commits.load(fixture(fixture_file))}
26
+
27
+ context "with commits exceeding limit" do
28
+ let(:limit) {2}
29
+ it do
30
+ commits.size.should == 3
31
+ commits.flush_commits
32
+ commits.size.should == 0
33
+ end
34
+ end
35
+
36
+ context "with commits equal to limit" do
37
+ let(:limit) {3}
38
+ it do
39
+ commits.size.should == 3
40
+ commits.flush_commits
41
+ commits.size.should == 0
42
+ end
43
+ end
44
+
45
+ context "with commits less than limit" do
46
+ let(:limit) {5}
47
+ it do
48
+ commits.size.should == 3
49
+ commits.flush_commits
50
+ commits.size.should == 3
51
+ end
52
+ end
53
+
54
+ context "with commits less than limit but forced" do
55
+ let(:limit) {5}
56
+ it do
57
+ commits.size.should == 3
58
+ commits.flush_commits(true)
59
+ commits.size.should == 0
60
+ end
61
+ end
62
+ end
6
63
 
7
64
  describe "#author_top_n_type" do
65
+ let(:sort) {:deletions}
66
+
8
67
  context "with valid data" do
9
- commits = Commits.new
10
- commits.load(fixture("multiple_authors.json"))
11
-
12
- commits.calculate_statistics(false, false)
13
- stats = commits.author_top_n_type(:deletions)
14
-
15
- # Check stats for first author
16
- author_1 = "John Smith"
17
- it {stats.has_key?(author_1).should be_true}
18
- it {stats[author_1][:commits].should == 1}
19
- it {stats[author_1][:additions].should == 64}
20
- it {stats[author_1][:deletions].should == 16}
21
-
22
- it {stats[author_1][:languages][:Ruby][:additions].should == 64}
23
- it {stats[author_1][:languages][:Ruby][:deletions].should == 16}
24
-
25
- # Check stats for second author
26
- author_2 = "Kevin Jalbert"
27
- it {stats.has_key?(author_2).should be_true}
28
- it {stats[author_2][:commits].should == 1}
29
- it {stats[author_2][:additions].should == 73}
30
- it {stats[author_2][:deletions].should == 0}
31
- it {stats[author_2][:create].should == 2}
32
-
33
- it {stats[author_2][:languages][:Markdown][:additions].should == 11}
34
- it {stats[author_2][:languages][:Markdown][:deletions].should == 0}
35
- it {stats[author_2][:languages][:Markdown][:create].should== 1}
36
- it {stats[author_2][:languages][:Ruby][:additions].should == 62}
37
- it {stats[author_2][:languages][:Ruby][:deletions].should == 0}
38
- it {stats[author_2][:languages][:Ruby][:create].should == 1}
68
+ context "on first author" do
69
+ author = "John Smith"
70
+ subject {stats[author]}
71
+ it {stats.has_key?(author).should be_true}
72
+ it {subject[:commits].should == 1}
73
+ it {subject[:deletions].should == 16}
74
+ it {subject[:additions].should == 64}
75
+
76
+ it {subject[:languages][:Ruby][:additions].should == 64}
77
+ it {subject[:languages][:Ruby][:deletions].should == 16}
78
+ end
79
+
80
+ context "on second author" do
81
+ author = "Kevin Jalbert"
82
+ subject {stats[author]}
83
+ it {stats.has_key?(author).should be_true}
84
+ it {subject[:commits].should == 1}
85
+ it {subject[:additions].should == 73}
86
+ it {subject[:deletions].should == 0}
87
+ it {subject[:create].should == 2}
88
+
89
+ it {subject[:languages][:Markdown][:additions].should == 11}
90
+ it {subject[:languages][:Markdown][:deletions].should == 0}
91
+ it {subject[:languages][:Markdown][:create].should== 1}
92
+ it {subject[:languages][:Ruby][:additions].should == 62}
93
+ it {subject[:languages][:Ruby][:deletions].should == 0}
94
+ it {subject[:languages][:Ruby][:create].should == 1}
95
+ end
39
96
  end
40
97
 
41
98
  context "with invalid type" do
42
- commits = Commits.new
43
- commits.load(fixture("multiple_authors.json"))
44
-
45
- commits.calculate_statistics(false, false)
46
- stats = commits.author_top_n_type(:wrong)
47
-
99
+ let(:sort) {:wrong}
48
100
  it {stats.should == nil}
49
101
  end
50
102
 
51
103
  context "with invalid data" do
52
- commits = Commits.new
53
-
54
- commits.calculate_statistics(false, false)
55
- stats = commits.author_top_n_type(:deletions)
56
-
104
+ let(:fixture_file) {nil}
57
105
  it {stats.should == nil}
58
106
  end
59
107
  end
60
108
 
61
109
  describe "#calculate_statistics" do
62
- context "with email" do
63
- commits = Commits.new
64
- commits.load(fixture("single_author_pretty.json"))
65
-
66
- commits.calculate_statistics(true, false)
67
- stats = commits.author_top_n_type(:commits)
110
+ let(:fixture_file) {"single_author_pretty.json"}
68
111
 
112
+ context "with email" do
113
+ let(:email) {true}
69
114
  author = "kevin.j.jalbert@gmail.com"
70
- it {stats.has_key?(author).should be_true}
71
- it {stats[author][:commits].should == 1}
72
- it {stats[author][:additions].should == 73}
73
- it {stats[author][:deletions].should == 0}
74
- it {stats[author][:create].should == 2}
115
+ subject {stats[author]}
75
116
 
76
- it {stats[author][:languages][:Markdown][:additions].should == 11}
77
- it {stats[author][:languages][:Markdown][:deletions].should == 0}
78
- it {stats[author][:languages][:Ruby][:additions].should == 62}
79
- it {stats[author][:languages][:Ruby][:deletions].should == 0}
80
- it {stats[author][:languages][:Ruby][:create].should == 1}
117
+ it {stats.has_key?(author).should be_true}
118
+ it {subject[:commits].should == 1}
119
+ it {subject[:additions].should == 73}
120
+ it {subject[:deletions].should == 0}
121
+ it {subject[:create].should == 2}
122
+
123
+ it {subject[:languages][:Markdown][:additions].should == 11}
124
+ it {subject[:languages][:Markdown][:deletions].should == 0}
125
+ it {subject[:languages][:Ruby][:additions].should == 62}
126
+ it {subject[:languages][:Ruby][:deletions].should == 0}
127
+ it {subject[:languages][:Ruby][:create].should == 1}
81
128
  end
82
129
 
83
130
  context "with merge" do
84
- commits = Commits.new
85
- commits.load(fixture("single_author_pretty.json"))
86
-
87
- commits.calculate_statistics(false, true)
88
- stats = commits.author_top_n_type(:commits)
89
-
131
+ let(:merge) {true}
90
132
  author = "Kevin Jalbert"
133
+ subject {stats[author]}
134
+
91
135
  it {stats.has_key?(author).should be_true}
92
- it {stats[author][:commits].should == 2}
93
- it {stats[author][:additions].should == 153}
94
- it {stats[author][:deletions].should == 5}
95
- it {stats[author][:create].should == 3}
96
- it {stats[author][:merges].should == 1}
97
-
98
- it {stats[author][:languages][:Markdown][:additions].should == 18}
99
- it {stats[author][:languages][:Markdown][:deletions].should == 1}
100
- it {stats[author][:languages][:Ruby][:additions].should == 135}
101
- it {stats[author][:languages][:Ruby][:deletions].should == 4}
102
- it {stats[author][:languages][:Ruby][:create].should == 2}
136
+ it {subject[:commits].should == 2}
137
+ it {subject[:additions].should == 153}
138
+ it {subject[:deletions].should == 5}
139
+ it {subject[:create].should == 3}
140
+ it {subject[:merges].should == 1}
141
+
142
+ it {subject[:languages][:Markdown][:additions].should == 18}
143
+ it {subject[:languages][:Markdown][:deletions].should == 1}
144
+ it {subject[:languages][:Ruby][:additions].should == 135}
145
+ it {subject[:languages][:Ruby][:deletions].should == 4}
146
+ it {subject[:languages][:Ruby][:create].should == 2}
103
147
  end
104
148
  end
105
149
 
106
150
  describe "#add_language_stats" do
107
- file = {:additions => 10,
108
- :deletions => 5}
109
151
 
110
152
  context "with file language" do
111
- commits = Commits.new
112
- data = Hash.new(0)
113
- data[:languages] = {}
153
+ let(:data) {
154
+ data = Hash.new(0)
155
+ data[:languages] = {}
114
156
 
115
- file[:language] = "Ruby"
157
+ file = {:additions => 10,
158
+ :deletions => 5}
116
159
 
117
- data = commits.add_language_stats(data, file)
160
+ file[:language] = "Ruby"
118
161
 
119
- it {data[:languages][:Ruby][:additions].should == 10}
120
- it {data[:languages][:Ruby][:deletions].should == 5}
162
+ data = commits.add_language_stats(data, file)
163
+ }
164
+
165
+ it {data[:languages][:Ruby][:additions].should == 10}
166
+ it {data[:languages][:Ruby][:deletions].should == 5}
121
167
  end
122
168
 
123
169
  context "with multiple files" do
124
- commits = Commits.new
125
- data = Hash.new(0)
126
- data[:languages] = {}
127
-
128
- # First file is "Ruby"
129
- file[:language] = "Ruby"
130
- data = commits.add_language_stats(data, file)
131
-
132
- # Second file is "Java"
133
- file[:language] = "Java"
134
- data = commits.add_language_stats(data, file)
135
-
136
- # Third file is "Ruby"
137
- file[:language] = "Ruby"
138
- data = commits.add_language_stats(data, file)
139
-
140
- it {data[:languages][:Ruby][:additions].should == 20}
141
- it {data[:languages][:Ruby][:deletions].should == 10}
142
- it {data[:languages][:Java][:additions].should == 10}
143
- it {data[:languages][:Java][:deletions].should == 5}
170
+ let(:data) {
171
+ data = Hash.new(0)
172
+ data[:languages] = {}
173
+
174
+ file = {:additions => 10,
175
+ :deletions => 5}
176
+
177
+ # First file is "Ruby"
178
+ file[:language] = "Ruby"
179
+ data = commits.add_language_stats(data, file)
180
+
181
+ # Second file is "Java"
182
+ file[:language] = "Java"
183
+ data = commits.add_language_stats(data, file)
184
+
185
+ # Third file is "Ruby"
186
+ file[:language] = "Ruby"
187
+ data = commits.add_language_stats(data, file)
188
+ }
189
+
190
+ it {data[:languages][:Ruby][:additions].should == 20}
191
+ it {data[:languages][:Ruby][:deletions].should == 10}
192
+ it {data[:languages][:Java][:additions].should == 10}
193
+ it {data[:languages][:Java][:deletions].should == 5}
144
194
  end
145
195
  end
146
196
 
147
197
  describe "#add_commit_stats" do
148
- commit = {:additions => 10,
149
- :deletions => 5,
150
- :merge => false}
151
-
152
198
  context "with valid commit" do
153
- commits = Commits.new
154
- data = Hash.new(0)
155
-
156
- data = commits.add_commit_stats(data, commit)
157
-
158
- it {data[:commits].should == 1}
159
- it {data[:additions].should == 10}
160
- it {data[:deletions].should == 5}
161
- it {data[:merges].should == 0}
199
+ let(:data) {
200
+ commit = {:additions => 10,
201
+ :deletions => 5,
202
+ :merge => false}
203
+
204
+ data = Hash.new(0)
205
+ data = commits.add_commit_stats(data, commit)
206
+ }
207
+
208
+ it {data[:commits].should == 1}
209
+ it {data[:additions].should == 10}
210
+ it {data[:deletions].should == 5}
211
+ it {data[:merges].should == 0}
162
212
  end
163
213
 
164
- context "with multiple commits" do
165
- commits = Commits.new
166
- data = Hash.new(0)
214
+ context "with multiple commits (one merge commit)" do
215
+ let(:data) {
216
+ commit = {:additions => 10,
217
+ :deletions => 5,
218
+ :merge => false}
167
219
 
168
- data = commits.add_commit_stats(data, commit)
220
+ data = Hash.new(0)
221
+ data = commits.add_commit_stats(data, commit)
169
222
 
170
- # Second commit has merge status
171
- commit[:merge] = true
223
+ # Second commit has merge status
224
+ commit[:merge] = true
225
+ data = commits.add_commit_stats(data, commit)
226
+ }
172
227
 
173
- data = commits.add_commit_stats(data, commit)
174
-
175
- it {data[:commits].should == 2}
176
- it {data[:additions].should == 20}
177
- it {data[:deletions].should == 10}
178
- it {data[:merges].should == 1}
228
+ it {data[:commits].should == 2}
229
+ it {data[:additions].should == 20}
230
+ it {data[:deletions].should == 10}
231
+ it {data[:merges].should == 1}
179
232
  end
180
233
 
181
234
  context "with commit that has file status changes" do
182
- commits = Commits.new
183
- data = Hash.new(0)
184
- commit[:create] = 1
185
- commit[:delete] = 2
186
- commit[:rename] = 3
187
- commit[:copy] = 4
188
-
189
- data = commits.add_commit_stats(data, commit)
190
-
191
- it {data[:commits].should == 1}
192
- it {data[:additions].should == 10}
193
- it {data[:deletions].should == 5}
194
- it {data[:create].should == 1}
195
- it {data[:delete].should == 2}
196
- it {data[:rename].should == 3}
197
- it {data[:copy].should == 4}
198
- end
199
-
200
- context "with merge commit" do
201
- commits = Commits.new
202
- data = Hash.new(0)
203
- commit[:merge] = true
204
-
205
- data = commits.add_commit_stats(data, commit)
206
-
207
- it {data[:commits].should == 1}
208
- it {data[:additions].should == 10}
209
- it {data[:deletions].should == 5}
210
- it {data[:merges].should == 1}
235
+ let(:data) {
236
+ commit = {:additions => 10,
237
+ :deletions => 5,
238
+ :create => 1,
239
+ :delete => 2,
240
+ :rename => 3,
241
+ :copy => 4,
242
+ :merge => false}
243
+
244
+ data = Hash.new(0)
245
+ data = commits.add_commit_stats(data, commit)
246
+ }
247
+
248
+ it {data[:commits].should == 1}
249
+ it {data[:additions].should == 10}
250
+ it {data[:deletions].should == 5}
251
+ it {data[:create].should == 1}
252
+ it {data[:delete].should == 2}
253
+ it {data[:rename].should == 3}
254
+ it {data[:copy].should == 4}
211
255
  end
212
256
  end
213
257
 
214
258
  describe "#save and #load" do
215
259
  context "with pretty" do
216
- commits = Commits.new
217
- commits.load(fixture("single_author_pretty.json"))
218
- commits.save("tmp.json", true)
260
+ let(:fixture_file) {"single_author_pretty.json"}
261
+ let(:pretty) {true}
262
+
263
+ it do
264
+ commits.load(fixture(fixture_file))
265
+ commits.save("tmp.json", pretty)
219
266
 
220
- same = FileUtils.compare_file("tmp.json", fixture("single_author_pretty.json"))
221
- FileUtils.remove_file("tmp.json")
267
+ same = FileUtils.compare_file("tmp.json", fixture(fixture_file))
268
+ FileUtils.remove_file("tmp.json")
222
269
 
223
- it {same.should be_true}
270
+ same.should be_true
271
+ end
224
272
  end
225
273
 
226
274
  context "with no pretty" do
227
- commits = Commits.new
228
- commits.load(fixture("multiple_authors.json"))
229
- commits.save("tmp.json", false)
275
+ let(:fixture_file) {"multiple_authors.json"}
276
+ let(:pretty) {false}
230
277
 
231
- same = FileUtils.compare_file("tmp.json", fixture("multiple_authors.json"))
232
- FileUtils.remove_file("tmp.json")
278
+ it do
279
+ commits.load(fixture(fixture_file))
280
+ commits.save("tmp.json", pretty)
233
281
 
234
- it {same.should be_true}
282
+ same = FileUtils.compare_file("tmp.json", fixture(fixture_file))
283
+ FileUtils.remove_file("tmp.json")
284
+
285
+ same.should be_true
286
+ end
235
287
  end
236
288
  end
237
-
238
289
  end