Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

""" 

@file 

@brief This extension contains various functionalities to help unittesting. 

""" 

import os 

import sys 

import unittest 

from datetime import datetime 

import warnings 

 

from ..loghelper.flog import noLOG 

from ..loghelper.os_helper import get_user 

from ..filehelper import synchronize_folder 

from .call_setup_hook import call_setup_hook 

from .code_exceptions import CoverageException, SetupHookException 

from .coverage_helper import publish_coverage_on_codecov, find_coverage_report, coverage_combine 

from .utils_tests_private import default_skip_function, main_run_test 

from .utils_tests_stringio import StringIOAndFile 

 

 

class TestWrappedException(Exception): 

"Raised by @see fn main_wrapper_tests" 

pass 

 

 

def main_wrapper_tests(logfile, skip_list=None, processes=False, add_coverage=False, report_folder=None, 

skip_function=default_skip_function, setup_params=None, only_setup_hook=False, 

coverage_options=None, coverage_exclude_lines=None, additional_ut_path=None, 

covtoken=None, hook_print=True, stdout=None, stderr=None, filter_warning=None, 

dump_coverage=None, add_coverage_folder=None, coverage_root="src", fLOG=noLOG): 

""" 

Calls function :func:`main <pyquickhelper.unittests.utils_tests.main>` 

and throws an exception if it fails. 

 

@param logfile locatio of a logfile 

@param skip_list to skip a list of unit tests (by index, starting by 1) 

@param processes to run the unit test in a separate process (with function @see fn run_cmd), 

however, to make that happen, you need to specify 

``exit=False`` for each test file, see `unittest.main 

<https://docs.python.org/3/library/unittest.html#unittest.main>`_ 

@param add_coverage (bool) run the unit tests and measure the coverage at the same time 

@param report_folder (str) folder where the coverage report will be stored 

@param skip_function *function(filename,content,duration) --> boolean* to skip a unit test 

@param setup_params parameters sent to @see fn call_setup_hook 

@param only_setup_hook calls only @see fn call_setup_hook, do not run the unit test 

@param coverage_options (dict) options for module coverage as a dictionary, see below, default is None 

@param coverage_exclude_lines (list) options for module coverage, lines to exclude from the coverage report, defaul is None 

@param additional_ut_path (list) additional paths to add when running the unit tests 

@param covtoken (str|tuple(str, str)) token used when publishing coverage report to `codecov <https://codecov.io/>`_ 

or None to not publish 

@param hook_print enable print display when calling *_setup_hook* 

@param stdout if not None, write output on this stream instead of *sys.stdout* 

@param stderr if not None, write errors on this stream instead of *sys.stderr* 

@param filter_warning function which removes some warnings in the final output, 

if None, the function filters out some recurrent warnings 

in jupyter (signature: ``def filter_warning(w: warning) -> bool``), 

@see fn default_filter_warning 

@param dump_coverage dump or copy the coverage at this location 

@param add_coverage_folder additional coverage folder reports 

@param coverage_root subfolder for the coverage 

@param fLOG ``function(*l, **p)``, logging function 

 

*covtoken* can be a string ``<token>`` or a 

tuple ``(<token>, <condition>)``. The condition is evaluated 

by the python interpreter and determines whether or not the coverage 

needs to be published. 

 

.. faqref:: 

:title: How to build pyquickhelper with Jenkins? 

:index: Jenkins 

 

:epkg:`Jenkins` is a task scheduler for continuous integration. 

You can easily schedule batch command to build and run unit tests for a specific project. 

To build pyquickhelper, you need to install :epkg:`python`, 

:epkg:`pymyinstall`, 

:epkg:`miktex`, :epkg:`pandoc`, 

:epkg:`sphinx`. 

 

Once Jenkins is installed, the command to schedule is:: 

 

set PATH=%PATH%;%USERPOFILE%\\AppData\\Local\\Pandoc 

build_setup_help_on_windows.bat 

 

This works if you installed Jenkins with your credentials. 

Otherwise the path to ``pandoc.exe`` needs to be changed. 

And you can also read `Schedule builds with Jenkins 

<http://www.xavierdupre.fr/blog/2014-12-06_nojs.html>`_. 

:epkg:`node.js` might be required if a notebooks contain javascript. 

 

Parameters *add_coverage* and *report_folder* are used to compute the coverage 

using the module `coverage <http://nedbatchelder.com/code/coverage/>`_. 

The function does something about the following error: 

 

_tkinter.TclError: no display name and no $DISPLAY environment variable 

 

It is due to :epkg:`matplotlib`. 

See `Generating matplotlib graphs without a running X server 

<http://stackoverflow.com/questions/4931376/generating-matplotlib-graphs-without-a-running-x-server>`_. 

If the skip function is None, it will replace it by the function @see fn default_skip_function. 

Calls function @see fn _setup_hook if it is available in the unit tested module. 

Parameter *tested_module* was added, the function then checks the presence of 

function @see fn _setup_hook, it is the case, it runs it. 

 

Parameter *setup_params*: a mechanism was put in place 

to let the module to test a possibility to run some preprocessing steps 

in a separate process. They are described in @see fn _setup_hook 

which must be found in the main file ``__init__.py``. 

Parameter *only_setup_hook*: 

saves the report in XML format, binary format, replace full paths by relative path. 

 

Parameters *coverage_options*, *coverage_exclude_lines*, *additional_ut_path*: 

see class `Coverage <http://coverage.readthedocs.org/en/coverage-4.0b1/api_coverage.html?highlight=coverage#coverage.Coverage.__init__>`_ 

and `Configuration files <http://coverage.readthedocs.org/en/coverage-4.0b1/config.html>`_ 

to specify those options. If both values are left to None, this function will 

compute the code coverage for all files in this module. The function 

now exports the coverage options which were used. 

For example, to exclude files from the coverage report:: 

 

coverage_options=dict(omit=["*exclude*.py"]) 

 

Parameter *covtoken*: used to post the coverage report to 

`codecov <https://codecov.io/>`_. 

 

.. versionchanged:: 1.8 

Parameter *coverage_root* was added. 

""" 

