Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit f7770d2

Browse files
committed
Create LastRunPersister.
1 parent 59a4464 commit f7770d2

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

lib/rspec/core.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
require "rspec/support"
66
RSpec::Support.require_rspec_support "caller_filter"
7+
RSpec::Support.require_rspec_support "directory_maker"
78

89
RSpec::Support.define_optimized_require_for_rspec(:core) { |f| require_relative f }
910

@@ -34,6 +35,7 @@
3435
example
3536
shared_example_group
3637
example_group
38+
last_run_persister
3739
].each { |name| RSpec::Support.require_rspec_core name }
3840

3941
# Namespace for all core RSpec code.

lib/rspec/core/formatters.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
RSpec::Support.require_rspec_support "directory_maker"
21
# ## Built-in Formatters
32
#
43
# * progress (default) - Prints dots for passing examples, `F` for failures, `*`

lib/rspec/core/last_run_persister.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module RSpec
2+
module Core
3+
# Persists data about the last run of your spec suite so that it
4+
# can be re-used on the next run. For now, it only stores a list
5+
# of what failed in order to support the `--rerun-faliures` flag.
6+
# @private
7+
class LastRunPersister
8+
def self.for_current_project
9+
new(File.join(Dir.home, ".rspec_last_run", RubyProject.root))
10+
end
11+
12+
attr_reader :directory
13+
14+
def initialize(directory)
15+
@directory = directory
16+
end
17+
18+
def persist_failures(failure_ids)
19+
File.open(failures_file_name, "w") do |f|
20+
f.write(failure_ids.join("\n"))
21+
end
22+
end
23+
24+
def failures_from_last_run
25+
File.read(failures_file_name).split("\n")
26+
end
27+
28+
private
29+
30+
def failures_file_name
31+
@failures_file_name ||= File.join(directory, "failures.txt").tap do |f|
32+
RSpec::Support::DirectoryMaker.mkdir_p(File.dirname(f))
33+
end
34+
end
35+
end
36+
end
37+
end

lib/rspec/core/project_initializer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
RSpec::Support.require_rspec_support "directory_maker"
2-
31
module RSpec
42
module Core
53
# @private
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module RSpec::Core
2+
RSpec.describe LastRunPersister do
3+
def persist_failures(dir, failures)
4+
LastRunPersister.new(dir).persist_failures(failures)
5+
end
6+
7+
def persisted_from(dir)
8+
LastRunPersister.new(dir).failures_from_last_run
9+
end
10+
11+
it 'persists the failures from the last run' do
12+
persist_failures("./tmp", %w[ failure1 failure17 ])
13+
expect(persisted_from("./tmp")).to eq(%w[ failure1 failure17 ])
14+
end
15+
16+
it 'stomps the failures written the prior time' do
17+
persist_failures("./tmp", %w[ failure1 failure17 ])
18+
persist_failures("./tmp", %w[ failure2 failure18 ])
19+
20+
expect(persisted_from("./tmp")).to eq(%w[ failure2 failure18 ])
21+
end
22+
23+
it 'separates its storage based on the given directory name' do
24+
FileUtils.mkdir_p("./tmp/project1")
25+
FileUtils.mkdir_p("./tmp/project2")
26+
27+
persist_failures("./tmp/project1", %w[ failure1 ])
28+
persist_failures("./tmp/project2", %w[ failure2 ])
29+
30+
expect(persisted_from("./tmp/project1")).to eq(%w[ failure1 ])
31+
expect(persisted_from("./tmp/project2")).to eq(%w[ failure2 ])
32+
end
33+
34+
it 'makes any intermediary directories that do not yet exist so that it can write there' do
35+
FileUtils.rm_rf("./tmp/start")
36+
persist_failures("./tmp/start/of/long/path", %w[ f1 ])
37+
expect(persisted_from("./tmp/start/of/long/path")).to eq(%w[ f1 ])
38+
end
39+
40+
describe ".for_current_project.directory", :isolated_home do
41+
let(:dir_for_current_project) { LastRunPersister.for_current_project.directory }
42+
43+
it 'starts with #{home}/.rspec_last_run so it is scoped to the user' do
44+
expect(dir_for_current_project).to start_with(File.join(Dir.home, ".rspec_last_run"))
45+
end
46+
47+
it 'includes the current project path so that it is scoped to the project' do
48+
expect(dir_for_current_project).to end_with(RubyProject.root)
49+
end
50+
end
51+
end
52+
end

0 commit comments

Comments
 (0)