document_generator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module DocumentGenerator
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ module DocumentGenerator
4
+ describe CLI do
5
+ describe '.start' do
6
+ context 'when a url is supplied' do
7
+ let(:args) { %w(--url http://some.repo.url) }
8
+ let(:repository) { instance_double('Repository') }
9
+
10
+ it 'sends the :generate message to a new Repository' do
11
+ expect(Repository).to receive(:new).with('http://some.repo.url').and_return repository
12
+ expect(repository).to receive(:generate)
13
+
14
+ CLI.start(args)
15
+ end
16
+
17
+ end
18
+ context 'when no args are supplied' do
19
+ let(:args) { [] }
20
+
21
+ it 'raises a MissingArgument error' do
22
+ expect { CLI.start(args) }.to raise_error(OptionParser::MissingArgument)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,243 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe DocumentGenerator::Commit do
6
+ let(:commit) { described_class.new(base_url, git_commit) }
7
+ let(:git_commit) { double(message: message, sha: sha) }
8
+ let(:base_url) { 'http://github.com/stevenhallen/document_generator' }
9
+ let(:message) { 'Some commit message' }
10
+ let(:sha) { 'da39a3ee5e6b4b0d3255bfef95601890afd80709' }
11
+
12
+ describe '#diff_files' do
13
+ let(:git_commit) { double(parent: parent) }
14
+
15
+ context 'when the git_commit has a parent' do
16
+ let(:parent) { double().as_null_object }
17
+ let(:git_diff) { %w(1 2 3) }
18
+ let(:wrapped_git_diff) do
19
+ git_diff.map do |git_diff_file|
20
+ DocumentGenerator::DiffFile.new(git_diff_file)
21
+ end
22
+ end
23
+
24
+ before { parent.should_receive(:diff).with(git_commit).and_return git_diff }
25
+
26
+ it 'is an array of wrapped diff files' do
27
+ expect(commit.diff_files).to be_an(Array)
28
+ end
29
+
30
+ it 'has three items' do
31
+ expect(commit.diff_files).to have(3).items
32
+ end
33
+
34
+ it 'is an array of DiffFiles wrapping each git diff file' do
35
+ commit.diff_files.zip(wrapped_git_diff).each do |actual, expected|
36
+ expect(actual).to be_a(DocumentGenerator::DiffFile)
37
+ expect(actual.git_diff_file).to eq expected.git_diff_file
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'when the git_commit does not have a parent' do
43
+ let(:parent) {nil}
44
+
45
+ it 'is empty' do
46
+ expect(commit.diff_files).to be_empty
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#relative_filename' do
52
+ it 'is the filename' do
53
+ expect(commit.relative_filename).to eq 'some-commit-message.md'
54
+ end
55
+ end
56
+
57
+ describe '#create' do
58
+ let(:parent) { double().as_null_object }
59
+ let(:git_commit) { double(parent: parent, message: message, sha: sha) }
60
+ let(:content) { File.open(commit.relative_filename).read }
61
+ let(:sha) { 'da39a3ee5e6b4b0d3255bfef95601890afd80709' }
62
+
63
+ context 'when the commit is the first commit (i.e., it has no parent)' do
64
+ let(:parent) { nil }
65
+ let(:message) { 'first commit' }
66
+ let(:expected_content) do
67
+ <<-EOF
68
+ ---
69
+ layout: default
70
+ title: first commit
71
+ ---
72
+
73
+ <h1 id="main">first commit</h1>
74
+
75
+ ### Additional Resources
76
+
77
+ * [Changes in this step in `diff` format](http://github.com/stevenhallen/commit/da39a3ee5e6b4b0d3255bfef95601890afd80709)
78
+
79
+ EOF
80
+
81
+ end
82
+
83
+ it 'writes the expected content to the file' do
84
+ commit.create
85
+ expect(content).to eq expected_content
86
+ end
87
+ end
88
+
89
+ context 'when the commit has a diff from its parent' do
90
+ before do
91
+ expect(parent).to receive(:diff).with(git_commit).and_return git_diff_files
92
+ expect(DocumentGenerator::DiffFile).to receive(:new).and_return diff_file
93
+ end
94
+
95
+ let(:message) { 'some commit message' }
96
+ let(:diff_file) { double(content: 'the diff_file content').as_null_object }
97
+ let(:git_diff_file) { double().as_null_object }
98
+ let(:git_diff_files) { [git_diff_file] }
99
+ let(:expected_content) do
100
+ <<-EOF
101
+ ---
102
+ layout: default
103
+ title: some commit message
104
+ ---
105
+
106
+ <h1 id="main">some commit message</h1>
107
+ the diff_file content
108
+ ### Additional Resources
109
+
110
+ * [Changes in this step in `diff` format](http://github.com/stevenhallen/commit/da39a3ee5e6b4b0d3255bfef95601890afd80709)
111
+
112
+ EOF
113
+ end
114
+
115
+ it 'writes the expected content to the file' do
116
+ commit.create
117
+ expect(content).to eq expected_content
118
+ end
119
+ end
120
+ end
121
+
122
+ describe '#header' do
123
+ context 'when the commit message has one line' do
124
+ let(:message) {'This is one line'}
125
+ let(:expected_header) do
126
+ <<-EXPECTED
127
+ ---
128
+ layout: default
129
+ title: This is one line
130
+ ---
131
+
132
+ <h1 id="main">This is one line</h1>
133
+ EXPECTED
134
+ end
135
+
136
+ it 'uses the entire commit message' do
137
+ expect(commit.header).to eq expected_header
138
+ end
139
+ end
140
+
141
+ context 'when the commit message has more than one line' do
142
+ let(:message) {"This is one line\nThis is a second line."}
143
+ let(:expected_header) do
144
+ <<-EXPECTED
145
+ ---
146
+ layout: default
147
+ title: This is one line
148
+ ---
149
+
150
+ <h1 id="main">This is one line</h1>
151
+ EXPECTED
152
+ end
153
+
154
+ it 'uses the first line of the commit message' do
155
+ expect(commit.header).to eq expected_header
156
+ end
157
+ end
158
+ end
159
+
160
+ describe '#additional' do
161
+ let(:expected_additional) do
162
+ <<-EXPECTED_ADDITIONAL
163
+
164
+ ### Additional Resources
165
+
166
+ * [Changes in this step in `diff` format](http://github.com/stevenhallen/commit/da39a3ee5e6b4b0d3255bfef95601890afd80709)
167
+
168
+ EXPECTED_ADDITIONAL
169
+ end
170
+
171
+ it 'includes a link to the commit' do
172
+ expect(commit.additional).to eq expected_additional
173
+ end
174
+ end
175
+
176
+ describe '#details_of_commit_message' do
177
+ context 'when the commit message is one line' do
178
+ let(:message) { 'One line' }
179
+
180
+ it 'is nil' do
181
+ expect(commit.details_of_commit_message).to be_nil
182
+ end
183
+ end
184
+
185
+ context 'when the commit message is multiple lines' do
186
+ let(:message) { "One line\nTwo lines\nThree and more" }
187
+
188
+ it 'is not empty' do
189
+ expect(commit.details_of_commit_message).to eq "Two lines\nThree and more"
190
+ end
191
+ end
192
+ end
193
+
194
+ describe '#basename_prefix' do
195
+ context 'when the commit message contains non word characters' do
196
+ let(:message) { 'Message with non->word characters!'}
197
+
198
+ it 'strips non word characters' do
199
+ expect(commit.basename_prefix).to eq 'message-with-non-word-characters'
200
+ end
201
+ end
202
+
203
+ context 'when the commit message contains an underscore' do
204
+ let(:message) { 'Message with an_underscore!'}
205
+
206
+ it 'replaces underscores with dashes' do
207
+ expect(commit.basename_prefix).to eq 'message-with-an-underscore'
208
+ end
209
+ end
210
+
211
+ context 'when the commit message contains non normalized space' do
212
+ let(:message) { 'Message with non normalized space'}
213
+
214
+ it 'normalizes whitespace' do
215
+ expect(commit.basename_prefix).to eq 'message-with-non-normalized-space'
216
+ end
217
+ end
218
+
219
+ context 'when the commit message contains unicode' do
220
+ let(:message) { 'Message with unicode‽'}
221
+
222
+ it 'removes unicode characters' do
223
+ expect(commit.basename_prefix).to eq 'message-with-unicode'
224
+ end
225
+ end
226
+ end
227
+
228
+ describe '#filename' do
229
+ let(:message) { 'Simple commit message' }
230
+
231
+ it 'appends .md to the basename_prefix' do
232
+ expect(commit.filename).to eq 'simple-commit-message.md'
233
+ end
234
+ end
235
+
236
+ describe '#link' do
237
+ let(:message) { 'Simple commit message' }
238
+
239
+ it 'links to the filename' do
240
+ expect(commit.link).to eq "<li><a href='simple-commit-message.html'>Simple commit message</a></li>"
241
+ end
242
+ end
243
+ end
@@ -0,0 +1,438 @@
1
+ require 'spec_helper'
2
+
3
+ describe DocumentGenerator::DiffFile do
4
+ let(:diff_file) { described_class.new(git_diff_file) }
5
+ let(:git_diff_file) { double(type: type, patch: patch, path: file_name) }
6
+
7
+ context 'when the git diff represents a new file' do
8
+ let(:type) { 'new' }
9
+ let(:file_name) { '/spec/support/capybara.rb' }
10
+
11
+ let(:patch) do
12
+ <<-PATCH
13
+ diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb
14
+ new file mode 100644
15
+ index 0000000..12cefac
16
+ --- /dev/null
17
+ +++ b/spec/support/capybara.rb
18
+ @@ -0,0 +1 @@
19
+ +Capybara.javascript_driver = :webkit
20
+
21
+ PATCH
22
+ end
23
+
24
+ let(:ending) do
25
+ <<-EXPECTED
26
+ Capybara.javascript_driver = :webkit
27
+ EXPECTED
28
+ end
29
+
30
+ let(:expected_content) do
31
+ <<-EXPECTED_CONTENT
32
+ Create file `/spec/support/capybara.rb`
33
+
34
+ Add
35
+ <pre><code> Capybara.javascript_driver = :webkit</code></pre>
36
+
37
+
38
+ EXPECTED_CONTENT
39
+ end
40
+
41
+ describe '#patch_heading' do
42
+ it 'has correct type and path' do
43
+ expect(diff_file.patch_heading).to eq "Create file `#{file_name}`"
44
+ end
45
+ end
46
+
47
+ describe '#content' do
48
+ it 'contains the expected content' do
49
+ expect(diff_file.content).to eq expected_content
50
+ end
51
+ end
52
+
53
+ describe '#ending_code' do
54
+ it 'contains the ending_code' do
55
+ expect(diff_file.ending_code).to eq ending.rstrip
56
+ end
57
+ end
58
+
59
+ describe '#action_type' do
60
+ it 'outputs correct phrase for action_type' do
61
+ expect(diff_file.action_type).to eq "Create file"
62
+ end
63
+ end
64
+
65
+ describe '#markdown_outputs' do
66
+ it 'has correct markdown outputs' do
67
+ expect(diff_file.markdown_outputs.first.description).to eq "Add"
68
+ expect(diff_file.markdown_outputs.first.escaped_content).to eq CGI.escapeHTML(" Capybara.javascript_driver = :webkit")
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'when the git diff represents a changed file' do
74
+ let(:type) { 'modified' }
75
+ let(:file_name) { '/app/views/posts/new.html.erb' }
76
+
77
+ let(:patch) do
78
+ <<-PATCH
79
+ diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb
80
+ index 58053b0..ca57827 100644
81
+ --- a/app/views/posts/new.html.erb
82
+ +++ b/app/views/posts/new.html.erb
83
+ @@ -1,13 +1,13 @@
84
+ <h1>New Post</h1>
85
+
86
+ -<%= form_for :post do |f| %>
87
+ +<%= form_for :post, url: posts_path do |f| %>
88
+ <p>
89
+ <%= f.label :title %><br>
90
+ <%= f.text_field :title %>
91
+ </p>
92
+
93
+ <p>
94
+ <%= f.label :text %><br>
95
+ <%= f.text_area :text %>
96
+ </p>
97
+ PATCH
98
+ end
99
+
100
+ let(:ending) do
101
+ <<-EXPECTED
102
+ <h1>New Post</h1>
103
+
104
+ <%= form_for :post, url: posts_path do |f| %>
105
+ <p>
106
+ <%= f.label :title %><br>
107
+ <%= f.text_field :title %>
108
+ </p>
109
+
110
+ <p>
111
+ <%= f.label :text %><br>
112
+ <%= f.text_area :text %>
113
+ </p>
114
+ EXPECTED
115
+ end
116
+
117
+ let(:expected_content) do
118
+ <<-EXPECTED_CONTENT
119
+ Update file `/app/views/posts/new.html.erb`
120
+
121
+ Change
122
+ <%= form_for :post do |f| %>
123
+
124
+
125
+ To
126
+ <%= form_for :post, url: posts_path do |f| %>
127
+
128
+
129
+ Becomes
130
+ <h1>New Post</h1>
131
+
132
+ <%= form_for :post, url: posts_path do |f| %>
133
+ <p>
134
+ <%= f.label :title %><br>
135
+ <%= f.text_field :title %>
136
+ </p>
137
+
138
+ <p>
139
+ <%= f.label :text %><br>
140
+ <%= f.text_area :text %>
141
+ </p>
142
+
143
+
144
+ EXPECTED_CONTENT
145
+ end
146
+
147
+
148
+ describe '#patch_heading' do
149
+ it 'has correct type and path' do
150
+ expect(diff_file.patch_heading).to eq "Update file `#{file_name}`"
151
+ end
152
+ end
153
+
154
+ describe '#ending_code' do
155
+ it 'contains the ending_code' do
156
+ expect(diff_file.ending_code).to eq DocumentGenerator::Output.no_really_escape(CGI.escapeHTML(ending.strip))
157
+ end
158
+ end
159
+
160
+ describe '#action_type' do
161
+ it 'outputs correct phrase for action_type' do
162
+ expect(diff_file.action_type).to eq "Update file"
163
+ end
164
+ end
165
+
166
+ describe '#markdown_outputs' do
167
+ it 'has correct markdown outputs' do
168
+ expect(diff_file.markdown_outputs.first.description).to eq "Change"
169
+ expect(diff_file.markdown_outputs.first.escaped_content).to eq CGI.escapeHTML(" <%= form_for :post do |f| %>")
170
+
171
+ expect(diff_file.markdown_outputs.last.description).to eq "To"
172
+ expect(diff_file.markdown_outputs.last.escaped_content).to eq CGI.escapeHTML(" <%= form_for :post, url: posts_path do |f| %>")
173
+ end
174
+ end
175
+
176
+ describe '#patch_heading' do
177
+ let(:patch) { double(path: 'foo') }
178
+ it 'has correct type and path' do
179
+ expect(diff_file.patch_heading).to eq "Update file `#{file_name}`"
180
+ end
181
+ end
182
+ end
183
+
184
+ context 'when the git diff file has two changes' do
185
+ let(:type) { 'modified' }
186
+ let(:file_name) { '/spec/controllers/posts_controller_spec.rb' }
187
+
188
+ let(:patch) do
189
+ <<-PATCH
190
+ diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb
191
+ index f531cdc..1982a3e 100644
192
+ --- a/spec/controllers/posts_controller_spec.rb
193
+ +++ b/spec/controllers/posts_controller_spec.rb
194
+ @@ -2,9 +2,9 @@ require 'spec_helper'
195
+ describe PostsController do
196
+
197
+ - describe GET new do
198
+ + describe GET new do
199
+ it returns http success do
200
+ - get new
201
+ + get new
202
+ response.should be_success
203
+ end
204
+ end
205
+ PATCH
206
+ end
207
+
208
+ let(:ending) do
209
+ <<-ENDING
210
+ describe PostsController do
211
+
212
+ describe GET new do
213
+ it returns http success do
214
+ get new
215
+ response.should be_success
216
+ end
217
+ end
218
+ ENDING
219
+ end
220
+
221
+ let(:expected_content) do
222
+ <<-EXPECTED_CONTENT
223
+ Update file `/spec/controllers/posts_controller_spec.rb`
224
+
225
+ Change
226
+ <pre><code> describe GET new do</code></pre>
227
+
228
+
229
+ To
230
+ <pre><code> describe GET new do</code></pre>
231
+
232
+
233
+ Change
234
+ <pre><code> get new</code></pre>
235
+
236
+
237
+ To
238
+ <pre><code> get new</code></pre>
239
+
240
+
241
+ Becomes
242
+ <pre><code>describe PostsController do
243
+ &nbsp;
244
+ describe GET new do
245
+ it returns http success do
246
+ get new
247
+ response.should be_success
248
+ end
249
+ end
250
+ </code></pre>
251
+
252
+
253
+ EXPECTED_CONTENT
254
+ end
255
+
256
+ describe '#content' do
257
+ it 'includes the Becomes section' do
258
+ expect(diff_file.content).to eq expected_content
259
+ end
260
+ end
261
+
262
+ describe '#ending_code' do
263
+ it 'contains the ending_code' do
264
+ expect(diff_file.ending_code).to eq DocumentGenerator::Output.no_really_escape(CGI.escapeHTML(ending.strip))
265
+ end
266
+ end
267
+
268
+ describe '#action_type' do
269
+ it 'outputs correct phrase for action_type' do
270
+ expect(diff_file.action_type).to eq "Update file"
271
+ end
272
+ end
273
+
274
+ describe '#markdown_outputs' do
275
+ it 'has correct markdown outputs' do
276
+ expect(diff_file.markdown_outputs.first.description).to eq "Change"
277
+ expect(diff_file.markdown_outputs.first.content).to eq [" describe GET new do"]
278
+
279
+ expect(diff_file.markdown_outputs[1].description).to eq "To"
280
+ expect(diff_file.markdown_outputs[1].content).to eq [" describe GET new do"]
281
+
282
+ expect(diff_file.markdown_outputs[2].description).to eq "Change"
283
+ expect(diff_file.markdown_outputs[2].content).to eq [" get new"]
284
+
285
+ expect(diff_file.markdown_outputs[3].description).to eq "To"
286
+ expect(diff_file.markdown_outputs[3].content).to eq [" get new"]
287
+ end
288
+ end
289
+ end
290
+
291
+ context 'when the git diff file has just a remove' do
292
+ let(:type) { 'modified' }
293
+ let(:file_name) { '/config/routes.rb' }
294
+
295
+ let(:patch) do
296
+ <<-PATCH
297
+ diff --git a/config/routes.rb b/config/routes.rb
298
+ index d81896f..9595f17 100644
299
+ --- a/config/routes.rb
300
+ +++ b/config/routes.rb
301
+ @@ -1,10 +1,9 @@
302
+ Blog::Application.routes.draw do
303
+ - get "welcome/index"
304
+ # The priority is based upon order of creation: first created -> highest priority.
305
+ # See how all your routes lay out with "rake routes".
306
+ PATCH
307
+ end
308
+
309
+ let(:ending) do
310
+ <<-EXPECTED
311
+ Blog::Application.routes.draw do
312
+ # The priority is based upon order of creation: first created -> highest priority.
313
+ # See how all your routes lay out with "rake routes".
314
+ EXPECTED
315
+ end
316
+
317
+ describe '#ending_code' do
318
+ it 'contains the ending_code' do
319
+ expect(diff_file.ending_code).to eq CGI.escapeHTML(ending.strip)
320
+ end
321
+ end
322
+
323
+ describe '#action_type' do
324
+ it 'outputs correct phrase for action_type' do
325
+ expect(diff_file.action_type).to eq "Update file"
326
+ end
327
+ end
328
+
329
+ describe '#markdown_outputs' do
330
+ it 'has correct markdown outputs' do
331
+ expect(diff_file.markdown_outputs.first.description).to eq "Remove"
332
+ expect(diff_file.markdown_outputs.first.content).to eq [" get \"welcome/index\""]
333
+ end
334
+ end
335
+ end
336
+
337
+ context 'when the patch contains one line' do
338
+ let(:type) { 'new' }
339
+ let(:file_name) { '/spec/support/capybara.rb' }
340
+
341
+ let(:patch) do
342
+ <<-PATCH
343
+ diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb
344
+ new file mode 100644
345
+ index 0000000..12cefac
346
+ --- /dev/null
347
+ +++ b/spec/support/capybara.rb
348
+ @@ -0,0 +1 @@
349
+ +Capybara.javascript_driver = :webkit
350
+ PATCH
351
+ end
352
+
353
+ let(:expected_content) do
354
+ <<-EXPECTED_CONTENT
355
+ Create file `/spec/support/capybara.rb`
356
+
357
+ Add
358
+ <pre><code> Capybara.javascript_driver = :webkit</code></pre>
359
+
360
+
361
+ EXPECTED_CONTENT
362
+ end
363
+
364
+ describe '#content' do
365
+ it 'does not contain Becomes content' do
366
+ expect(diff_file.content).to eq expected_content
367
+ end
368
+ end
369
+ end
370
+
371
+ context 'when the patch contains multiple new lines' do
372
+ let(:type) { 'new' }
373
+ let(:file_name) { '/spec/support/capybara.rb' }
374
+
375
+ let(:patch) do
376
+ <<-PATCH
377
+ diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb
378
+ new file mode 100644
379
+ index 0000000..12cefac
380
+ --- /dev/null
381
+ +++ b/spec/support/capybara.rb
382
+ @@ -0,0 +1 @@
383
+ +this
384
+ +that
385
+ +other
386
+ PATCH
387
+ end
388
+
389
+ let(:expected_content) do
390
+ <<-EXPECTED_CONTENT
391
+ Create file `/spec/support/capybara.rb`
392
+
393
+ Add
394
+
395
+ Capybara.javascript_driver = :webkit
396
+
397
+
398
+ EXPECTED_CONTENT
399
+ end
400
+
401
+ describe '#markdown_outputs' do
402
+ it 'has correct markdown outputs' do
403
+ expect(diff_file.markdown_outputs.size).to eq 1
404
+ end
405
+ end
406
+ end
407
+
408
+ context 'when the patch type is deleted' do
409
+ let(:type) { 'deleted' }
410
+ let(:file_name) { '/app/helpers/welcome_helper.rb' }
411
+
412
+ let(:patch) do
413
+ <<-PATCH
414
+ diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb
415
+ deleted file mode 100644
416
+ index eeead45..0000000
417
+ --- a/app/helpers/welcome_helper.rb
418
+ +++ /dev/null
419
+ @@ -1,2 +0,0 @@
420
+ -module WelcomeHelper
421
+ -end
422
+ PATCH
423
+ end
424
+
425
+ let(:expected_content) do
426
+ <<-EXPECTED_CONTENT
427
+ Remove file `/app/helpers/welcome_helper.rb`
428
+
429
+ EXPECTED_CONTENT
430
+ end
431
+
432
+ describe '#content' do
433
+ it 'has correct markdown outputs' do
434
+ expect(diff_file.content).to eq expected_content
435
+ end
436
+ end
437
+ end
438
+ end