Skip to content

Commit

Permalink
add success test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryfu-msft committed Jun 18, 2024
1 parent 56b558e commit 3557d05
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/AppInstallerCLICore/Workflows/ShellExecuteInstallerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,38 @@ namespace AppInstaller::CLI::Workflow
}
}

#ifndef AICLI_DISABLE_TEST_HOOKS
std::optional<DWORD> s_ExtractArchiveWithTarResult_Override{};

void TestHook_SetExtractArchiveWithTarResult_Override(std::optional<DWORD>&& result)
{
s_ExtractArchiveWithTarResult_Override = std::move(result);
}
#endif

void ShellExecuteExtractArchive::operator()(Execution::Context& context) const
{
auto tarExecPath = AppInstaller::Filesystem::GetExpandedPath("%windir%\\system32\\tar.exe");

std::string args = "-xf \"" + m_archivePath.u8string() + "\" -C \"" + m_destPath.u8string() + "\"";

auto extractArchiveResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
tarExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));
std::optional<DWORD> extractArchiveResult;
#ifndef AICLI_DISABLE_TEST_HOOKS
if (s_ExtractArchiveWithTarResult_Override)
{
extractArchiveResult = *s_ExtractArchiveWithTarResult_Override;
}
else
#endif
{
extractArchiveResult = context.Reporter.ExecuteWithProgress(
std::bind(InvokeShellExecuteEx,
tarExecPath,
args,
false,
SW_HIDE,
std::placeholders::_1));
}

if (!extractArchiveResult)
{
Expand Down
35 changes: 35 additions & 0 deletions src/AppInstallerCLITests/InstallFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,47 @@ TEST_CASE("ExtractInstallerFromArchive_InvalidZip", "[InstallFlow][workflow]")
auto manifest = YamlParser::CreateFromPath(TestDataFile("InstallFlowTest_Zip_Exe.yaml"));
context.Add<Data::Manifest>(manifest);
context.Add<Data::Installer>(manifest.Installers.at(0));

// Provide an invalid zip file which should be handled appropriately.
context.Add<Data::InstallerPath>(TestDataFile("AppInstallerTestExeInstaller.exe"));
context << ExtractFilesFromArchive;
REQUIRE_TERMINATED_WITH(context, APPINSTALLER_CLI_ERROR_EXTRACT_ARCHIVE_FAILED);
REQUIRE(installOutput.str().find(Resource::LocString(Resource::String::ExtractArchiveFailed).get()) != std::string::npos);
}

TEST_CASE("ExtractInstallerFromArchiveWithTar", "[InstallFlow][workflow]")
{
TestCommon::TestUserSettings testSettings;
testSettings.Set<Setting::ArchiveExtractionMethod>(AppInstaller::Archive::ExtractionMethod::Tar);

TestCommon::TempFile installResultPath("TestExeInstalled.txt");

std::ostringstream installOutput;
TestContext context{ installOutput, std::cin };
auto previousThreadGlobals = context.SetForCurrentThread();

OverrideForShellExecute(context);
OverrideForVerifyAndSetNestedInstaller(context);
context.Args.AddArg(Execution::Args::Type::Manifest, TestDataFile("InstallFlowTest_Zip_Exe.yaml").GetPath().u8string());

TestHook::SetScanArchiveResult_Override scanArchiveResultOverride(true);
TestHook::SetExtractArchiveWithTarResult_Override setExtractArchiveWithTarResultOverride(ERROR_SUCCESS);

InstallCommand install({});
install.Execute(context);
INFO(installOutput.str());
REQUIRE(installOutput.str().find(Resource::LocString(Resource::String::ExtractArchiveSucceeded).get()) != std::string::npos);

// Verify Installer is called and parameters are passed in.
REQUIRE(std::filesystem::exists(installResultPath.GetPath()));
std::ifstream installResultFile(installResultPath.GetPath());
REQUIRE(installResultFile.is_open());
std::string installResultStr;
std::getline(installResultFile, installResultStr);
REQUIRE(installResultStr.find("/custom") != std::string::npos);
REQUIRE(installResultStr.find("/silentwithprogress") != std::string::npos);
}

TEST_CASE("ExtractInstallerFromArchiveWithTar_InvalidZip", "[InstallFlow][workflow]")
{
TestCommon::TestUserSettings testSettings;
Expand All @@ -525,6 +559,7 @@ TEST_CASE("ExtractInstallerFromArchiveWithTar_InvalidZip", "[InstallFlow][workfl
auto manifest = YamlParser::CreateFromPath(TestDataFile("InstallFlowTest_Zip_Exe.yaml"));
context.Add<Data::Manifest>(manifest);
context.Add<Data::Installer>(manifest.Installers.at(0));

// Provide an invalid zip file which should be handled appropriately.
context.Add<Data::InstallerPath>(TestDataFile("AppInstallerTestExeInstaller.exe"));
context << ExtractFilesFromArchive;
Expand Down
14 changes: 14 additions & 0 deletions src/AppInstallerCLITests/TestHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace AppInstaller
{
void TestHook_SetEnableWindowsFeatureResult_Override(std::optional<DWORD>&& result);
void TestHook_SetDoesWindowsFeatureExistResult_Override(std::optional<DWORD>&& result);
void TestHook_SetExtractArchiveWithTarResult_Override(std::optional<DWORD>&& result);
}

namespace Reboot
Expand Down Expand Up @@ -183,6 +184,19 @@ namespace TestHook
}
};

struct SetExtractArchiveWithTarResult_Override
{
SetExtractArchiveWithTarResult_Override(DWORD result)
{
AppInstaller::CLI::Workflow::TestHook_SetExtractArchiveWithTarResult_Override(result);
}

~SetExtractArchiveWithTarResult_Override()
{
AppInstaller::CLI::Workflow::TestHook_SetExtractArchiveWithTarResult_Override({});
}
};

struct SetInitiateRebootResult_Override
{
SetInitiateRebootResult_Override(bool status) : m_status(status)
Expand Down

0 comments on commit 3557d05

Please sign in to comment.