@@ -11,41 +11,65 @@ def mock_file(stubs = {})
11
11
@mock_file ||= double ( File , stubs )
12
12
end
13
13
14
+ def magic_comments_list_each
15
+ [
16
+ '# encoding: UTF-8' ,
17
+ '# coding: UTF-8' ,
18
+ '# -*- coding: UTF-8 -*-' ,
19
+ '#encoding: utf-8' ,
20
+ '# encoding: utf-8' ,
21
+ '# -*- encoding : utf-8 -*-' ,
22
+ "# encoding: utf-8\n # frozen_string_literal: true" ,
23
+ "# frozen_string_literal: true\n # encoding: utf-8" ,
24
+ '# frozen_string_literal: true' ,
25
+ '#frozen_string_literal: false' ,
26
+ '# -*- frozen_string_literal : true -*-'
27
+ ] . each { |magic_comment | yield magic_comment }
28
+ end
29
+
14
30
it 'should check if routes.rb exists' do
15
31
expect ( File ) . to receive ( :exists? ) . with ( ROUTE_FILE ) . and_return ( false )
16
32
expect ( AnnotateRoutes ) . to receive ( :puts ) . with ( "Can't find routes.rb" )
17
33
AnnotateRoutes . do_annotations
18
34
end
19
35
20
36
describe 'Annotate#example' do
21
- before ( :each ) do
22
- expect ( File ) . to receive ( :exists? ) . with ( ROUTE_FILE ) . and_return ( true )
23
-
24
- expect ( File ) . to receive ( :read ) . with ( ROUTE_FILE ) . and_return ( "" )
25
- expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' ) . and_return ( " Prefix Verb URI Pattern Controller#Action
37
+ let ( :rake_routes_content ) do
38
+ " Prefix Verb URI Pattern Controller#Action
26
39
myaction1 GET /url1(.:format) mycontroller1#action
27
40
myaction2 POST /url2(.:format) mycontroller2#action
28
- myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n " )
41
+ myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n "
42
+ end
29
43
30
- expect ( AnnotateRoutes ) . to receive ( :puts ) . with ( ANNOTATION_ADDED )
44
+ before ( :each ) do
45
+ expect ( File ) . to receive ( :exists? ) . with ( ROUTE_FILE ) . and_return ( true ) . at_least ( :once )
46
+
47
+ expect ( File ) . to receive ( :read ) . with ( ROUTE_FILE ) . and_return ( "" ) . at_least ( :once )
48
+
49
+ expect ( AnnotateRoutes ) . to receive ( :puts ) . with ( ANNOTATION_ADDED ) . at_least ( :once )
31
50
end
32
51
33
- it 'annotate normal' do
34
- expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
35
- expect ( @mock_file ) . to receive ( :puts ) . with ( "
52
+ context 'without magic comments' do
53
+ before ( :each ) do
54
+ expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' ) . and_return ( rake_routes_content )
55
+ end
56
+
57
+ it 'annotate normal' do
58
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
59
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "
36
60
# == Route Map
37
61
#
38
62
# Prefix Verb URI Pattern Controller#Action
39
63
# myaction1 GET /url1(.:format) mycontroller1#action
40
64
# myaction2 POST /url2(.:format) mycontroller2#action
41
65
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n " )
42
66
43
- AnnotateRoutes . do_annotations
44
- end
67
+ AnnotateRoutes . do_annotations
68
+ end
45
69
46
- it 'annotate markdown' do
47
- expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
48
- expect ( @mock_file ) . to receive ( :puts ) . with ( "
70
+ it 'annotate markdown' do
71
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
72
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "
49
73
# ## Route Map
50
74
#
51
75
# Prefix | Verb | URI Pattern | Controller#Action
@@ -54,12 +78,72 @@ def mock_file(stubs = {})
54
78
# myaction2 | POST | /url2(.:format) | mycontroller2#action
55
79
# myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action\n " )
56
80
57
- AnnotateRoutes . do_annotations ( format_markdown : true )
81
+ AnnotateRoutes . do_annotations ( format_markdown : true )
82
+ end
83
+
84
+ it 'wraps annotation if wrapper is specified' do
85
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
86
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "
87
+ # START
88
+ # == Route Map
89
+ #
90
+ # Prefix Verb URI Pattern Controller#Action
91
+ # myaction1 GET /url1(.:format) mycontroller1#action
92
+ # myaction2 POST /url2(.:format) mycontroller2#action
93
+ # myaction3 DELETE|GET /url3(.:format) mycontroller3#action
94
+ # END\n " )
95
+
96
+ AnnotateRoutes . do_annotations ( wrapper_open : 'START' , wrapper_close : 'END' )
97
+ end
58
98
end
59
99
60
- it 'wraps annotation if wrapper is specified' do
61
- expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
62
- expect ( @mock_file ) . to receive ( :puts ) . with ( "
100
+ context 'file with magic comments' do
101
+ it 'should not remove magic comments' do
102
+ magic_comments_list_each do |magic_comment |
103
+ expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' )
104
+ . and_return ( "#{ magic_comment } \n #{ rake_routes_content } " )
105
+
106
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
107
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "
108
+ #{ magic_comment }
109
+ # == Route Map
110
+ #
111
+ # Prefix Verb URI Pattern Controller#Action
112
+ # myaction1 GET /url1(.:format) mycontroller1#action
113
+ # myaction2 POST /url2(.:format) mycontroller2#action
114
+ # myaction3 DELETE|GET /url3(.:format) mycontroller3#action\n " )
115
+
116
+ AnnotateRoutes . do_annotations
117
+ end
118
+ end
119
+
120
+ it 'annotate markdown' do
121
+ magic_comments_list_each do |magic_comment |
122
+ expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' )
123
+ . and_return ( "#{ magic_comment } \n #{ rake_routes_content } " )
124
+
125
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
126
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "
127
+ #{ magic_comment }
128
+ # ## Route Map
129
+ #
130
+ # Prefix | Verb | URI Pattern | Controller#Action
131
+ # --------- | ---------- | --------------- | --------------------
132
+ # myaction1 | GET | /url1(.:format) | mycontroller1#action
133
+ # myaction2 | POST | /url2(.:format) | mycontroller2#action
134
+ # myaction3 | DELETE-GET | /url3(.:format) | mycontroller3#action\n " )
135
+
136
+ AnnotateRoutes . do_annotations ( format_markdown : true )
137
+ end
138
+ end
139
+
140
+ it 'wraps annotation if wrapper is specified' do
141
+ magic_comments_list_each do |magic_comment |
142
+ expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' )
143
+ . and_return ( "#{ magic_comment } \n #{ rake_routes_content } " )
144
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' ) . and_yield ( mock_file )
145
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "
146
+ #{ magic_comment }
63
147
# START
64
148
# == Route Map
65
149
#
@@ -69,14 +153,18 @@ def mock_file(stubs = {})
69
153
# myaction3 DELETE|GET /url3(.:format) mycontroller3#action
70
154
# END\n " )
71
155
72
- AnnotateRoutes . do_annotations ( wrapper_open : 'START' , wrapper_close : 'END' )
156
+ AnnotateRoutes . do_annotations ( wrapper_open : 'START' , wrapper_close : 'END' )
157
+ end
158
+ end
73
159
end
74
160
end
75
161
76
162
describe 'When adding' do
77
163
before ( :each ) do
78
- expect ( File ) . to receive ( :exists? ) . with ( ROUTE_FILE ) . and_return ( true )
79
- expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' ) . and_return ( '' )
164
+ expect ( File ) . to receive ( :exists? ) . with ( ROUTE_FILE )
165
+ . and_return ( true ) . at_least ( :once )
166
+ expect ( AnnotateRoutes ) . to receive ( :` ) . with ( 'rake routes' )
167
+ . and_return ( '' ) . at_least ( :once )
80
168
end
81
169
82
170
it 'should insert annotations if file does not contain annotations' do
@@ -112,6 +200,42 @@ def mock_file(stubs = {})
112
200
113
201
AnnotateRoutes . do_annotations
114
202
end
203
+
204
+ context 'file with magic comments' do
205
+ it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :top)' do
206
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' )
207
+ . and_yield ( mock_file ) . at_least ( :once )
208
+
209
+ magic_comments_list_each do |magic_comment |
210
+ expect ( File ) . to receive ( :read ) . with ( ROUTE_FILE ) . and_return ( "#{ magic_comment } \n Something" )
211
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "#{ magic_comment } \n # == Route Map\n #\n \n Something\n " )
212
+ expect ( AnnotateRoutes ) . to receive ( :puts ) . with ( ANNOTATION_ADDED )
213
+ AnnotateRoutes . do_annotations ( position_in_routes : 'top' )
214
+ end
215
+ end
216
+
217
+ it 'leaves magic comment on top, adds an empty line between magic comment and annotation (position_in_routes :bottom)' do
218
+ expect ( File ) . to receive ( :open ) . with ( ROUTE_FILE , 'wb' )
219
+ . and_yield ( mock_file ) . at_least ( :once )
220
+
221
+ magic_comments_list_each do |magic_comment |
222
+ expect ( File ) . to receive ( :read ) . with ( ROUTE_FILE ) . and_return ( "#{ magic_comment } \n Something" )
223
+ expect ( @mock_file ) . to receive ( :puts ) . with ( "#{ magic_comment } \n Something\n \n # == Route Map\n #\n " )
224
+ expect ( AnnotateRoutes ) . to receive ( :puts ) . with ( ANNOTATION_ADDED )
225
+ AnnotateRoutes . do_annotations ( position_in_routes : 'bottom' )
226
+ end
227
+ end
228
+
229
+ it 'skips annotations if file does already contain annotation' do
230
+ magic_comments_list_each do |magic_comment |
231
+ expect ( File ) . to receive ( :read ) . with ( ROUTE_FILE )
232
+ . and_return ( "#{ magic_comment } \n \n # == Route Map\n #\n " )
233
+ expect ( AnnotateRoutes ) . to receive ( :puts ) . with ( FILE_UNCHANGED )
234
+
235
+ AnnotateRoutes . do_annotations
236
+ end
237
+ end
238
+ end
115
239
end
116
240
117
241
describe 'When adding with older Rake versions' do
0 commit comments