-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.py
More file actions
223 lines (171 loc) · 5.06 KB
/
tests.py
File metadata and controls
223 lines (171 loc) · 5.06 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# -*- coding: utf-8 -*-
import unittest
import sys
from contextlib import contextmanager
from io import StringIO
import click
from click_command_tree import _build_command_tree, _print_tree
@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
class CommandTreeTestCase(unittest.TestCase):
def test_single_command_group(self):
@click.group(name='root')
def root():
pass
@root.command(name='command-one')
def command_one():
pass
@root.command(name='command-two')
def command_two():
pass
expected_output = """
root
├── command-one
└── command-two
"""[1:] # strip off the first newline
self._assert_correct_output(root, expected_output)
def test_nested_command_groups(self):
@click.group()
def root(name='root'):
pass
@root.group(name='group-one')
def group_one():
pass
@group_one.command(name='command-one-a')
def command_one_a():
pass
@group_one.command(name='command-one-b')
def command_one_b():
pass
@group_one.command(name='command-one-c')
def command_one_c():
pass
@root.group(name='group-two')
def group_two():
pass
@group_two.group(name='group-two-a')
def group_two_a():
pass
@group_two.group(name='group-two-b')
def group_two_b():
pass
expected_output = """
root
├── group-one
│ ├── command-one-a
│ ├── command-one-b
│ └── command-one-c
└── group-two
├── group-two-a
└── group-two-b
"""[1:]
self._assert_correct_output(root, expected_output)
def test_last_is_command(self):
@click.group(name='root')
def root():
pass
@root.group(name='group-a')
def group_a():
pass
@group_a.command(name='command-a-a')
def command_a_a():
pass
@root.group(name='group-b')
def group_b():
pass
@group_b.command(name='command-b-a')
def command_b_a():
pass
@root.group(name='group-c')
def group_c():
pass
expected_output = """
root
├── group-a
│ └── command-a-a
├── group-b
│ └── command-b-a
└── group-c
"""[1:]
self._assert_correct_output(root, expected_output)
def test_single_command(self):
@click.command(name='root')
def root():
pass
expected_output = 'root\n'
self._assert_correct_output(root, expected_output)
def test_using_name_argument(self):
@click.group(name='root-command')
def root():
pass
@root.command(name='name-of-command')
def thing():
pass
expected_output = """
root-command
└── name-of-command
"""[1:]
self._assert_correct_output(root, expected_output)
def test_command_with_docs(self):
@click.group(name='root')
def root():
pass
@root.command(name='thing')
def thing():
"""do a thing"""
pass
expected_output = """
root
└── thing - do a thing
"""[1:]
self._assert_correct_output(root, expected_output)
def test_hidden_command(self):
major_version = int(click.__version__.split('.')[0])
if major_version < 7:
return
@click.group(name='root')
def root():
pass
@root.command(name='command-one')
def command_one():
pass
@root.command(name='command-two', hidden=True)
def command_two():
pass
@root.command(name='command-three')
def command_three():
pass
expected_output = """
root
├── command-one
└── command-three
"""[1:]
self._assert_correct_output(root, expected_output)
def test_long_docstring(self):
@click.group(name='root')
def root():
pass
@root.command(name='command')
def command():
"""
this is a really long multi line doc string this is a really long multi line doc string
this is a really long multi line doc string this is a really long multi line doc string
"""
pass
expected_output = """
root
└── command - this is a really long multi line doc string this is a really long multi line doc ...
"""[1:]
self._assert_correct_output(root, expected_output)
def _assert_correct_output(self, root, expected_output):
with captured_output() as (out, err):
tree = _build_command_tree(root)
_print_tree(tree)
self.assertEqual(expected_output, out.getvalue())