diff --git a/src/wix/WixToolset.Core/HarvestFilesCommand.cs b/src/wix/WixToolset.Core/HarvestFilesCommand.cs index a8c716d04..6927f741c 100644 --- a/src/wix/WixToolset.Core/HarvestFilesCommand.cs +++ b/src/wix/WixToolset.Core/HarvestFilesCommand.cs @@ -52,28 +52,19 @@ private void HarvestFiles(HarvestFilesSymbol harvestFile, IntermediateSection se var resolvedFiles = Enumerable.Empty(); - try - { - var included = this.GetWildcardFiles(harvestFile, inclusions); - var excluded = this.GetWildcardFiles(harvestFile, exclusions); + var included = this.GetWildcardFiles(harvestFile, inclusions); + var excluded = this.GetWildcardFiles(harvestFile, exclusions); - foreach (var excludedFile in excluded) - { - this.Messaging.Write(OptimizerVerboses.ExcludedFile(harvestFile.SourceLineNumbers, excludedFile.Path)); - } + foreach (var excludedFile in excluded) + { + this.Messaging.Write(OptimizerVerboses.ExcludedFile(harvestFile.SourceLineNumbers, excludedFile.Path)); + } - resolvedFiles = included.Except(excluded, comparer).ToList(); + resolvedFiles = included.Except(excluded, comparer).ToList(); - if (!resolvedFiles.Any()) - { - this.Messaging.Write(OptimizerWarnings.ZeroFilesHarvested(harvestFile.SourceLineNumbers)); - } - } - catch (DirectoryNotFoundException e) + if (!resolvedFiles.Any()) { - this.Messaging.Write(OptimizerWarnings.ExpectedDirectory(harvestFile.SourceLineNumbers, e.Message)); - - return; + this.Messaging.Write(OptimizerWarnings.ZeroFilesHarvested(harvestFile.SourceLineNumbers)); } foreach (var fileByRecursiveDir in resolvedFiles.GroupBy(resolvedFile => resolvedFile.RecursiveDir, resolvedFile => resolvedFile.Path)) @@ -139,48 +130,53 @@ private IEnumerable GetWildcardFiles(HarvestFilesSymbol harvestFil var files = new List(); - foreach (var pattern in patterns) + try { - // Resolve bind paths, if any, which might result in multiple directories. - foreach (var path in this.ResolveBindPaths(sourceLineNumbers, pattern)) + foreach (var pattern in patterns) { - var sourceDirectory = String.IsNullOrEmpty(sourcePath) ? Path.GetDirectoryName(sourceLineNumbers.FileName) : sourcePath; - var recursive = path.IndexOf("**") >= 0; - var filePortion = Path.GetFileName(path); - var directoryPortion = Path.GetDirectoryName(path); - - if (directoryPortion?.EndsWith(@"\**") == true) + // Resolve bind paths, if any, which might result in multiple directories. + foreach (var path in this.ResolveBindPaths(sourceLineNumbers, pattern)) { - directoryPortion = directoryPortion.Substring(0, directoryPortion.Length - 3); - } + var sourceDirectory = String.IsNullOrEmpty(sourcePath) ? Path.GetDirectoryName(sourceLineNumbers.FileName) : sourcePath; + var recursive = path.IndexOf("**") >= 0; + var filePortion = Path.GetFileName(path); + var directoryPortion = Path.GetDirectoryName(path).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - var recursiveDirOffset = directoryPortion.Length + 1; + if (directoryPortion?.EndsWith(@"\**") == true) + { + directoryPortion = directoryPortion.Substring(0, directoryPortion.Length - 3); + } - if (directoryPortion is null || directoryPortion.Length == 0 || directoryPortion == "**") - { - directoryPortion = sourceDirectory; - recursiveDirOffset = sourceDirectory.Length + 1; + if (directoryPortion is null || directoryPortion.Length == 0 || directoryPortion == "**") + { + directoryPortion = sourceDirectory; - } - else if (!Path.IsPathRooted(directoryPortion)) - { - directoryPortion = Path.Combine(sourceDirectory, directoryPortion); - recursiveDirOffset = directoryPortion.Length + 1; - } + } + else if (!Path.IsPathRooted(directoryPortion)) + { + directoryPortion = Path.Combine(sourceDirectory, directoryPortion); + } - var foundFiles = Directory.EnumerateFiles(directoryPortion, filePortion, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); + var recursiveDirOffset = directoryPortion.Length + 1; - foreach (var foundFile in foundFiles) - { - var recursiveDir = Path.GetDirectoryName(foundFile.Substring(recursiveDirOffset)); - files.Add(new WildcardFile() + var foundFiles = Directory.EnumerateFiles(directoryPortion, filePortion, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); + + foreach (var foundFile in foundFiles) { - RecursiveDir = recursiveDir, - Path = foundFile, - }); + var recursiveDir = Path.GetDirectoryName(foundFile.Substring(recursiveDirOffset)); + files.Add(new WildcardFile() + { + RecursiveDir = recursiveDir, + Path = foundFile, + }); + } } } } + catch (DirectoryNotFoundException e) + { + this.Messaging.Write(OptimizerWarnings.ExpectedDirectory(harvestFile.SourceLineNumbers, e.Message)); + } return files; } diff --git a/src/wix/test/WixToolsetTest.CoreIntegration/HarvestFilesFixture.cs b/src/wix/test/WixToolsetTest.CoreIntegration/HarvestFilesFixture.cs index 7da47f6b3..e189c42db 100644 --- a/src/wix/test/WixToolsetTest.CoreIntegration/HarvestFilesFixture.cs +++ b/src/wix/test/WixToolsetTest.CoreIntegration/HarvestFilesFixture.cs @@ -48,7 +48,9 @@ public void MissingHarvestDirectoryIsAWarning() Assert.Equal(new[] { 8601, + 8600, 8601, + 8600, }, messages); }); } @@ -272,7 +274,11 @@ public void CanHarvestFilesInStandardDirectory() @"flsCgt3Noa1VJHlHG5HOVjD5vdJm5Q=PFiles\MsiPackage\files2_sub3\FileName.Extension", }; - Build("StandardDirectory.wxs", (msiPath, _) => AssertFileIdsAndTargetPaths(msiPath, expected)); + Build("StandardDirectory.wxs", (msiPath, result) => + { + result.AssertSuccess(); + AssertFileIdsAndTargetPaths(msiPath, expected); + }, warningsAsErrors: false); } [Fact] @@ -319,7 +325,7 @@ private static void AssertFileIdsAndTargetPaths(string msiPath, string[] expecte Assert.Equal(sortedExpected, actual); } - private static void Build(string file, Action tester, bool isPackage = true, params string[] additionalCommandLineArguments) + private static void Build(string file, Action tester, bool isPackage = true, bool warningsAsErrors = true, params string[] additionalCommandLineArguments) { var folder = TestData.Get("TestData", "HarvestFiles"); @@ -346,7 +352,7 @@ private static void Build(string file, Action tester, b arguments.AddRange(additionalCommandLineArguments); } - var result = WixRunner.Execute(arguments.ToArray()); + var result = WixRunner.Execute(warningsAsErrors, arguments.ToArray()); tester(msiPath, result); } diff --git a/src/wix/test/WixToolsetTest.CoreIntegration/TestData/HarvestFiles/StandardDirectory.wxs b/src/wix/test/WixToolsetTest.CoreIntegration/TestData/HarvestFiles/StandardDirectory.wxs index 1838ed66b..8b30e1185 100644 --- a/src/wix/test/WixToolsetTest.CoreIntegration/TestData/HarvestFiles/StandardDirectory.wxs +++ b/src/wix/test/WixToolsetTest.CoreIntegration/TestData/HarvestFiles/StandardDirectory.wxs @@ -5,7 +5,9 @@ - + + +