Refactor command dispatch to improve unit testing and support better error messages.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-02-29 14:35:23 -08:00
commit 53bea8a720
3 changed files with 75 additions and 66 deletions

View file

@ -64,26 +64,20 @@ class CLITestCase(unittest.TestCase):
self.assertTrue(project.client)
self.assertTrue(project.services)
def test_help(self):
command = TopLevelCommand()
with self.assertRaises(SystemExit):
command.dispatch(['-h'])
def test_command_help(self):
with self.assertRaises(SystemExit) as ctx:
TopLevelCommand().dispatch(['help', 'up'])
with pytest.raises(SystemExit) as exc:
TopLevelCommand.help({'COMMAND': 'up'})
self.assertIn('Usage: up', str(ctx.exception))
assert 'Usage: up' in exc.exconly()
def test_command_help_nonexistent(self):
with self.assertRaises(NoSuchCommand):
TopLevelCommand().dispatch(['help', 'nonexistent'])
with pytest.raises(NoSuchCommand):
TopLevelCommand.help({'COMMAND': 'nonexistent'})
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason="requires dockerpty")
@mock.patch('compose.cli.main.RunOperation', autospec=True)
@mock.patch('compose.cli.main.PseudoTerminal', autospec=True)
def test_run_interactive_passes_logs_false(self, mock_pseudo_terminal, mock_run_operation):
command = TopLevelCommand()
mock_client = mock.create_autospec(docker.Client)
project = Project.from_config(
name='composetest',
@ -92,6 +86,7 @@ class CLITestCase(unittest.TestCase):
'service': {'image': 'busybox'}
}),
)
command = TopLevelCommand(project)
with pytest.raises(SystemExit):
command.run(project, {
@ -126,7 +121,7 @@ class CLITestCase(unittest.TestCase):
}),
)
command = TopLevelCommand()
command = TopLevelCommand(project)
command.run(project, {
'SERVICE': 'service',
'COMMAND': None,
@ -147,7 +142,7 @@ class CLITestCase(unittest.TestCase):
'always'
)
command = TopLevelCommand()
command = TopLevelCommand(project)
command.run(project, {
'SERVICE': 'service',
'COMMAND': None,
@ -168,7 +163,6 @@ class CLITestCase(unittest.TestCase):
)
def test_command_manula_and_service_ports_together(self):
command = TopLevelCommand()
project = Project.from_config(
name='composetest',
client=None,
@ -176,6 +170,7 @@ class CLITestCase(unittest.TestCase):
'service': {'image': 'busybox'},
}),
)
command = TopLevelCommand(project)
with self.assertRaises(UserError):
command.run(project, {