Skip to content

Commit 8ad1a29

Browse files
Keep policy errors parseable in list JSON
1 parent 0eb19f8 commit 8ad1a29

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/AppInstallerCLICore/Workflows/WorkflowBase.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
2626

2727
using namespace std::string_literals;
28+
using namespace std::string_view_literals;
2829
using namespace AppInstaller::Utility::literals;
2930
using namespace AppInstaller::Pinning;
3031
using namespace AppInstaller::Repository;
@@ -813,7 +814,16 @@ namespace AppInstaller::CLI::Workflow
813814
{
814815
auto policy = Settings::TogglePolicy::GetPolicy(e.Policy());
815816
auto policyNameId = policy.PolicyName();
816-
context->Reporter.Error() << Resource::String::DisabledByGroupPolicy(policyNameId) << std::endl;
817+
auto message = Resource::String::DisabledByGroupPolicy(policyNameId);
818+
819+
if (IsJsonOutputFormat(context->Args))
820+
{
821+
OutputInstalledPackagesJsonError(*context, APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY, message);
822+
}
823+
else
824+
{
825+
context->Reporter.Error() << message << std::endl;
826+
}
817827
}
818828
return APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY;
819829
}

src/AppInstallerCLITests/UpdateFlow.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ TEST_CASE("ListFlow_JsonOutputWithSettingsWarnings", "[ListFlow][workflow]")
377377
REQUIRE(json["packages"].size() == 1);
378378
}
379379

380+
TEST_CASE("ListFlow_JsonOutputWinGetPolicyDisabled", "[ListFlow][workflow]")
381+
{
382+
GroupPolicyTestOverride policies;
383+
policies.SetState(TogglePolicy::Policy::WinGet, PolicyState::Disabled);
384+
385+
std::ostringstream listOutput;
386+
TestContext context{ listOutput, std::cin };
387+
auto previousThreadGlobals = context.SetForCurrentThread();
388+
context.Args.AddArg(Execution::Args::Type::OutputFormat, "json"sv);
389+
390+
ListCommand list({});
391+
context.SetExecutingCommand(&list);
392+
ExecuteWithoutLoggingSuccess(context, &list);
393+
INFO(listOutput.str());
394+
395+
Json::Value json = ConvertToJson(listOutput.str());
396+
REQUIRE(json["packages"].isArray());
397+
REQUIRE(json["packages"].empty());
398+
REQUIRE(json["sourceFailures"].isArray());
399+
REQUIRE(json["sourceFailures"].empty());
400+
REQUIRE(json["error"]["code"].asString() == "0x8a15003a");
401+
REQUIRE(json["error"]["message"].asString().empty() == false);
402+
REQUIRE(context.GetTerminationHR() == APPINSTALLER_CLI_ERROR_BLOCKED_BY_POLICY);
403+
}
404+
380405
TEST_CASE("ListFlow_JsonOutputBadSource", "[ListFlow][workflow]")
381406
{
382407
SetSetting(Stream::UserSources, R"(

0 commit comments

Comments
 (0)