Repository navigation
Expand file tree
/
Copy pathdoUnitTests.py
More file actions
181 lines (146 loc) · 6.6 KB
/
Copy pathdoUnitTests.py
File metadata and controls
181 lines (146 loc) · 6.6 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
#!/usr/local/bin/python3
import json
import os
import io
import sys
import shutil
import inspect
from timeit import default_timer as timer
import subprocess
api_url = os.environ.get('APPVEYOR_API_URL')
has_error = False
bin_dir = sys.argv[1] if sys.argv[1] else './PowerEditor/bin'
npp = os.path.join(bin_dir, 'notepad++.exe')
def post_error(message):
global has_error
has_error = True
message = {
"message": message,
"category": "error",
"details": ""
}
if api_url:
requests.post(api_url + "api/build/messages", json=message)
else:
from pprint import pprint
pprint(message)
def json_to_unitTest_launcher():
udlfile = json.loads(open("../udl-list.json", encoding="utf8").read())
# generate a map to determine which ids have ac, fl, and/or udls
print("\nLook for functionList definitions in %s" % udlfile["name"])
for udl in udlfile["UDLs"]:
id_str = udl["id-name"]
# print("- %s" % id_str)
if 'functionList' in udl:
fl = udl['functionList']
if not fl: next # do nothing if it's non-true
print("+ found functionList for %s" % udl['id-name'])
#print(json.dumps({"UDL+FL Info": udl}, sort_keys=True, indent=2, separators=(',',':')))
if fl == True:
fl = id_str
ufile = os.path.join('..', 'UDLs', id_str + ".xml")
if not os.path.exists(ufile):
ufile = udl['repository']
if not os.path.exists(ufile) and not(len(ufile)>4 and ufile[0:4] == 'http'):
post_error("Could not resolve %s" % ufile)
umap = { 'id': udl['id-name'], 'display': udl['display-name'] }
umap['udl'] = {
'src': ufile,
'dst': os.path.join(bin_dir, "userDefineLangs", id_str + ".xml")
}
umap['om'] = {
'dst': os.path.join(bin_dir, "functionList", "overrideMap.xml")
}
srcFl = fl
fl_xml = fl + ".xml"
if not(len(fl)>4 and fl[0:4] == 'http'):
srcFl = os.path.join('..', 'functionList', fl_xml)
umap['fl'] = {
'src': srcFl,
'dst': os.path.join(bin_dir, "functionList", fl_xml),
'assoc_id': fl_xml
}
smpFile = udl['sample']
umap['sample'] = {
'dst': os.path.join('functionList', id_str, 'unitTest')
}
if len(smpFile)>4 and smpFile[0:4]=='http':
post_error("TODO: don't know how to download %s yet" % (smpFile))
umap['sample']['src'] = None # TODO: the path to the file saved from download
elif smpFile:
umap['sample']['src'] = os.path.join('..', 'UDL-samples', smpFile)
umap['output'] = {
'exp': os.path.join('functionList', id_str, 'unitTest.expected.result'),
'got': os.path.join('functionList', id_str, 'unitTest.result.json')
}
#print("Sources? " + json.dumps({"fl": fl, "srcFl": srcFl, "smpFile":smpFile, "assoc_id":umap['fl']['assoc_id']}, sort_keys=True, indent=2, separators=(',',':')))
#print(json.dumps(umap, sort_keys=True, indent=2, separators=(',',':')))
for k in ('udl', 'fl', 'sample'):
src = umap[k]['src']
dst = umap[k]['dst']
if os.path.exists(src):
print(" + Copy %s to %s" % (src, dst))
shutil.copy(src, dst)
elif len(src)>4 and src[0:4]=='http':
post_error("TODO: don't know how to download %s to %s" % (src, dst))
src = None # TODO: the path to the file saved from download
else:
post_error("Could not copy %s to %s" % (src, dst))
omTxt = """<?xml version="1.0" encoding="UTF-8" ?>
<NotepadPlus>
<functionList>
<associationMap>
<association id="%s" userDefinedLangName="%s"/>
</associationMap>
</functionList>
</NotepadPlus>
""" % (umap['fl']['assoc_id'], udl['display-name'])
print(" + Generate %s with association id='%s' userDefinedLangname='%s'" % (umap['om']['dst'], umap['fl']['assoc_id'], udl['display-name']))
with open(umap['om']['dst'], 'w') as f:
f.write(inspect.cleandoc(omTxt))
if not has_error:
one_err = run_unit_test(umap)
# delete generated files
for k in ('udl', 'fl', 'om', 'sample'):
dst = umap[k]['dst']
if os.path.exists(dst):
print(" - Remove %s" % (dst))
os.remove(dst)
if not one_err:
dst = umap['output']['got']
if os.path.exists(dst):
print(" - Remove %s" % (dst))
os.remove(dst)
def run_unit_test(umap):
"""Launch notepad++.exe with -export=functionList and process the output"""
t0 = timer()
print(" * Running unit test on %s:" % (umap['id']))
# print(json.dumps(umap, sort_keys=True, indent=2, separators=(',',':')))
cmd_str = '"%s" -multiInst -nosession -export=functionList -udl="%s" "%s"' % (npp, umap['display'], umap['sample']['dst'])
cmpl = subprocess.run(cmd_str)
print(" => exported functionList in %.1fms, returning %d" % ( (timer()-t0)*1000, cmpl.returncode))
try:
with open(umap['output']['exp']) as f:
exp = f.read().replace('\r\n', '\n').rstrip('\n')
with open(umap['output']['got']) as f:
got = f.read().replace('\r\n', '\n').rstrip('\n')
if got == exp:
print(" => compare OK")
return False
else:
print(" => compare MISMATCH")
post_error("functionList export got:\n%s\nvs expected:\n%s" % (got, exp))
except Error as e:
post_error(" => error while comparing %s to %s: %s" % (umap['output']['got'], umap['output']['exp'], str(e)))
return True
# verify it finds notepad++.exe
print("npp = %s" % npp)
if not os.path.exists(npp):
post_error("could not find %s" % npp)
sys.exit(-2)
# copy the files to the right place for FunctionList UnitTesting
json_to_unitTest_launcher()
if has_error:
sys.exit(-2)
else:
sys.exit()