Skip to content

Commit f4e2f2c

Browse files
Copilotnhorton
andcommitted
DRY up spec with let statements for file lists
- Extract Dir.glob calls to top-level let statements - Add key_files, value_files, all_files, and key_contents helpers - Simplify shared examples to use let helpers - Simplify test assertions by reusing all_files instead of inline Dir.glob - Reduces code duplication and improves readability Co-authored-by: nhorton <[email protected]>
1 parent 0eec3e5 commit f4e2f2c

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

spec/committed_cache_spec.rb

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,46 @@
1818
}
1919
end
2020

21+
# File list helpers
22+
let(:key_files) { Dir.glob(File.join(committed_cache_path, "*.key")) }
23+
let(:value_files) { Dir.glob(File.join(committed_cache_path, "*.value")) }
24+
let(:all_files) do
25+
Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
26+
.reject { |f| File.directory?(f) }
27+
.sort
28+
end
29+
let(:key_contents) { key_files.map { |f| File.read(f) }.sort }
30+
2131
# Shared examples for validating cache state
2232
shared_examples "validates committed cache files" do
2333
it "has the expected number of key files" do
24-
key_files = Dir.glob(File.join(committed_cache_path, "*.key"))
2534
expect(key_files.length).to eq(3)
2635
end
2736

2837
it "has the expected number of value files" do
29-
value_files = Dir.glob(File.join(committed_cache_path, "*.value"))
3038
expect(value_files.length).to eq(3)
3139
end
3240

3341
it "preserves original keys in .key files" do
34-
key_files = Dir.glob(File.join(committed_cache_path, "*.key"))
35-
key_contents = key_files.map { |f| File.read(f) }.sort
3642
expect(key_contents).to contain_exactly(*cache_entries.keys.sort)
3743
end
3844

3945
it "has valid value files that can be deserialized" do
40-
value_files = Dir.glob(File.join(committed_cache_path, "*.value"))
4146
# All value files should be readable
4247
value_files.each do |value_file|
4348
expect(File.read(value_file).length).to be > 0
4449
end
4550
end
4651

4752
it "maintains the exact file count" do
48-
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
49-
.reject { |f| File.directory?(f) }
50-
.sort
5153
# Should have exactly 7 files (3 entries × 2 files each + 1 README.md)
52-
expect(current_files.length).to eq(7)
54+
expect(all_files.length).to eq(7)
5355
end
5456
end
5557

5658
describe "cache stability verification" do
5759
# Capture initial state for comparison
58-
let(:initial_file_list) do
59-
Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
60-
.reject { |f| File.directory?(f) }
61-
.sort
62-
end
60+
let(:initial_file_list) { all_files }
6361

6462
before(:each) do
6563
# Ensure cache entries exist before each test using raw mode
@@ -77,13 +75,8 @@
7775
store.read(key, raw: true)
7876
end
7977

80-
# Get current file list
81-
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
82-
.reject { |f| File.directory?(f) }
83-
.sort
84-
8578
# Verify no new files were created
86-
expect(current_files).to eq(files_before)
79+
expect(all_files).to eq(files_before)
8780
end
8881

8982
it "does not create new files when writing to existing keys with same values" do
@@ -95,13 +88,8 @@
9588
store.write(key, value, raw: true)
9689
end
9790

98-
# Get current file list
99-
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
100-
.reject { |f| File.directory?(f) }
101-
.sort
102-
10391
# Verify no new files were created (same files should exist)
104-
expect(current_files).to eq(files_before)
92+
expect(all_files).to eq(files_before)
10593
end
10694

10795
it "has all expected cache files present" do
@@ -122,13 +110,8 @@
122110
end
123111
end
124112

125-
# Get current file list
126-
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
127-
.reject { |f| File.directory?(f) }
128-
.sort
129-
130113
# Verify no new files were created
131-
expect(current_files).to eq(files_before)
114+
expect(all_files).to eq(files_before)
132115
end
133116

134117
include_examples "validates committed cache files"

0 commit comments

Comments
 (0)