|
| 1 | +module RSpec::Core |
| 2 | + RSpec.describe Configuration, "--only-failures support" do |
| 3 | + let(:config) { Configuration.new } |
| 4 | + let(:world) { World.new(config) } |
| 5 | + |
| 6 | + def simulate_persisted_examples(*examples) |
| 7 | + config.example_status_persistence_file_path = "examples.txt" |
| 8 | + persister = class_double(ExampleStatusPersister).as_stubbed_const |
| 9 | + |
| 10 | + allow(persister).to receive(:load_from).with("examples.txt").and_return(examples) |
| 11 | + end |
| 12 | + |
| 13 | + describe "#last_run_statuses" do |
| 14 | + def last_run_statuses |
| 15 | + world.last_run_statuses |
| 16 | + end |
| 17 | + |
| 18 | + context "when `example_status_persistence_file_path` is configured" do |
| 19 | + it 'gets the last run statuses from the ExampleStatusPersister' do |
| 20 | + simulate_persisted_examples( |
| 21 | + { :example_id => "id_1", :status => "passed" }, |
| 22 | + { :example_id => "id_2", :status => "failed" } |
| 23 | + ) |
| 24 | + |
| 25 | + expect(last_run_statuses).to eq( |
| 26 | + 'id_1' => 'passed', 'id_2' => 'failed' |
| 27 | + ) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + context "when `example_status_persistence_file_path` is not configured" do |
| 32 | + it 'returns a memoized value' do |
| 33 | + expect(last_run_statuses).to be(world.last_run_statuses) |
| 34 | + end |
| 35 | + |
| 36 | + it 'returns a blank hash without attempting to load the persisted statuses' do |
| 37 | + config.example_status_persistence_file_path = nil |
| 38 | + |
| 39 | + persister = class_double(ExampleStatusPersister).as_stubbed_const |
| 40 | + expect(persister).not_to receive(:load_from) |
| 41 | + |
| 42 | + expect(last_run_statuses).to eq({}) |
| 43 | + end |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "#spec_files_with_failures" do |
| 48 | + def spec_files_with_failures |
| 49 | + world.spec_files_with_failures |
| 50 | + end |
| 51 | + |
| 52 | + context "when `example_status_persistence_file_path` is configured" do |
| 53 | + it 'returns a memoized array of unique spec files that contain failed exaples' do |
| 54 | + simulate_persisted_examples( |
| 55 | + { :example_id => "./spec_1.rb[1:1]", :status => "failed" }, |
| 56 | + { :example_id => "./spec_1.rb[1:2]", :status => "failed" }, |
| 57 | + { :example_id => "./spec_2.rb[1:2]", :status => "passed" }, |
| 58 | + { :example_id => "./spec_3.rb[1:2]", :status => "pending" }, |
| 59 | + { :example_id => "./spec_4.rb[1:2]", :status => "unknown" }, |
| 60 | + { :example_id => "./spec_5.rb[1:2]", :status => "failed" } |
| 61 | + ) |
| 62 | + |
| 63 | + expect(spec_files_with_failures).to( |
| 64 | + be_an(Array) & |
| 65 | + be(world.spec_files_with_failures) & |
| 66 | + contain_exactly("./spec_1.rb", "./spec_5.rb") |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context "when `example_status_persistence_file_path` is not configured" do |
| 72 | + it "returns a memoized blank array" do |
| 73 | + config.example_status_persistence_file_path = nil |
| 74 | + |
| 75 | + expect(spec_files_with_failures).to( |
| 76 | + eq([]) & be(world.spec_files_with_failures) |
| 77 | + ) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + describe "#files_to_run, when `only_failures` is set" do |
| 83 | + around { |ex| Dir.chdir("spec/rspec/core", &ex) } |
| 84 | + let(:default_path) { "resources" } |
| 85 | + let(:files_with_failures) { ["resources/a_spec.rb"] } |
| 86 | + let(:files_loaded_via_default_path) do |
| 87 | + configuration = Configuration.new |
| 88 | + configuration.default_path = default_path |
| 89 | + configuration.files_or_directories_to_run = [] |
| 90 | + configuration.files_to_run |
| 91 | + end |
| 92 | + |
| 93 | + before do |
| 94 | + expect(files_loaded_via_default_path).not_to eq(files_with_failures) |
| 95 | + config.default_path = default_path |
| 96 | + allow(RSpec.world).to receive_messages(:spec_files_with_failures => files_with_failures) |
| 97 | + config.force(:only_failures => true) |
| 98 | + end |
| 99 | + |
| 100 | + context "and no explicit paths have been set" do |
| 101 | + it 'loads only the files that have failures' do |
| 102 | + config.files_or_directories_to_run = [] |
| 103 | + expect(config.files_to_run).to eq(files_with_failures) |
| 104 | + end |
| 105 | + |
| 106 | + it 'loads the default path if there are no files with failures' do |
| 107 | + allow(RSpec.world).to receive_messages(:spec_files_with_failures => []) |
| 108 | + config.files_or_directories_to_run = [] |
| 109 | + expect(config.files_to_run).to eq(files_loaded_via_default_path) |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context "and a path has been set" do |
| 114 | + it "ignores the list of files with failures, loading the configured path instead" do |
| 115 | + config.files_or_directories_to_run = ["resources/acceptance"] |
| 116 | + expect(config.files_to_run).to contain_files("resources/acceptance/foo_spec.rb") |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context "and the default path has been explicitly set" do |
| 121 | + it "ignores the list of files with failures, loading the configured path instead" do |
| 122 | + config.files_or_directories_to_run = [default_path] |
| 123 | + expect(config.files_to_run).to eq(files_loaded_via_default_path) |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments