def test_cache_invalidation(self):
        """Test using a hash to force cache invalidation."""
        cache_dir = self.get_temp_dir()
        src_path = os.path.join(self.get_temp_dir(), "test.txt")
        with open(src_path, "w") as text_file:
            text_file.write("Float like a butterfly, sting like a bee.")
        orig_hash = file_utils.hash_file(src_path)
        origin = urllib.parse.urljoin(
            "file://", urllib.request.pathname2url(os.path.abspath(src_path))
        )
        # Download into the cache.
        dest_path = file_utils.get_file(
            "test.txt", origin, file_hash=orig_hash, cache_dir=cache_dir
        )
        self.assertEqual(orig_hash, file_utils.hash_file(dest_path))

        with open(src_path, "w") as text_file:
            text_file.write("Float like a zeppelin, sting like a jellyfish.")
        new_hash = file_utils.hash_file(src_path)
        # Without a hash, we should get the cached version.
        dest_path = file_utils.get_file("test.txt", origin, cache_dir=cache_dir)
        self.assertEqual(orig_hash, file_utils.hash_file(dest_path))
        # Without the new hash, we should re-download.
        dest_path = file_utils.get_file(
            "test.txt", origin, file_hash=new_hash, cache_dir=cache_dir
        )
        self.assertEqual(new_hash, file_utils.hash_file(dest_path))
Test using a hash to force cache invalidation.
keras/src/utils/file_utils_test.py