Files
OpenMaopaoya/frontend-uni/node_modules/@dcloudio/uni-automator/dist/runner.js
T

2 lines
7.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";var e=require("path"),t=require("licia/dateFormat"),s=require("licia/waitUntil");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=r(e),o=r(t),n=r(s);const a="__UNI_AUTOMATOR_SOCKET_CONNECTION_CLOSED__",l="__UNI_AUTOMATOR_SOCKET_RETRY_STATE__";function c(){return"true"===process.env.UNI_AUTOMATOR_RETRY_ON_SOCKET_CLOSE}const u=function(){var e;const t=[process.argv[1]?i.default.dirname(process.argv[1]):"",(null===(e=require.main)||void 0===e?void 0:e.filename)?i.default.dirname(require.main.filename):"",process.cwd(),__dirname].filter(((e,t,s)=>e&&s.indexOf(e)===t));try{const e=require.resolve("jest/package.json",{paths:t}),s=require.resolve("@jest/core/package.json",{paths:[i.default.dirname(e)]});return require.resolve("jest-runner",{paths:[i.default.dirname(s)]})}catch(e){}return require.resolve("jest-runner",{paths:t})}(),d=i.default.join(i.default.dirname(u),"runTest.js"),h=require(d).default,g="Connection closed";module.exports=class{constructor(e,t){this.supportsEventEmitters=!1,this.__PRIVATE_UNSTABLE_API_supportsEventEmitters__=!1,this.isSerial=!0,this._globalConfig=e,this._context=t||{}}async runTests(e,t,s,r,i){process.env.JEST_WORKER_ID=process.env.JEST_WORKER_ID||"1";const o=c();o&&console.warn(`[uni-automator] socket retry runner enabled, test files: ${e.length}.`);for(const n of e){if(t.isInterrupted())return;await s(n);try{o&&await this.recoverSocketConnectionIfBroken();const e=await this.runTestFile(n);if(!this.shouldRetry(e)){await r(n,e);continue}this.recordSocketRetry(n.path,"retry",this.getSocketRetryInfo(e)),await this.retryTestFile(n,r,i)}catch(e){if(!o||!this.isSocketClosedError(e)){await i(n,e);continue}this.recordSocketRetry(n.path,"retry",this.getSocketRetryInfo(void 0,e)),await this.retryTestFile(n,r,i)}}}runTestFile(e){const t=()=>h(e.path,this._globalConfig,e.context.config,e.context.resolver,this._context,(()=>{}));return c()?this.captureSocketClosedOutput(t):t()}async retryTestFile(e,t,s){try{await this.recoverSocketConnection();const s=await this.runTestFile(e);await this.handleFailedAfterRetryResult(e.path,s),await t(e,s)}catch(t){await this.handleFailedAfterRetryError(e.path,t),await s(e,t)}}async captureSocketClosedOutput(e){const t=global,s=process.stdout.write,r=process.stderr.write,i=e=>{this.isSocketClosedMessage(String(e))&&(t[a]=!0)},o=e=>{const t=e[0];if(i(t),!this.isPlainSocketClosedOutput(t))return!1;const s=e.find((e=>"function"==typeof e));return s&&process.nextTick(s),!0};process.stdout.write=function(...e){return!!o(e)||s.apply(this,e)},process.stderr.write=function(...e){return!!o(e)||r.apply(this,e)};try{return await e()}finally{process.stdout.write=s,process.stderr.write=r}}shouldRetry(e){if(!c())return!1;return((null==e?void 0:e.numFailingTests)>0||(null==e?void 0:e.testExecError)||(null==e?void 0:e.failureMessage)||this.hasSocketClosed())&&(this.isSocketClosedMessage(this.getFailureMessage(e))||this.isRuntimeConnectionBroken()||this.hasSocketClosed())}getFailureMessage(e){var t,s;const r=Array.isArray(null==e?void 0:e.console)?e.console.map((e=>null==e?void 0:e.message)):[];return[null==e?void 0:e.failureMessage,null===(t=null==e?void 0:e.testExecError)||void 0===t?void 0:t.message,null===(s=null==e?void 0:e.testExecError)||void 0===s?void 0:s.stack,...r,...((null==e?void 0:e.testResults)||[]).flatMap((e=>[...(null==e?void 0:e.failureMessages)||[],...(null==e?void 0:e.errors)||[]]))].filter(Boolean).join("\n")}getSocketRetryInfo(e,t){const s=e?this.getFailureMessage(e):this.getErrorMessage(t),r=e?this.findSocketClosedAssertionResult(e):void 0;return{message:this.getSocketRetryMessage(s),caseTitle:this.getCaseTitle(r)||this.getCaseTitleFromMessage(s)}}async handleFailedAfterRetryResult(e,t){if(!this.shouldRetry(t))return;const s=this.getSocketRetryInfo(t);this.recordSocketRetry(e,"failedAfterRetry",s),this.normalizeSocketClosedFailureForReport(t,s),this.clearSocketClosedState()}async handleFailedAfterRetryError(e,t){this.isSocketClosedError(t)&&(this.recordSocketRetry(e,"failedAfterRetry",this.getSocketRetryInfo(void 0,t)),this.clearSocketClosedState())}normalizeSocketClosedFailureForReport(e,t){if(!e||!this.isSocketClosedMessage(this.getFailureMessage(e)))return;const s=this.getFailedAssertionResults(e);if(!s.length)return;s.some((e=>{const t=this.getAssertionFailureMessage(e);return!t||!this.isSocketClosedMessage(t)}))||(e.failureMessage=this.formatSocketClosedFailureMessage(t.caseTitle))}getFailedAssertionResults(e){return(Array.isArray(null==e?void 0:e.testResults)?e.testResults:[]).filter((e=>"failed"===(null==e?void 0:e.status)||this.getAssertionFailureMessage(e)))}formatSocketClosedFailureMessage(e){return`${e?` ● ${e}`:" ● Connection closed"}\n\n ${g}\n`}getSocketRetryMessage(e){return this.isSocketClosedMessage(e)||this.isRuntimeConnectionBroken()||this.hasSocketClosed()?g:this.normalizeLogText(e)||g}findSocketClosedAssertionResult(e){const t=Array.isArray(null==e?void 0:e.testResults)?e.testResults:[];return t.find((e=>this.isSocketClosedMessage(this.getAssertionFailureMessage(e))))||t.find((e=>"failed"===(null==e?void 0:e.status)))}getAssertionFailureMessage(e){return[...(null==e?void 0:e.failureMessages)||[],...(null==e?void 0:e.errors)||[]].filter(Boolean).join("\n")}getCaseTitle(e){if(!e)return"";const t=[...Array.isArray(e.ancestorTitles)?e.ancestorTitles:[],e.title].filter(Boolean);return t.length?t.map((e=>this.normalizeLogText(e))).join(" › "):this.normalizeLogText(e.fullName)}getCaseTitleFromMessage(e){const t=String(e||"").match(/^\s*●\s+(.+?)\s*$/m);return t?this.normalizeLogText(t[1]):""}isSocketClosedError(e){return this.isSocketClosedMessage(this.getErrorMessage(e))||this.isRuntimeConnectionBroken()||this.hasSocketClosed()}isSocketClosedMessage(e){return e.includes(g)}isPlainSocketClosedOutput(e){const t=this.stripAnsi(String(e||"")).split(/\r?\n/).map((e=>e.trim())).filter(Boolean);return t.length>0&&t.every((e=>e===g))}getErrorMessage(e){const t=[null==e?void 0:e.message,null==e?void 0:e.stack].filter(Boolean);return t.length?t.join("\n"):null==e?"":String(e)}normalizeLogText(e){return this.stripAnsi(String(e||"")).replace(/\s+/g," ").trim()}stripAnsi(e){return e.replace(/\u001b\[[0-9;]*m/g,"")}isRuntimeConnectionBroken(){var e,t;return!0===(null===(t=null===(e=this.getRuntimeConnection())||void 0===e?void 0:e.isBroken)||void 0===t?void 0:t.call(e))}getRuntimeConnection(){var e,t;return null===(t=null===(e=global.program)||void 0===e?void 0:e.puppet)||void 0===t?void 0:t.runtimeConnection}hasSocketClosed(){return!!global[a]}clearSocketClosedState(){global[a]=!1}getRetryState(){const e=global;return e[l]||(e[l]={retried:[],failedAfterRetry:[]}),e[l]}recordSocketRetry(e,t,s){const r={testFilePath:e,testFileName:i.default.basename(e),status:t,message:s.message,caseTitle:s.caseTitle,timeStamp:Date.now(),time:o.default("yyyy-mm-dd HH:MM:ss")},n=this.getRetryState();n.latest=r,"retry"===t?n.retried.push(r):n.failedAfterRetry.push(r),console.warn(`[uni-automator] record socket closed, file: ${r.testFileName}, status: ${r.status}. message: ${r.message}, caseTitle: ${r.caseTitle}, timeStamp: ${r.timeStamp}, time: ${r.time}.`)}async recoverSocketConnection(){const e=global,t=e.program;if(!t)return;const s=this.getRuntimeConnection();e.socketConnected=!1,e[a]=!1,await t.start("jest"),await n.default((()=>{var t;const r=this.getRuntimeConnection();return r&&r!==s&&!1===(null===(t=r.isBroken)||void 0===t?void 0:t.call(r))&&e.socketConnected}),1e4,100).catch((()=>{throw new Error("Socket reconnect failed")}))}async recoverSocketConnectionIfBroken(){(this.isRuntimeConnectionBroken()||this.hasSocketClosed())&&await this.recoverSocketConnection()}};