def rewrite_import(app, id, callback, title='Modified Import'): """Calls a callback with the blog from the dump `id` for rewriting. The callback can modify the blog in place (it's passed as first argument) and the changes are written back to the filesystem as new dump. `app` can either be a `Zine` object that is also bound to the active thread or a string with the path to the instance folder. The latter is useful for simple scripts that should rewrite imports. """ if isinstance(app, basestring): from zine import setup app = setup(app) blog = load_import_dump(app, id) callback(blog) f = file(os.path.join(app.instance_folder, 'import_queue', '%d' % time()), 'wb') try: blog.dump(f, title) finally: f.close()
def suite(modnames=[], return_covermods=False): """Generate the test suite. The first argument is a list of modules to be tested. If it is empty (which it is by default), all sub-modules of the zine package are tested. If the second argument is True, this function returns two objects: a TestSuite instance and a list of the names of the tested modules. Otherwise (which is the default) it only returns the former. This is done so that this function can be used as setuptools' test_suite. """ # the app object is used for two purposes: # 1) plugins are not usable (i.e. not testable) without an initialised app # 2) for functions that require an application object as argument, you can # write >>> my_function(app, ...) in the tests # The instance directory of this object is located in the tests directory. # # setup isn't imported at module level because this way coverage # can track the whole zine imports from zine import setup instance_path = join(dirname(__file__), 'instance') app = setup(instance_path) if return_covermods: covermods = [] suite = TestSuite() if modnames == []: modnames = find_tp_modules() test_files = os.listdir(dirname(__file__)) for modname in modnames: if modname in untested: continue # the fromlist must contain something, otherwise the zine # package is returned, not our module try: mod = __import__(modname, None, None, ['']) except ImportError: # some plugins can have external dependencies (e.g. creoleparser, # pygments) that are not installed on the machine the tests are # run on. Therefore, just skip those (with an error message) if 'plugins.' in modname: sys.stderr.write('could not import plugin %s\n' % modname) continue else: raise suites = [DocTestSuite(mod, extraglobs={'app': app})] filename = modname[10:] + '.txt' if filename in test_files: globs = {'app': app} globs.update(mod.__dict__) suites.append(DocFileSuite(filename, globs=globs)) for i, subsuite in enumerate(suites): # skip modules without any tests if subsuite.countTestCases(): suite.addTest(subsuite) if return_covermods and i == 0: covermods.append(mod) if return_covermods: return suite, covermods else: return suite
def get_zine_instance(self): if not self.instance_path: self.parser.error("you need to pass your Zine instance path.") if not hasattr(self, "zine_instance"): self.zine_instance = setup(expanduser(self.instance_path)) return self.zine_instance
def get_zine_instance(self): if not self.instance_path: self.parser.error('you need to pass your Zine instance path.') if not hasattr(self, 'zine_instance'): self.zine_instance = setup(expanduser(self.instance_path)) return self.zine_instance
def suite(modnames=[], return_covermods=False): """Generate the test suite. The first argument is a list of modules to be tested. If it is empty (which it is by default), all sub-modules of the zine package are tested. If the second argument is True, this function returns two objects: a TestSuite instance and a list of the names of the tested modules. Otherwise (which is the default) it only returns the former. This is done so that this function can be used as setuptools' test_suite. """ # the app object is used for two purposes: # 1) plugins are not usable (i.e. not testable) without an initialised app # 2) for functions that require an application object as argument, you can # write >>> my_function(app, ...) in the tests # The instance directory of this object is located in the tests directory. # # setup isn't imported at module level because this way coverage # can track the whole zine imports from zine import setup instance_path = join(dirname(__file__), 'instance') app = setup(instance_path) if return_covermods: covermods = [] suite = TestSuite() if modnames == []: modnames = find_tp_modules() test_files = os.listdir(dirname(__file__)) for modname in modnames: if modname in untested: continue # the fromlist must contain something, otherwise the zine # package is returned, not our module try: mod = __import__(modname, None, None, ['']) except ImportError: # some plugins can have external dependencies (e.g. creoleparser, # pygments) that are not installed on the machine the tests are # run on. Therefore, just skip those (with an error message) if 'plugins.' in modname: sys.stderr.write('could not import plugin %s\n' % modname) continue else: raise suites = [DocTestSuite(mod, extraglobs={'app': app})] filename = modname[5:] + '.txt' if filename in test_files: globs = {'app': app} globs.update(mod.__dict__) suites.append(DocFileSuite(filename, globs=globs)) for i, subsuite in enumerate(suites): # skip modules without any tests if subsuite.countTestCases(): suite.addTest(subsuite) if return_covermods and i == 0: covermods.append(mod) if return_covermods: return suite, covermods else: return suite