Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/cov_report/__init__.py: 62.00%

Shortcuts 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

40 statements  

1""" 

2### `cov_report` 

3 

4Inserts a coverage report. 

5 

6Based on 

7[pawamoy](https://github.com/pawamoy/mkdocs-coverage/tree/master/src/mkdocs_coverage)' 

8work. 

9 

10The report must exist in the file system at evaluation time at 

11- dir parameter or 

12- $d_cover_html env var 

13- LP Header Syntax: `bash lp mode=cov_report [dir=<report dir>]` 

14 

15#### Features: 

16 

17- While LP blocks are running, the LP plugin creates a [coverage context](https://coverage.readthedocs.io/en/coverage-5.5/contexts.html#dynamic-contexts), 

18 with the current markdown file. 

19- When the lp plugin has a non empty config value for `coverage_backrefs`, a link will be created back to the markdown source with the lp block. 

20 

21Mechanics how to create coverage reports in general: See the coverage setup in this repo's `/make` file. 

22 

23""" 

24 

25 

26import os lp|index.md

27from distutils.dir_util import copy_tree lp|index.md

28from functools import partial lp|index.md

29 

30from lcdoc import lp lp|index.md

31from lcdoc.mkdocs.lp.plugs import python lp|index.md

32from lcdoc.mkdocs.tools import make_img lp|index.md

33from lcdoc.tools import dirname, exists, os, project lp|index.md

34 

35config, page, Session = (python.config, python.page, python.Session) lp|index.md

36 

37 

38def register(fmts): lp|index.md

39 fmts['coverage'] = incl_coverage lp|index.md

40 

41 

42cov_files = lambda d: [k for k in os.listdir(d) if k.startswith('.coverage')] 42 ↛ exitline 42 didn't run the lambda on line 42 or line 42 didn't run the list comprehension on line 42lp|index.md

43err = lp.err lp|index.md

44 

45# thanks timothy: 

46T = '''  

47<style> 

48.md-main__inner { 

49 max-width: none; 

50} 

51article h1, article > a { 

52 display: none; 

53} 

54</style> 

55 

56[<small>Full Page</small>](%(d_rel_html)s) 

57<iframe id="coviframe_%(name)s" src="%(d_rel_html)s/index.html" frameborder="0" scrolling="no" 

58onload="resizeIframe();" width="100%%"></iframe> 

59 

60<script> 

61var coviframe = document.getElementById("coviframe_%(name)s"); 

62 

63function resizeIframe() { 

64 coviframe.style.height = coviframe.contentWindow.document.documentElement.offsetHeight + 'px'; 

65} 

66 

67coviframe.contentWindow.document.body.onclick = function() { 

68 coviframe.contentWindow.location.reload(); 

69} 

70</script> 

71 

72''' 

73 

74 

75def incl_html_report(**kw): lp|index.md

76 return T % kw 

77 

78 

79idx = lambda d: d + '/index.html' lp|about/coverage.mdlp|index.md

80import time lp|index.md

81 

82now = time.time lp|index.md

83 

84 

85def copy_files_to_html_site_dir(LP, d_rel_html, d_cover): lp|index.md

86 """have to copy all report files over to site dir, since at *first* build those 

87 had not been scanned into the files list, when we only symlink them from the cover dir 

88 """ 

89 # e.g. '/home/gk/repos/docutools/site' or the tmp dir at mkdocs serve: 

90 d_site = LP.config['site_dir'] 

91 d_dest = d_site + '/' + dirname(LP.page.file.src_path) + d_rel_html 

92 # it never exists, server cleans it off at reruns: 

93 # if time would be critical we also needed to link them over to docs/autodocs which 

94 # is not scanned for changes 

95 # if exists(idx(d_dest)) and os.stat(idx(d_dest)).st_mtime >= os.stat(idx(d_cover)): return 

96 python.app.info('copying coverage html', frm=d_cover, to=d_dest) 

97 os.makedirs(d_dest, exist_ok=True) 

98 try: 

99 copy_tree(d_cover, d_dest) 

100 except Exception as ex: 

101 return err('Could not copy cover html', frm=d_cover, to=d_dest) 

102 

103 

104d_cover_dflt = os.environ.get('d_cover_html', 'build/coverage/overall') lp|index.md

105 

106from lcdoc.mkdocs import lp lp|index.md

107 

108LP = lp.LP lp|index.md

109 

110 

111def incl_coverage(s, name='overall', dir=d_cover_dflt, **kw): lp|index.md

112 if not dir[0] == '/': 112 ↛ 114line 112 didn't jump to line 114, because the condition on line 112 was never falselp|about/coverage.md

113 dir = project.root(config()) + '/' + dir lp|about/coverage.md

114 if not exists(idx(dir)): 114 ↛ 116line 114 didn't jump to line 116, because the condition on line 114 was never falselp|about/coverage.md

115 return 'No coverage data yet in %s' % dir lp|about/coverage.md

116 d_rel_html = '/media/cov/%s' % name 

117 

118 err = copy_files_to_html_site_dir(LP, d_rel_html, dir) 

119 if err: 

120 return err 

121 return { 

122 'nocache': True, 

123 'res': incl_html_report(name=name, d_rel_html='..' + d_rel_html), 

124 }