Skip to content

Commit

Permalink
Add site and outdir to the build command.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jan 6, 2016
1 parent 30623b3 commit 029f64d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 31 deletions.
8 changes: 8 additions & 0 deletions handroll/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class BuildCommand(Command):
description = _('Build a site in an output directory.')
help = _('build a site')

def register(self, subparsers):
parser = super(BuildCommand, self).register(subparsers)
parser.add_argument(
'site', nargs='?', help=_('the path to your website'))
parser.add_argument('outdir', nargs='?', help=_(
'an optional output directory to create or'
' update if it already exists'))

def run(self, args):
site = Site.build(args)
director = prepare_director(args, site)
Expand Down
8 changes: 3 additions & 5 deletions handroll/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def parse(argv):
args = parser.parse_args(argv[1:])

# Normalize the site path so that all sites are handled consistently.
if args.site:
# Not every command will have the site argument, but the normalization
# occurs here to keep the check in one place.
if 'site' in vars(args) and args.site:
args.site = args.site.rstrip(os.sep)

return args
Expand All @@ -63,10 +65,6 @@ def build_parser():
"""Build the parser that will have all available commands and options."""
description = _('A website generator for software artisans')
parser = argparse.ArgumentParser(description=description)
parser.add_argument('site', nargs='?', help=_('the path to your website'))
parser.add_argument(
'outdir', nargs='?', help=_('an optional output directory to create or'
' update if it already exists'))
parser.add_argument(
'-w', '--watch', action='store_true',
help=_('watch the site for changes and'
Expand Down
18 changes: 18 additions & 0 deletions handroll/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def test_run_not_implemented(self):

class TestBuildCommand(TestCase):

def test_register_site(self):
parser = mock.Mock()
subparsers = mock.Mock()
subparsers.add_parser.return_value = parser
command = BuildCommand()
command.register(subparsers)
site_call = (('site',), {'nargs': '?', 'help': mock.ANY})
self.assertIn(site_call, parser.add_argument.call_args_list)

def test_register_outdir(self):
parser = mock.Mock()
subparsers = mock.Mock()
subparsers.add_parser.return_value = parser
command = BuildCommand()
command.register(subparsers)
outdir_call = (('outdir',), {'nargs': '?', 'help': mock.ANY})
self.assertIn(outdir_call, parser.add_argument.call_args_list)

@mock.patch('handroll.commands.build.finish')
def test_complete_build(self, finish):
site = self.factory.make_site()
Expand Down
54 changes: 28 additions & 26 deletions handroll/tests/test_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setUp(self):
self.arguments = ['/fake/bin/handroll']

def _make_argv_with(self, argument=None):
# A subcommand is required.
# A command is required.
if argument is None:
return ['/fake/bin/handroll', 'build']
else:
Expand Down Expand Up @@ -79,29 +79,26 @@ def test_watch_argument(self):
self.assertTrue(args.watch)

def test_site_argument(self):
# FIXME: I promise I'm coming right back to this.
return
site = 'fake_site'
self.arguments.append(site)
args = entry.parse(self.arguments)
argv = self._make_argv_with()
argv.append(site)
args = entry.parse(argv)
self.assertEqual(site, args.site)

def test_site_argument_is_normalized(self):
"""Test that trailing path separator is removed so that a site is
consistently handled."""
# FIXME: I promise I'm coming right back to this.
return
site = 'fake_site' + os.sep
self.arguments.append(site)
args = entry.parse(self.arguments)
argv = self._make_argv_with()
argv.append(site)
args = entry.parse(argv)
self.assertEqual('fake_site', args.site)

def test_outdir_argument(self):
# FIXME: I promise I'm coming right back to this.
return
outdir = 'fake_outdir'
self.arguments.extend(['fake_site', outdir])
args = entry.parse(self.arguments)
argv = self._make_argv_with()
argv.extend(['fake_site', outdir])
args = entry.parse(argv)
self.assertEqual(outdir, args.outdir)

def test_force_argument(self):
Expand Down Expand Up @@ -144,33 +141,38 @@ class TestMain(TestCase):
def setUp(self):
self.arguments = ['/fake/bin/handroll']

def _make_argv_with(self, argument=None):
# A command is required.
if argument is None:
return ['/fake/bin/handroll', 'build']
else:
return ['/fake/bin/handroll', argument, 'build']

def test_verbose_sets_logging(self):
logger.setLevel(logging.CRITICAL)
self.arguments.extend(['-v', 'build'])
self.assertRaises(SystemExit, entry.main, self.arguments)
argv = self._make_argv_with('-v')
self.assertRaises(SystemExit, entry.main, argv)
self.assertEqual(logging.INFO, logger.getEffectiveLevel())

def test_debug_sets_logging(self):
logger.setLevel(logging.CRITICAL)
self.arguments.extend(['-d', 'build'])
self.assertRaises(SystemExit, entry.main, self.arguments)
argv = self._make_argv_with('-d')
self.assertRaises(SystemExit, entry.main, argv)
self.assertEqual(logging.DEBUG, logger.getEffectiveLevel())

def test_site_directory_is_file(self):
# FIXME: I promise I'm coming right back to this.
return
site = tempfile.mkdtemp()
file_site = os.path.join(site, 'fake')
self.arguments.append(file_site)
self.assertRaises(SystemExit, entry.main, self.arguments)
argv = self._make_argv_with()
argv.append(file_site)
self.assertRaises(SystemExit, entry.main, argv)

@mock.patch('handroll.entry.finish')
@mock.patch('handroll.commands.build.finish')
def test_complete_site_generation(self, finish):
# FIXME: I promise I'm coming right back to this.
return
site = self.factory.make_site()
self.arguments.append(site.path)
entry.main(self.arguments)
argv = self._make_argv_with()
argv.append(site.path)
entry.main(argv)
self.assertTrue(finish.called)

@mock.patch('handroll.entry.serve')
Expand Down
4 changes: 4 additions & 0 deletions handroll/tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def __init__(self, methodName='runTest'):
super(TestCase, self).__init__(methodName)
self.factory = Factory()

def assertIn(self, a, b):
"""Backport for Python 2.6."""
self.assertTrue(a in b)

def assertIsNone(self, x):
"""Backport for Python 2.6."""
self.assertTrue(x is None)

0 comments on commit 029f64d

Please sign in to comment.