Debian Codegraph @

tests/test_cmd_list.py

raw symbols SCIP
1 from io import StringIO
2
3 import pytest
4
5 from doit.exceptions import InvalidCommand
6 from doit.task import Task
7 from doit.tools import result_dep
8 from doit.cmd_list import List
9 from tests.conftest import tasks_sample, tasks_bad_sample, CmdFactory
10
11
12 class TestCmdList(object):
13
14 def testQuiet(self):
15 output = StringIO()
16 tasks = tasks_sample()
17 cmd_list = CmdFactory(List, outstream=output, task_list=tasks)
18 cmd_list._execute()
19 got = [line.strip() for line in output.getvalue().split('\n') if line]
20 expected = [t.name for t in tasks if not t.subtask_of]
21 assert sorted(expected) == got
22
23 def testDoc(self):
24 output = StringIO()
25 tasks = tasks_sample()
26 cmd_list = CmdFactory(List, outstream=output, task_list=tasks)
27 cmd_list._execute(quiet=False)
28 got = [line for line in output.getvalue().split('\n') if line]
29 expected = []
30 for t in sorted(tasks):
31 if not t.subtask_of:
32 expected.append([t.name, t.doc])
33 assert len(expected) == len(got)
34 for exp1, got1 in zip(expected, got):
35 assert exp1 == got1.split(None, 1)
36
37 def testCustomTemplate(self):
38 output = StringIO()
39 tasks = tasks_sample()
40 cmd_list = CmdFactory(List, outstream=output, task_list=tasks)
41 cmd_list._execute(template='xxx {name} xxx {doc}')
42 got = [line.strip() for line in output.getvalue().split('\n') if line]
43 assert 'xxx g1 xxx g1 doc string' == got[0]
44 assert 'xxx t3 xxx t3 doc string' == got[3]
45
46 def testDependencies(self):
47 my_task = Task("t2", [""], file_dep=['d2.txt'])
48 output = StringIO()
49 cmd_list = CmdFactory(List, outstream=output, task_list=[my_task])
50 cmd_list._execute(list_deps=True)
51 got = output.getvalue()
52 assert "d2.txt" in got
53
54 def testSubTask(self):
55 output = StringIO()
56 tasks = tasks_sample()
57 cmd_list = CmdFactory(List, outstream=output, task_list=tasks)
58 cmd_list._execute(subtasks=True)
59 got = [line.strip() for line in output.getvalue().split('\n') if line]
60 expected = [t.name for t in sorted(tasks)]
61 assert expected == got
62
63 def testFilter(self):
64 output = StringIO()
65 cmd_list = CmdFactory(List, outstream=output, task_list=tasks_sample())
66 cmd_list._execute(pos_args=['g1', 't2'])
67 got = [line.strip() for line in output.getvalue().split('\n') if line]
68 expected = ['g1', 't2']
69 assert expected == got
70
71 def testFilterSubtask(self):
72 output = StringIO()
73 cmd_list = CmdFactory(List, outstream=output, task_list=tasks_sample())
74 cmd_list._execute(pos_args=['g1.a'])
75 got = [line.strip() for line in output.getvalue().split('\n') if line]
76 expected = ['g1.a']
77 assert expected == got
78
79 def testFilterAll(self):
80 output = StringIO()
81 cmd_list = CmdFactory(List, outstream=output, task_list=tasks_sample())
82 cmd_list._execute(subtasks=True, pos_args=['g1'])
83 got = [line.strip() for line in output.getvalue().split('\n') if line]
84 expected = ['g1', 'g1.a', 'g1.b']
85 assert expected == got
86
87 def testStatus(self, dependency1, dep_manager):
88 task_list = tasks_sample()
89 dep_manager.ignore(task_list[0]) # t1
90 dep_manager.save_success(task_list[1]) # t2
91 dep_manager.close()
92
93 output = StringIO()
94 cmd_list = CmdFactory(List, outstream=output, dep_file=dep_manager.name,
95 backend='dbm', task_list=task_list)
96 cmd_list._execute(status=True)
97 got = [line.strip() for line in output.getvalue().split('\n') if line]
98 assert 'R g1' in got
99 assert 'I t1' in got
100 assert 'U t2' in got
101
102
103 def testErrorStatus(self, dependency1, dep_manager):
104 """Check that problematic tasks show an 'E' as status."""
105 task_list = tasks_bad_sample()
106
107 output = StringIO()
108 cmd_list = CmdFactory(List, outstream=output, dep_manager=dep_manager,
109 task_list=task_list)
110 cmd_list._execute(status=True)
111 for line in output.getvalue().split('\n'):
112 if line:
113 assert line.strip().startswith('E ')
114
115
116 def testStatus_result_dep_bug_gh44(self, dependency1, dep_manager):
117 # make sure task dict is passed when checking up-to-date
118 task_list = [Task("t1", [""], doc="t1 doc string"),
119 Task("t2", [""], uptodate=[result_dep('t1')]),]
120
121 dep_manager.save_success(task_list[0]) # t1
122 dep_manager.close()
123
124 output = StringIO()
125 cmd_list = CmdFactory(List, outstream=output, dep_file=dep_manager.name,
126 backend='dbm', task_list=task_list)
127 cmd_list._execute(status=True)
128 got = [line.strip() for line in output.getvalue().split('\n') if line]
129 assert 'R t1' in got
130 assert 'R t2' in got
131
132 def testNoPrivate(self):
133 task_list = list(tasks_sample())
134 task_list.append(Task("_s3", [""]))
135 output = StringIO()
136 cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
137 cmd_list._execute(pos_args=['_s3'])
138 got = [line.strip() for line in output.getvalue().split('\n') if line]
139 expected = []
140 assert expected == got
141
142 def testWithPrivate(self):
143 task_list = list(tasks_sample())
144 task_list.append(Task("_s3", [""]))
145 output = StringIO()
146 cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
147 cmd_list._execute(private=True, pos_args=['_s3'])
148 got = [line.strip() for line in output.getvalue().split('\n') if line]
149 expected = ['_s3']
150 assert expected == got
151
152 def testListInvalidTask(self):
153 output = StringIO()
154 cmd_list = CmdFactory(List, outstream=output, task_list=tasks_sample())
155 pytest.raises(InvalidCommand, cmd_list._execute, pos_args=['xxx'])
156
157
158 def test_unicode_name(self, dep_manager):
159 task_list = [Task("t做", [""], doc="t1 doc string 做"),]
160 output = StringIO()
161 cmd_list = CmdFactory(List, outstream=output, dep_file=dep_manager.name,
162 task_list=task_list)
163 cmd_list._execute()
164 got = [line.strip() for line in output.getvalue().split('\n') if line]
165 assert 't做' == got[0]
166
167 def testSortByName(self):
168 # by default, the task list should be ordered by name
169 task_list = list(tasks_sample())
170 output = StringIO()
171 cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
172 cmd_list._execute()
173 got = [line.strip() for line in output.getvalue().split('\n') if line]
174 expected = ['g1', 't1', 't2', 't3']
175 assert expected == got
176
177 def testSortByDefinition(self):
178 # test sorting task list by order of definition
179 task_list = list(tasks_sample())
180 output = StringIO()
181 cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
182 cmd_list._execute(sort='definition')
183 got = [line.strip() for line in output.getvalue().split('\n') if line]
184 expected = ['t1', 't2', 'g1', 't3']
185 assert expected == got