whole_ouput = StringIOAndFile(logfile) 

runner = unittest.TextTestRunner(verbosity=0, stream=whole_ouput) 

path = os.path.abspath(os.path.join(os.path.split(logfile)[0])) 

stdout_this = stdout if stdout else sys.stdout 

datetime_begin = datetime.now() 

 

def _find_source(fold): 

fold0 = fold 

exists = os.path.exists(os.path.join(fold, ".gitignore")) 

while not exists: 

if len(fold) < 2: 

raise FileNotFoundError( 

"Unable to guess source from '{0}'.".format(fold0)) 

fold = os.path.split(fold)[0] 

exists = os.path.exists(os.path.join(fold, ".gitignore")) 

return os.path.normpath(os.path.abspath(fold)) 

 

def run_main(): 

res = main_run_test(runner, path_test=path, skip=-1, skip_list=skip_list, 

processes=processes, skip_function=skip_function, 

additional_ut_path=additional_ut_path, stdout=stdout, stderr=stderr, 

filter_warning=filter_warning, fLOG=fLOG) 

return res 

 

if "win" not in sys.platform and "DISPLAY" not in os.environ: 

# issue detected with travis 

# _tkinter.TclError: no display name and no $DISPLAY environment variable 

#os.environ["DISPLAY"] = "localhost:0" 

pass 

 

# other coverage reports 

if add_coverage_folder is not None and dump_coverage is not None: 

sub = os.path.split(dump_coverage)[0] 

sub = os.path.split(sub)[-1] 

other_cov_folders = find_coverage_report( 

add_coverage_folder, exclude=sub) 

mes = "[main_wrapper_tests] other_cov_folders...sub='{0}'".format(sub) 

stdout_this.write(mes + "\n") 

for k, v in sorted(other_cov_folders.items()): 

mes = "[main_wrapper_tests] k='{0}' v={1}".format(k, v) 

stdout_this.write(mes + "\n") 

if len(other_cov_folders) == 0: 

