Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoveFoldersEx test and error message correction #556

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/ext/Util/ca/RemoveFoldersEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include "precomp.h"

// Whilst ::GetFileAttributesW might return FILE_ATTRIBUTE_INVALID for an invalid path, it's not a header defined variable.
// so define it here, but guard it to avoid redefining it if Microsoft change their mind
#ifndef FILE_ATTRIBUTE_INVALID
#define FILE_ATTRIBUTE_INVALID ((DWORD)-1)
#endif

LPCWSTR vcsRemoveFolderExQuery =
L"SELECT `RemoveFolderEx`, `Component_`, `Property`, `InstallMode`, `Wix4RemoveFolderEx`.`Condition`, `Component`.`Attributes` "
L"FROM `Wix4RemoveFolderEx`,`Component` "
Expand Down Expand Up @@ -38,8 +44,14 @@ static HRESULT RecursePath(
}
#endif

// Do NOT follow junctions.
DWORD dwAttributes = ::GetFileAttributesW(wzPath);
if (FILE_ATTRIBUTE_INVALID == dwAttributes)
{
WcaLog(LOGMSG_STANDARD, "Path is invalid: %ls", wzPath);
ExitFunction();
}

// Do NOT follow junctions.
if (dwAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
{
WcaLog(LOGMSG_STANDARD, "Path is a junction; skipping: %ls", wzPath);
Expand Down Expand Up @@ -232,7 +244,7 @@ extern "C" UINT WINAPI WixRemoveFoldersEx(
{
hr = S_OK;
}
ExitOnFailure(hr, "Failure occured while processing Wix4RemoveFolderEx table");
ExitOnFailure(hr, "Failure occurred while processing Wix4RemoveFolderEx table");

LExit:
#ifndef _WIN64
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"
xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util">
<Fragment>
<ComponentGroup Id="ProductComponents">
<ComponentRef Id="Component1" />
</ComponentGroup>
</Fragment>

<Fragment>
<SetProperty Id="REMOVEFOLDEREXTESTDIR" Value="C:\RemoveFolderExTest" Sequence="execute" Before="Wix4RemoveFoldersEx_X86" />
<Component Id="Component1" Guid="{2D735A5F-D152-4B2E-B935-E11AD8C3FB25}">
<util:RemoveFolderEx Id ="RemoveAllTheFolders" On="both" Property="REMOVEFOLDEREXTESTDIR" />
</Component>
</Fragment>
</Wix>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. -->
<Project Sdk="WixToolset.Sdk">
<PropertyGroup>
<UpgradeCode>{A75B81F4-3335-4B4D-B766-303E136ED374}</UpgradeCode>
<ProductComponentsRef>true</ProductComponentsRef>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\Templates\Product.wxs" Link="Product.wxs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="WixToolset.Util.wixext" />
</ItemGroup>
</Project>
82 changes: 82 additions & 0 deletions src/test/msi/WixToolsetTest.MsiE2E/RemoveFolderExTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolsetTest.MsiE2E;

using System;
using System.IO;
using WixTestTools;
using Xunit;
using Xunit.Abstractions;

public class RemoveFolderExTests : MsiE2ETests
{
public RemoveFolderExTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

[Fact]
public void CanRemoveFolderExOnInstallAndUninstall()
{
var removeFolderExTestDir1 = "C:\\RemoveFolderExTest";
var removeFolderExTestFile1 = Path.Combine(removeFolderExTestDir1, "testfile.txt");
var removeFolderExTestDir2 = Path.Combine(removeFolderExTestDir1, "TestFolder1");
var removeFolderExTestFile2 = Path.Combine(removeFolderExTestDir1, "TestFolder1", "testfile");

try
{
var product = this.CreatePackageInstaller("RemoveFolderExTest");

Directory.CreateDirectory(removeFolderExTestDir1);
File.Create(removeFolderExTestFile1).Dispose();
Directory.CreateDirectory(removeFolderExTestDir2);
File.Create(removeFolderExTestFile2).Dispose();

if( !Directory.Exists(removeFolderExTestDir1)
|| !File.Exists(removeFolderExTestFile1)
|| !Directory.Exists(removeFolderExTestDir2)
|| !File.Exists(removeFolderExTestFile2))
{
Assert.Fail("Failed to create initial folder and file structure before install test");
}

product.InstallProduct(MSIExec.MSIExecReturnCode.SUCCESS);

Assert.False(Directory.Exists(removeFolderExTestDir1), $"Failed to remove {removeFolderExTestDir1} on install");
Assert.False(File.Exists(removeFolderExTestFile1), $"Failed to remove {removeFolderExTestFile1} on install");
Assert.False(Directory.Exists(removeFolderExTestDir2), $"Failed to remove {removeFolderExTestDir2} on install");
Assert.False(File.Exists(removeFolderExTestFile1), $"Failed to remove {removeFolderExTestFile2} on install");


Directory.CreateDirectory(removeFolderExTestDir1);
File.Create(removeFolderExTestFile1).Dispose();
Directory.CreateDirectory(removeFolderExTestDir2);
File.Create(removeFolderExTestFile2).Dispose();

if (!Directory.Exists(removeFolderExTestDir1)
|| !File.Exists(removeFolderExTestFile1)
|| !Directory.Exists(removeFolderExTestDir2)
|| !File.Exists(removeFolderExTestFile2))
{
Assert.Fail("Failed to create initial folder and file structure before uninstall test");
}

product.UninstallProduct(MSIExec.MSIExecReturnCode.SUCCESS);

Assert.False(Directory.Exists(removeFolderExTestDir1), $"Failed to remove {removeFolderExTestDir1} on uninstall");
Assert.False(File.Exists(removeFolderExTestFile1), $"Failed to remove {removeFolderExTestFile1} on uninstall");
Assert.False(Directory.Exists(removeFolderExTestDir2), $"Failed to remove {removeFolderExTestDir2} on uninstall");
Assert.False(File.Exists(removeFolderExTestFile2), $"Failed to remove {removeFolderExTestFile2} on uninstall");

}
finally
{
try
{
Directory.Delete(removeFolderExTestDir1, true);
}
catch
{
}
}
}
}
Loading