forked from aboutcode-org/commoncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cliutils.py
More file actions
168 lines (133 loc) · 6.82 KB
/
test_cliutils.py
File metadata and controls
168 lines (133 loc) · 6.82 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
#
# Copyright (c) 2018 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# Data generated with ScanCode require an acknowledgment.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# When you publish or redistribute any data created with ScanCode or any ScanCode
# derivative work, you must accompany this data with the following acknowledgment:
#
# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
# ScanCode is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode-toolkit/ for support and download.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import click
click.disable_unicode_literals_warning = True
from click.termui import progressbar
from click.testing import CliRunner
from commoncode.testcase import FileDrivenTesting
from commoncode.cliutils import fixed_width_file_name
from commoncode.cliutils import GroupedHelpCommand
from commoncode.cliutils import PluggableCommandLineOption
class TestUtils(FileDrivenTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def test_click_progressbar_with_labels(self):
# test related to https://github.com/mitsuhiko/click/issues/406
@click.command()
def mycli():
"""Sample cmd with progress bar"""
click.echo('Start')
with progressbar(range(10), label='xyz') as it:
for _ in it:
pass
click.echo('End')
runner = CliRunner()
result = runner.invoke(mycli)
assert result.exit_code == 0
expected = '''Start
End
'''
assert expected == result.output
class TestFixedWidthFilename(FileDrivenTesting):
def test_fixed_width_file_name_with_file_name_larger_than_max_length_is_shortened(self):
test = fixed_width_file_name('0123456789012345678901234.c', 25)
expected = '0123456789...5678901234.c'
assert expected == test
def test_fixed_width_file_name_with_file_name_smaller_than_max_length_is_not_shortened(self):
file_name = '0123456789012345678901234.c'
test = fixed_width_file_name(file_name, max_length=50)
assert file_name == test
def test_fixed_width_file_name_with_file_name_at_max_length_is_not_shortened(self):
test = fixed_width_file_name('01234567890123456789012.c', 25)
expected = '01234567890123456789012.c'
assert expected == test
def test_fixed_width_file_name_with_file_name_smaller_than_max_length_not_shortened(self):
test = fixed_width_file_name('0123456789012345678901.c', 25)
expected = '0123456789012345678901.c'
assert expected == test
def test_fixed_width_file_name_with_none_filename_return_empty_string(self):
test = fixed_width_file_name(None, 25)
expected = ''
assert expected == test
def test_fixed_width_file_name_without_extension(self):
test = fixed_width_file_name('012345678901234567890123456', 25)
expected = '01234567890...67890123456'
assert expected == test
def test_fixed_width_file_name_with_posix_path_without_shortening(self):
test = fixed_width_file_name('C/Documents_and_Settings/Boki/Desktop/head/patches/drupal6/drupal.js', 25)
expected = 'drupal.js'
assert expected == test
def test_fixed_width_file_name_with_posix_path_with_shortening(self):
test = fixed_width_file_name('C/Documents_and_Settings/Boki/Desktop/head/patches/drupal6/012345678901234567890123.c', 25)
expected = '0123456789...4567890123.c'
assert expected == test
def test_fixed_width_file_name_with_win_path_without_shortening(self):
test = fixed_width_file_name('C\\:Documents_and_Settings\\Boki\\Desktop\\head\\patches\\drupal6\\drupal.js', 25)
expected = 'drupal.js'
assert expected == test
def test_fixed_width_file_name_with_win_path_with_shortening(self):
test = fixed_width_file_name('C\\:Documents_and_Settings\\Boki\\Desktop\\head\\patches\\drupal6\\012345678901234567890123.c', 25)
expected = '0123456789...4567890123.c'
assert expected == test
def test_fixed_width_file_name_with_very_small_file_name_and_long_extension(self):
test = fixed_width_file_name('abc.abcdef', 5)
# FIXME: what is expected is TBD
expected = ''
assert expected == test
class TestGroupedHelpCommand(FileDrivenTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def test_GroupedHelpCommand_help_group_and_sort_order_without_custom_class(self):
@click.command(name='scan', cls=GroupedHelpCommand)
@click.option('--opt', is_flag=True, help='Help text for option')
def scan(opt):
pass
runner = CliRunner()
result = runner.invoke(scan, ['--help'])
from commoncode.cliutils import MISC_GROUP
assert MISC_GROUP in result.output
assert '--opt Help text for option' in result.output
def test_GroupedHelpCommand_with_help_group_and_sort_order_with_custom_class(self):
@click.command(name='scan', cls=GroupedHelpCommand)
@click.option('--opt', is_flag=True, sort_order=10,
help='Help text for option', cls=PluggableCommandLineOption)
def scan(opt):
pass
runner = CliRunner()
result = runner.invoke(scan, ['--help'])
from commoncode.cliutils import MISC_GROUP
assert MISC_GROUP + ':\n --opt Help text for option\n' in result.output
def test_GroupedHelpCommand_help_with_group(self):
from commoncode.cliutils import CORE_GROUP
@click.command(name='scan', cls=GroupedHelpCommand)
@click.option('--opt', is_flag=True, help='Help text for option',
help_group=CORE_GROUP, cls=PluggableCommandLineOption)
def scan(opt):
pass
runner = CliRunner()
result = runner.invoke(scan, ['--help'])
assert CORE_GROUP + ':\n --opt Help text for option\n' in result.output