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.

summarizer-factory.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const coverage = require('istanbul-lib-coverage');
  7. const Path = require('./path');
  8. const { BaseNode, BaseTree } = require('./tree');
  9. class ReportNode extends BaseNode {
  10. constructor(path, fileCoverage) {
  11. super();
  12. this.path = path;
  13. this.parent = null;
  14. this.fileCoverage = fileCoverage;
  15. this.children = [];
  16. }
  17. static createRoot(children) {
  18. const root = new ReportNode(new Path([]));
  19. children.forEach(child => {
  20. root.addChild(child);
  21. });
  22. return root;
  23. }
  24. addChild(child) {
  25. child.parent = this;
  26. this.children.push(child);
  27. }
  28. asRelative(p) {
  29. if (p.substring(0, 1) === '/') {
  30. return p.substring(1);
  31. }
  32. return p;
  33. }
  34. getQualifiedName() {
  35. return this.asRelative(this.path.toString());
  36. }
  37. getRelativeName() {
  38. const parent = this.getParent();
  39. const myPath = this.path;
  40. let relPath;
  41. let i;
  42. const parentPath = parent ? parent.path : new Path([]);
  43. if (parentPath.ancestorOf(myPath)) {
  44. relPath = new Path(myPath.elements());
  45. for (i = 0; i < parentPath.length; i += 1) {
  46. relPath.shift();
  47. }
  48. return this.asRelative(relPath.toString());
  49. }
  50. return this.asRelative(this.path.toString());
  51. }
  52. getParent() {
  53. return this.parent;
  54. }
  55. getChildren() {
  56. return this.children;
  57. }
  58. isSummary() {
  59. return !this.fileCoverage;
  60. }
  61. getFileCoverage() {
  62. return this.fileCoverage;
  63. }
  64. getCoverageSummary(filesOnly) {
  65. const cacheProp = `c_${filesOnly ? 'files' : 'full'}`;
  66. let summary;
  67. if (Object.prototype.hasOwnProperty.call(this, cacheProp)) {
  68. return this[cacheProp];
  69. }
  70. if (!this.isSummary()) {
  71. summary = this.getFileCoverage().toSummary();
  72. } else {
  73. let count = 0;
  74. summary = coverage.createCoverageSummary();
  75. this.getChildren().forEach(child => {
  76. if (filesOnly && child.isSummary()) {
  77. return;
  78. }
  79. count += 1;
  80. summary.merge(child.getCoverageSummary(filesOnly));
  81. });
  82. if (count === 0 && filesOnly) {
  83. summary = null;
  84. }
  85. }
  86. this[cacheProp] = summary;
  87. return summary;
  88. }
  89. }
  90. class ReportTree extends BaseTree {
  91. constructor(root, childPrefix) {
  92. super(root);
  93. const maybePrefix = node => {
  94. if (childPrefix && !node.isRoot()) {
  95. node.path.unshift(childPrefix);
  96. }
  97. };
  98. this.visit({
  99. onDetail: maybePrefix,
  100. onSummary(node) {
  101. maybePrefix(node);
  102. node.children.sort((a, b) => {
  103. const astr = a.path.toString();
  104. const bstr = b.path.toString();
  105. return astr < bstr
  106. ? -1
  107. : astr > bstr
  108. ? 1
  109. : /* istanbul ignore next */ 0;
  110. });
  111. }
  112. });
  113. }
  114. }
  115. function findCommonParent(paths) {
  116. return paths.reduce(
  117. (common, path) => common.commonPrefixPath(path),
  118. paths[0] || new Path([])
  119. );
  120. }
  121. function findOrCreateParent(parentPath, nodeMap, created = () => {}) {
  122. let parent = nodeMap[parentPath.toString()];
  123. if (!parent) {
  124. parent = new ReportNode(parentPath);
  125. nodeMap[parentPath.toString()] = parent;
  126. created(parentPath, parent);
  127. }
  128. return parent;
  129. }
  130. function toDirParents(list) {
  131. const nodeMap = Object.create(null);
  132. list.forEach(o => {
  133. const parent = findOrCreateParent(o.path.parent(), nodeMap);
  134. parent.addChild(new ReportNode(o.path, o.fileCoverage));
  135. });
  136. return Object.values(nodeMap);
  137. }
  138. function addAllPaths(topPaths, nodeMap, path, node) {
  139. const parent = findOrCreateParent(
  140. path.parent(),
  141. nodeMap,
  142. (parentPath, parent) => {
  143. if (parentPath.hasParent()) {
  144. addAllPaths(topPaths, nodeMap, parentPath, parent);
  145. } else {
  146. topPaths.push(parent);
  147. }
  148. }
  149. );
  150. parent.addChild(node);
  151. }
  152. function foldIntoOneDir(node, parent) {
  153. const { children } = node;
  154. if (children.length === 1 && !children[0].fileCoverage) {
  155. children[0].parent = parent;
  156. return foldIntoOneDir(children[0], parent);
  157. }
  158. node.children = children.map(child => foldIntoOneDir(child, node));
  159. return node;
  160. }
  161. function pkgSummaryPrefix(dirParents, commonParent) {
  162. if (!dirParents.some(dp => dp.path.length === 0)) {
  163. return;
  164. }
  165. if (commonParent.length === 0) {
  166. return 'root';
  167. }
  168. return commonParent.name();
  169. }
  170. class SummarizerFactory {
  171. constructor(coverageMap, defaultSummarizer = 'pkg') {
  172. this._coverageMap = coverageMap;
  173. this._defaultSummarizer = defaultSummarizer;
  174. this._initialList = coverageMap.files().map(filePath => ({
  175. filePath,
  176. path: new Path(filePath),
  177. fileCoverage: coverageMap.fileCoverageFor(filePath)
  178. }));
  179. this._commonParent = findCommonParent(
  180. this._initialList.map(o => o.path.parent())
  181. );
  182. if (this._commonParent.length > 0) {
  183. this._initialList.forEach(o => {
  184. o.path.splice(0, this._commonParent.length);
  185. });
  186. }
  187. }
  188. get defaultSummarizer() {
  189. return this[this._defaultSummarizer];
  190. }
  191. get flat() {
  192. if (!this._flat) {
  193. this._flat = new ReportTree(
  194. ReportNode.createRoot(
  195. this._initialList.map(
  196. node => new ReportNode(node.path, node.fileCoverage)
  197. )
  198. )
  199. );
  200. }
  201. return this._flat;
  202. }
  203. _createPkg() {
  204. const dirParents = toDirParents(this._initialList);
  205. if (dirParents.length === 1) {
  206. return new ReportTree(dirParents[0]);
  207. }
  208. return new ReportTree(
  209. ReportNode.createRoot(dirParents),
  210. pkgSummaryPrefix(dirParents, this._commonParent)
  211. );
  212. }
  213. get pkg() {
  214. if (!this._pkg) {
  215. this._pkg = this._createPkg();
  216. }
  217. return this._pkg;
  218. }
  219. _createNested() {
  220. const nodeMap = Object.create(null);
  221. const topPaths = [];
  222. this._initialList.forEach(o => {
  223. const node = new ReportNode(o.path, o.fileCoverage);
  224. addAllPaths(topPaths, nodeMap, o.path, node);
  225. });
  226. const topNodes = topPaths.map(node => foldIntoOneDir(node));
  227. if (topNodes.length === 1) {
  228. return new ReportTree(topNodes[0]);
  229. }
  230. return new ReportTree(ReportNode.createRoot(topNodes));
  231. }
  232. get nested() {
  233. if (!this._nested) {
  234. this._nested = this._createNested();
  235. }
  236. return this._nested;
  237. }
  238. }
  239. module.exports = SummarizerFactory;