Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. 'use strict';
  2. /*
  3. Copyright 2012-2015, Yahoo Inc.
  4. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  5. */
  6. const fs = require('fs');
  7. const path = require('path');
  8. const html = require('html-escaper');
  9. const { ReportBase } = require('istanbul-lib-report');
  10. const annotator = require('./annotator');
  11. function htmlHead(details) {
  12. return `
  13. <head>
  14. <title>Code coverage report for ${html.escape(details.entity)}</title>
  15. <meta charset="utf-8" />
  16. <link rel="stylesheet" href="${html.escape(details.prettify.css)}" />
  17. <link rel="stylesheet" href="${html.escape(details.base.css)}" />
  18. <link rel="shortcut icon" type="image/x-icon" href="${html.escape(
  19. details.favicon
  20. )}" />
  21. <meta name="viewport" content="width=device-width, initial-scale=1" />
  22. <style type='text/css'>
  23. .coverage-summary .sorter {
  24. background-image: url(${html.escape(details.sorter.image)});
  25. }
  26. </style>
  27. </head>
  28. `;
  29. }
  30. function headerTemplate(details) {
  31. function metricsTemplate({ pct, covered, total }, kind) {
  32. return `
  33. <div class='fl pad1y space-right2'>
  34. <span class="strong">${pct}% </span>
  35. <span class="quiet">${kind}</span>
  36. <span class='fraction'>${covered}/${total}</span>
  37. </div>
  38. `;
  39. }
  40. function skipTemplate(metrics) {
  41. const statements = metrics.statements.skipped;
  42. const branches = metrics.branches.skipped;
  43. const functions = metrics.functions.skipped;
  44. const countLabel = (c, label, plural) =>
  45. c === 0 ? [] : `${c} ${label}${c === 1 ? '' : plural}`;
  46. const skips = [].concat(
  47. countLabel(statements, 'statement', 's'),
  48. countLabel(functions, 'function', 's'),
  49. countLabel(branches, 'branch', 'es')
  50. );
  51. if (skips.length === 0) {
  52. return '';
  53. }
  54. return `
  55. <div class='fl pad1y'>
  56. <span class="strong">${skips.join(', ')}</span>
  57. <span class="quiet">Ignored</span> &nbsp;&nbsp;&nbsp;&nbsp;
  58. </div>
  59. `;
  60. }
  61. return `
  62. <!doctype html>
  63. <html lang="en">
  64. ${htmlHead(details)}
  65. <body>
  66. <div class='wrapper'>
  67. <div class='pad1'>
  68. <h1>${details.pathHtml}</h1>
  69. <div class='clearfix'>
  70. ${metricsTemplate(details.metrics.statements, 'Statements')}
  71. ${metricsTemplate(details.metrics.branches, 'Branches')}
  72. ${metricsTemplate(details.metrics.functions, 'Functions')}
  73. ${metricsTemplate(details.metrics.lines, 'Lines')}
  74. ${skipTemplate(details.metrics)}
  75. </div>
  76. <p class="quiet">
  77. Press <em>n</em> or <em>j</em> to go to the next uncovered block, <em>b</em>, <em>p</em> or <em>k</em> for the previous block.
  78. </p>
  79. </div>
  80. <div class='status-line ${details.reportClass}'></div>
  81. `;
  82. }
  83. function footerTemplate(details) {
  84. return `
  85. <div class='push'></div><!-- for sticky footer -->
  86. </div><!-- /wrapper -->
  87. <div class='footer quiet pad2 space-top1 center small'>
  88. Code coverage generated by
  89. <a href="https://istanbul.js.org/" target="_blank">istanbul</a>
  90. at ${html.escape(details.datetime)}
  91. </div>
  92. </div>
  93. <script src="${html.escape(details.prettify.js)}"></script>
  94. <script>
  95. window.onload = function () {
  96. prettyPrint();
  97. };
  98. </script>
  99. <script src="${html.escape(details.sorter.js)}"></script>
  100. <script src="${html.escape(details.blockNavigation.js)}"></script>
  101. </body>
  102. </html>
  103. `;
  104. }
  105. function detailTemplate(data) {
  106. const lineNumbers = new Array(data.maxLines).fill().map((_, i) => i + 1);
  107. const lineLink = num =>
  108. `<a name='L${num}'></a><a href='#L${num}'>${num}</a>`;
  109. const lineCount = line =>
  110. `<span class="cline-any cline-${line.covered}">${line.hits}</span>`;
  111. /* This is rendered in a `<pre>`, need control of all whitespace. */
  112. return [
  113. '<tr>',
  114. `<td class="line-count quiet">${lineNumbers
  115. .map(lineLink)
  116. .join('\n')}</td>`,
  117. `<td class="line-coverage quiet">${data.lineCoverage
  118. .map(lineCount)
  119. .join('\n')}</td>`,
  120. `<td class="text"><pre class="prettyprint lang-js">${data.annotatedCode.join(
  121. '\n'
  122. )}</pre></td>`,
  123. '</tr>'
  124. ].join('');
  125. }
  126. const summaryTableHeader = [
  127. '<div class="pad1">',
  128. '<table class="coverage-summary">',
  129. '<thead>',
  130. '<tr>',
  131. ' <th data-col="file" data-fmt="html" data-html="true" class="file">File</th>',
  132. ' <th data-col="pic" data-type="number" data-fmt="html" data-html="true" class="pic"></th>',
  133. ' <th data-col="statements" data-type="number" data-fmt="pct" class="pct">Statements</th>',
  134. ' <th data-col="statements_raw" data-type="number" data-fmt="html" class="abs"></th>',
  135. ' <th data-col="branches" data-type="number" data-fmt="pct" class="pct">Branches</th>',
  136. ' <th data-col="branches_raw" data-type="number" data-fmt="html" class="abs"></th>',
  137. ' <th data-col="functions" data-type="number" data-fmt="pct" class="pct">Functions</th>',
  138. ' <th data-col="functions_raw" data-type="number" data-fmt="html" class="abs"></th>',
  139. ' <th data-col="lines" data-type="number" data-fmt="pct" class="pct">Lines</th>',
  140. ' <th data-col="lines_raw" data-type="number" data-fmt="html" class="abs"></th>',
  141. '</tr>',
  142. '</thead>',
  143. '<tbody>'
  144. ].join('\n');
  145. function summaryLineTemplate(details) {
  146. const { reportClasses, metrics, file, output } = details;
  147. const percentGraph = pct => {
  148. if (!isFinite(pct)) {
  149. return '';
  150. }
  151. const cls = ['cover-fill'];
  152. if (pct === 100) {
  153. cls.push('cover-full');
  154. }
  155. pct = Math.floor(pct);
  156. return [
  157. `<div class="${cls.join(' ')}" style="width: ${pct}%"></div>`,
  158. `<div class="cover-empty" style="width: ${100 - pct}%"></div>`
  159. ].join('');
  160. };
  161. const summaryType = (type, showGraph = false) => {
  162. const info = metrics[type];
  163. const reportClass = reportClasses[type];
  164. const result = [
  165. `<td data-value="${info.pct}" class="pct ${reportClass}">${info.pct}%</td>`,
  166. `<td data-value="${info.total}" class="abs ${reportClass}">${info.covered}/${info.total}</td>`
  167. ];
  168. if (showGraph) {
  169. result.unshift(
  170. `<td data-value="${info.pct}" class="pic ${reportClass}">`,
  171. `<div class="chart">${percentGraph(info.pct)}</div>`,
  172. `</td>`
  173. );
  174. }
  175. return result;
  176. };
  177. return []
  178. .concat(
  179. '<tr>',
  180. `<td class="file ${
  181. reportClasses.statements
  182. }" data-value="${html.escape(file)}"><a href="${html.escape(
  183. output
  184. )}">${html.escape(file)}</a></td>`,
  185. summaryType('statements', true),
  186. summaryType('branches'),
  187. summaryType('functions'),
  188. summaryType('lines'),
  189. '</tr>\n'
  190. )
  191. .join('\n\t');
  192. }
  193. const summaryTableFooter = ['</tbody>', '</table>', '</div>'].join('\n');
  194. const emptyClasses = {
  195. statements: 'empty',
  196. lines: 'empty',
  197. functions: 'empty',
  198. branches: 'empty'
  199. };
  200. const standardLinkMapper = {
  201. getPath(node) {
  202. if (typeof node === 'string') {
  203. return node;
  204. }
  205. let filePath = node.getQualifiedName();
  206. if (node.isSummary()) {
  207. if (filePath !== '') {
  208. filePath += '/index.html';
  209. } else {
  210. filePath = 'index.html';
  211. }
  212. } else {
  213. filePath += '.html';
  214. }
  215. return filePath;
  216. },
  217. relativePath(source, target) {
  218. const targetPath = this.getPath(target);
  219. const sourcePath = path.dirname(this.getPath(source));
  220. return path.posix.relative(sourcePath, targetPath);
  221. },
  222. assetPath(node, name) {
  223. return this.relativePath(this.getPath(node), name);
  224. }
  225. };
  226. function fixPct(metrics) {
  227. Object.keys(emptyClasses).forEach(key => {
  228. metrics[key].pct = 0;
  229. });
  230. return metrics;
  231. }
  232. class HtmlReport extends ReportBase {
  233. constructor(opts) {
  234. super();
  235. this.verbose = opts.verbose;
  236. this.linkMapper = opts.linkMapper || standardLinkMapper;
  237. this.subdir = opts.subdir || '';
  238. this.date = Date();
  239. this.skipEmpty = opts.skipEmpty;
  240. }
  241. getBreadcrumbHtml(node) {
  242. let parent = node.getParent();
  243. const nodePath = [];
  244. while (parent) {
  245. nodePath.push(parent);
  246. parent = parent.getParent();
  247. }
  248. const linkPath = nodePath.map(ancestor => {
  249. const target = this.linkMapper.relativePath(node, ancestor);
  250. const name = ancestor.getRelativeName() || 'All files';
  251. return '<a href="' + target + '">' + name + '</a>';
  252. });
  253. linkPath.reverse();
  254. return linkPath.length > 0
  255. ? linkPath.join(' / ') + ' ' + node.getRelativeName()
  256. : 'All files';
  257. }
  258. fillTemplate(node, templateData, context) {
  259. const linkMapper = this.linkMapper;
  260. const summary = node.getCoverageSummary();
  261. templateData.entity = node.getQualifiedName() || 'All files';
  262. templateData.metrics = summary;
  263. templateData.reportClass = context.classForPercent(
  264. 'statements',
  265. summary.statements.pct
  266. );
  267. templateData.pathHtml = this.getBreadcrumbHtml(node);
  268. templateData.base = {
  269. css: linkMapper.assetPath(node, 'base.css')
  270. };
  271. templateData.sorter = {
  272. js: linkMapper.assetPath(node, 'sorter.js'),
  273. image: linkMapper.assetPath(node, 'sort-arrow-sprite.png')
  274. };
  275. templateData.blockNavigation = {
  276. js: linkMapper.assetPath(node, 'block-navigation.js')
  277. };
  278. templateData.prettify = {
  279. js: linkMapper.assetPath(node, 'prettify.js'),
  280. css: linkMapper.assetPath(node, 'prettify.css')
  281. };
  282. templateData.favicon = linkMapper.assetPath(node, 'favicon.png');
  283. }
  284. getTemplateData() {
  285. return { datetime: this.date };
  286. }
  287. getWriter(context) {
  288. if (!this.subdir) {
  289. return context.writer;
  290. }
  291. return context.writer.writerForDir(this.subdir);
  292. }
  293. onStart(root, context) {
  294. const assetHeaders = {
  295. '.js': '/* eslint-disable */\n'
  296. };
  297. ['.', 'vendor'].forEach(subdir => {
  298. const writer = this.getWriter(context);
  299. const srcDir = path.resolve(__dirname, 'assets', subdir);
  300. fs.readdirSync(srcDir).forEach(f => {
  301. const resolvedSource = path.resolve(srcDir, f);
  302. const resolvedDestination = '.';
  303. const stat = fs.statSync(resolvedSource);
  304. let dest;
  305. if (stat.isFile()) {
  306. dest = resolvedDestination + '/' + f;
  307. if (this.verbose) {
  308. console.log('Write asset: ' + dest);
  309. }
  310. writer.copyFile(
  311. resolvedSource,
  312. dest,
  313. assetHeaders[path.extname(f)]
  314. );
  315. }
  316. });
  317. });
  318. }
  319. onSummary(node, context) {
  320. const linkMapper = this.linkMapper;
  321. const templateData = this.getTemplateData();
  322. const children = node.getChildren();
  323. const skipEmpty = this.skipEmpty;
  324. this.fillTemplate(node, templateData, context);
  325. const cw = this.getWriter(context).writeFile(linkMapper.getPath(node));
  326. cw.write(headerTemplate(templateData));
  327. cw.write(summaryTableHeader);
  328. children.forEach(child => {
  329. const metrics = child.getCoverageSummary();
  330. const isEmpty = metrics.isEmpty();
  331. if (skipEmpty && isEmpty) {
  332. return;
  333. }
  334. const reportClasses = isEmpty
  335. ? emptyClasses
  336. : {
  337. statements: context.classForPercent(
  338. 'statements',
  339. metrics.statements.pct
  340. ),
  341. lines: context.classForPercent(
  342. 'lines',
  343. metrics.lines.pct
  344. ),
  345. functions: context.classForPercent(
  346. 'functions',
  347. metrics.functions.pct
  348. ),
  349. branches: context.classForPercent(
  350. 'branches',
  351. metrics.branches.pct
  352. )
  353. };
  354. const data = {
  355. metrics: isEmpty ? fixPct(metrics) : metrics,
  356. reportClasses,
  357. file: child.getRelativeName(),
  358. output: linkMapper.relativePath(node, child)
  359. };
  360. cw.write(summaryLineTemplate(data) + '\n');
  361. });
  362. cw.write(summaryTableFooter);
  363. cw.write(footerTemplate(templateData));
  364. cw.close();
  365. }
  366. onDetail(node, context) {
  367. const linkMapper = this.linkMapper;
  368. const templateData = this.getTemplateData();
  369. this.fillTemplate(node, templateData, context);
  370. const cw = this.getWriter(context).writeFile(linkMapper.getPath(node));
  371. cw.write(headerTemplate(templateData));
  372. cw.write('<pre><table class="coverage">\n');
  373. cw.write(detailTemplate(annotator(node.getFileCoverage(), context)));
  374. cw.write('</table></pre>\n');
  375. cw.write(footerTemplate(templateData));
  376. cw.close();
  377. }
  378. }
  379. module.exports = HtmlReport;