forked from lord63/tldr.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·259 lines (210 loc) · 10 KB
/
Copy pathtest.py
File metadata and controls
executable file
·259 lines (210 loc) · 10 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3
# encoding: utf-8
import os
import copy
import unittest
import unittest.mock
import tldr
ROOT = os.path.dirname(os.path.realpath(__file__))
print(f'Testing tldr: {tldr!r}')
print(f'Root dir of project: {ROOT!r}')
ok_config = {
"repo_directory_list": [
os.path.join(ROOT, 'tldr-pages-test', 'pages1'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2')
],
"color_output": "auto",
"colors": {
"description": "bright_yellow",
"usage": "green",
"command": "white",
"param": "cyan"
},
"command_indent_size": 4,
"platform_list": [
"common",
"osx",
"linux"
],
"compact_output": False
}
class TldrPureFunctionTests(unittest.TestCase):
def test_check_config(self):
self.assertRaises(AssertionError, tldr.check_config, '')
self.assertRaises(AssertionError, tldr.check_config, [])
tldr.check_config(ok_config)
tldr.check_config(tldr.config)
config = copy.deepcopy(ok_config)
config['repo_directory_list'] = ''
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['colors'] = []
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['platform_list'] = {}
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['compact_output'] = 1
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['repo_directory_list'] = ['/not.exist.dir']
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['colors']['description'] = 'not-exist-color'
self.assertRaises(ValueError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['platform_list'] = 'not_exist_os'
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['command_indent_size'] = '4'
self.assertRaises(AssertionError, tldr.check_config, config)
config = copy.deepcopy(ok_config)
config['command_indent_size'] = -1
self.assertRaises(AssertionError, tldr.check_config, config)
def test_parse_args(self):
tldr._parse_args(['--init'])
tldr._parse_args(['--list'])
tldr._parse_args(['--list', 'tar'])
tldr._parse_args(['--list', '-p', 'linux'])
tldr._parse_args(['--list', '-p', 'linux', 'tar'])
tldr._parse_args(['--update'])
args = tldr._parse_args(['-p', 'linux', 'git', 'pull'])
self.assertEqual(args.command, 'git-pull')
args = tldr._parse_args(['aaa', 'bbb', 'ccc', 'ddd'])
self.assertEqual(args.command, 'aaa-bbb-ccc-ddd')
for args in (
['--xxxx'],
['--init', '-p', 'linux'],
['--init', 'tar'],
['--init', '-p', 'linux', 'tar'],
['--update', '-p', 'linux'],
['--update', 'tar'],
['--update', '-p', 'linux', 'tar'],
['--init', '--list'],
['--init', '--update'],
['--list', '--update'],
['--init', '--list', '--update'],
):
self.assertRaises(SystemExit, tldr._parse_args, args)
class ConfigTests(unittest.TestCase):
path_no_sub_dir = '~/aaaa/bbbb/cccc'
path_no_sub_dir_check = os.path.abspath(os.path.expanduser(path_no_sub_dir))
path_sub_dir = '~/dddd/eeee/ffff/gggg'
path_sub_dir_check = os.path.abspath(os.path.expanduser(os.path.join(path_sub_dir, 'multi-tldr')))
path_default_check = os.path.abspath(os.path.expanduser('~/.config/multi-tldr/'))
def setUp(self):
if 'TLDR_CONFIG_DIR' in os.environ:
del os.environ['TLDR_CONFIG_DIR']
if 'XDG_CONFIG_HOME' in os.environ:
del os.environ['XDG_CONFIG_HOME']
def tearDown(self):
if 'TLDR_CONFIG_DIR' in os.environ:
del os.environ['TLDR_CONFIG_DIR']
if 'XDG_CONFIG_HOME' in os.environ:
del os.environ['XDG_CONFIG_HOME']
def test_default(self):
self.assertTrue(tldr.get_config_file_path().startswith(self.path_default_check))
def test1(self):
os.environ['TLDR_CONFIG_DIR'] = self.path_no_sub_dir
result = tldr.get_config_file_path()
self.assertTrue(result.startswith(self.path_no_sub_dir_check))
def test2(self):
os.environ['TLDR_CONFIG_DIR'] = self.path_no_sub_dir
os.environ['XDG_CONFIG_HOME'] = self.path_sub_dir
result = tldr.get_config_file_path()
self.assertTrue(result.startswith(self.path_no_sub_dir_check))
def test3(self):
os.environ['XDG_CONFIG_HOME'] = self.path_sub_dir
result = tldr.get_config_file_path()
self.assertTrue(result.startswith(self.path_sub_dir_check))
class TestsWithConfig(unittest.TestCase):
def setUp(self):
tldr.get_index.cache_clear()
tldr.get_escape_str.cache_clear()
tldr.get_escape_str_by_type.cache_clear()
self.tldr_default_config = copy.deepcopy(tldr.config)
tldr.config = copy.deepcopy(ok_config)
def tearDown(self):
tldr.config = self.tldr_default_config
def test_get_escape_str(self):
self.assertEqual(tldr.get_escape_str(fg='red'), '\x1b[31m')
self.assertEqual(tldr.get_escape_str(fg='reset'), '\x1b[39m')
self.assertEqual(tldr.get_escape_str(reset=True), '\x1b[0m')
self.assertEqual(tldr.get_escape_str(), '')
def test_get_escape_str_by_type(self):
self.assertEqual(tldr.get_escape_str_by_type('description'), '\x1b[93m\x1b[24m')
self.assertEqual(tldr.get_escape_str_by_type('usage'), '\x1b[32m\x1b[24m')
self.assertEqual(tldr.get_escape_str_by_type('command'), '\x1b[37m\x1b[24m')
self.assertEqual(tldr.get_escape_str_by_type('param'), '\x1b[36m\x1b[4m')
def test_parse_inline_md(self):
line = 'usage 1 `command 1` usage 2 `command 2 {{param 1}}` usage 3 `command 3 {{param 2}} command 4` usage 4 `{{param 3}} command 5 end` usage 5 `{{param 4}}` usage 6 {{param 5 end}} usage 7 end'
line = 'usage `command` usage'
result = '\x1b[32m\x1b[24musage \x1b[37m\x1b[24mcommand\x1b[32m\x1b[24m usage\x1b[0m'
self.assertEqual(tldr.parse_inline_md(line, 'usage'), result)
line = 'usage `command {{param}} command` usage'
result = '\x1b[32m\x1b[24musage \x1b[37m\x1b[24mcommand \x1b[36m\x1b[4mparam\x1b[37m\x1b[24m command\x1b[32m\x1b[24m usage\x1b[0m'
self.assertEqual(tldr.parse_inline_md(line, 'usage'), result)
def test_get_index(self):
result = [
('osx', 'airport'),
('osx', 'du'),
('linux', 'du'),
('linux', 'tcpflow'),
('common', 'tldr-test')
]
repo_path = os.path.join(ROOT, 'tldr-pages-test', 'pages1')
self.assertEqual(sorted(tldr.get_index(repo_path)), sorted(result))
result = [
('common', 'tldr-test'),
('sunos', 'tldr-test'),
]
repo_path = os.path.join(ROOT, 'tldr-pages-test', 'pages2')
self.assertEqual(sorted(tldr.get_index(repo_path)), sorted(result))
def test_get_page_path_list(self):
result_expected = [
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'sunos', 'tldr-test.md'),
]
result = tldr.get_page_path_list('tldr-test', 'all')
self.assertEqual(sorted(result_expected), sorted(result))
result_expected = [
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'common', 'tldr-test.md'),
]
result = tldr.get_page_path_list('tldr-test', 'default')
self.assertEqual(sorted(result_expected), sorted(result))
result_expected = [
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'linux', 'du.md'),
]
result = tldr.get_page_path_list('du', 'linux')
self.assertEqual(sorted(result_expected), sorted(result))
result_expected = [
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'linux', 'du.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'linux', 'tcpflow.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'osx', 'airport.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'osx', 'du.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'sunos', 'tldr-test.md'),
]
result = tldr.get_page_path_list(None, 'all')
self.assertEqual(sorted(result_expected), sorted(result))
result_expected = [
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'linux', 'du.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'linux', 'tcpflow.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'osx', 'airport.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'osx', 'du.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'common', 'tldr-test.md'),
]
result = tldr.get_page_path_list(None, 'default')
self.assertEqual(sorted(result_expected), sorted(result))
result_expected = [
os.path.join(ROOT, 'tldr-pages-test', 'pages1', 'common', 'tldr-test.md'),
os.path.join(ROOT, 'tldr-pages-test', 'pages2', 'common', 'tldr-test.md'),
]
result = tldr.get_page_path_list(None, 'common')
self.assertEqual(sorted(result_expected), sorted(result))
if __name__ == "__main__":
unittest.main()