Exemple #1
0
 def test_eq(self) -> None:
     """test FileCache.__eq__"""
     with FileCache(Directory("tests/embedding", compiler), "tmp") as cache1, \
             FileCache(Directory("tests/embedding", compiler2), "tmp") as cache2, \
             FileCache(Directory("tests/embedding", compiler), "tmp2") as cache3:
         self.assertEqual(cache1, cache1)
         self.assertNotEqual(cache1, cache2)
         self.assertNotEqual(cache2, cache3)
         self.assertNotEqual(1, cache1)
Exemple #2
0
 def test_paths(self) -> None:
     """test FileCache.paths"""
     with tempfile.TemporaryDirectory() as directory, \
             FileCache(Directory("tests/embedding", compiler), directory) as cache:
         tmp_path = os.path.join(directory, "test.txt")
         dir_path = os.path.join(directory, "test" + FILE_EXTENSION)
         with cache["syntax.pyhp"] as source:
             source.fetch()
         with cache["shebang.pyhp"] as source:
             source.fetch()
         try:
             open(tmp_path, "xb").close()
             try:
                 os.mkdir(dir_path)
                 try:
                     paths = list(cache.paths())
                     self.assertEqual(len(paths), 2)
                     self.assertEqual(  # order not important, use sets
                         set(paths), {
                             cache.path("syntax.pyhp"),
                             cache.path("shebang.pyhp")
                         })
                 finally:
                     os.rmdir(dir_path)
             finally:
                 os.unlink(tmp_path)
         finally:
             cache.clear()
Exemple #3
0
 def test_reconstruct_name(self) -> None:
     """test FileCache.reconstruct_name"""
     names = ["test", "test/42/9", "äöü|<", "\n\n\n"]
     with FileCache(Directory("tests/embedding", compiler), "tmp") as cache:
         for name in names:
             path = cache.path(name)
             self.assertEqual(cache.reconstruct_name(path.lower()), name)
             self.assertEqual(cache.reconstruct_name(path.upper()), name)
Exemple #4
0
 def test_path(self) -> None:
     """test FileCache.path"""
     with FileCache(Directory("tests/embedding", compiler), "tmp") as cache:
         self.assertTrue(cache.path("test").startswith("tmp"))
         self.assertNotEqual(cache.path("test"), cache.path("test2"))
         self.assertNotIn("|", cache.path("|"))
         self.assertEqual(cache.reconstruct_name(cache.path("test2")),
                          "test2")
Exemple #5
0
 def test_access(self) -> None:
     """test FileCache.__getitem__"""
     with FileCache(Directory("tests/embedding", compiler), "tmp") as cache, \
             cache["syntax.pyhp"] as source:
         self.assertTrue(source.path.startswith("tmp"))
         self.assertEqual(source.ttl, 0)
         self.assertEqual(os.path.normpath(source.code_source.fd.name),
                          os.path.normpath("tests/embedding/syntax.pyhp"))
Exemple #6
0
 def test_clear(self) -> None:
     """test FileCache.clear"""
     with tempfile.TemporaryDirectory() as directory, \
             FileCache(Directory("tests/embedding", compiler), directory) as cache:
         with cache["syntax.pyhp"] as source:
             source.fetch()
         with cache["shebang.pyhp"] as source:
             source.fetch()
         self.assertTrue(os.path.exists(cache.path("syntax.pyhp")))
         self.assertTrue(os.path.exists(cache.path("shebang.pyhp")))
         cache.clear()
         self.assertFalse(os.path.exists(cache.path("syntax.pyhp")))
         self.assertFalse(os.path.exists(cache.path("shebang.pyhp")))
Exemple #7
0
 def test_from_config(self) -> None:
     """test FileCache.from_config"""
     container = Directory("tests/embedding", compiler)
     with FileCache.from_config({"directory_name": "~"},
                                container) as cache:
         self.assertEqual(cache.directory_name, os.path.expanduser("~"))
         self.assertEqual(cache.ttl, 0)
     with FileCache.from_config({
             "directory_name": "~",
             "ttl": 9
     }, container) as cache:
         self.assertEqual(cache.ttl, 9e9)
     with self.assertRaises(KeyError):
         FileCache.from_config({}, container)
     with self.assertRaises(ValueError):
         FileCache.from_config({"directory_name": 9}, container)
     with self.assertRaises(ValueError):
         FileCache.from_config({
             "directory_name": "~",
             "ttl": "a"
         }, container)
     with self.assertRaises(ValueError):
         FileCache.from_config({"directory_name": "~"}, compiler)
Exemple #8
0
 def test_timestamps(self) -> None:
     """test FileCache timestamp methods"""
     mock = unittest.mock.Mock(spec_set=TimestampedCodeSourceContainer)
     mock.mtime.configure_mock(
         side_effect=lambda name: 1 if name == "test" else 0)
     mock.ctime.configure_mock(
         side_effect=lambda name: 2 if name == "test" else 0)
     mock.atime.configure_mock(
         side_effect=lambda name: 3 if name == "test" else 0)
     mock.info.configure_mock(side_effect=lambda name: SourceInfo(4, 5, 6)
                              if name == "test" else SourceInfo(0, 0, 0))
     with FileCache(mock, "tmp") as cache:
         self.assertEqual(cache.mtime("test"), 1)
         self.assertEqual(cache.ctime("test"), 2)
         self.assertEqual(cache.atime("test"), 3)
         self.assertEqual(cache.info("test"), SourceInfo(4, 5, 6))
Exemple #9
0
 def test_gc_clear(self) -> None:
     """test FileCache.gc and FileCache.clear"""
     with tempfile.TemporaryDirectory() as directory, \
             FileCache(Directory("tests/embedding", compiler), directory) as cache:
         tmp_file = os.path.join(directory, "tmp.pyhp")
         open(tmp_file, "x").close()
         try:
             with cache["syntax.pyhp"] as source:
                 source.fetch()
             with cache["shebang.pyhp"] as source:
                 source.fetch()
             with cache[tmp_file] as source:
                 source.fetch()
             os.utime(cache.path("shebang.pyhp"), (0, 0))
         finally:
             os.unlink(tmp_file)
         try:
             self.assertEqual(cache.gc(), 2)
             self.assertTrue(os.path.exists(cache.path("syntax.pyhp")))
             self.assertFalse(os.path.exists(cache.path("shebang.pyhp")))
             self.assertFalse(os.path.exists(cache.path(tmp_file)))
         finally:
             cache.clear()