other_cov_folders = None 

else: 

other_cov_folders = None 

 

# to deal with: _tkinter.TclError: no display name and no $DISPLAY 

# environment variable 

from .tkinter_helper import fix_tkinter_issues_virtualenv, _first_execution 

fLOG("[main_wrapper_tests] MODULES (1): matplotlib already imported", 

"matplotlib" in sys.modules, "first execution", _first_execution) 

r = fix_tkinter_issues_virtualenv(fLOG=fLOG) 

fLOG("[main_wrapper_tests] MODULES (2): matplotlib imported", 

"matplotlib" in sys.modules, "first execution", _first_execution) 

fLOG("[main_wrapper_tests] fix_tkinter_issues_virtualenv", r) 

 

def tested_module(folder, project_var_name, setup_params): 

# module mod 

if setup_params is None: 

setup_params = {} 

out, err = call_setup_hook( 

folder, project_var_name, fLOG=fLOG, use_print=hook_print, **setup_params) 

if len(err) > 0 and err != "no _setup_hook": 

# fix introduced because pip 8.0 displays annoying warnings 

# RuntimeWarning: Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect 

# RuntimeWarning: Config variable 'WITH_PYMALLOC' is unset, Python 

# ABI tag may be incorrect 

lines = err.split("\n") 

keep = [] 

for line in lines: 

line = line.rstrip("\r\t ") 

if line and not line.startswith(" ") and "RuntimeWarning: Config variable" not in line: 

keep.append(line) 

if len(keep) > 0: 

raise SetupHookException( 

"unable to run _setup_hook\n**OUT:\n{0}\n**[pyqerror]\n{1}\n**FOLDER:\n{2}\n**NAME:\n{3}\n**KEEP:\n{4}\n**" 

.format(out, err, folder, project_var_name, "\n".join(keep))) 

out += "\nWARNINGS:\n" + err 

err = None 

return out, err 

 

# project_var_name 

folder = os.path.normpath( 

os.path.join(os.path.dirname(logfile), "..", "src")) 

if not os.path.exists(folder): 

folder = os.path.normpath( 

os.path.join(os.path.dirname(logfile), "..")) 

if not os.path.exists(folder): 

raise FileNotFoundError(folder) 

 

def selec_name(folder, name): 

if name.startswith('_') or name.startswith('.'): 

return False 

if name in ('bin', 'dist', 'build'): 

return False 

if '.egg' in name or 'dist_module27' in name: 

return False 

fold = os.path.join(folder, name) 

if not os.path.isdir(fold): 

return False 

init = os.path.join(fold, '__init__.py') 

if not os.path.exists(init): 

return False 

return True 

 

content = [_ for _ in os.listdir(folder) if selec_name(folder, _)] 

if len(content) != 1: 

raise FileNotFoundError( 

"Unable to guess the project name in {0}\n{1}".format(folder, "\n".join(content))) 

 

project_var_name = content[0] 

src_abs = os.path.normpath(os.path.abspath( 

os.path.join(os.path.dirname(logfile), ".."))) 

 

root_src = os.path.join(src_abs, "src", project_var_name) 

if not os.path.exists(root_src): 

root_src = os.path.join(src_abs, project_var_name) 

if not os.path.exists(root_src): 

raise FileNotFoundError("Unable to find '{}'.".format(root_src)) 

srcp = os.path.relpath(root_src, os.getcwd()) 

 

if get_user() in srcp: 

raise Exception( 

"The location of the source should not contain '{0}': {1}".format(get_user(), srcp)) 

 

if only_setup_hook: 

tested_module(src_abs, project_var_name, setup_params) 

 

else: 

# coverage 

if add_coverage: 

stdout_this.write("[main_wrapper_tests] --- COVERAGE BEGIN ---\n") 

if report_folder is None: 

report_folder = os.path.join( 

os.path.abspath(os.path.dirname(logfile)), "..", "_doc", "sphinxdoc", "source", "coverage") 

 

fLOG("[main_wrapper_tests] call _setup_hook", 

src_abs, "name=", project_var_name) 

