File: file_manipulation_spec.rb

package info (click to toggle)
ruby-thor 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 924 kB
  • sloc: ruby: 9,491; makefile: 8; sh: 1
file content (680 lines) | stat: -rw-r--r-- 25,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
require "helper"

describe Thor::Actions do
  def runner(options = {}, behavior = :invoke)
    @runner ||= MyCounter.new([1], options, destination_root: destination_root, behavior: behavior)
  end

  def action(*args, &block)
    capture(:stdout) { runner.send(*args, &block) }
  end

  def exists_and_identical?(source, destination)
    destination = File.join(destination_root, destination)
    expect(File.exist?(destination)).to be true

    source = File.join(source_root, source)
    expect(FileUtils).to be_identical(source, destination)
  end

  def file
    File.join(destination_root, "foo")
  end

  before do
    ::FileUtils.rm_rf(destination_root)
  end

  describe "#chmod" do
    it "executes the command given" do
      expect(FileUtils).to receive(:chmod_R).with(0755, file)
      action :chmod, "foo", 0755
    end

    it "does not execute the command if pretending" do
      expect(FileUtils).not_to receive(:chmod_R)
      runner(pretend: true)
      action :chmod, "foo", 0755
    end

    it "logs status" do
      expect(FileUtils).to receive(:chmod_R).with(0755, file)
      expect(action(:chmod, "foo", 0755)).to eq("       chmod  foo\n")
    end

    it "does not log status if required" do
      expect(FileUtils).to receive(:chmod_R).with(0755, file)
      expect(action(:chmod, "foo", 0755, verbose: false)).to be_empty
    end
  end

  describe "#copy_file" do
    it "copies file from source to default destination" do
      action :copy_file, "command.thor"
      exists_and_identical?("command.thor", "command.thor")
    end

    it "copies file from source to the specified destination" do
      action :copy_file, "command.thor", "foo.thor"
      exists_and_identical?("command.thor", "foo.thor")
    end

    it "copies file from the source relative to the current path" do
      runner.inside("doc") do
        action :copy_file, "README"
      end
      exists_and_identical?("doc/README", "doc/README")
    end

    it "copies file from source to default destination and preserves file mode" do
      action :copy_file, "preserve/script.sh", mode: :preserve
      original = File.join(source_root, "preserve/script.sh")
      copy = File.join(destination_root, "preserve/script.sh")
      expect(File.stat(original).mode).to eq(File.stat(copy).mode)
    end

    it "copies file from source to default destination and preserves file mode for templated filenames" do
      expect(runner).to receive(:filename).and_return("app")
      action :copy_file, "preserve/%filename%.sh", mode: :preserve
      original = File.join(source_root, "preserve/%filename%.sh")
      copy = File.join(destination_root, "preserve/app.sh")
      expect(File.stat(original).mode).to eq(File.stat(copy).mode)
    end

    it "shows the diff when there is a collision and source has utf-8 characters" do
      previous_internal = Encoding.default_internal

      silence_warnings do
        Encoding.default_internal = Encoding::UTF_8
      end

      destination = File.join(destination_root, "encoding_with_utf8.thor")
      FileUtils.mkdir_p(destination_root)

      File.write(destination, "blabla")

      expect(Thor::LineEditor).to receive(:readline).and_return("d", "y")
      expect(runner.shell).to receive(:system).with("diff", "-u", /encoding_with_utf8.thor/, /encoding_with_utf8.thor/)
      action :copy_file, "encoding_with_utf8.thor"

      exists_and_identical?("encoding_with_utf8.thor", "encoding_with_utf8.thor")
    ensure
      silence_warnings do
        Encoding.default_internal = previous_internal
      end
    end

    it "launches the merge tool when there is a collision and source has utf-8 characters" do
      previous_internal = Encoding.default_internal

      silence_warnings do
        Encoding.default_internal = Encoding::UTF_8
      end

      destination = File.join(destination_root, "encoding_with_utf8.thor")
      FileUtils.mkdir_p(destination_root)

      File.write(destination, "blabla")

      allow(runner.shell).to receive(:merge_tool).and_return("meld")
      expect(Thor::LineEditor).to receive(:readline).and_return("m")
      expect(runner.shell).to receive(:system).with("meld", /encoding_with_utf8.thor/, /encoding_with_utf8.thor/)
      action :copy_file, "encoding_with_utf8.thor"
    ensure
      silence_warnings do
        Encoding.default_internal = previous_internal
      end
    end

    it "logs status" do
      expect(action(:copy_file, "command.thor")).to eq("      create  command.thor\n")
    end

    it "accepts a block to change output" do
      action :copy_file, "command.thor" do |content|
        "OMG" + content
      end
      expect(File.read(File.join(destination_root, "command.thor"))).to match(/^OMG/)
    end
  end

  describe "#link_file", unless: windows? do
    it "links file from source to default destination" do
      action :link_file, "command.thor"
      exists_and_identical?("command.thor", "command.thor")
    end

    it "links file from source to the specified destination" do
      action :link_file, "command.thor", "foo.thor"
      exists_and_identical?("command.thor", "foo.thor")
    end

    it "links file from the source relative to the current path" do
      runner.inside("doc") do
        action :link_file, "README"
      end
      exists_and_identical?("doc/README", "doc/README")
    end

    it "logs status" do
      expect(action(:link_file, "command.thor")).to eq("      create  command.thor\n")
    end
  end

  describe "#get" do
    it "copies file from source to the specified destination" do
      action :get, "doc/README", "docs/README"
      exists_and_identical?("doc/README", "docs/README")
    end

    it "uses just the source basename as destination if none is specified" do
      action :get, "doc/README"
      exists_and_identical?("doc/README", "README")
    end

    it "allows the destination to be set as a block result" do
      action(:get, "doc/README") { "docs/README" }
      exists_and_identical?("doc/README", "docs/README")
    end

    it "yields file content to a block" do
      action :get, "doc/README" do |content|
        expect(content).to eq("__start__\nREADME\n__end__\n")
      end
    end

    it "logs status" do
      expect(action(:get, "doc/README", "docs/README")).to eq("      create  docs/README\n")
    end

    it "accepts http remote sources" do
      body = "__start__\nHTTPFILE\n__end__\n"
      stub_request(:get, "http://example.com/file.txt").to_return(body: body.dup)
      action :get, "http://example.com/file.txt" do |content|
        expect(a_request(:get, "http://example.com/file.txt")).to have_been_made
        expect(content).to eq(body)
      end
    end

    it "accepts https remote sources" do
      body = "__start__\nHTTPSFILE\n__end__\n"
      stub_request(:get, "https://example.com/file.txt").to_return(body: body.dup)
      action :get, "https://example.com/file.txt" do |content|
        expect(a_request(:get, "https://example.com/file.txt")).to have_been_made
        expect(content).to eq(body)
      end
    end

    it "accepts http headers" do
      body = "__start__\nHTTPFILE\n__end__\n"
      headers = {"Content-Type" => "application/json"}
      stub_request(:get, "https://example.com/file.txt").with(headers: headers).to_return(body: body.dup)
      action :get, "https://example.com/file.txt", {http_headers: headers} do |content|
        expect(a_request(:get, "https://example.com/file.txt")).to have_been_made
        expect(content).to eq(body)
      end
    end
  end

  describe "#template" do
    it "allows using block helpers in the template" do
      action :template, "doc/block_helper.rb"

      file = File.join(destination_root, "doc/block_helper.rb")
      expect(File.read(file)).to eq("Hello world!")
    end

    it "evaluates the template given as source" do
      runner.instance_variable_set("@klass", "Config")
      action :template, "doc/config.rb"

      file = File.join(destination_root, "doc/config.rb")
      expect(File.read(file)).to eq("class Config; end\n")
    end

    it "copies the template to the specified destination" do
      runner.instance_variable_set("@klass", "Config")
      action :template, "doc/config.rb", "doc/configuration.rb"
      file = File.join(destination_root, "doc/configuration.rb")
      expect(File.exist?(file)).to be true
    end

    it "converts encoded instructions" do
      expect(runner).to receive(:file_name).and_return("rdoc")
      action :template, "doc/%file_name%.rb.tt"
      file = File.join(destination_root, "doc/rdoc.rb")
      expect(File.exist?(file)).to be true
    end

    it "accepts filename without .tt for template method" do
      expect(runner).to receive(:file_name).and_return("rdoc")
      action :template, "doc/%file_name%.rb"
      file = File.join(destination_root, "doc/rdoc.rb")
      expect(File.exist?(file)).to be true
    end

    it "logs status" do
      runner.instance_variable_set("@klass", "Config")
      expect(capture(:stdout) { runner.template("doc/config.rb") }).to eq("      create  doc/config.rb\n")
    end

    it "accepts a block to change output" do
      runner.instance_variable_set("@klass", "Config")
      action :template, "doc/config.rb" do |content|
        "OMG" + content
      end
      expect(File.read(File.join(destination_root, "doc/config.rb"))).to match(/^OMG/)
    end

    it "accepts a context to use as the binding" do
      begin
        @klass = "FooBar"
        action :template, "doc/config.rb", context: eval("binding")
        expect(File.read(File.join(destination_root, "doc/config.rb"))).to eq("class FooBar; end\n")
      ensure
        remove_instance_variable(:@klass)
      end
    end

    it "guesses the destination name when given only a source" do
      action :template, "doc/config.yaml.tt"

      file = File.join(destination_root, "doc/config.yaml")
      expect(File.exist?(file)).to be true
    end

    it "has proper ERB stacktraces" do
      error = nil
      begin
        action :template, "template/bad_config.yaml.tt"
      rescue => e
        error = e
      end

      expect(error).to be_a NameError
      expect(error.backtrace.to_s).to include("bad_config.yaml.tt:2")
    end
  end

  describe "when changing existent files" do
    before do
      ::FileUtils.cp_r(source_root, destination_root)
    end

    def file
      File.join(destination_root, "doc", "README")
    end

    describe "#remove_file" do
      it "removes the file given" do
        action :remove_file, "doc/README"
        expect(File.exist?(file)).to be false
      end

      it "removes broken symlinks too" do
        link_path = File.join(destination_root, "broken_symlink")
        ::FileUtils.ln_s("invalid_reference", link_path)
        action :remove_file, "broken_symlink"
        expect(File.symlink?(link_path) || File.exist?(link_path)).to be false
      end

      it "removes directories too" do
        action :remove_dir, "doc"
        expect(File.exist?(File.join(destination_root, "doc"))).to be false
      end

      it "does not remove if pretending" do
        runner(pretend: true)
        action :remove_file, "doc/README"
        expect(File.exist?(file)).to be true
      end

      it "logs status" do
        expect(action(:remove_file, "doc/README")).to eq("      remove  doc/README\n")
      end

      it "does not log status if required" do
        expect(action(:remove_file, "doc/README", verbose: false)).to be_empty
      end
    end

    describe "#gsub_file!" do
      context "with invoke behavior" do
        it "replaces the content in the file" do
          action :gsub_file!, "doc/README", "__start__", "START"
          expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
        end

        it "does not replace if pretending" do
          runner(pretend: true)
          action :gsub_file!, "doc/README", "__start__", "START"
          expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
        end

        it "accepts a block" do
          action(:gsub_file!, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
          expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
        end

        it "logs status" do
          expect(action(:gsub_file!, "doc/README", "__start__", "START")).to eq("        gsub  doc/README\n")
        end

        it "does not log status if required" do
          expect(action(:gsub_file!, file, "__", verbose: false) { |match| match * 2 }).to be_empty
        end

        it "cares if the file contents did not change" do
          expect do
            action :gsub_file!, "doc/README", "___start___", "START"
          end.to raise_error(Thor::Error, "The content of #{destination_root}/doc/README did not change")

          expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
        end
      end

      context "with revoke behavior" do
        context "and no force option" do
          it "does not replace the content in the file" do
            runner({}, :revoke)
            action :gsub_file!, "doc/README", "__start__", "START"
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "does not replace if pretending" do
            runner({pretend: true}, :revoke)
            action :gsub_file!, "doc/README", "__start__", "START"
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "does not replace the content in the file when given a block" do
            runner({}, :revoke)
            action(:gsub_file!, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "does not log status" do
            runner({}, :revoke)
            expect(action(:gsub_file!, "doc/README", "__start__", "START")).to be_empty
          end

          it "does not log status if required" do
            runner({}, :revoke)
            expect(action(:gsub_file!, file, "__", verbose: false) { |match| match * 2 }).to be_empty
          end
        end

        context "and force option" do
          it "replaces the content in the file" do
            runner({}, :revoke)
            action :gsub_file!, "doc/README", "__start__", "START", force: true
            expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
          end

          it "does not replace if pretending" do
            runner({pretend: true}, :revoke)
            action :gsub_file!, "doc/README", "__start__", "START", force: true
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "replaces the content in the file when given a block" do
            runner({}, :revoke)
            action(:gsub_file!, "doc/README", "__start__", force: true) { |match| match.gsub("__", "").upcase }
            expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
          end

          it "logs status" do
            runner({}, :revoke)
            expect(action(:gsub_file!, "doc/README", "__start__", "START", force: true)).to eq("        gsub  doc/README\n")
          end

          it "does not log status if required" do
            runner({}, :revoke)
            expect(action(:gsub_file!, file, "__", verbose: false, force: true) { |match| match * 2 }).to be_empty
          end
        end
      end
    end

    describe "#gsub_file" do
      context "with invoke behavior" do
        it "replaces the content in the file" do
          action :gsub_file, "doc/README", "__start__", "START"
          expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
        end

        it "does not replace if pretending" do
          runner(pretend: true)
          action :gsub_file, "doc/README", "__start__", "START"
          expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
        end

        it "accepts a block" do
          action(:gsub_file, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
          expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
        end

        it "logs status" do
          expect(action(:gsub_file, "doc/README", "__start__", "START")).to eq("        gsub  doc/README\n")
        end

        it "does not log status if required" do
          expect(action(:gsub_file, file, "__", verbose: false) { |match| match * 2 }).to be_empty
        end

        it "does not care if the file contents did not change" do
          action :gsub_file, "doc/README", "___start___", "START"
          expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
        end
      end

      context "with revoke behavior" do
        context "and no force option" do
          it "does not replace the content in the file" do
            runner({}, :revoke)
            action :gsub_file, "doc/README", "__start__", "START"
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "does not replace if pretending" do
            runner({pretend: true}, :revoke)
            action :gsub_file, "doc/README", "__start__", "START"
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "does not replace the content in the file when given a block" do
            runner({}, :revoke)
            action(:gsub_file, "doc/README", "__start__") { |match| match.gsub("__", "").upcase }
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "does not log status" do
            runner({}, :revoke)
            expect(action(:gsub_file, "doc/README", "__start__", "START")).to be_empty
          end

          it "does not log status if required" do
            runner({}, :revoke)
            expect(action(:gsub_file, file, "__", verbose: false) { |match| match * 2 }).to be_empty
          end
        end

        context "and force option" do
          it "replaces the content in the file" do
            runner({}, :revoke)
            action :gsub_file, "doc/README", "__start__", "START", force: true
            expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
          end

          it "does not replace if pretending" do
            runner({pretend: true}, :revoke)
            action :gsub_file, "doc/README", "__start__", "START", force: true
            expect(File.binread(file)).to eq("__start__\nREADME\n__end__\n")
          end

          it "replaces the content in the file when given a block" do
            runner({}, :revoke)
            action(:gsub_file, "doc/README", "__start__", force: true) { |match| match.gsub("__", "").upcase }
            expect(File.binread(file)).to eq("START\nREADME\n__end__\n")
          end

          it "logs status" do
            runner({}, :revoke)
            expect(action(:gsub_file, "doc/README", "__start__", "START", force: true)).to eq("        gsub  doc/README\n")
          end

          it "does not log status if required" do
            runner({}, :revoke)
            expect(action(:gsub_file, file, "__", verbose: false, force: true) { |match| match * 2 }).to be_empty
          end
        end
      end
    end

    describe "#append_to_file" do
      it "appends content to the file" do
        action :append_to_file, "doc/README", "END\n"
        expect(File.binread(file)).to eq("__start__\nREADME\n__end__\nEND\n")
      end

      it "accepts a block" do
        action(:append_to_file, "doc/README") { "END\n" }
        expect(File.binread(file)).to eq("__start__\nREADME\n__end__\nEND\n")
      end

      it "logs status" do
        expect(action(:append_to_file, "doc/README", "END")).to eq("      append  doc/README\n")
      end
    end

    describe "#prepend_to_file" do
      it "prepends content to the file" do
        action :prepend_to_file, "doc/README", "START\n"
        expect(File.binread(file)).to eq("START\n__start__\nREADME\n__end__\n")
      end

      it "accepts a block" do
        action(:prepend_to_file, "doc/README") { "START\n" }
        expect(File.binread(file)).to eq("START\n__start__\nREADME\n__end__\n")
      end

      it "logs status" do
        expect(action(:prepend_to_file, "doc/README", "START")).to eq("     prepend  doc/README\n")
      end
    end

    describe "#inject_into_class" do
      def file
        File.join(destination_root, "application.rb")
      end

      it "appends content to a class" do
        action :inject_into_class, "application.rb", "Application", "  filter_parameters :password\n"
        expect(File.binread(file)).to eq("class Application < Base\n  filter_parameters :password\nend\n")
      end

      it "accepts a block" do
        action(:inject_into_class, "application.rb", "Application") { "  filter_parameters :password\n" }
        expect(File.binread(file)).to eq("class Application < Base\n  filter_parameters :password\nend\n")
      end

      it "logs status" do
        expect(action(:inject_into_class, "application.rb", "Application", "  filter_parameters :password\n")).to eq("      insert  application.rb\n")
      end

      it "does not append if class name does not match" do
        action :inject_into_class, "application.rb", "App", "  filter_parameters :password\n"
        expect(File.binread(file)).to eq("class Application < Base\nend\n")
      end
    end

    describe "#inject_into_module" do
      def file
        File.join(destination_root, "application_helper.rb")
      end

      it "appends content to a module" do
        action :inject_into_module, "application_helper.rb", "ApplicationHelper", "  def help; 'help'; end\n"
        expect(File.binread(file)).to eq("module ApplicationHelper\n  def help; 'help'; end\nend\n")
      end

      it "accepts a block" do
        action(:inject_into_module, "application_helper.rb", "ApplicationHelper") { "  def help; 'help'; end\n" }
        expect(File.binread(file)).to eq("module ApplicationHelper\n  def help; 'help'; end\nend\n")
      end

      it "logs status" do
        expect(action(:inject_into_module, "application_helper.rb", "ApplicationHelper", "  def help; 'help'; end\n")).to eq("      insert  application_helper.rb\n")
      end

      it "does not append if module name does not match" do
        action :inject_into_module, "application_helper.rb", "App", "  def help; 'help'; end\n"
        expect(File.binread(file)).to eq("module ApplicationHelper\nend\n")
      end
    end
  end

  describe "when adjusting comments" do
    before do
      ::FileUtils.cp_r(source_root, destination_root)
    end

    def file
      File.join(destination_root, "doc", "COMMENTER")
    end

    unmodified_comments_file = /__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n    purple\n  ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/

    describe "#uncomment_lines" do
      it "uncomments all matching lines in the file" do
        action :uncomment_lines, "doc/COMMENTER", "green"
        expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\n#yellowred\n greenred\norange\n    purple\n  ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/)

        action :uncomment_lines, "doc/COMMENTER", "red"
        expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\nyellowred\n greenred\norange\n    purple\n  ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/)
      end

      it "correctly uncomments lines with hashes in them" do
        action :uncomment_lines, "doc/COMMENTER", "ind#igo"
        expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n    purple\n  ind#igo\n  ind#igo\n  #   spaces_between\n__end__/)
      end

      it "will leave the space which existed before the comment hash in tact" do
        action :uncomment_lines, "doc/COMMENTER", "ind#igo"
        action :uncomment_lines, "doc/COMMENTER", "spaces_between"
        expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n    purple\n  ind#igo\n  ind#igo\n    spaces_between\n__end__/)
      end

      it "does not modify already uncommented lines in the file" do
        action :uncomment_lines, "doc/COMMENTER", "orange"
        action :uncomment_lines, "doc/COMMENTER", "purple"
        expect(File.binread(file)).to match(unmodified_comments_file)
      end

      it "does not uncomment the wrong line when uncommenting lines preceded by blank commented line" do
        action :uncomment_lines, "doc/COMMENTER", "yellow"
        expect(File.binread(file)).to match(/__start__\n # greenblue\n#\nyellowblue\nyellowred\n #greenred\norange\n    purple\n  ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/)
      end
    end

    describe "#comment_lines" do
      it "comments lines which are not commented" do
        action :comment_lines, "doc/COMMENTER", "orange"
        expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n    purple\n  ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/)

        action :comment_lines, "doc/COMMENTER", "purple"
        expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n    # purple\n  ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/)
      end

      it "correctly comments lines with hashes in them" do
        action :comment_lines, "doc/COMMENTER", "ind#igo"
        expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n    purple\n  # ind#igo\n  # ind#igo\n  #   spaces_between\n__end__/)
      end

      it "does not modify already commented lines" do
        action :comment_lines, "doc/COMMENTER", "green"
        expect(File.binread(file)).to match(unmodified_comments_file)
      end
    end
  end
end