Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions spec/committed_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# frozen_string_literal: true

require "spec_helper"
require "json"

RSpec.describe "Committed Cache Directory" do
# Use a non-git-ignored directory for cache storage
let(:committed_cache_path) { File.join(__dir__, "fixtures", "committed_cache") }
# Configure store with raw mode to avoid compression
let(:store) { ActiveSupport::Cache::SourceControlCacheStore.new(cache_path: committed_cache_path, compress: false) }

# Predefined cache entries (keys and values as JSON strings for readability)
let(:cache_entries) do
{
"user:123:profile" => { name: "John Doe", email: "john@example.com" }.to_json,
"user:456:profile" => { name: "Jane Smith", email: "jane@example.com" }.to_json,
"config:app:settings" => { theme: "dark", language: "en" }.to_json
}
end

# File list helpers
let(:key_files) { Dir.glob(File.join(committed_cache_path, "*.key")) }
let(:value_files) { Dir.glob(File.join(committed_cache_path, "*.value")) }
let(:all_files) do
Dir.glob(File.join(committed_cache_path, "**", "*"), File::FNM_DOTMATCH)
.reject { |f| File.directory?(f) }
.sort
end
let(:key_contents) { key_files.map { |f| File.read(f) }.sort }

# Shared examples for validating cache state
shared_examples "validates committed cache files" do
it "has the expected number of key files" do
expect(key_files.length).to eq(3)
end

it "has the expected number of value files" do
expect(value_files.length).to eq(3)
end

it "preserves original keys in .key files" do
expect(key_contents).to contain_exactly(*cache_entries.keys.sort)
end

it "has valid value files that can be deserialized" do
# All value files should be readable
value_files.each do |value_file|
expect(File.read(value_file).length).to be > 0
end
end

it "maintains the exact file count" do
# Should have exactly 7 files (3 entries × 2 files each + 1 README.md)
expect(all_files.length).to eq(7)
end
end

describe "cache stability verification" do
# Capture initial state for comparison
let(:initial_file_list) { all_files }

before(:each) do
# Ensure cache entries exist before each test using raw mode
cache_entries.each do |key, value|
store.write(key, value, raw: true)
end
end

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

# Read existing entries with raw mode
cache_entries.keys.each do |key|
store.read(key, raw: true)
end

# Verify no new files were created
expect(all_files).to eq(files_before)
end

it "does not create new files when writing to existing keys with same values" do
# Capture state before writing
files_before = initial_file_list

# Write same values to existing keys with raw mode
cache_entries.each do |key, value|
store.write(key, value, raw: true)
end

# Verify no new files were created (same files should exist)
expect(all_files).to eq(files_before)
end

it "has all expected cache files present" do
# Verify that all expected keys exist
cache_entries.each do |key, expected_value|
expect(store.read(key, raw: true)).to eq(expected_value)
end
end

it "does not create new files during multiple read operations" do
# Capture state before reading
files_before = initial_file_list

# Perform multiple read operations
10.times do
cache_entries.keys.each do |key|
store.read(key, raw: true)
end
end

# Verify no new files were created
expect(all_files).to eq(files_before)
end

include_examples "validates committed cache files"
end

describe "file content verification" do
include_examples "validates committed cache files"

it "includes README.md documentation" do
readme_path = File.join(committed_cache_path, "README.md")
expect(File.exist?(readme_path)).to be true
expect(File.read(readme_path)).to include("Committed Cache Directory")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config:app:settings
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user:123:profile
Binary file not shown.
20 changes: 20 additions & 0 deletions spec/fixtures/committed_cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Committed Cache Directory

This directory contains cache files that are intentionally committed to source control for testing purposes.

## Purpose

This directory is used by the `committed_cache_spec.rb` test suite to verify that:
1. Cache files can be stored in a non-git-ignored directory
2. Once populated, no new files are created on subsequent runs
3. The cache remains stable across different environments and PRs

## Files

The cache files in this directory are generated by the test suite and should not be manually modified.
They represent a stable set of cache entries used for validation testing.

## Do Not Ignore

**Important:** This directory and its contents should NOT be added to `.gitignore`.
The cache files are intentionally version-controlled to ensure test stability.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user:456:profile
Binary file not shown.