forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_instructions.py
119 lines (81 loc) · 2.99 KB
/
test_instructions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from logging import getLogger, Handler, DEBUG
import unittest
from conda import exceptions
from conda import instructions
from conda.instructions import execute_instructions, commands, PROGRESS_CMD
def test_expected_operation_order():
"""Ensure expected order of operations"""
expected = (
instructions.FETCH,
instructions.EXTRACT,
instructions.UNLINK,
instructions.LINK,
instructions.SYMLINK_CONDA,
instructions.RM_EXTRACTED,
instructions.RM_FETCHED,
)
assert expected == instructions.action_codes
class TestHandler(Handler):
def __init__(self):
Handler.__init__(self)
self.setLevel(DEBUG)
self.records = []
def handle(self, record):
self.records.append((record.name, record.msg))
class TestExecutePlan(unittest.TestCase):
def test_invalid_instruction(self):
index = {'This is an index': True}
plan = [('DOES_NOT_EXIST', ())]
with self.assertRaises(exceptions.InvalidInstruction):
execute_instructions(plan, index, verbose=False)
def test_simple_instruction(self):
index = {'This is an index': True}
def simple_cmd(state, arg):
simple_cmd.called = True
simple_cmd.call_args = (arg,)
commands['SIMPLE'] = simple_cmd
plan = [('SIMPLE', ('arg1',))]
execute_instructions(plan, index, verbose=False)
self.assertTrue(simple_cmd.called)
self.assertTrue(simple_cmd.call_args, ('arg1',))
def test_state(self):
index = {'This is an index': True}
def simple_cmd(state, arg):
expect, x = arg
state.setdefault('x', 1)
self.assertEqual(state['x'], expect)
state['x'] = x
simple_cmd.called = True
commands['SIMPLE'] = simple_cmd
plan = [('SIMPLE', (1, 5)),
('SIMPLE', (5, None)),
]
execute_instructions(plan, index, verbose=False)
self.assertTrue(simple_cmd.called)
def test_progess(self):
index = {'This is an index': True}
plan = [
('PROGRESS', '2'),
('LINK', 'ipython'),
('LINK', 'menuinst'),
]
def cmd(state, arg):
pass # NO-OP
_commands = {'PROGRESS': PROGRESS_CMD, 'LINK': cmd}
h = TestHandler()
update_logger = getLogger('progress.update')
update_logger.setLevel(DEBUG)
update_logger.addHandler(h)
stop_logger = getLogger('progress.stop')
stop_logger.setLevel(DEBUG)
stop_logger.addHandler(h)
execute_instructions(plan, index, _commands=_commands)
update_logger.removeHandler(h)
stop_logger.removeHandler(h)
expected = [('progress.update', ('ipython', 0)),
('progress.update', ('menuinst', 1)),
('progress.stop', None)
]
self.assertEqual(h.records, expected)
if __name__ == '__main__':
unittest.main()