Skip to content

Commit

Permalink
Fix file overwrite warning displayed on clean first install (#2375)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryfu-msft authored Jul 26, 2022
1 parent bd08499 commit f66e330
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/AppInstallerCLICore/Workflows/PortableFlow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ namespace AppInstaller::CLI::Workflow
AICLI_LOG(CLI, Info, << "Unable to create symlink. '" << symlinkFullPath << "points to an existing directory.");
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_PORTABLE_SYMLINK_PATH_IS_DIRECTORY);
}
else
else if (std::filesystem::remove(symlinkFullPath))
{
context.Reporter.Warn() << Resource::String::OverwritingExistingFileAtMessage << symlinkFullPath.u8string() << std::endl;
std::filesystem::remove(symlinkFullPath);
AICLI_LOG(CLI, Info, << "Removed existing file at " << symlinkFullPath);
context.Reporter.Warn() << Resource::String::OverwritingExistingFileAtMessage << ' ' << symlinkFullPath.u8string() << std::endl;
}

std::filesystem::create_symlink(targetFullPath, symlinkFullPath);
Expand Down
36 changes: 32 additions & 4 deletions src/AppInstallerCLIE2ETests/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void InstallExeWithAlternateSourceFailure()
[Test]
public void InstallPortableExe()
{
string installDir = Path.Combine(System.Environment.GetEnvironmentVariable("LocalAppData"), "Microsoft", "WinGet", "Packages");
string installDir = TestCommon.GetPortablePackagesDirectory();
string packageId, commandAlias, fileName, packageDirName, productCode;
packageId = "AppInstallerTest.TestPortableExe";
packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
Expand Down Expand Up @@ -251,15 +251,15 @@ public void InstallPortableToExistingDirectory()
[Test]
public void InstallPortableFailsWithCleanup()
{
string winGetDir = Path.Combine(System.Environment.GetEnvironmentVariable("LocalAppData"), "Microsoft", "WinGet");
string installDir = Path.Combine(winGetDir, "Packages");
string installDir = TestCommon.GetPortablePackagesDirectory();
string winGetDir = Directory.GetParent(installDir).FullName;
string packageId, commandAlias, fileName, packageDirName, productCode;
packageId = "AppInstallerTest.TestPortableExe";
packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
commandAlias = fileName = "AppInstallerTestExeInstaller.exe";

// Create a directory with the same name as the symlink in order to cause install to fail.
string symlinkDirectory = Path.Combine(winGetDir, "Links");
string symlinkDirectory = TestCommon.GetPortableSymlinkDirectory();
string conflictDirectory = Path.Combine(symlinkDirectory, commandAlias);
Directory.CreateDirectory(conflictDirectory);

Expand All @@ -273,6 +273,34 @@ public void InstallPortableFailsWithCleanup()
TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, false);
}

[Test]
public void ReinstallPortable()
{
string installDir = TestCommon.GetPortablePackagesDirectory();
string packageId, commandAlias, fileName, packageDirName, productCode;
packageId = "AppInstallerTest.TestPortableExe";
packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
commandAlias = fileName = "AppInstallerTestExeInstaller.exe";

var result = TestCommon.RunAICLICommand("install", "AppInstallerTest.TestPortableExe");
Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);

string symlinkDirectory = TestCommon.GetPortableSymlinkDirectory();
string symlinkPath = Path.Combine(symlinkDirectory, commandAlias);

// Clean first install should not display file overwrite message.
Assert.True(result.StdOut.Contains("Successfully installed"));
Assert.False(result.StdOut.Contains($"Overwriting existing file: {symlinkPath}"));

// Perform second install and verify that file overwrite message is displayed.
var result2 = TestCommon.RunAICLICommand("install", "AppInstallerTest.TestPortableExe");
Assert.AreEqual(Constants.ErrorCode.S_OK, result2.ExitCode);
Assert.True(result2.StdOut.Contains("Successfully installed"));
Assert.True(result2.StdOut.Contains($"Overwriting existing file: {symlinkPath}"));

TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true);
}

[Test]
public void InstallZipWithExe()
{
Expand Down
12 changes: 11 additions & 1 deletion src/AppInstallerCLIE2ETests/TestCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ public static bool RemoveMsix(string name)
return RunCommand("powershell", $"Get-AppxPackage \"{name}\" | Remove-AppxPackage");
}

public static string GetPortableSymlinkDirectory()
{
return Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "Microsoft", "WinGet", "Links");
}

public static string GetPortablePackagesDirectory()
{
return Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), "Microsoft", "WinGet", "Packages");
}

public static void VerifyPortablePackage(
string installDir,
string commandAlias,
Expand All @@ -290,7 +300,7 @@ public static void VerifyPortablePackage(
string exePath = Path.Combine(installDir, filename);
bool exeExists = File.Exists(exePath);

string symlinkDirectory = Path.Combine(System.Environment.GetEnvironmentVariable("LocalAppData"), "Microsoft", "WinGet", "Links");
string symlinkDirectory = GetPortableSymlinkDirectory();
string symlinkPath = Path.Combine(symlinkDirectory, commandAlias);
bool symlinkExists = File.Exists(symlinkPath);

Expand Down

0 comments on commit f66e330

Please sign in to comment.