tested_module(src_abs, project_var_name, setup_params) 

fLOG("[main_wrapper_tests] end _setup_hook") 

 

fLOG("[main_wrapper_tests] current folder", os.getcwd()) 

fLOG("[main_wrapper_tests] enabling coverage", srcp) 

dfile = os.path.join(report_folder, ".coverage") 

 

# we clean previous report or we create an empty folder 

if os.path.exists(report_folder): 

for afile in os.listdir(report_folder): 

full = os.path.join(report_folder, afile) 

os.remove(full) 

 

# we run the coverage 

if coverage_options is None: 

coverage_options = {} 

if "source" in coverage_options: 

coverage_options["source"].append(srcp) 

else: 

coverage_options["source"] = [srcp] 

if "data_file" not in coverage_options: 

coverage_options["data_file"] = dfile 

 

from coverage import coverage 

cov = coverage(**coverage_options) 

if coverage_exclude_lines is not None: 

for line in coverage_exclude_lines: 

cov.exclude(line) 

else: 

cov.exclude("raise NotImplementedError") 

stdout_this.write("[main_wrapper_tests] ENABLE COVERAGE\n") 

cov.start() 

 

res = run_main() 

 

cov.stop() 

stdout_this.write( 

"[main_wrapper_tests] STOP COVERAGE + REPORT into '{0}\n'".format(report_folder)) 

 

from coverage.misc import CoverageException as RawCoverageException 

try: 

cov.html_report(directory=report_folder) 

except RawCoverageException as e: 

raise RuntimeError("Unable to publish the coverage repot into '{}'," 

"\nsource='{}'\ndata='{}'".format( 

report_folder, coverage_options["source"], 

coverage_options.get("data_file", ''))) from e 

outfile = os.path.join(report_folder, "coverage_report.xml") 

cov.xml_report(outfile=outfile) 

cov.save() 

srcp_s = [] 

 

# we clean absolute path from the produced files 

def clean_absolute_path(): 

fLOG("[main_wrapper_tests] replace ", 

srcp, ' by ', project_var_name) 

srcp_s.clear() 

srcp_s.extend([os.path.abspath(os.path.normpath(srcp)), 

os.path.normpath(srcp)]) 

bsrcp = [bytes(b, encoding="utf-8") for b in srcp_s] 

bproj = bytes(project_var_name, encoding="utf-8") 

for afile in os.listdir(report_folder): 

full = os.path.join(report_folder, afile) 

with open(full, "rb") as f: 

content = f.read() 

for b in bsrcp: 

content = content.replace(b, bproj) 

with open(full, "wb") as f: 

f.write(content) 

 

clean_absolute_path() 

 

# we print debug information for the coverage 

def write_covlog(covs): 

fLOG("[main_wrapper_tests] add debug information") 

outcov = os.path.join(report_folder, "covlog.txt") 

rows = [] 

rows.append("COVERAGE OPTIONS") 

for k, v in sorted(coverage_options.items()): 

rows.append("{0}={1}".format(k, v)) 

rows.append("") 

rows.append("EXCLUDE LINES") 

for k in cov.get_exclude_list(): 

rows.append(k) 

rows.append("") 

rows.append("OPTIONS") 

for option_spec in sorted(cov.config.CONFIG_FILE_OPTIONS): 

attr = option_spec[0] 

if attr == "sort": 

# we skip, it raises an exception with coverage 4.2 

continue 

v = getattr(cov.config, attr) 

st = "{0}={1}".format(attr, v) 

rows.append(st) 

rows.append("") 

 

if covs is not None: 

for add in sorted(covs): 

rows.append("MERGE='{0}'".format(add)) 

 

content = "\n".join(rows) 

 

reps = [] 

for _ in srcp_s[:1]: 

__ = os.path.normpath(os.path.join(_, "..", "..", "..")) 

__ += "/" 

reps.append(__) 

reps.append(__.replace("/", "\\")) 

reps.append(__.replace("/", "\\\\")) 

reps.append(__.replace("\\", "\\\\")) 

 

