Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/fixture_kit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/fixture_kit/file_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Comment on lines +36 to +39
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you localize these rescues so it's not rescuing errors across many statements doing very different things? Maybe move the JSON.parse(...) into its own method and do the rescue there. To DRY up the error message, you can bake it into the error class and you'd just pass in the error and the path.

end

def write(data)
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/file_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down