Skip to content

Commit a128599

Browse files
refactor(test): improve readability and maintainability
by extracting setup data into lets, enhancing context and example descriptions, and removing duplication
1 parent 934d312 commit a128599

9 files changed

Lines changed: 88 additions & 69 deletions

File tree

spec/doc_guard/assess_documentation_relevance/process_spec.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
require "spec_helper"
44

55
RSpec.describe DocGuard::AssessDocumentationRelevance::Process do
6-
subject(:process) { described_class.new(config: config) }
6+
describe ".run" do
7+
subject(:run_process) { described_class.run(config: config) }
78

8-
let(:config) { instance_double(DocGuard::Config) }
9+
let(:config) { instance_double(DocGuard::Config) }
10+
let(:process_instance) { instance_double(described_class) }
911

10-
describe ".run" do
1112
it "instantiates and calls #call" do
12-
instance = instance_double(described_class)
13-
expect(described_class).to receive(:new).with(config: config).and_return(instance)
14-
expect(instance).to receive(:call)
13+
expect(described_class).to receive(:new).with(config: config).and_return(process_instance)
14+
expect(process_instance).to receive(:call)
1515

16-
described_class.run(config: config)
16+
run_process
1717
end
1818
end
1919

2020
describe "#call" do
21+
subject(:process) { described_class.new(config: config) }
22+
23+
let(:config) { instance_double(DocGuard::Config) }
2124
let(:tracked_files) { { "docs/a.md" => ["lib/a.rb", "lib/b.rb"] } }
2225
let(:stored_digests) { { "docs/a.md" => { "lib/a.rb" => "digest1", "lib/b.rb" => "digest2" } } }
2326
let(:current_digests) { { "docs/a.md" => { "lib/a.rb" => "digest1", "lib/b.rb" => "digest_changed" } } }

spec/doc_guard/assess_documentation_relevance/subprocesses/assess_relevance_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
context "when no source files have changed" do
1616
let(:mismatches) { [] }
1717

18-
it "returns relevant: false and empty mismatches" do
18+
it "returns `relevant: false` and empty mismatches" do
1919
expect(assessment[:relevant]).to be false
2020
expect(assessment[:mismatches]).to be_empty
2121
end
@@ -24,7 +24,7 @@
2424
context "when some source files have changed" do
2525
let(:mismatches) { ["app/models/user.rb"] }
2626

27-
it "returns relevant: true" do
27+
it "returns `relevant: true`" do
2828
expect(assessment[:relevant]).to be true
2929
end
3030

spec/doc_guard/assess_documentation_relevance/subprocesses/compare_digests_spec.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
RSpec.describe DocGuard::AssessDocumentationRelevance::Subprocesses::CompareDigests do
66
describe ".run" do
7-
subject(:result) { described_class.run(stored_digests: stored, current_digests: current) }
7+
subject(:result) { described_class.run(stored_digests: stored_digests, current_digests: current_digests) }
88

99
context "when there are no tracked files" do
10-
let(:stored) { {} }
11-
let(:current) { {} }
10+
let(:stored_digests) { {} }
11+
let(:current_digests) { {} }
1212

1313
it "returns an empty list" do
1414
expect(result).to eq([])
1515
end
1616
end
1717