for s in reps: 

content = content.replace(s, "") 

 

with open(outcov, "w", encoding="utf8") as f: 

f.write(content) 

 

write_covlog(None) 

 

if dump_coverage is not None: 

src = os.path.dirname(outfile) 

stdout_this.write("[main_wrapper_tests] dump coverage from '{1}' to '{0}'\n".format( 

dump_coverage, outfile)) 

synchronize_folder(src, dump_coverage, 

copy_1to2=True, fLOG=fLOG) 

 

if other_cov_folders is not None: 

source = _find_source(src) 

if not source: 

raise FileNotFoundError( 

"Unable to find source '{0}' from '{1}'".format(source, src)) 

if coverage_root: 

source_src = os.path.join(source, coverage_root) 

if os.path.exists(source_src): 

source = source_src 

stdout_this.write( 

"[main_wrapper_tests] ADD COVERAGE for source='{0}'\n".format(source)) 

covs = list(_[0] for _ in other_cov_folders.values()) 

covs.append(os.path.abspath( 

os.path.normpath(os.path.join(src, '.coverage')))) 

stdout_this.write( 

"[main_wrapper_tests] ADD COVERAGE COMBINE={0}\n".format(covs)) 

stdout_this.write( 

"[main_wrapper_tests] DUMP INTO='{0}'\n".format(src)) 

try: 

coverage_combine(covs, src, source) 

write_covlog(covs) 

except Exception as e: 

warnings.warn("[main_wrapper_tests] {}".format( 

str(e).replace("\n", " "))) 

 

if covtoken: 

if isinstance(covtoken, tuple): 

if eval(covtoken[1]): 

# publishing token 

mes = "[main_wrapper_tests] PUBLISH COVERAGE to codecov '{0}' EVAL ({1})".format( 

covtoken[0], covtoken[1]) 

if stdout is not None: 

stdout.write(mes) 

stdout_this.write(mes + '\n') 

fLOG(mes) 

publish_coverage_on_codecov( 

token=covtoken[0], path=outfile, fLOG=fLOG) 

else: 

fLOG( 

"[main_wrapper_tests] skip publishing coverage to codecov due to False:", covtoken[1]) 

else: 

# publishing token 

fLOG( 

"[main_wrapper_tests] publishing coverage to codecov", covtoken) 

publish_coverage_on_codecov( 

token=covtoken, path=outfile, fLOG=fLOG) 

stdout_this.write("[main_wrapper_tests] --- COVERAGE END ---\n") 

else: 

stdout_this.write( 

"[main_wrapper_tests] --- NO COVERAGE BEGIN ---\n") 

if covtoken and (not isinstance(covtoken, tuple) or eval(covtoken[1])): 

raise CoverageException( 

"covtoken is not null but add_coverage is not True, coverage cannot be published") 

tested_module(src_abs, project_var_name, setup_params) 

res = run_main() 

stdout_this.write("[main_wrapper_tests] --- NO COVERAGE END ---\n") 

 

fLOG("[main_wrapper_tests] SUMMARY -------------------------") 

for r in res["tests"]: 

k = str(r[1]) 

if "errors=0" not in k or "failures=0" not in k: 

fLOG("*", r[1], r[0]) 

 

fLOG("[main_wrapper_tests] CHECK EXCEPTION -----------------") 

err = res.get("err", "") 

if len(err) > 0: 

# Remove most of the Sphinx warnings (sphinx < 1.8) 

lines = err.split("\n") 

lines = [ 

_ for _ in lines if _ and "is already registered, it will be overridden" not in _] 

err = "\n".join(lines) 

if len(err) > 0: 

raise TestWrappedException(err) 

 

datetime_end = datetime.now() 

 

rows = ["[main_wrapper_tests] END", 

"[main_wrapper_tests] begin time {0}".format(datetime_begin), 

"[main_wrapper_tests] end time {0}".format(datetime_end), 

"[main_wrapper_tests] duration {0}".format(datetime_end - datetime_begin)] 

for row in rows: 

fLOG(row) 

stdout_this.write(row + "\n")