# frozen_string_literal: true require "test_helper" class TestCache < Minitest::Test def with_cache Dir.mktmpdir("picopackage-cache") { |root| yield Picopackage::Cache.new(root: root) } end def test_stores_and_fetches_by_content with_cache do |cache| checksum = cache.store("hello\n") assert_equal Picopackage::Payload.checksum("hello\n"), checksum assert_equal "hello\n", cache.fetch(checksum) end end def test_storing_twice_is_harmless with_cache do |cache| a = cache.store("hello\n") b = cache.store("hello\n") assert_equal a, b assert_equal "hello\n", cache.fetch(a) end end def test_unknown_and_malformed_checksums_return_nil with_cache do |cache| assert_nil cache.fetch(Picopackage::Payload.checksum("never stored")) assert_nil cache.fetch(nil) assert_nil cache.fetch("") assert_nil cache.fetch("sha256:../../escape") refute cache.include?(nil) end end end