PageRenderTime 33ms CodeModel.GetById 11ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/spec/rspec/core/rake_task_spec.rb

https://github.com/delowong/logstash
Ruby | 181 lines | 156 code | 23 blank | 2 comment | 0 complexity | 052144a678a68de1b75eedcc2a89f56f MD5 | raw file
  1require "spec_helper"
  2require "rspec/core/rake_task"
  3require 'tempfile'
  4
  5module RSpec::Core
  6  describe RakeTask do
  7    let(:task) { RakeTask.new }
  8
  9    def ruby
 10      FileUtils::RUBY
 11    end
 12
 13    def with_rcov
 14      task.rcov = true
 15      yield
 16    end
 17
 18    def spec_command
 19      task.__send__(:spec_command)
 20    end
 21
 22    context "with a name passed to the constructor" do
 23      let(:task) { RakeTask.new(:task_name) }
 24
 25      it "correctly sets the name" do
 26        expect(task.name).to eq :task_name
 27      end
 28    end
 29
 30    context "with args passed to the rake task" do
 31      it "correctly passes along task arguments" do
 32        task = RakeTask.new(:rake_task_args, :files) do |t, args|
 33          expect(args[:files]).to eq "first_spec.rb"
 34        end
 35
 36        task.should_receive(:run_task) { true }
 37        expect(Rake.application.invoke_task("rake_task_args[first_spec.rb]")).to be_true
 38      end
 39    end
 40
 41    context "default" do
 42      it "renders rspec" do
 43        expect(spec_command).to match(/^#{ruby} -S rspec/)
 44      end
 45    end
 46
 47    context "with rcov" do
 48      it "renders rcov" do
 49        with_rcov do
 50          expect(spec_command).to match(/^#{ruby} -S rcov/)
 51        end
 52      end
 53    end
 54
 55    context "with ruby options" do
 56      it "renders them before -S" do
 57          task.ruby_opts = "-w"
 58          expect(spec_command).to match(/^#{ruby} -w -S rspec/)
 59      end
 60    end
 61
 62    context "with rcov_opts" do
 63      context "with rcov=false (default)" do
 64        it "does not add the rcov options to the command" do
 65          task.rcov_opts = '--exclude "mocks"'
 66          expect(spec_command).not_to match(/--exclude "mocks"/)
 67        end
 68      end
 69
 70      context "with rcov=true" do
 71        it "renders them after rcov" do
 72          task.rcov = true
 73          task.rcov_opts = '--exclude "mocks"'
 74          expect(spec_command).to match(/rcov.*--exclude "mocks"/)
 75        end
 76
 77        it "ensures that -Ispec:lib is in the resulting command" do
 78          task.rcov = true
 79          task.rcov_opts = '--exclude "mocks"'
 80          expect(spec_command).to match(/rcov.*-Ispec:lib/)
 81        end
 82      end
 83    end
 84
 85    context "with rspec_opts" do
 86      context "with rcov=true" do
 87        it "adds the rspec_opts after the rcov_opts and files" do
 88          task.stub(:files_to_run) { "this.rb that.rb" }
 89          task.rcov = true
 90          task.rspec_opts = "-Ifoo"
 91          expect(spec_command).to match(/this.rb that.rb -- -Ifoo/)
 92        end
 93      end
 94      context "with rcov=false (default)" do
 95        it "adds the rspec_opts" do
 96          task.rspec_opts = "-Ifoo"
 97          expect(spec_command).to match(/rspec.*-Ifoo/)
 98        end
 99      end
100    end
101
102    def specify_consistent_ordering_of_files_to_run(pattern, task)
103      orderings = [
104        %w[ a/1.rb a/2.rb a/3.rb ],
105        %w[ a/2.rb a/1.rb a/3.rb ],
106        %w[ a/3.rb a/2.rb a/1.rb ]
107      ].map do |files|
108        FileList.should_receive(:[]).with(pattern) { files }
109        task.__send__(:files_to_run)
110      end
111
112      expect(orderings.uniq.size).to eq(1)
113    end
114
115    context "with SPEC env var set" do
116      it "sets files to run" do
117        with_env_vars 'SPEC' => 'path/to/file' do
118          expect(task.__send__(:files_to_run)).to eq(["path/to/file"])
119        end
120      end
121
122      it "sets the files to run in a consistent order, regardless of the underlying FileList ordering" do
123        with_env_vars 'SPEC' => 'a/*.rb' do
124          specify_consistent_ordering_of_files_to_run('a/*.rb', task)
125        end
126      end
127    end
128
129    it "sets the files to run in a consistent order, regardless of the underlying FileList ordering" do
130      task = RakeTask.new(:consistent_file_order) do |t|
131        t.pattern = 'a/*.rb'
132      end
133
134      # since the config block is deferred til task invocation, must fake
135      # calling the task so the expected pattern is picked up
136      task.should_receive(:run_task) { true }
137      expect(Rake.application.invoke_task(task.name)).to be_true
138
139      specify_consistent_ordering_of_files_to_run('a/*.rb', task)
140    end
141
142    context "with paths with quotes or spaces" do
143      it "escapes the quotes and spaces" do
144        task.pattern = File.join(Dir.tmpdir, "*spec.rb")
145        ["first_spec.rb", "second_\"spec.rb", "third_\'spec.rb", "fourth spec.rb"].each do |file_name|
146          FileUtils.touch(File.join(Dir.tmpdir, file_name))
147        end
148        expect(task.__send__(:files_to_run).sort).to eq([
149          File.join(Dir.tmpdir, "first_spec.rb"),
150          File.join(Dir.tmpdir, "fourth\\ spec.rb"),
151          File.join(Dir.tmpdir, "second_\\\"spec.rb"),
152          File.join(Dir.tmpdir, "third_\\\'spec.rb")
153        ])
154      end
155    end
156
157    context "with paths including symlinked directories" do
158      it "finds the files" do
159        project_dir = File.join(Dir.tmpdir, "project")
160        FileUtils.rm_rf project_dir
161
162        foos_dir = File.join(project_dir, "spec/foos")
163        FileUtils.mkdir_p foos_dir
164        FileUtils.touch(File.join(foos_dir, "foo_spec.rb"))
165
166        bars_dir = File.join(Dir.tmpdir, "shared/spec/bars")
167        FileUtils.mkdir_p bars_dir
168        FileUtils.touch(File.join(bars_dir, "bar_spec.rb"))
169
170        FileUtils.ln_s bars_dir, File.join(project_dir, "spec/bars")
171
172        FileUtils.cd(project_dir) do
173          expect(RakeTask.new.__send__(:files_to_run).sort).to eq([
174            "./spec/bars/bar_spec.rb",
175            "./spec/foos/foo_spec.rb"
176          ])
177        end
178      end
179    end
180  end
181end