Debian Codegraph @

addons/web/static/lib/hoot/core/suite.js

raw
1 /** @odoo-module */
2
3 import { Callbacks, HootError, createReporting, stringify } from "../hoot_utils";
4 import { Job } from "./job";
5
6 /**
7 * @typedef {import("./tag").Tag} Tag
8 *
9 * @typedef {import("./test").Test} Test
10 */
11
12 //-----------------------------------------------------------------------------
13 // Global
14 //-----------------------------------------------------------------------------
15
16 const {
17 Object: { freeze: $freeze },
18 } = globalThis;
19
20 //-----------------------------------------------------------------------------
21 // Internal
22 //-----------------------------------------------------------------------------
23
24 class MinimalCallbacks extends Callbacks {
25 add() {}
26 call() {}
27 callSync() {}
28 clear() {}
29 }
30
31 const SHARED_CALLBACKS = new MinimalCallbacks();
32 const SHARED_CURRENT_JOBS = $freeze([]);
33
34 //-----------------------------------------------------------------------------
35 // Exports
36 //-----------------------------------------------------------------------------
37
38 /**
39 * @param {Pick<Suite, "name" | "parent">} suite
40 * @param {Error | string} message
41 * @returns {HootError}
42 */
43 export function suiteError({ name, parent }, message) {
44 const parentString = parent ? ` (in parent suite ${stringify(parent.name)})` : "";
45 const errorOptions = { level: "critical" };
46 let errorMessage = `error while registering suite ${stringify(name)}${parentString}`;
47 if (message instanceof Error) {
48 errorOptions.cause = message;
49 } else {
50 errorMessage += `: ${message}`;
51 }
52 return new HootError(errorMessage, errorOptions);
53 }
54
55 export class Suite extends Job {
56 callbacks = new Callbacks();
57 currentJobIndex = 0;
58 /** @type {(Suite | Test)[]} */
59 currentJobs = [];
60 /** @type {(Suite | Test)[]} */
61 jobs = [];
62 reporting = createReporting();
63
64 totalSuiteCount = 0;
65 totalTestCount = 0;
66
67 get weight() {
68 return this.totalTestCount;
69 }
70
71 addJob(job) {
72 this.jobs.push(job);
73
74 if (job instanceof Suite) {
75 this.increaseSuiteCount();
76 } else {
77 this.increaseTestCount();
78 }
79 }
80
81 cleanup() {
82 this.parent?.reporting.add({ suites: +1 });
83 this.minimize();
84 }
85
86 minimize() {
87 super.minimize();
88
89 this.callbacks.clear();
90
91 this.callbacks = SHARED_CALLBACKS;
92 this.currentJobs = SHARED_CURRENT_JOBS;
93 }
94
95 increaseSuiteCount() {
96 this.totalSuiteCount++;
97 this.parent?.increaseSuiteCount();
98 }
99
100 increaseTestCount() {
101 this.totalTestCount++;
102 this.parent?.increaseTestCount();
103 }
104
105 reset() {
106 this.currentJobIndex = 0;
107
108 for (const job of this.jobs) {
109 job.runCount = 0;
110
111 if (job instanceof Suite) {
112 job.reset();
113 }
114 }
115 }
116
117 /**
118 * @param {Job[]} jobs
119 */
120 setCurrentJobs(jobs) {
121 if (this.isMinimized) {
122 return;
123 }
124 this.currentJobs = jobs;
125 this.currentJobIndex = 0;
126 }
127 }