Skip to content

Commit

Permalink
Merge ce1be80 into 50f0c7e
Browse files Browse the repository at this point in the history
  • Loading branch information
wmvanvliet committed Sep 3, 2018
2 parents 50f0c7e + ce1be80 commit 498fe31
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
30 changes: 24 additions & 6 deletions doit/cmd_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,27 @@
'help': "display entries with template"
}

opt_sort = {
'name': 'sort',
'short': '',
'long': 'sort',
'type': str,
'choices': [('name', 'sort by task name'),
('definition', 'list tasks in the order they were defined')],
'default': 'name',
'help': ("choose the manner in which the task list is sorted. "
"[default: %(default)s]")
}


class List(DoitCmdBase):
doc_purpose = "list tasks from dodo file"
doc_usage = "[TASK ...]"
doc_description = None

cmd_options = (opt_listall, opt_list_quiet, opt_list_status,
opt_list_private, opt_list_dependencies, opt_template)
opt_list_private, opt_list_dependencies, opt_template,
opt_sort)


STATUS_MAP = {'ignore': 'I', 'up-to-date': 'U', 'run': 'R', 'error': 'E'}
Expand Down Expand Up @@ -113,10 +126,9 @@ def _list_all(self, include_subtasks):
return print_list


def _execute(self, subtasks=False, quiet=True, status=False,
private=False, list_deps=False, template=None, pos_args=None):
"""List task generators, in the order they were defined.
"""
def _execute(self, subtasks=False, quiet=True, status=False, private=False,
list_deps=False, template=None, sort='name', pos_args=None):
"""List task generators"""
filter_tasks = pos_args
# dict of all tasks
tasks = dict([(t.name, t) for t in self.task_list])
Expand Down Expand Up @@ -144,7 +156,13 @@ def _execute(self, subtasks=False, quiet=True, status=False,
template = '{status} ' + template
template += '\n'

# sort list of tasks
if sort == 'name':
print_list = sorted(print_list)
elif sort == 'definition':
pass # task list is already sorted in order of definition

# print list of tasks
for task in sorted(print_list):
for task in print_list:
self._print_task(template, task, status, list_deps, tasks)
return 0
30 changes: 30 additions & 0 deletions tests/test_cmd_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,33 @@ def test_unicode_name(self, dep_manager):
cmd_list._execute()
got = [line.strip() for line in output.getvalue().split('\n') if line]
assert 't做' == got[0]

def testSort(self):
# by default, the task list is ordered by name
task_list = list(tasks_sample())
output = StringIO()
cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
cmd_list._execute()
got = [line.strip() for line in output.getvalue().split('\n') if line]
expected = ['g1', 't1', 't2', 't3']
assert expected == got

def testSortByName(self):
# test explicitly sorting task list by name
task_list = list(tasks_sample())
output = StringIO()
cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
cmd_list._execute(sort='name')
got = [line.strip() for line in output.getvalue().split('\n') if line]
expected = ['g1', 't1', 't2', 't3']
assert expected == got

def testSortByDefinition(self):
# test sorting task list by order of definition
task_list = list(tasks_sample())
output = StringIO()
cmd_list = CmdFactory(List, outstream=output, task_list=task_list)
cmd_list._execute(sort='definition')
got = [line.strip() for line in output.getvalue().split('\n') if line]
expected = ['t1', 't2', 'g1', 't3']
assert expected == got

0 comments on commit 498fe31

Please sign in to comment.