/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 · 151 code · 28 blank · 2 comment · 0 complexity · 052144a678a68de1b75eedcc2a89f56f MD5 · raw file

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