@@ -20,6 +20,10 @@ def invoke!(*args, &block)
20
20
capture ( :stdout ) { invoker . insert_into_file ( *args , &block ) }
21
21
end
22
22
23
+ def invoke_with_a_bang! ( *args , &block )
24
+ capture ( :stdout ) { invoker . insert_into_file! ( *args , &block ) }
25
+ end
26
+
23
27
def revoke! ( *args , &block )
24
28
capture ( :stdout ) { revoker . insert_into_file ( *args , &block ) }
25
29
end
@@ -170,6 +174,164 @@ def file
170
174
end
171
175
end
172
176
end
177
+
178
+ context "with a bang" do
179
+ it "changes the file adding content after the flag" do
180
+ invoke_with_a_bang! "doc/README" , "\n more content" , after : "__start__"
181
+ expect ( File . read ( file ) ) . to eq ( "__start__\n more content\n README\n __end__\n " )
182
+ end
183
+
184
+ it "changes the file adding content before the flag" do
185
+ invoke_with_a_bang! "doc/README" , "more content\n " , before : "__end__"
186
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
187
+ end
188
+
189
+ it "appends content to the file if before and after arguments not provided" do
190
+ invoke_with_a_bang! ( "doc/README" , "more content\n " )
191
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n __end__\n more content\n " )
192
+ end
193
+
194
+ it "does not change the file and raises an error if replacement present in the file" do
195
+ invoke_with_a_bang! ( "doc/README" , "more specific content\n " )
196
+ expect do
197
+ invoke_with_a_bang! ( "doc/README" , "more specific content\n " )
198
+ end . to raise_error ( Thor ::Error , "The content of #{ destination_root } /doc/README did not change" )
199
+ end
200
+
201
+ it "does not change the file and raises an error if flag not found in the file" do
202
+ expect do
203
+ invoke_with_a_bang! ( "doc/README" , "more content\n " , after : "whatever" )
204
+ end . to raise_error ( Thor ::Error , "The content of #{ destination_root } /doc/README did not change" )
205
+ end
206
+
207
+ it "accepts data as a block" do
208
+ invoke_with_a_bang! "doc/README" , before : "__end__" do
209
+ "more content\n "
210
+ end
211
+
212
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
213
+ end
214
+
215
+ it "logs status" do
216
+ expect ( invoke_with_a_bang! ( "doc/README" , "\n more content" , after : "__start__" ) ) . to eq ( " insert doc/README\n " )
217
+ end
218
+
219
+ it "logs status if pretending" do
220
+ invoker ( pretend : true )
221
+ expect ( invoke_with_a_bang! ( "doc/README" , "\n more content" , after : "__start__" ) ) . to eq ( " insert doc/README\n " )
222
+ end
223
+
224
+ it "does not change the file if pretending" do
225
+ invoker pretend : true
226
+ invoke_with_a_bang! "doc/README" , "\n more content" , after : "__start__"
227
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n __end__\n " )
228
+ end
229
+
230
+ it "does not change the file and raises an error if already includes content" do
231
+ invoke_with_a_bang! "doc/README" , before : "__end__" do
232
+ "more content\n "
233
+ end
234
+
235
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
236
+
237
+ expect do
238
+ invoke_with_a_bang! "doc/README" , before : "__end__" do
239
+ "more content\n "
240
+ end
241
+ end . to raise_error ( Thor ::Error , "The content of #{ destination_root } /doc/README did not change" )
242
+
243
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
244
+ end
245
+
246
+ it "does not change the file and raises an error if already includes content using before with capture" do
247
+ invoke_with_a_bang! "doc/README" , before : /(__end__)/ do
248
+ "more content\n "
249
+ end
250
+
251
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
252
+
253
+ expect do
254
+ invoke_with_a_bang! "doc/README" , before : /(__end__)/ do
255
+ "more content\n "
256
+ end
257
+ end . to raise_error ( Thor ::Error , "The content of #{ destination_root } /doc/README did not change" )
258
+
259
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
260
+ end
261
+
262
+ it "does not change the file and raises an error if already includes content using after with capture" do
263
+ invoke_with_a_bang! "doc/README" , after : /(README\n )/ do
264
+ "more content\n "
265
+ end
266
+
267
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
268
+
269
+ expect do
270
+ invoke_with_a_bang! "doc/README" , after : /(README\n )/ do
271
+ "more content\n "
272
+ end
273
+ end . to raise_error ( Thor ::Error , "The content of #{ destination_root } /doc/README did not change" )
274
+
275
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
276
+ end
277
+
278
+ it "does not attempt to change the file if it doesn't exist - instead raises Thor::Error" do
279
+ expect do
280
+ invoke_with_a_bang! "idontexist" , before : "something" do
281
+ "any content"
282
+ end
283
+ end . to raise_error ( Thor ::Error , /does not appear to exist/ )
284
+ expect ( File . exist? ( "idontexist" ) ) . to be_falsey
285
+ end
286
+
287
+ it "does not attempt to change the file if it doesn't exist and pretending" do
288
+ expect do
289
+ invoker pretend : true
290
+ invoke_with_a_bang! "idontexist" , before : "something" do
291
+ "any content"
292
+ end
293
+ end . not_to raise_error
294
+ expect ( File . exist? ( "idontexist" ) ) . to be_falsey
295
+ end
296
+
297
+ it "does change the file if already includes content and :force is true" do
298
+ invoke_with_a_bang! "doc/README" , before : "__end__" do
299
+ "more content\n "
300
+ end
301
+
302
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n __end__\n " )
303
+
304
+ invoke_with_a_bang! "doc/README" , before : "__end__" , force : true do
305
+ "more content\n "
306
+ end
307
+
308
+ expect ( File . read ( file ) ) . to eq ( "__start__\n README\n more content\n more content\n __end__\n " )
309
+ end
310
+
311
+ it "can insert chinese" do
312
+ encoding_original = Encoding . default_external
313
+
314
+ begin
315
+ silence_warnings do
316
+ Encoding . default_external = Encoding . find ( "UTF-8" )
317
+ end
318
+ invoke_with_a_bang! "doc/README.zh" , "\n 中文" , after : "__start__"
319
+ expect ( File . read ( File . join ( destination_root , "doc/README.zh" ) ) ) . to eq ( "__start__\n 中文\n 说明\n __end__\n " )
320
+ ensure
321
+ silence_warnings do
322
+ Encoding . default_external = encoding_original
323
+ end
324
+ end
325
+ end
326
+
327
+ it "does not mutate the provided config" do
328
+ config = { after : "__start__" }
329
+ invoke_with_a_bang! "doc/README" , "\n more content" , config
330
+ expect ( File . read ( file ) ) . to eq ( "__start__\n more content\n README\n __end__\n " )
331
+
332
+ expect ( config ) . to eq ( { after : "__start__" } )
333
+ end
334
+ end
173
335
end
174
336
175
337
describe "#revoke!" do
0 commit comments