-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathclicktestcase.py
159 lines (132 loc) · 6.45 KB
/
clicktestcase.py
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
import os
import shlex
import unittest
from typing import Union, List, Optional, Callable
from warnings import warn
from tests.support.dirutils import make_and_clear_directory
from tests.support.compare_rdf import compare_rdf
from tests.support.test_environment import TestEnvironmentTestCase, TestEnvironment
class ClickTestCase(TestEnvironmentTestCase):
""" Common tools for testing command line functions """
env: TestEnvironment = None
# The variables below must be set by the inheriting class
testdir: str = None # subdirectory within outdir
click_ep = None # entry point for particular function
prog_name: str = None # executable name
test_base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'output'))
temp_base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'temp'))
def source_file_path(self, *path: str) -> str:
""" Return the full file name of path in the input directory """
return self.env.input_path(*path)
def expected_file_path(self, *path: str) -> str:
""" Return the fill file path of the script subdirectory in the output directory """
return self.env.expected_path(self.testdir, *path)
def temp_file_path(self, *path: str, is_dir: bool = False) -> str:
""" Create subdirectory in the temp directory to hold path """
full_path = self.env.temp_file_path(self.testdir, *path)
self.env.make_testing_directory(full_path if is_dir else os.path.dirname(full_path))
return full_path
@staticmethod
def jsonld_comparator(expected_data: str, actual_data: str) -> str:
""" Compare expected data in json-ld format to actual data in json-ld format """
return compare_rdf(expected_data, actual_data, "json-ld")
@staticmethod
def n3_comparator(expected_data: str, actual_data: str) -> str:
""" compare expected_data in n3 format to actual_data in n3 format """
return compare_rdf(expected_data, actual_data, "n3")
@staticmethod
def rdf_comparator(expected_data: str, actual_data: str, fmt: Optional[str] = 'turtle') -> str:
""" compare expected_data to actual_data using basic RDF comparator method """
return compare_rdf(expected_data, actual_data, fmt=fmt)
@staticmethod
def always_pass_comparator(expected_data: str, new_data: str) -> Optional[str]:
"""
No-op comparator -- everyone passes!
:param expected_data:
:param new_data:
:return:
"""
return None
@staticmethod
def closein_comparison(expected_txt: str, actual_txt: str) -> None:
""" Assist with testing comparison -- zero in on the first difference in a big string
@param expected_txt:
@param actual_txt:
"""
window = 30
view = 120
nw = nt = actual_txt.strip()
ow = ot = expected_txt.strip()
if ot != nt:
offset = 0
while nt and ot and nt[:window] == ot[:window]:
offset += window
nt = nt[window:]
ot = ot[window:]
offset = max(offset-view, 0)
msg = '\n'.join([
" - - EXPECTED - -",
str(ow[offset:offset+view+view]),
" - - ACTUAL - -",
str(nw[offset:offset+view+view])
])
raise ValueError(msg)
def do_test(self,
args: Union[str, List[str]],
testFileOrDirectory: Optional[str] = None,
*,
expected_error: type(Exception) = None,
filtr: Optional[Callable[[str], str]] = None,
is_directory: bool = False,
add_yaml: bool = True,
comparator: Callable[[type(unittest.TestCase), str, str, str], str] = None, ) -> None:
""" Execute a command test
@param args: Argument string or list to command. 'meta.yaml' is always supplied
@param testFileOrDirectory: name of file or directory to record output in
@param expected_error: If present, we expect this error
@param filtr: Filter to remove date and app specific information from text. Only used for single file generation
@param is_directory: True means output is to a directory
@param add_yaml: True means add the default meta.yaml file. False means both yaml and importmap
are pre-supplied
@param comparator: If present, use this method for comparison
"""
assert testFileOrDirectory
arg_list = shlex.split(args) if isinstance(args, str) else args
if is_directory and (filtr or comparator):
warn("filtr and comparator parameters aren't implemented for directory generation")
if add_yaml and (not arg_list or arg_list[0] != '--help'):
raise NotImplementedError("This is an artifact from elsewhere")
# arg_list.insert(0, self.env.meta_yaml)
# arg_list += ["--importmap", self.env.import_map, "--log_level", DEFAULT_LOG_LEVEL_TEXT]
target = os.path.join(self.testdir, testFileOrDirectory)
self.temp_file_path(self.testdir, is_dir=True)
def do_gen():
if is_directory:
self.env.generate_directory(target,
lambda target_dir: self.click_ep(arg_list + ["-d", target_dir],
prog_name=self.prog_name,
standalone_mode=False))
else:
self.env.generate_single_file(target,
lambda: self.click_ep(arg_list, prog_name=self.prog_name,
standalone_mode=False), filtr=filtr,
comparator=comparator)
if expected_error:
with self.assertRaises(expected_error):
do_gen()
return
else:
do_gen()
@classmethod
def temp_directory(cls, base: str) -> str:
"""
Create a temporary directory and return the path
:param base:
:return: directory
"""
cls.env.make_testing_directory(cls.env.tempdir, clear=True)
new_directory = os.path.join(cls.temp_base_dir, base)
make_and_clear_directory(new_directory)
return new_directory
if __name__ == '__main__':
unittest.main()