def test_memory_cache(self): c = MemoryCache(capacity=2) assert c.get('non-existant') == None c.set('foo', 'bar') assert c.get('foo') == 'bar' # Since we have capacity=2, adding two more keys will # remove the first one. c.set('key2', 'value2') c.set('key3', 'value3') assert c.get('foo') == None
def test_dependency_refresh_with_cache(self): """If a bundle dependency is changed, the cache may not be used; otherwise, we'd be using previous build results from the cache, where we really need to do a refresh, because, for example, an included file has changed. """ def depends_fake(in_, out): out.write(self.get('d.sass')) self.m.updater = 'timestamp' self.m.cache = MemoryCache(100) self.create_files({'d.sass': 'initial', 'in': ''}) bundle = self.mkbundle('in', output='out', depends=('*.sass', ), filters=depends_fake) # Do an initial build to ensure we have the build steps in # the cache. bundle.build() assert self.get('out') == 'initial' assert self.m.cache.keys # Change the dependency self.create_files({'d.sass': 'new-value-12345'}) # Ensure the timestamps are such that dependency will # cause the rebuild. now = self.setmtime('out') self.setmtime('in', mtime=now - 100) self.setmtime('d.sass', mtime=now + 100) # Build again, verify result bundle.build() assert self.get('out') == 'new-value-12345'
def test_basic(self): with TempDirHelper() as helper: for cache in (FilesystemCache(helper.tempdir), MemoryCache(capacity=10000)): yield self._test_simple, cache yield self._test_hunks, cache yield self._test_filters, cache yield self._test_dicts, cache
def c(request): helper = TempDirHelper() helper.__enter__() if request.param == FilesystemCache: c = FilesystemCache(helper.tempdir) elif request.param == MemoryCache: c = MemoryCache(capacity=10000) else: raise ValueError(request.param) def end(): helper.__exit__(None, None, None) request.addfinalizer(end) return c
def test_bundle_definition_change(self): """Test that the timestamp updater uses the base class functionality of determining a bundle definition change as well. """ self.env.cache = MemoryCache(capacity=100) bundle = self.mkbundle('in', output='out') # Fake an initial build self.updater.build_done(bundle, self.env) # Make in file older than out file now = self.setmtime('out') self.setmtime('in', mtime=now - 100) assert self.updater.needs_rebuild(bundle, self.env) == False # Change the bundle definition bundle.filters = 'jsmin' # Timestamp updater will says we need to rebuild. assert self.updater.needs_rebuild(bundle, self.env) == True self.updater.build_done(bundle, self.env)
def _dependency_refresh_with_cache(self, rebuild_with_force): # We have to repeat these a lot DEPENDENCY = 'dependency.sass' DEPENDENCY_SUB = 'dependency_sub.sass' # Init a environment with a cache self.env.updater = TimestampUpdater() self.env.cache = MemoryCache(100) self.create_files({ DEPENDENCY: '-main', DEPENDENCY_SUB: '-sub', 'in': '', 'in_sub': '' }) # Create a bundle with a dependency, and a filter which # will cause the dependency content to be written. If # everything works as it should, a change in the # dependency should thus cause the output to change. bundle = self.mkbundle('in', output='out', depends=(DEPENDENCY, ), filters=lambda in_, out: out.write(in_.read( ) + self.get(DEPENDENCY))) # Additionally, to test how the cache usage of the parent # bundle affects child bundles, create a child bundle. # This one also writes the final content based on a dependency, # but one that is undeclared, so to not override the parent # cache behavior, the passing down of which we intend to test. # # This is a constructed setup so we can test whether the child # bundle was using the cache by looking at the output, not # something that makes sense in real usage. bundle.contents += (self.mkbundle( 'in_sub', filters=lambda in_, out: out.write(self.get(DEPENDENCY_SUB))), ) # Do an initial build to ensure we have the build steps in # the cache. bundle.build() assert self.get('out') == '\n-sub-main' assert self.env.cache.keys # Change the dependencies self.create_files({DEPENDENCY: '-main12345'}) self.create_files({DEPENDENCY_SUB: '-subABCDE'}) # Ensure the timestamps are such that dependency will # cause the rebuild. now = self.setmtime('out') self.setmtime('in', 'in_sub', mtime=now - 100) self.setmtime(DEPENDENCY, DEPENDENCY_SUB, mtime=now + 100) # Build again, verify result bundle.build(force=rebuild_with_force) # The main bundle has always updated (i.e. the cache # was invalidated/not used). # # The child bundle is still using the cache if force=True is # used, but will inherit the 'skip the cache' flag from the # parent when force=False. # There is no reason for this, it's just the way the code # currently works, and liable to change in the future. if rebuild_with_force: assert self.get('out') == '\n-sub-main12345' else: assert self.get('out') == '\n-subABCDE-main12345'
def get(self, key): self.getc += 1 return MemoryCache.get(self, key)
def setup(self): self.env = Environment(None, None) # we won't create files self.env.cache = MemoryCache(capacity=100) self.bundle = Bundle(output="target") self.updater = BundleDefUpdater()