File: project_initializer_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (122 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download | duplicates (4)
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
require 'rspec/core/project_initializer'

module RSpec::Core
  RSpec.describe ProjectInitializer, :isolated_directory => true do

    describe "#run" do
      context "with no args" do
        subject(:command_line_config) { ProjectInitializer.new(:report_stream => output) }

        let(:output) { StringIO.new }

        context "with no .rspec file" do
          it "says it's creating .rspec " do
            expect{ command_line_config.run }.to change{
              output.rewind
              output.read
            }.to(include 'create   .rspec')
          end

          it "generates a .rspec" do
            command_line_config.run
            expect(File.read('.rspec')).to match(/--require spec_helper/m)
          end
        end

        context "with a .rspec file" do
          it "says .rspec exists" do
            FileUtils.touch('.rspec')
            expect{ command_line_config.run }.to change{
              output.rewind
              output.read
            }.to(include 'exist   .rspec')
          end

          it "doesn't create a new one" do
            File.open('.rspec', 'w') {|f| f << '--not-a-real-flag'}
            command_line_config.run
            expect(File.read('.rspec')).to eq('--not-a-real-flag')
          end
        end

        context "with no spec/spec_helper.rb file" do
          it "says it's creating spec/spec_helper.rb " do
            expect{ command_line_config.run }.to change{
              output.rewind
              output.read
            }.to(include 'create   spec/spec_helper.rb')
          end

          it "generates a spec/spec_helper.rb" do
            command_line_config.run
            expect(File.read('spec/spec_helper.rb')).to match(/RSpec\.configure do \|config\|/m)
          end
        end

        context "with a spec/spec_helper.rb file" do
          before { FileUtils.mkdir('spec') }

          it "says spec/spec_helper.rb exists" do
            FileUtils.touch('spec/spec_helper.rb')
            expect{ command_line_config.run }.to change{
              output.rewind
              output.read
            }.to(include 'exist   spec/spec_helper.rb')
          end

          it "doesn't create a new one" do
            random_content = "content #{rand}"
            File.open('spec/spec_helper.rb', 'w') {|f| f << random_content}
            command_line_config.run
            expect(File.read('spec/spec_helper.rb')).to eq(random_content)
          end
        end
      end
    end

    describe "#run", "with a target directory" do
      subject(:command_line_config) {
        ProjectInitializer.new(:destination => tmpdir, :report_stream => StringIO.new)
      }

      let(:tmpdir) { 'relative/destination/' }

      before { FileUtils.mkdir_p(tmpdir) }

      context "with no .rspec file" do
        it "generates a .rspec" do
          command_line_config.run
          expect(File.read(File.join(tmpdir, '.rspec'))).to match(/--require spec_helper/m)
        end
      end

      context "with a .rspec file" do
        it "doesn't create a new one" do
          dot_rspec_file = File.join(tmpdir, '.rspec')
          File.open(dot_rspec_file, 'w') {|f| f << '--not-a-real-flag'}
          command_line_config.run
          expect(File.read(dot_rspec_file)).to eq('--not-a-real-flag')
        end
      end

      context "with no spec/spec_helper.rb file" do
        it "generates a spec/spec_helper.rb" do
          command_line_config.run
          expect(File.read(File.join(tmpdir, 'spec/spec_helper.rb'))).to match(/RSpec\.configure do \|config\|/m)
        end
      end

      context "with a spec/spec_helper.rb file" do
        it "doesn't create a new one" do
          FileUtils.mkdir File.join(tmpdir, 'spec')
          spec_helper_file = File.join(tmpdir, 'spec', 'spec_helper.rb')
          random_content = "content #{rand}"
          File.open(spec_helper_file, 'w') {|f| f << random_content}
          command_line_config.run
          expect(File.read(spec_helper_file)).to eq(random_content)
        end
      end
    end

  end
end