diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index b6503e25cdef57d9208a0df103fcff127b02b936..1f64b1e2962113888ba42f1d974634be3cc85ded 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -115,6 +115,7 @@ private: ErrCode RunAsSetAppDistributionTypes(); ErrCode RunAsGetBundleNamesForUidExtCommand(); ErrCode RunAsGetAppIdentifierAndAppIndex(); + ErrCode RunAsGetTestRunner(); std::condition_variable cv_; std::mutex mutex_; diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 02e8edd551f757033f8293444a3a0dc438806a56..d3585062b438a0a64840c8287e6f33bc8d7e8a6d 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -201,6 +201,7 @@ static const std::string HELP_MSG = " getAssetAccessGroups get asset access groups by bundlename\n" " getAppIdentifierAndAppIndex get appIdentifier and appIndex\n" " setAppDistributionTypes set white list of appDistributionType\n"; + " getTestRunner get test runner\n"; const std::string HELP_MSG_GET_REMOVABLE = @@ -677,6 +678,18 @@ const std::string HELP_MSG_SET_APP_DISTRIBUTION_TYPES = " -h, --help list available commands\n" " -a, --app_distribution_types specify app distribution type list\n"; +const std::string HELP_MSG_GET_TESTRUNNER = + "usage: bundle_test_tool getTestRunner \n" + "eg:bundle_test_tool getTestRunner -n -m \n" + "options list:\n" + " -h, --help list available commands\n" + " -n, --bundle-name get testRunner by bundleName and moduleName\n" + " -m, --module-name get testRunner by bundleName and moduleName\n"; + +const std::string HELP_MSG_NO_GET_TEST_RUNNER_OPTION = + "error: you must specify a bundle name with '-n' or '--bundle-name' \n" + "and a module name with '-m' or '--module-name' \n"; + const std::string STRING_IS_BUNDLE_INSTALLED_OK = "IsBundleInstalled is ok \n"; const std::string STRING_IS_BUNDLE_INSTALLED_NG = "error: failed to IsBundleInstalled \n"; @@ -794,6 +807,7 @@ const std::string STRING_GET_APPIDENTIFIER_AND_APPINDEX_NG = "getAppIdentifierAn const std::string STRING_SET_APP_DISTRIBUTION_TYPES_OK = "setAppDistributionTypes successfully\n"; const std::string STRING_SET_APP_DISTRIBUTION_TYPES_NG = "setAppDistributionTypes failed\n"; +const std::string STRING_GET_TEST_RUNNER_NG = "getTestRunner failed\n"; const std::string GET_BUNDLE_STATS_ARRAY[] = { "app data size: ", @@ -1115,6 +1129,14 @@ const struct option LONG_OPTIONS_GET_APPIDENTIFIER_AND_APPINDEX[] = { {nullptr, 0, nullptr, 0}, }; +const std::string SHORT_OPTIONS_GET_TEST_RUNNER = "hn:m:"; +const struct option LONG_OPTIONS_GET_TEST_RUNNER[] = { + {"help", no_argument, nullptr, 'h'}, + {"bundle-name", required_argument, nullptr, 'n'}, + {"module-name", required_argument, nullptr, 'm'}, + {nullptr, 0, nullptr, 0}, +}; + } // namespace class ProcessCacheCallbackImpl : public ProcessCacheCallbackHost { @@ -1283,7 +1305,8 @@ ErrCode BundleTestTool::CreateCommandMap() std::bind(&BundleTestTool::RunAsGetAppIdentifierAndAppIndex, this)}, {"setAppDistributionTypes", std::bind(&BundleTestTool::RunAsSetAppDistributionTypes, this)}, - {"getBundleNamesForUidExt", std::bind(&BundleTestTool::RunAsGetBundleNamesForUidExtCommand, this)} + {"getBundleNamesForUidExt", std::bind(&BundleTestTool::RunAsGetBundleNamesForUidExtCommand, this)}, + {"getTestRunner", std::bind(&BundleTestTool::RunAsGetTestRunner, this)} }; return OHOS::ERR_OK; @@ -5916,5 +5939,53 @@ ErrCode BundleTestTool::RunAsGetBundleNamesForUidExtCommand() APP_LOGI("RunAsGetBundleNameForUidExtCommand end"); return result; } + +ErrCode BundleTestTool::RunAsGetTestRunner() +{ + APP_LOGI("RunAsGetTestRunner start"); + int result = OHOS::ERR_OK; + int counter = 0; + std::string name = ""; + std::string bundleName = ""; + std::string moduleName = ""; + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET_TEST_RUNNER.c_str(), + LONG_OPTIONS_GET_TEST_RUNNER, nullptr); + APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind); + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + if (option == -1) { + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + resultReceiver_.append(HELP_MSG_NO_GET_TEST_RUNNER_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + int temp = 0; + result = !CheckGetStringCorrectOption(option, commandName, temp, name) + ? OHOS::ERR_INVALID_VALUE : result; + bundleName = option == 'n' ? name : bundleName; + moduleName = option == 'm' ? name : appIndex; + } + + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_NO_GET_TEST_RUNNER_OPTION); + } else { + ArkTsTestRunner testRunner; + auto res = bundleMgrProxy_->GetTestRunner(bundleName, moduleName, testRunner); + if (res != OHOS::ERR_OK) { + resultReceiver_.append(STRING_GET_TEST_RUNNER_NG); + return result; + } + nlohmann::json jsonObject = testRunner; + std::string results = jsonObject.dump(Constants::DUMP_INDENT); + resultReceiver_.append(results); + resultReceiver_.append("\n"); + } + APP_LOGI("RunAsGetTestRunner end"); + return result; +} } // AppExecFwk } // OHOS \ No newline at end of file