diff --git a/casimport/__init__.py b/casimport/__init__.py index 36515b8..5098480 100644 --- a/casimport/__init__.py +++ b/casimport/__init__.py @@ -398,6 +398,11 @@ class CASFinder(MetaPathFinder, Loader): else: raise ValueError('unable to find loader for url %s' % repr(urllib.parse.urlunparse(url))) + hash = hashlib.sha256(data).hexdigest() + wrtr = self._writer + if wrtr is not None: + wrtr.write_cache(hash, data) + exec(data, module.__dict__) # we were successful, add the aliases @@ -767,7 +772,7 @@ class Test(unittest.TestCase): sys.path.append(fixdir) # that a fake ipfsloader - with open(self.fixtures / 'hello.py') as fp: + with open(self.fixtures / 'hello.py', 'rb') as fp: # that returns the correct data fakedata = fp.read() @@ -784,9 +789,20 @@ class Test(unittest.TestCase): with CASFinder() as f, \ tempattrset(sys.modules[__name__], 'load_mod_aliases', f.load_mod_aliases): + # that a cache dir + cachedir = self.tempdir / 'cache' + fd = FileDirCAS(cachedir) + # when registered + f.register(fd) + + # as a writer + f.register_write(fd) + + # and the fake ipfs module f.register(fakeipfsloader) + # and the pkg we are about to load, isn't self.assertNotIn('randpkg', sys.modules) # that the import is successful @@ -795,6 +811,12 @@ class Test(unittest.TestCase): # and pulled in the method self.assertTrue(hasattr(randpkg, 'hello')) + # that the cache was written to, and matches + hash = hashlib.sha256(fakedata).hexdigest() + filecmp.cmp(cachedir / '3' / (hash + '.py'), + self.fixtures / 'hello.py') + + # clean up del sys.modules['randpkg'] finally: sys.path.remove(fixdir)