Skip to content

Commit 0eec3e5

Browse files
Copilotnhorton
andcommitted
Refactor committed cache specs based on PR feedback
- Remove initial cache population block (files now exist) - Add shared examples for cache validation - Move cache entries to top-level let blocks - Configure store with compress: false and raw: true - Use JSON for readable value files - Regenerate value files with readable JSON format Co-authored-by: nhorton <[email protected]>
1 parent 3cc89fc commit 0eec3e5

File tree

4 files changed

+65
-80
lines changed

4 files changed

+65
-80
lines changed

spec/committed_cache_spec.rb

Lines changed: 65 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,55 @@
11
# frozen_string_literal: true
22

33
require "spec_helper"
4+
require "json"
45

56
RSpec.describe "Committed Cache Directory" do
67
# Use a non-git-ignored directory for cache storage
78
let(:committed_cache_path) { File.join(__dir__, "fixtures", "committed_cache") }
8-
let(:store) { ActiveSupport::Cache::SourceControlCacheStore.new(cache_path: committed_cache_path) }
9-
10-
describe "initial cache population" do
11-
it "creates the cache directory if it doesn't exist" do
12-
expect(File.directory?(committed_cache_path)).to be true
9+
# Configure store with raw mode to avoid compression
10+
let(:store) { ActiveSupport::Cache::SourceControlCacheStore.new(cache_path: committed_cache_path, compress: false) }
11+
12+
# Predefined cache entries (keys and values as JSON strings for readability)
13+
let(:cache_entries) do
14+
{
15+
"user:123:profile" => { name: "John Doe", email: "[email protected]" }.to_json,
16+
"user:456:profile" => { name: "Jane Smith", email: "[email protected]" }.to_json,
17+
"config:app:settings" => { theme: "dark", language: "en" }.to_json
18+
}
19+
end
20+
21+
# Shared examples for validating cache state
22+
shared_examples "validates committed cache files" do
23+
it "has the expected number of key files" do
24+
key_files = Dir.glob(File.join(committed_cache_path, "*.key"))
25+
expect(key_files.length).to eq(3)
1326
end
14-
15-
it "populates cache with predefined entries" do
16-
# Write predefined cache entries that will be committed
17-
store.write("user:123:profile", { name: "John Doe", email: "[email protected]" })
18-
store.write("user:456:profile", { name: "Jane Smith", email: "[email protected]" })
19-
store.write("config:app:settings", { theme: "dark", language: "en" })
20-
21-
# Verify the entries were written
22-
expect(store.read("user:123:profile")).to eq({ name: "John Doe", email: "[email protected]" })
23-
expect(store.read("user:456:profile")).to eq({ name: "Jane Smith", email: "[email protected]" })
24-
expect(store.read("config:app:settings")).to eq({ theme: "dark", language: "en" })
27+
28+
it "has the expected number of value files" do
29+
value_files = Dir.glob(File.join(committed_cache_path, "*.value"))
30+
expect(value_files.length).to eq(3)
2531
end
26-
27-
it "creates .key and .value files for each entry" do
28-
# Ensure files exist
29-
cache_files = Dir.glob(File.join(committed_cache_path, "*"))
30-
31-
# We expect at least 6 files (3 entries × 2 files each)
32-
expect(cache_files.length).to be >= 6
33-
34-
# Check that we have both .key and .value files
35-
key_files = cache_files.select { |f| f.end_with?(".key") }
36-
value_files = cache_files.select { |f| f.end_with?(".value") }
37-
38-
expect(key_files.length).to be >= 3
39-
expect(value_files.length).to be >= 3
32+
33+
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
36+
expect(key_contents).to contain_exactly(*cache_entries.keys.sort)
37+
end
38+
39+
it "has valid value files that can be deserialized" do
40+
value_files = Dir.glob(File.join(committed_cache_path, "*.value"))
41+
# All value files should be readable
42+
value_files.each do |value_file|
43+
expect(File.read(value_file).length).to be > 0
44+
end
45+
end
46+
47+
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
51+
# Should have exactly 7 files (3 entries × 2 files each + 1 README.md)
52+
expect(current_files.length).to eq(7)
4053
end
4154
end
4255

