14
14
from pathlib import Path
15
15
import os
16
16
import errno
17
- import glob
18
17
import shutil
18
+ import glob
19
19
import logging
20
20
import pdb
21
- from common_tasks import process_glob_string , run_check_call , cleanup_folder
22
- from ToxWorkItem import ToxWorkItem
23
- from simple_threadpool import execute_parallel_workload
24
- from subprocess import Popen , PIPE , STDOUT
21
+ from common_tasks import process_glob_string , run_check_call , cleanup_folder , clean_coverage , MANAGEMENT_PACKAGE_IDENTIFIERS
22
+ from tox_harness import prep_and_run_tox
23
+
25
24
26
25
logging .getLogger ().setLevel (logging .INFO )
27
26
28
27
root_dir = os .path .abspath (os .path .join (os .path .abspath (__file__ ), ".." , ".." , ".." ))
29
28
coverage_dir = os .path .join (root_dir , "_coverage/" )
30
29
dev_setup_script_location = os .path .join (root_dir , "scripts/dev_setup.py" )
31
30
32
- DEFAULT_TOX_INI_LOCATION = os .path .join (root_dir , "eng/tox/tox.ini" )
33
-
34
- MANAGEMENT_PACKAGE_IDENTIFIERS = [
35
- "mgmt" ,
36
- "azure-cognitiveservices" ,
37
- "azure-servicefabric" ,
38
- "azure-nspkg" ,
39
- ]
40
-
41
- # helper functions
42
- def clean_coverage ():
43
- try :
44
- os .mkdir (coverage_dir )
45
- except OSError as e :
46
- if e .errno == errno .EEXIST :
47
- logging .info ("Coverage dir already exists. Cleaning." )
48
- cleanup_folder (coverage_dir )
49
- else :
50
- raise
51
-
52
31
def log_file (file_location , is_error = False ):
53
32
with open (file_location , 'r' ) as file :
54
33
for line in file :
@@ -79,36 +58,6 @@ def collect_pytest_coverage_files(targeted_packages):
79
58
80
59
shutil .move (source , dest )
81
60
82
- # TODO, dedup this function with collect_pytest
83
- def collect_tox_coverage_files (targeted_packages ):
84
- root_coverage_dir = os .path .join (root_dir , "_coverage/" )
85
-
86
- clean_coverage ()
87
-
88
- coverage_files = []
89
- # generate coverage files
90
- for package_dir in [package for package in targeted_packages ]:
91
- coverage_file = os .path .join (package_dir , ".coverage" )
92
- if os .path .isfile (coverage_file ):
93
- destination_file = os .path .join (
94
- root_coverage_dir , ".coverage_{}" .format (os .path .basename (package_dir ))
95
- )
96
- shutil .copyfile (coverage_file , destination_file )
97
- coverage_files .append (destination_file )
98
-
99
- logging .info ("Visible uncombined .coverage files: {}" .format (coverage_files ))
100
-
101
- if len (coverage_files ):
102
- cov_cmd_array = ["coverage" , "combine" ]
103
- cov_cmd_array .extend (coverage_files )
104
-
105
- # merge them with coverage combine and copy to root
106
- run_check_call (cov_cmd_array , os .path .join (root_dir , "_coverage/" ))
107
-
108
- source = os .path .join (coverage_dir , "./.coverage" )
109
- dest = os .path .join (root_dir , ".coverage" )
110
-
111
- shutil .move (source , dest )
112
61
113
62
def prep_tests (targeted_packages , python_version ):
114
63
logging .info ("running test setup for {}" .format (targeted_packages ))
@@ -125,7 +74,7 @@ def prep_tests(targeted_packages, python_version):
125
74
def run_tests (targeted_packages , python_version , test_output_location , test_res ):
126
75
err_results = []
127
76
128
- clean_coverage ()
77
+ clean_coverage (coverage_dir )
129
78
130
79
# base command array without a targeted package
131
80
command_array = [python_version , "-m" , "pytest" ]
@@ -194,74 +143,6 @@ def run_tests(targeted_packages, python_version, test_output_location, test_res)
194
143
if err_results :
195
144
exit (1 )
196
145
197
- def prep_and_run_tox (targeted_packages , tox_env , options_array = []):
198
- tox_command_tuples = []
199
-
200
- for index , package_dir in enumerate (targeted_packages ):
201
- destination_tox_ini = os .path .join (package_dir , "tox.ini" )
202
- destination_dev_req = os .path .join (package_dir , "dev_requirements.txt" )
203
- tox_execution_array = ["tox" ]
204
- local_options_array = options_array [:]
205
-
206
- # if we are targeting only packages that are management plane, it is a possibility
207
- # that no tests running is an acceptable situation
208
- # we explicitly handle this here.
209
- if all (
210
- map (
211
- lambda x : any (
212
- [pkg_id in x for pkg_id in MANAGEMENT_PACKAGE_IDENTIFIERS ]
213
- ),
214
- [package_dir ],
215
- )
216
- ):
217
- local_options_array .append ("--suppress-no-test-exit-code" )
218
-
219
- # if not present, re-use base
220
- if not os .path .exists (destination_tox_ini ):
221
- logging .info ("No customized tox.ini present, using common eng/tox/tox.ini." )
222
- tox_execution_array .extend (["-c" , DEFAULT_TOX_INI_LOCATION ])
223
-
224
- # handle empty file
225
- if not os .path .exists (destination_dev_req ):
226
- logging .info ("No dev_requirements present." )
227
- with open (destination_dev_req , "w+" ) as file :
228
- file .write ("-e ../../../tools/azure-sdk-tools" )
229
-
230
- if tox_env :
231
- tox_execution_array .extend (["-e" , tox_env ])
232
-
233
- if local_options_array :
234
- tox_execution_array .extend (["--" ] + local_options_array )
235
-
236
- tox_command_tuples .append ((tox_execution_array , package_dir ))
237
-
238
- procs_list = []
239
-
240
- for cmd_tuple in tox_command_tuples :
241
- package_dir = cmd_tuple [1 ]
242
-
243
- logging .info (
244
- "Running tox for {}. {} of {}." .format (
245
- package_dir , index , len (targeted_packages )
246
- )
247
- )
248
-
249
- with open (os .path .join (package_dir , 'stdout.txt' ), 'w' ) as f_stdout , open (os .path .join (package_dir , 'stderr.txt' ), 'w' ) as f_stderr :
250
- proc = Popen (cmd_tuple [0 ], stdout = f_stdout , stderr = f_stderr , cwd = package_dir , env = os .environ .copy ())
251
- procs_list .append (proc )
252
-
253
- failed_test_run = False
254
-
255
- for index , proc in enumerate (procs_list ):
256
- proc .wait ()
257
-
258
- if proc .returncode != 0 :
259
- logging .error ("Package returned with code {}" .format (proc .returncode ))
260
-
261
- # TODO: get a bit smarter here
262
- if not tox_env :
263
- collect_tox_coverage_files (targeted_packages )
264
-
265
146
def execute_global_install_and_test (
266
147
parsed_args , targeted_packages , extended_pytest_args
267
148
):
@@ -286,7 +167,7 @@ def execute_tox_harness(parsed_args, targeted_packages, extended_pytest_args):
286
167
if args .mark_arg :
287
168
extended_pytest_args .extend (["-m" , "'{}'" .format (args .mark_arg )])
288
169
289
- prep_and_run_tox (targeted_packages , args .tox_env , extended_pytest_args )
170
+ prep_and_run_tox (targeted_packages , args .tox_env , extended_pytest_args , parsed_args . tparallel )
290
171
291
172
if __name__ == "__main__" :
292
173
parser = argparse .ArgumentParser (
0 commit comments