12
from utility import temp_dir
15
class ConcurrentlyTest(TestCase):
17
log_level = logging.ERROR
20
with patch('concurrently.run_all') as r_mock:
21
with patch('concurrently.summarise_tasks',
22
return_value=0) as s_mock:
23
returncode = concurrently.main(
24
['-v', '-l', '.', 'one=foo a b', 'two=bar c'])
25
self.assertEqual(0, returncode)
26
task_one = concurrently.Task('one=foo a b')
27
task_two = concurrently.Task('two=bar c')
28
r_mock.assert_called_once_with([task_one, task_two])
29
s_mock.assert_called_once_with([task_one, task_two])
32
def test_main_error(self, se_mock):
33
with patch('concurrently.run_all', side_effect=ValueError('bad')):
34
returncode = concurrently.main(['-v', 'one=foo a b', 'two=bar c'])
35
self.assertEqual(253, returncode)
36
self.assertIn('ERROR bad', self.log_stream.getvalue())
38
def test_parse_args(self):
39
args = concurrently.parse_args(
40
['-v', '-l', '~/logs', 'one=foo a b', 'two=bar c'])
41
self.assertEqual(logging.DEBUG, args.verbose)
42
self.assertEqual(os.path.expanduser('~/logs'), args.log_dir)
44
['one=foo a b', 'two=bar c'], args.tasks)
46
def test_summarise_tasks(self):
47
task_one = concurrently.Task('one=foo a')
48
task_one.returncode = 0
49
task_two = concurrently.Task('two=bar b')
50
task_two.returncode = 0
51
tasks = [task_one, task_two]
52
self.assertEqual(0, concurrently.summarise_tasks(tasks))
53
task_one.returncode = 1
54
self.assertEqual(1, concurrently.summarise_tasks(tasks))
55
task_two.returncode = 3
56
self.assertEqual(4, concurrently.summarise_tasks(tasks))
58
def test_run_all(self):
59
task_one = concurrently.Task('one=foo a')
60
task_two = concurrently.Task('two=bar b')
61
mutable_tasks = [task_one, task_two]
62
with patch.object(task_one, 'finish', autospec=True) as f1_mock:
63
with patch.object(task_one, 'start', autospec=True) as s1_mock:
64
with patch.object(task_two, 'finish',
65
autospec=True,) as f2_mock:
66
with patch.object(task_two, 'start',
67
autospec=True) as s2_mock:
68
concurrently.run_all(mutable_tasks)
69
s1_mock.assert_called_once_with()
70
f1_mock.assert_called_once_with()
71
s2_mock.assert_called_once_with()
72
f2_mock.assert_called_once_with()
73
self.assertEqual([], mutable_tasks)
76
class TaskTest(TestCase):
79
with temp_dir() as base:
80
task = concurrently.Task('one=foo a b c', log_dir=base)
81
self.assertEqual('one', task.name)
82
self.assertEqual('foo a b c', task.commandline)
83
self.assertEqual(['foo', 'a', 'b', 'c'], task.command)
85
os.path.join(base, 'one-out.log'), task.out_log_name)
87
os.path.join(base, 'one-err.log'), task.err_log_name)
88
self.assertIsNone(task.returncode)
89
self.assertIsNone(task.proc)
92
with temp_dir() as base:
93
task = concurrently.Task('one=foo a', log_dir=base)
94
with patch('subprocess.Popen',
95
autospec=True, return_value='proc') as p_mock:
96
with task.start() as proc:
97
self.assertEqual('proc', proc)
98
self.assertEqual('proc', task.proc)
99
self.assertEqual(1, p_mock.call_count)
100
args, kwargs = p_mock.call_args
101
self.assertEqual((['foo', 'a'], ), args)
102
kwargs['stdout'].write('out')
103
kwargs['stderr'].write('err')
106
os.path.exists(os.path.join(base, 'one-out.log')))
109
os.path.exists(os.path.join(base, 'one-out.log')))
111
def test_finish(self):
112
task = concurrently.Task('one=foo a')
113
task.proc = Mock(spec=['wait'])
115
task.proc.wait.assert_called_once_with()