diff --git a/lib/fixture_kit.rb b/lib/fixture_kit.rb index 7caf094..33557a1 100644 --- a/lib/fixture_kit.rb +++ b/lib/fixture_kit.rb @@ -6,6 +6,7 @@ class DuplicateNameError < Error; end class InvalidFixtureDeclaration < Error; end class MultipleFixtures < Error; end class CacheMissingError < Error; end + class CacheCorruptError < Error; end class FixtureDefinitionNotFound < Error; end class RunnerAlreadyStartedError < Error; end class CircularFixtureInheritance < Error; end diff --git a/lib/fixture_kit/file_cache.rb b/lib/fixture_kit/file_cache.rb index 51da248..b58bd91 100644 --- a/lib/fixture_kit/file_cache.rb +++ b/lib/fixture_kit/file_cache.rb @@ -33,6 +33,10 @@ def read end MemoryCache.new(data: data, exposed: exposed) + rescue JSON::ParserError, KeyError => e + raise FixtureKit::CacheCorruptError, + "FixtureKit cache file at #{path} is corrupt or malformed (#{e.class}: #{e.message}). " \ + "Delete it and re-run to regenerate." end def write(data) diff --git a/spec/unit/file_cache_spec.rb b/spec/unit/file_cache_spec.rb index e3415e8..0342548 100644 --- a/spec/unit/file_cache_spec.rb +++ b/spec/unit/file_cache_spec.rb @@ -83,6 +83,24 @@ expect(File.exist?(nested_path)).to be(true) end + + it "raises CacheCorruptError when the file contains invalid JSON" do + FileUtils.mkdir_p(cache_path) + File.write(file_path, "this is not json") + + expect { file_cache.read }.to raise_error(FixtureKit::CacheCorruptError) do |error| + expect(error.message).to include(file_path) + expect(error.message).to include("JSON::ParserError") + end + end + + it "raises CacheCorruptError when the JSON is missing required keys" do + FileUtils.mkdir_p(cache_path) + File.write(file_path, JSON.dump({ "data" => {} })) # no "exposed" key + + expect { file_cache.read } + .to raise_error(FixtureKit::CacheCorruptError, /is corrupt or malformed.*KeyError/) + end end describe "#serialize_exposed" do