diff --git a/src/HBuilderXCli.js b/src/HBuilderXCli.js index 0f3bf91..4f5f47e 100644 --- a/src/HBuilderXCli.js +++ b/src/HBuilderXCli.js @@ -90,7 +90,7 @@ class Common { */ async checkAndSetEnv(platform = undefined, projectPath) { let testEnv = true; - if (nodeStatus == undefined || nodeStatus == 'N') { + if (nodeStatus == undefined || nodeStatus === 'N') { try { nodeStatus = await checkNode(); } catch (error) { @@ -145,7 +145,7 @@ class Common { // 判断是否安装测试环境必须的插件 let uni_plugin_check = true; for (let e of Object.keys(plugin_list)) { - if (fs.existsSync(plugin_list[e]) == false) { + if (fs.existsSync(plugin_list[e]) === false) { uni_plugin_check = false; const pluginDisplayName = config.HX_PLUGINS_DISPLAYNAME_LIST[e] ? config.HX_PLUGINS_DISPLAYNAME_LIST[e] : e; const log_for_plugin = `[提示]:测试环境检查,未安装 ${e} 。点击菜单【工具 - 插件安装】,安装【${pluginDisplayName}】插件。`; @@ -169,12 +169,12 @@ class Common { */ async checkAndSetUTSTestEnv() { let path_gradleHome = await getPluginConfig('uts-development-android.gradleHome'); - if (path_gradleHome != undefined && path_gradleHome.trim() != "" && fs.existsSync(path_gradleHome)) { + if (path_gradleHome != undefined && path_gradleHome.trim() !== "" && fs.existsSync(path_gradleHome)) { config.UTS_GRADLE_HOME = path_gradleHome; }; let path_Android_sdkDir = await getPluginConfig('uts-development-android.sdkDir'); - if (path_Android_sdkDir != undefined && path_Android_sdkDir.trim() != "" && fs.existsSync(path_Android_sdkDir)) { + if (path_Android_sdkDir != undefined && path_Android_sdkDir.trim() !== "" && fs.existsSync(path_Android_sdkDir)) { config.UTS_JDK_PATH = path_Android_sdkDir; }; }; diff --git a/src/Initialize.js b/src/Initialize.js index 01d9abb..ad820d9 100644 --- a/src/Initialize.js +++ b/src/Initialize.js @@ -69,7 +69,7 @@ class Initialize extends Common { let isPrintLog = is_hbuidlerx_cli ? false : true; // 检查jest.config.js是否存在,如不存在,则创建 - if (file_type == 'all' || file_type == 'jest.config.js') { + if (file_type === 'all' || file_type === 'jest.config.js') { if (!fs.existsSync(jestConfigFile)){ let jest_template_path = path.join(path.resolve(__dirname), 'template', 'jest.config.js'); await this.createFile("jest.config.js", jest_template_path, jestConfigFile, isPrintLog); @@ -80,7 +80,7 @@ class Initialize extends Common { // 检查env.js是否存在,如不存在,则创建 - if (file_type == 'all' || file_type == 'env.js') { + if (file_type === 'all' || file_type === 'env.js') { if (!fs.existsSync(jestEnvFile)){ let env_template_path = path.join(path.resolve(__dirname), 'template', 'env.js'); await this.createFile('env.js', env_template_path, jestEnvFile, isPrintLog); @@ -97,20 +97,20 @@ class Initialize extends Common { * @param {String} cmd */ async installTestLibs(runDir, actions, cmd) { - if (cmd == undefined) { + if (cmd === undefined) { cmd = `"${HBuilderX_NPM_PATH}" install --save --registry=https://registry.npmmirror.com` }; let { action, source_file, target_file} = actions; let Notes = `\n\n安装方式:\n1. 命令行进入 ${runDir}目录 \n2. 输入 ${cmd}` - let prompt = action == 'upgrade' + let prompt = action === 'upgrade' ? `自动化测试环境,依赖的jest、adbkit、puppeteer等库有更新,请选择是否更新? \n\n强烈建议您选择更新。更新升级命令,请参考控制台输出。` : `自动化测试环境,需要安装jest、adbkit、puppeteer等库,是否安装?安装环境之后,才可以正常使用此插件。 ${Notes}`; - let title = action == 'upgrade' ? '更新uni-app自动化测试依赖' : '安装uni-app自动化测试依赖'; + let title = action === 'upgrade' ? '更新uni-app自动化测试依赖' : '安装uni-app自动化测试依赖'; let btn = await hxShowMessageBox(title, prompt, ['去升级', '忽略升级']).then( btn => { return btn; }); - if (['好的', '去升级'].includes(btn) && action == 'upgrade') { + if (['好的', '去升级'].includes(btn) && action === 'upgrade') { await this.createFile("package.json", source_file, target_file); const cmd_npm_install = `npm install --save --registry=https://registry.npmmirror.com`; @@ -191,7 +191,7 @@ class Initialize extends Common { 'target_file': lib_package_path, }; - if (lib_version != template_version && terminal_id == "") { + if (lib_version !== template_version && terminal_id === "") { if (current_ignore_upgrade) return true; actions['action'] = 'upgrade'; const _i_result = await this.installTestLibs(test_lib_dir, actions); @@ -201,7 +201,7 @@ class Initialize extends Common { }; return false; }; - if (lib_version != template_version && terminal_id != "") { + if (lib_version !== template_version && terminal_id !== "") { await logger(`[uniapp.test] 建议:uni-app自动化测试插件,检测到依赖库有更新,请在菜单【运行 - uni-app自动化测试辅助插件 - 重装测试环境依赖】中,重新安装依赖。`, 'warning'); }; @@ -265,7 +265,7 @@ class Initialize extends Common { if (msg) { await logger(`uniapp-cli项目,${projectPath} 自动化测试运行缺少必要的依赖 ${msg},需要安装相关依赖。`, 'warning'); await logger(`如果自动安装失败,打开终端,进入 ${projectPath} 目录,运行命令: npm install --save ${msg}`, 'info'); - if (terminal_id == "") { + if (terminal_id === "") { this.installTestLibs(projectPath, {}, `npm install --save ${msg}`); }; return false; diff --git a/src/TestCaseRun.js b/src/TestCaseRun.js index 9a3c3f4..78f535d 100644 --- a/src/TestCaseRun.js +++ b/src/TestCaseRun.js @@ -92,7 +92,7 @@ class Common { */ async checkAndSetEnv(platform = undefined, projectPath) { let testEnv = true; - if (nodeStatus == undefined || nodeStatus == 'N') { + if (nodeStatus == undefined || nodeStatus === 'N') { nodeStatus = await checkNode().catch(error => { createOutputChannel(config.i18n.msg_env_node_check, "error"); return error; @@ -146,7 +146,7 @@ class Common { let uni_plugin_check = true; for (let e of Object.keys(plugin_list)) { // console.error("=============", plugin_list[e], fs.existsSync(plugin_list[e])); - if (fs.existsSync(plugin_list[e]) == false) { + if (fs.existsSync(plugin_list[e]) === false) { uni_plugin_check = false; const pluginDisplayName = config.HX_PLUGINS_DISPLAYNAME_LIST[e] ? config.HX_PLUGINS_DISPLAYNAME_LIST[e] : e; const log_for_plugin = `[提示]:测试环境检查,未安装 ${e} 。点击菜单【工具 - 插件安装】,安装【${pluginDisplayName}】插件。`; @@ -154,10 +154,10 @@ class Common { }; }; - if (uni_plugin_check == false && is_uniapp_x) { + if (uni_plugin_check === false && is_uniapp_x) { createOutputChannel(config.i18n.msg_warning_uniappx_env, 'info'); }; - if (uni_plugin_check == false && is_uts_project) { + if (uni_plugin_check === false && is_uts_project) { createOutputChannel(config.i18n.msg_warning_uts_env, 'info'); }; return testEnv; @@ -168,12 +168,12 @@ class Common { */ async checkAndSetUTSTestEnv() { let path_gradleHome = await getPluginConfig('uts-development-android.gradleHome'); - if (path_gradleHome != undefined && path_gradleHome.trim() != "" && fs.existsSync(path_gradleHome)) { + if (path_gradleHome != undefined && path_gradleHome.trim() !== "" && fs.existsSync(path_gradleHome)) { config.UTS_GRADLE_HOME = path_gradleHome; }; let path_Android_sdkDir = await getPluginConfig('uts-development-android.sdkDir'); - if (path_Android_sdkDir != undefined && path_Android_sdkDir.trim() != "" && fs.existsSync(path_Android_sdkDir)) { + if (path_Android_sdkDir != undefined && path_Android_sdkDir.trim() !== "" && fs.existsSync(path_Android_sdkDir)) { config.UTS_JDK_PATH = path_Android_sdkDir; }; }; diff --git a/src/core/core.js b/src/core/core.js index 9d4a029..b8539ca 100644 --- a/src/core/core.js +++ b/src/core/core.js @@ -21,7 +21,7 @@ async function isUniAppCli(projectPath) { let t2 = fs.existsSync(path.join(projectPath, "src/pages")); let t3 = fs.existsSync(path.join(projectPath, "src/manifest.json")); let t4 = fs.existsSync(path.join(projectPath, "package.json")); - return t1 && t2 && t3 && t4 ? true : false; + return t1 && t2 && t3 && t4; }; /** @@ -39,7 +39,7 @@ async function isUniAppX(projectPath) { let result = await hx.util.readJSONValue(manifestPath, "uni-app-x").then((data) => { return data; }); - return result["data"] != null && is_main_ts ? true : false; + return result["data"] != null && is_main_ts; } catch (error) { return false; }; @@ -58,7 +58,7 @@ async function isUniAppX(projectPath) { function hxShowMessageBox(title, text, buttons = ['关闭']) { return new Promise((resolve, reject) => { if ( buttons.length > 1 && (buttons.includes('关闭') || buttons.includes('取消')) ) { - if (osName == 'darwin') { + if (osName === 'darwin') { buttons = buttons.reverse(); }; }; @@ -82,7 +82,7 @@ function hxShowMessageBox(title, text, buttons = ['关闭']) { * @param {version} String 插件版本 */ function installPlugin() { - let terminalName = osName == "win32" ? "builtincef3terminal" : "builtinterminal"; + let terminalName = osName === "win32" ? "builtincef3terminal" : "builtinterminal"; return new Promise((resolve, reject) => { let info = { "id": terminalName, @@ -129,8 +129,8 @@ async function getPluginConfig(options) { */ const uniMap = new Map(); function createOutputChannel(msg, msgLevel = 'info', viewID = undefined) { - let oID = viewID == undefined || viewID == '' ? 'hbuilderx.uniapp.test' : 'hbuilderx.uniapp.test' + `.${viewID}`; - let title = viewID != 'log' ? "uni-app自动化测试" : "uni-app自动化测试 - 运行日志"; + let oID = viewID == undefined || viewID === '' ? 'hbuilderx.uniapp.test' : 'hbuilderx.uniapp.test' + `.${viewID}`; + let title = viewID !== 'log' ? "uni-app自动化测试" : "uni-app自动化测试 - 运行日志"; let output = uniMap.get(oID); // console.error("..........output", output?.timer?._destroyed); // if(output?.timer?._destroyed === undefined || output?.timer?._destroyed === false) { @@ -235,10 +235,10 @@ function printTestRunLog(MessagePrefix, msg) { if ((msg.includes("Module Error") && msg.includes("Errors compiling")) || msg.includes("语法错误")) { lastMsg = lastMsg + "\n"; msgLevel = "error"; - } else if (theFour == "FAIL" || msg.includes('TypeError') || (msg.includes('Expected') && msg.includes('Received')) || msg.includes('Test suite failed to run')) { + } else if (theFour === "FAIL" || msg.includes('TypeError') || (msg.includes('Expected') && msg.includes('Received')) || msg.includes('Test suite failed to run')) { lastMsg = lastMsg + "\n"; msgLevel = "error"; - } else if (theFour == "PASS") { + } else if (theFour === "PASS") { lastMsg = lastMsg + "\n"; msgLevel = "success"; } else if (msg.includes('Ran all test suites.') && msg.includes("Tests") && msg.includes("total")) { @@ -256,10 +256,10 @@ async function printTestRunLogForHBuilderXCli(MessagePrefix, msg, logger) { if ((msg.includes("Module Error") && msg.includes("Errors compiling")) || msg.includes("语法错误")) { lastMsg = lastMsg + "\n"; msgLevel = "error"; - } else if (theFour == "FAIL" || msg.includes('TypeError') || (msg.includes('Expected') && msg.includes('Received')) || msg.includes('Test suite failed to run')) { + } else if (theFour === "FAIL" || msg.includes('TypeError') || (msg.includes('Expected') && msg.includes('Received')) || msg.includes('Test suite failed to run')) { lastMsg = lastMsg + "\n"; msgLevel = "error"; - } else if (theFour == "PASS") { + } else if (theFour === "PASS") { lastMsg = lastMsg + "\n"; msgLevel = "success"; } else if (msg.includes('Ran all test suites.') && msg.includes("Tests") && msg.includes("total")) { diff --git a/src/core/edit_env_js_file.js b/src/core/edit_env_js_file.js index a1f1c5e..416e1dd 100644 --- a/src/core/edit_env_js_file.js +++ b/src/core/edit_env_js_file.js @@ -69,7 +69,7 @@ async function editEnvjsFile(env_js_path="", testPlatform="", deviceId="", uniPr launcherExecutablePath = envjs['app-plus'][testPlatform]['executablePath']; }; } catch (error) {}; - if (launcherExecutablePath == undefined || launcherExecutablePath == "") { + if (launcherExecutablePath == undefined || launcherExecutablePath === "") { launcherExecutablePath = await getPluginConfig("harmony.devTools.path"); }; }; @@ -80,13 +80,13 @@ async function editEnvjsFile(env_js_path="", testPlatform="", deviceId="", uniPr if (is_uniapp_x) { let oldLauncherVersion = envjs["app-plus"]?.["uni-app-x"]?.version; - if (oldLauncherVersion == undefined || oldLauncherVersion.trim() == '' || oldLauncherVersion != config.UNIAPP_X_LAUNCHER_VERSION_TXT) { + if (!oldLauncherVersion || oldLauncherVersion.trim() === '' || oldLauncherVersion !== config.UNIAPP_X_LAUNCHER_VERSION_TXT) { envjs['app-plus']['uni-app-x'] = {}; envjs['app-plus']['uni-app-x']['version'] = config.UNIAPP_X_LAUNCHER_VERSION_TXT; }; } else { let oldLauncherVersion = envjs['app-plus']['version']; - if (oldLauncherVersion.trim() == '' || oldLauncherVersion == undefined || oldLauncherVersion != config.LAUNCHER_VERSION_TXT) { + if (!oldLauncherVersion || oldLauncherVersion.trim() === '' || oldLauncherVersion !== config.LAUNCHER_VERSION_TXT) { envjs['app-plus']['version'] = config.LAUNCHER_VERSION_TXT; }; }; @@ -95,28 +95,28 @@ async function editEnvjsFile(env_js_path="", testPlatform="", deviceId="", uniPr envjs['app-plus']['uni-app-x']?.[testPlatform] : envjs['app-plus'][testPlatform]; - if (oldPhoneData == undefined || typeof oldPhoneData != 'object') { + if (oldPhoneData == undefined || typeof oldPhoneData !== 'object') { oldPhoneData = {}; }; let {id,executablePath} = oldPhoneData; // console.log("=======", oldPhoneData, launcherExecutablePath); - if (id != deviceId || executablePath != launcherExecutablePath) { + if (id !== deviceId || executablePath !== launcherExecutablePath) { if (is_uniapp_x) { - if (envjs['app-plus']['uni-app-x'][testPlatform] == undefined) { + if (envjs['app-plus']['uni-app-x'][testPlatform] === undefined) { envjs['app-plus']['uni-app-x'][testPlatform] = {}; }; envjs['app-plus']['uni-app-x'][testPlatform]["id"] = deviceId; - if (isCustomRuntime == false || isCustomRuntime == undefined) { + if (isCustomRuntime === false || isCustomRuntime === undefined) { envjs['app-plus']['uni-app-x'][testPlatform]["executablePath"] = launcherExecutablePath; }; } else { - if (envjs['app-plus'][testPlatform] == undefined) { + if (envjs['app-plus'][testPlatform] === undefined) { envjs['app-plus'][testPlatform] = {}; }; envjs['app-plus'][testPlatform]['id'] = deviceId; - if (isCustomRuntime == false || isCustomRuntime == undefined) { + if (isCustomRuntime === false || isCustomRuntime === undefined) { envjs['app-plus'][testPlatform]['executablePath'] = launcherExecutablePath; }; }; @@ -125,7 +125,7 @@ async function editEnvjsFile(env_js_path="", testPlatform="", deviceId="", uniPr let lastContent = `module.exports = ${tmp_data}`; let writeResult = await fsWriteFile(env_js_path, lastContent); - if (writeResult != 'success') { + if (writeResult !== 'success') { await logger(`将测试设备( $deviceInfo )信息写入 ${envjs} 文件时失败,终止后续操作。`, 'warning'); return false; }; diff --git a/src/core/edit_jest_config_js_file.js b/src/core/edit_jest_config_js_file.js index 841f101..72b63a2 100644 --- a/src/core/edit_jest_config_js_file.js +++ b/src/core/edit_jest_config_js_file.js @@ -42,7 +42,7 @@ async function modifyJestConfigJSFile(scope="", proj={}, client_id) { // 插件配置项:是否自动修改jest.config.js文件中的testMatch let userConfig = await getPluginConfig('hbuilderx-for-uniapp-test.AutomaticModificationTestMatch'); // console.error("=========>", userConfig); - if (userConfig == false) { + if (userConfig === false) { await logger(`[uniapp.test] 您已关闭自动修改 jest.config.js 配置文件中的 testMatch 字段,跳过此操作。`); return true; }; @@ -55,13 +55,13 @@ async function modifyJestConfigJSFile(scope="", proj={}, client_id) { : "/pages/**/*test.[jt]s?(x)"; // one:代指仅测试单条用例 - if (scope == 'one') { + if (scope === 'one') { let testcase_file_relative_path = selectedFile.slice(projectPath.length + 1); projectTestMatch = `/${testcase_file_relative_path}`; // 用于特定测试项目:dcloud-uts和uni-api let test_js_path = path.basename(selectedFile) + ".test.js"; - if (testcase_file_relative_path.substr(0,19) == "pages/autotest/uni-" && fs.existsSync(path.join(selectedFile, test_js_path))) { + if (testcase_file_relative_path.substr(0,19) === "pages/autotest/uni-" && fs.existsSync(path.join(selectedFile, test_js_path))) { projectTestMatch = `/${testcase_file_relative_path}/${test_js_path}`; }; @@ -82,7 +82,7 @@ async function modifyJestConfigJSFile(scope="", proj={}, client_id) { }; try { - if (oldTestMatch[0] == projectTestMatch) { + if (oldTestMatch[0] === projectTestMatch) { return true; }; let jestFileContent = fs.readFileSync(jest_config_js_path, 'utf-8'); @@ -92,7 +92,7 @@ async function modifyJestConfigJSFile(scope="", proj={}, client_id) { let lastContent = jestFileContent.replace(/testMatch\s*:{1}\s*\[\S*\]/gi, replaceText); let writeResult = await fsWriteFile(jest_config_js_path, lastContent); - if (writeResult != 'success') { + if (writeResult !== 'success') { await logger(`${jest_config_js_path} 修改测试配置文件失败,终止后续操作,请检查此文件。`, 'warning'); return false; }; diff --git a/src/utils/check_network.js b/src/utils/check_network.js index 133a558..604b857 100644 --- a/src/utils/check_network.js +++ b/src/utils/check_network.js @@ -35,7 +35,7 @@ async function checkPhoneDeviceNetworkStatus(platform, deviceId) { deviceWlanData = await execCommand(`hdc -t ${deviceId} shell ifconfig wlan0`); }; - if (deviceWlanData != "") { + if (deviceWlanData !== "") { const isCheckInetAddr = deviceWlanData.includes("inet addr:"); if (!isCheckInetAddr) { isConnected = false; diff --git a/src/utils/utils_files.js b/src/utils/utils_files.js index dbbbdd7..6381d13 100644 --- a/src/utils/utils_files.js +++ b/src/utils/utils_files.js @@ -44,7 +44,7 @@ async function fsWriteFile(fpath, filecontent) { return new Promise(function(resolve, reject) { fs.writeFile(fpath, filecontent, function (err) { if (err) { - console.eror(err); + console.error(err); reject(err); }; resolve('success'); diff --git a/src/utils/utils_public.js b/src/utils/utils_public.js index bb86843..3d9afc6 100644 --- a/src/utils/utils_public.js +++ b/src/utils/utils_public.js @@ -17,9 +17,10 @@ function checkNode() { }, function(error, stdout, stderr) { if (error) { reject("N"); + return; }; try { - let version = stdout.match(/(\d{1,3}.\d{1,3}.\d{1,3})/g)[0]; + let version = stdout.match(/(\d{1,3}\.\d{1,3}\.\d{1,3})/g)[0]; resolve('Y'); } catch (e) { reject('N');