@ohos-ports/floss 5.0.1-beta.6 → 5.0.1-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -68,6 +68,71 @@ function execOhosBm(args, timeout) {
68
68
  return execCmd(OHOS_BM_PATH, args, timeout);
69
69
  }
70
70
 
71
+ // ─── Browser process cleanup ─────────────────────────────────────────
72
+
73
+ var killedPids = new Set();
74
+
75
+ function getBrowserPids() {
76
+ try {
77
+ var { execFileSync } = require("child_process");
78
+ var output = execFileSync("ps", ["-A", "-o", "pid,name"], {
79
+ timeout: 5000,
80
+ encoding: "utf-8",
81
+ }).trim();
82
+ var pids = [];
83
+ var lines = output.split("\n");
84
+ for (var i = 0; i < lines.length; i++) {
85
+ var line = lines[i].trim();
86
+ if (line.indexOf("htbrowser") !== -1 || line.indexOf(BROWSER_PKG) !== -1) {
87
+ var parts = line.split(/\s+/);
88
+ var pid = parseInt(parts[0], 10);
89
+ if (!isNaN(pid) && pid > 0) {
90
+ pids.push(pid);
91
+ }
92
+ }
93
+ }
94
+ return pids;
95
+ } catch (e) {
96
+ return [];
97
+ }
98
+ }
99
+
100
+ function isProcessAlive(pid) {
101
+ try {
102
+ process.kill(pid, 0);
103
+ return true;
104
+ } catch (e) {
105
+ return false;
106
+ }
107
+ }
108
+
109
+ async function forceKillBrowserProcesses() {
110
+ for (var attempt = 0; attempt < 3; attempt++) {
111
+ var pids = getBrowserPids();
112
+ if (pids.length === 0) break;
113
+
114
+ var anyKilled = false;
115
+ for (var i = 0; i < pids.length; i++) {
116
+ var pid = pids[i];
117
+ if (killedPids.has(pid)) continue;
118
+ if (isProcessAlive(pid)) {
119
+ try {
120
+ process.kill(pid, "SIGKILL");
121
+ killedPids.add(pid);
122
+ anyKilled = true;
123
+ } catch (e) {
124
+ killedPids.add(pid);
125
+ }
126
+ } else {
127
+ killedPids.add(pid);
128
+ }
129
+ }
130
+
131
+ if (!anyKilled) break;
132
+ await sleep(500);
133
+ }
134
+ }
135
+
71
136
  // ─── Browser launch / close ───────────────────────────────────────────
72
137
 
73
138
  function buildLaunchArgs() {
@@ -99,6 +164,8 @@ async function launchBrowser() {
99
164
  // 2. Kill existing browser process
100
165
  try { execOhosAa(["force-stop", "--bundlename", BROWSER_PKG], 10000); } catch {}
101
166
  await sleep(1000);
167
+ // Poll for surviving processes and SIGKILL stragglers
168
+ await forceKillBrowserProcesses();
102
169
 
103
170
  // 3. Start browser with CDP enabled
104
171
  var cmdArgs = buildLaunchArgs();
@@ -116,6 +183,7 @@ async function launchBrowser() {
116
183
 
117
184
  async function closeBrowser() {
118
185
  try { execOhosAa(["force-stop", "--bundlename", BROWSER_PKG], 10000); } catch {}
186
+ await forceKillBrowserProcesses();
119
187
  }
120
188
 
121
189
  // ─── CDP endpoint discovery ──────────────────────────────────────────
@@ -18,7 +18,48 @@ const querystring = require("querystring");
18
18
  */
19
19
  function runInNode(opts) {
20
20
  return new Promise((resolve, reject) => {
21
+ // Keep a console restore callback so quiet mode never leaks into the host process
22
+ let restoreConsole = null;
21
23
  try {
24
+ // Set global.options for test files that depend on it (same as renderer.js),
25
+ // done first so any require below can already see the options
26
+ global.options = opts;
27
+ // Load mocha BEFORE overriding console: mocha's lib/reporters/base.js
28
+ // captures console references at module-load time, so requiring it
29
+ // after the quiet-mode noop overwrite would silence the spec
30
+ // reporter's "passing/failing" output as well
31
+ try {
32
+ require('mocha/mocha');
33
+ }
34
+ catch (e) {
35
+ // mocha browser bundle not available, that's fine for headless mode
36
+ }
37
+ const Mocha = require('mocha');
38
+ // Save the original console methods; when quiet is requested, replace the
39
+ // documented console[log/info/error/warn/dir] methods with no-ops
40
+ // (mirrors upstream renderer.js setupConsoleOutput behavior)
41
+ const originalConsole = {
42
+ log: console.log,
43
+ info: console.info,
44
+ error: console.error,
45
+ warn: console.warn,
46
+ dir: console.dir,
47
+ };
48
+ const noop = () => { };
49
+ if (opts.quiet) {
50
+ console.log = noop;
51
+ console.info = noop;
52
+ console.error = noop;
53
+ console.warn = noop;
54
+ console.dir = noop;
55
+ }
56
+ restoreConsole = () => {
57
+ console.log = originalConsole.log;
58
+ console.info = originalConsole.info;
59
+ console.error = originalConsole.error;
60
+ console.warn = originalConsole.warn;
61
+ console.dir = originalConsole.dir;
62
+ };
22
63
  // Set up jsdom environment for DOM-dependent tests
23
64
  // jsdom provides a real DOM implementation (not a stub)
24
65
  const { JSDOM } = require('jsdom');
@@ -37,15 +78,8 @@ function runInNode(opts) {
37
78
  catch (e) {
38
79
  // navigator is a getter in Node.js >= 21, skip it
39
80
  }
40
- // Load mocha browser support if available (sets up global mocha for bdd)
41
- try {
42
- require('mocha/mocha');
43
- }
44
- catch (e) {
45
- // mocha browser bundle not available, that's fine for headless mode
46
- }
47
81
  // In headless mode, use Node.js Mocha API (same as renderer.js does)
48
- const Mocha = require('mocha');
82
+ // Mocha already required above (before console override)
49
83
  // Parse reporter options (same logic as renderer.js)
50
84
  let reporterOptions;
51
85
  if (typeof opts.reporterOptions === 'string') {
@@ -97,10 +131,13 @@ function runInNode(opts) {
97
131
  }
98
132
  // Add resolved files to Mocha
99
133
  resolvedFiles.forEach((file) => mochaInst.addFile(file));
100
- // Set global.options for test files that depend on it (same as renderer.js)
101
- global.options = opts;
102
134
  // Run tests
103
135
  mochaInst.run((errorCount) => {
136
+ // Restore console methods so the host process is not polluted by quiet mode
137
+ if (restoreConsole) {
138
+ restoreConsole();
139
+ restoreConsole = null;
140
+ }
104
141
  if (errorCount > 0) {
105
142
  // No error needed, Mocha will report this (same as renderer.js)
106
143
  reject(new Error('Mocha tests failed.'));
@@ -111,6 +148,11 @@ function runInNode(opts) {
111
148
  });
112
149
  }
113
150
  catch (e) {
151
+ // Restore console methods on failure as well
152
+ if (restoreConsole) {
153
+ restoreConsole();
154
+ restoreConsole = null;
155
+ }
114
156
  reject(new Error(`Node.js test runner error: ${e.message}`));
115
157
  }
116
158
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohos-ports/floss",
3
- "version": "5.0.1-beta.6",
3
+ "version": "5.0.1-beta.8",
4
4
  "description": "Unit-testing for those hard to reach places",
5
5
  "bin": "./lib/floss.js",
6
6
  "main": "./lib/index.js",