1818
context "when stored and current digests are identical" do
19-
let(:stored) do
19+
let(:stored_digests) do
2020
{
2121
"docs/user.md" => {
2222
"app/models/user.rb" => "abc123",
@@ -25,7 +25,7 @@
2525
}
2626
end
2727

28-
let(:current) do
28+
let(:current_digests) do
2929
{
3030
"docs/user.md" => {
3131
"app/models/user.rb" => "abc123",
@@ -40,7 +40,7 @@
4040
end
4141

4242
context "when there is a mismatch in digests for a tracked file" do
43-
let(:stored) do
43+
let(:stored_digests) do
4444
{
4545
"docs/user.md" => {
4646
"app/models/user.rb" => "abc123",
@@ -49,7 +49,7 @@
4949
}
5050
end
5151

52-
let(:current) do
52+
let(:current_digests) do
5353
{
5454
"docs/user.md" => {
5555
"app/models/user.rb" => "abc123",
@@ -64,15 +64,15 @@
6464
end
6565

6666
context "when a source file is missing in current digests" do
67-
let(:stored) do
67+
let(:stored_digests) do
6868
{
6969
"docs/admin.md" => {
7070
"app/models/admin.rb" => "abc123"
7171
}
7272
}
7373
end
7474

75-
let(:current) do
75+
let(:current_digests) do
7676
{
7777
"docs/admin.md" => {
7878
# missing app/models/admin.rb
@@ -86,15 +86,15 @@
8686
end
8787

8888
context "when new documentation files are introduced in current digests" do
89-
let(:stored) do
89+
let(:stored_digests) do
9090
{
9191
"docs/user.md" => {
9292
"app/models/user.rb" => "abc123"
9393
}
9494
}
9595
end
9696

97-
let(:current) do
97+
let(:current_digests) do
9898
{
9999
"docs/user.md" => {
100100
"app/models/user.rb" => "abc123"
@@ -111,15 +111,15 @@
111111
end
112112

113113
context "when stored digests have a doc file that current digests do not" do
114-
let(:stored) do
114+
let(:stored_digests) do
115115
{
116116
"docs/old.md" => {
117117
"app/old/file.rb" => "old_digest"
118118
}
119119
}
120120
end
121121

122-
let(:current) do
122+
let(:current_digests) do
123123
{
124124
# docs/old.md missing entirely
125125
}
@@ -131,7 +131,7 @@
131131
end
132132

133133
context "when multiple doc files have mismatches" do
134-
let(:stored) do
134+
let(:stored_digests) do
135135
{
136136
"docs/user.md" => {
137137
"app/models/user.rb" => "abc123",
@@ -143,7 +143,7 @@
143143
}
144144
end
145145

146-
let(:current) do
146+
let(:current_digests) do
147147
{
148148
"docs/user.md" => {
149149
"app/models/user.rb" => "changed_digest",

spec/doc_guard/assess_documentation_relevance/subprocesses/report_assessment_spec.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717
}
1818
end
1919

20-
it "warns the user and exits with code 1" do
21-
expect { report }.to output(/‼️ Documentation may be outdated!/).to_stderr.and raise_error(SystemExit) do |error|
20+
it "warns the user with a message on stderr" do
21+
expect { expect { report }.to raise_error(SystemExit) }
22+
.to output(/‼️ Documentation may be outdated!/).to_stderr
23+
end
24+
25+
it "exits with code 1" do
26+
expect { report }.to raise_error(SystemExit) do |error|
2227
expect(error.status).to eq(1)
2328
end
2429
end
2530

26-
# rubocop:disable Layout/LineLength
2731
it "lists affected files in stderr" do
28-
expect { report }
32+
expect { expect { report }.to raise_error(SystemExit) }
2933
.to output(
30-
/‼️ Documentation may be outdated!\n\n📄 docs\/user.md\n- app\/models\/user.rb\n- app\/services\/authenticator.rb\n\n📄 docs\/admin.md\n- app\/models\/admin.rb\n/
31-
)
32-
.to_stderr
33-
.and raise_error(SystemExit) { |error| expect(error.status).to eq(1) }
34+
/📄 docs\/user.md\n- app\/models\/user.rb\n- app\/services\/authenticator.rb\n\n📄 docs\/admin.md\n- app\/models\/admin.rb\n/
35+
).to_stderr
3436
end
35-
# rubocop:enable Layout/LineLength
3637
end
3738

3839
context "when documentation is up to date" do

spec/doc_guard/cli_spec.rb

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
subject(:cli) { described_class.new }
77

88
describe "assess command" do
9-
let(:process_instance) { instance_double(DocGuard::AssessDocumentationRelevance::Process) }
109
let(:config_instance) { instance_double(DocGuard::Config) }
1110

1211
before do
@@ -27,10 +26,15 @@
2726
end
2827
end
2928

30-
context "When the assessment process found code changes relevant to the documentation" do
29+
context "when the assessment process found code changes relevant to the documentation" do
3130
let(:relevant) { true }
3231

33-
it "exits with SystemExit" do
32+
it "warns the user with a message on stderr" do
33+
expect { expect { cli.invoke(:assess) }.to raise_error(SystemExit) }
34+
.to output(/‼️ Documentation may be outdated!/).to_stderr
35+
end
36+
37+
it "exits with code 1" do
3438
expect { cli.invoke(:assess) }.to raise_error(SystemExit) do |error|
3539
expect(error.status).to eq(1)
3640
end
@@ -40,13 +44,15 @@
4044
context "when process raises an unexpected error" do
4145
before { allow(DocGuard::AssessDocumentationRelevance::Process).to receive(:run).and_raise(StandardError.new("unexpected error")) }
4246

43-
it "prints error message to stderr and exits with status 2" do
44-
expect { cli.invoke(:assess) }
45-
.to output(/‼️ Error: unexpected error/)
46-
.to_stderr
47-
.and raise_error(SystemExit) do |error|
48-
expect(error.status).to eq(2)
49-
end
47+
it "warns the user with a message on stderr" do
48+
expect { expect { cli.invoke(:assess) }.to raise_error(SystemExit) }
49+
.to output(/‼️ Error: unexpected error/).to_stderr
50+
end
51+
52+
it "exits with code 2" do
53+
expect { cli.invoke(:assess) }.to raise_error(SystemExit) do |error|
54+
expect(error.status).to eq(2)
55+
end
5056
end
5157
end
5258
end
@@ -83,11 +89,15 @@
8389
end
8490
end
8591

86-
it "prints failure message and exits with status 1" do
87-
expect { cli.invoke(:record) }
88-
.to output(/‼️ Failed to record documentation state: file write failed/)
89-
.to_stderr
90-
.and raise_error(SystemExit) { |e| expect(e.status).to eq(1) }
92+
it "warns the user with a message on stderr" do
93+
expect { expect { cli.invoke(:record) }.to raise_error(SystemExit) }
94+
.to output(/‼️ Failed to record documentation state: file write failed/).to_stderr
95+
end
96+
97+
it "exits with code 1" do
98+
expect { cli.invoke(:record) }.to raise_error(SystemExit) do |error|
99+
expect(error.status).to eq(1)
100+
end
91101
end
92102
end
93103

@@ -96,11 +106,15 @@
96106
allow(DocGuard::RecordDocumentationRelevance::Process).to receive(:run).and_raise(StandardError.new("unexpected failure"))
97107
end
98108

99-
it "prints error message and exits with status 2" do
100-
expect { cli.invoke(:record) }
101-
.to output(/‼️ Error: unexpected failure/)
102-
.to_stderr
103-
.and raise_error(SystemExit) { |e| expect(e.status).to eq(2) }
109+
it "warns the user with a message on stderr" do
110+
expect { expect { cli.invoke(:record) }.to raise_error(SystemExit) }
111+
.to output(/‼️ Error: unexpected failure/).to_stderr
112+
end
113+
114+
it "exits with code 2" do
115+
expect { cli.invoke(:record) }.to raise_error(SystemExit) do |error|
116+
expect(error.status).to eq(2)
117+
end
104118
end
105119
end
106120
end

spec/doc_guard/config_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,7 @@
146146

147147
around do |example|
148148
ENV["DOC_GUARD_CONFIG_FILE_PATH"] = yaml_path.path
149-
150149
example.run
151-
152150
ENV.delete("DOC_GUARD_CONFIG_FILE_PATH")
153151
end
154152

spec/doc_guard/record_documentation_relevance/process_spec.rb

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

55
RSpec.describe DocGuard::RecordDocumentationRelevance::Process do
66
describe ".run" do
7-
let(:process) { instance_double(described_class) }
7+
subject(:run_process) { described_class.run(config: config) }
8+
89
let(:config) { instance_double(DocGuard::Config) }
10+
let(:process_instance) { instance_double(described_class) }
911

1012
it "instantiates and calls the process" do
11-
expect(described_class).to receive(:new).with(config: config).and_return(process)
12-
expect(process).to receive(:call)
13+
expect(described_class).to receive(:new).with(config: config).and_return(process_instance)
14+
expect(process_instance).to receive(:call)
1315

14-
described_class.run(config: config)
16+
run_process
1517
end
1618
end
1719

1820
describe "#call" do
1921
subject(:process) { described_class.new(config: config) }
2022

2123
let(:config) { instance_double(DocGuard::Config) }
22-
2324
let(:tracked_files) do
2425
{
2526
"docs/README.md" => ["lib/foo.rb", "lib/bar.rb"]
2627
}
2728
end
28-
2929
let(:current_digests) do
3030
{
3131
"docs/README.md" => {
@@ -34,7 +34,6 @@
3434
}
3535
}
3636
end
37-
3837
let(:recording) { { status: :ok } }
3938

4039
before do

spec/doc_guard/record_documentation_relevance/subprocesses/record_digests_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@
1919

2020
context "when writing to file succeeds" do
2121
let(:config) { DocGuard::Config.new(digests_store_file_path: tmp_file_path) }
22+
let(:stored_digests_content) { JSON.parse(File.read(tmp_file_path)) }
2223

2324
it "writes the digests as JSON and returns `ok` status" do
2425
expect(recording[:status]).to eq(:ok)
2526
expect(File).to exist(tmp_file_path)
26-
27-
json = JSON.parse(File.read(tmp_file_path))
28-
expect(json).to eq(digests.transform_keys(&:to_s))
27+
expect(stored_digests_content).to eq(digests.transform_keys(&:to_s))
2928
end
3029
end
3130

spec/doc_guard/record_documentation_relevance/subprocesses/report_recording_spec.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
describe ".run" do
77
subject(:report) { described_class.run(recording: recording) }
88

9-
context "when recording status is :ok" do
9+
context "when recording status is `ok`" do
1010
let(:recording) { { status: :ok } }
1111

1212
it "prints success message to stdout" do
1313
expect { report }.to output("✅ Documentation state recorded successfully.\n").to_stdout
1414
end
1515
end
1616

17-
context "when recording status is :error" do
17+
context "when recording status is `error`" do
1818
let(:recording) { { status: :error, error: error } }
1919
let(:error) { StandardError.new("something went wrong") }
2020

21-
it "warns the user and exits with code 1" do
22-
expect { report }.to output(/‼️ Failed to record documentation state: something went wrong/).to_stderr.and raise_error(SystemExit) do |error|
21+
it "warns the user with a message on stderr" do
22+
expect { expect { report }.to raise_error(SystemExit) }
23+
.to output(/‼️ Failed to record documentation state: something went wrong/).to_stderr
24+
end
25+
26+
it "exits with code 1" do
27+
expect { report }.to raise_error(SystemExit) do |error|
2328
expect(error.status).to eq(1)
2429
end
2530
end

0 commit comments

Comments
 (0)