@@ -49,20 +62,20 @@
4962
end
5063

5164
before(:each) do
52-
# Ensure cache entries exist before each test
53-
store.write("user:123:profile", { name: "John Doe", email: "[email protected]" })
54-
store.write("user:456:profile", { name: "Jane Smith", email: "[email protected]" })
55-
store.write("config:app:settings", { theme: "dark", language: "en" })
65+
# Ensure cache entries exist before each test using raw mode
66+
cache_entries.each do |key, value|
67+
store.write(key, value, raw: true)
68+
end
5669
end
5770

5871
it "does not create new files when reading existing entries" do
5972
# Capture state before reading
6073
files_before = initial_file_list
6174

62-
# Read existing entries
63-
store.read("user:123:profile")
64-
store.read("user:456:profile")
65-
store.read("config:app:settings")
75+
# Read existing entries with raw mode
76+
cache_entries.keys.each do |key|
77+
store.read(key, raw: true)
78+
end
6679

6780
# Get current file list
6881
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
@@ -77,10 +90,10 @@
7790
# Capture state before writing
7891
files_before = initial_file_list
7992

80-
# Write same values to existing keys
81-
store.write("user:123:profile", { name: "John Doe", email: "[email protected]" })
82-
store.write("user:456:profile", { name: "Jane Smith", email: "[email protected]" })
83-
store.write("config:app:settings", { theme: "dark", language: "en" })
93+
# Write same values to existing keys with raw mode
94+
cache_entries.each do |key, value|
95+
store.write(key, value, raw: true)
96+
end
8497

8598
# Get current file list
8699
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
@@ -93,18 +106,9 @@
93106

94107
it "has all expected cache files present" do
95108
# Verify that all expected keys exist
96-
expect(store.read("user:123:profile")).to eq({ name: "John Doe", email: "[email protected]" })
97-
expect(store.read("user:456:profile")).to eq({ name: "Jane Smith", email: "[email protected]" })
98-
expect(store.read("config:app:settings")).to eq({ theme: "dark", language: "en" })
99-
end
100-
101-
it "maintains the exact file count" do
102-
current_files = Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
103-
.reject { |f| File.directory?(f) }
104-
.sort
105-
106-
# Should have exactly 7 files (3 entries × 2 files each + 1 README.md)
107-
expect(current_files.length).to eq(7)
109+
cache_entries.each do |key, expected_value|
110+
expect(store.read(key, raw: true)).to eq(expected_value)
111+
end
108112
end
109113

110114
it "does not create new files during multiple read operations" do
@@ -113,9 +117,9 @@
113117

114118
# Perform multiple read operations
115119
10.times do
116-
store.read("user:123:profile")
117-
store.read("user:456:profile")
118-
store.read("config:app:settings")
120+
cache_entries.keys.each do |key|
121+
store.read(key, raw: true)
122+
end
119123
end
120124

121125
# Get current file list
@@ -126,32 +130,13 @@
126130
# Verify no new files were created
127131
expect(current_files).to eq(files_before)
128132
end
133+
134+
include_examples "validates committed cache files"
129135
end
130136

131137
describe "file content verification" do
132-
it "preserves original keys in .key files" do
133-
key_files = Dir.glob(File.join(committed_cache_path, "*.key"))
134-
expect(key_files.length).to eq(3)
135-
136-
# Read all key files and verify they contain expected keys
137-
key_contents = key_files.map { |f| File.read(f) }.sort
138-
expect(key_contents).to contain_exactly(
139-
"config:app:settings",
140-
"user:123:profile",
141-
"user:456:profile"
142-
)
143-
end
144-
145-
it "has valid value files that can be deserialized" do
146-
value_files = Dir.glob(File.join(committed_cache_path, "*.value"))
147-
expect(value_files.length).to eq(3)
148-
149-
# All value files should be readable and deserializable
150-
value_files.each do |value_file|
151-
expect(File.read(value_file).length).to be > 0
152-
end
153-
end
154-
138+
include_examples "validates committed cache files"
139+
155140
it "includes README.md documentation" do
156141
readme_path = File.join(committed_cache_path, "README.md")
157142
expect(File.exist?(readme_path)).to be true
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)