def test_override(self): '''Ensure we can track overrides from one file or another''' shovel = Shovel() for name in ['one', 'two']: pth = 'test/examples/overrides/%s' % name shovel.read(pth, pth) self.assertNotEqual(shovel, None) self.assertNotEqual(shovel['foo.bar'].overrides, None)
def test_errors(self): '''Make sure getting non-existant tasks throws errors''' shovel = Shovel() self.assertRaises(KeyError, shovel.__getitem__, 'foo.bar.whiz') self.assertRaises(KeyError, shovel.__getitem__, 'foo') self.assertFalse('foo' in shovel) # We can't have functor classes that take arguments shovel.read('test/examples/errors/') self.assertFalse('Foo' in shovel) self.assertRaises(TypeError, Task, 'foo')
def test_shovel(self): '''For shovel/*, shovel.py, .shovel/* and .shovel.py, tasks should be top-level''' shovel = Shovel.load('test/examples/toplevel/one', 'test/examples/toplevel/one') _, tasks = zip(*shovel.items()) # self.assertEqual(len(tasks), 4) self.assertEqual(set([t.fullname for t in tasks]), set(['whiz', 'bang'])) shovel = Shovel.load('test/examples/toplevel/two', 'test/examples/toplevel/two') _, tasks = zip(*shovel.items()) self.assertEqual(set([t.fullname for t in tasks]), set(['foo', 'bar']))
def test_help_missing_docstring(self): '''We should print '(No docstring)' for tasks missing a docstring''' shovel = Shovel.load( 'test/examples/docstring/', 'test/examples/docstring/') actual = [line.strip() for line in help.shovel_help(shovel).split('\n')] expected = ['one/', 'one.foo => (No docstring)'] self.assertEqual(actual, expected)
def test_dry_run(self): '''Make sure that dry runs don't blow up on us''' shovel = Shovel.load('test/examples/multiple/', 'test/examples/multiple/') _, tasks = zip(*shovel.items()) self.assertGreater(len(tasks), 0) for task in tasks: self.assertNotEqual(task.dry(), '')
def test_help(self): '''Just make sure that help doesn't blow up on us''' shovel = Shovel.load('test/examples/overrides/', 'test/examples/overrides/') _, tasks = zip(*shovel.items()) self.assertGreater(len(tasks), 0) for task in tasks: self.assertNotEqual(task.help(), '')
def test_capture(self): '''Make sure we can capture output from a function''' shovel = Shovel.load('test/examples/capture/', 'test/examples/capture') self.assertEqual(shovel['foo'].capture(1, 2, 3), { 'stderr': '', 'stdout': 'foo\n', 'return': 6, 'exception': None }) self.assertNotEqual(shovel['bar'].capture()['exception'], None)
def test_multi_load(self): '''Load from multiple paths''' shovel = Shovel() shovel.read('test/examples/multiple/one/', 'test/examples/multiple/one/') shovel.read('test/examples/multiple/two/', 'test/examples/multiple/two/') keys = [ 'whiz', 'bar.bar' ] self.assertEqual(set(shovel.keys()), set(keys))
def test_nested(self): '''Ensure we can do nested loading''' shovel = Shovel.load('test/examples/nested/', 'test/examples/nested/') self.assertNotEqual(shovel, None) examples = [ 'foo.bar', 'foo.whiz', 'foo.baz.hello', 'foo.baz.howdy.what' ] for example in examples: self.assertTrue(example in shovel)
def test_keys_items(self): '''Shovels should provide a list of all the tasks they know about''' shovel = Shovel.load('test/examples/nested/', 'test/examples/nested/') keys = [ 'foo.bar', 'foo.whiz', 'foo.baz.hello', 'foo.baz.howdy.what' ] self.assertEqual(set(shovel.keys()), set(keys)) for key, pair in zip(sorted(keys), sorted(shovel.items())): self.assertEqual(key, pair[0]) self.assertEqual(key, pair[1].fullname)
def extend(self, tasks): '''Add tasks to this particular shovel''' self._tasks.extend(tasks) for task in tasks: # We'll now go through all of our tasks and group them into # sub-shovels current = self.map modules = task.fullname.split('.') for module in modules[:-1]: if not isinstance(current[module], Shovel): logger.warn('Overriding task %s with a module' % current[module].file) shovel = Shovel() shovel.overrides = current[module] current[module] = shovel current = current[module].map # Now we'll put the task in this particular sub-shovel name = modules[-1] if name in current: logger.warn('Overriding %s with %s' % ( '.'.join(modules), task.file)) task.overrides = current[name] current[name] = task
def setUp(self): self.shovel = Shovel.load('test/examples/help/', 'test/examples/help/')
def test_basic(self): '''Ensure that we can get all the basic tasks in a shovel file''' shovel = Shovel.load('test/examples/basic/shovel.py', 'test/examples/basic') self.assertNotEqual(shovel, None) self.assertTrue('widget' in shovel)
def setUp(self): self.shovel = Shovel.load( 'test/examples/help/', 'test/examples/help/')
def run(*args): '''Run the normal shovel functionality''' import os import sys import argparse import pkg_resources # First off, read the arguments parser = argparse.ArgumentParser(description='Rake, for Python') parser.add_argument('method', help='The task to run') parser.add_argument('--verbose', dest='verbose', action='store_true', help='Be extra talkative') parser.add_argument('--dry-run', dest='dryRun', action='store_true', help='Show the args that would be used') ver = pkg_resources.require('shovel')[0].version parser.add_argument('--version', action='version', version='Shovel v %s' % ver, help='print the version of Shovel.') # Parse our arguments if args: clargs, remaining = parser.parse_known_args(args=args) else: # pragma: no cover clargs, remaining = parser.parse_known_args() if clargs.verbose: logger.setLevel(logging.DEBUG) args, kwargs = parse(remaining) # Import all of the files we want shovel = Shovel() # Read in any tasks that have already been defined shovel.extend(Task.clear()) for path in [ os.path.expanduser('~/.shovel.py'), os.path.expanduser('~/.shovel')]: if os.path.exists(path): # pragma: no cover shovel.read(path, os.path.expanduser('~/')) for path in ['shovel.py', 'shovel']: if os.path.exists(path): shovel.read(path) # If it's help we're looking for, look no further if clargs.method == 'help': print(help.shovel_help(shovel, *args, **kwargs)) elif clargs.method == 'tasks': tasks = list(v for _, v in shovel.items()) if not tasks: print('No tasks found!') else: names = list(t.fullname for t in tasks) docs = list(t.doc for t in tasks) # The width of the screen width = 80 import shutil try: width, _ = shutil.get_terminal_size(fallback=(0, width)) except AttributeError: pass # Create the format with padding for the longest name, and to # accomodate the screen width format = '%%-%is # %%-%is' % ( max(len(name) for name in names), width) for name, doc in zip(names, docs): print(format % (name, doc)) elif clargs.method: # Try to get the first command provided try: tasks = shovel.tasks(clargs.method) except KeyError: print('Could not find task "%s"' % clargs.method, file=sys.stderr) exit(1) if len(tasks) > 1: print('Specifier "%s" matches multiple tasks:' % clargs.method, file=sys.stderr) for task in tasks: print('\t%s' % task.fullname, file=sys.stderr) exit(2) task = tasks[0] if clargs.dryRun: print(task.dry(*args, **kwargs)) else: task(*args, **kwargs)
def test_folder(self): '''Ensure we can import from a folder structure''' shovel = Shovel.load('test/examples/folder/', 'test/examples/folder/') self.assertNotEqual(shovel, None) self.assertTrue('foo.widget' in shovel)
def test_init(self): '''Ensure we can recognize things in __init__ correctly''' shovel = Shovel.load('test/examples/init/', 'test/examples/init/') self.assertNotEqual(shovel, None) self.assertTrue('widget' in shovel)
def test_tasks(self): '''We should be able to get a list of all tasks that match a path''' shovel = Shovel.load('test/examples/tasks/', 'test/examples/tasks/') self.assertEqual(set(t.fullname for t in shovel.tasks('foo')), set(('foo.bar', 'foo.whiz')))
def test_atts(self): '''Make sure some of the attributes are what we expect''' shovel = Shovel.load('test/examples/capture/', 'test/examples/capture') task = shovel['foo'] self.assertEqual(task.doc, 'Dummy function')
def test_classes(self): '''We should be able to also use functor classes''' shovel = Shovel.load('test/examples/classes', 'test/examples/classes') self.assertTrue('Foo' in shovel) self.assertEqual(shovel['Foo'](5), 5) self.assertRaises(TypeError, shovel['Bar'], 5)