From a92eeffef570c00cd7cfc30a5e62b27b1691eee6 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 15 Oct 2006 19:33:19 +0000 Subject: [PATCH] Fixed illegal WiX ids being generated when the filename had an extension of less than three characters and when the parent directory was concatenated with the file or component id. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1908 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../WixBinding/Project/Src/ShortFileName.cs | 17 +++++++++++++ .../Project/Src/WixComponentElement.cs | 1 + .../WixBinding/Project/Src/WixFileElement.cs | 4 ++-- .../DuplicateComponentIdTestFixture.cs | 24 +++++++++++++++---- .../Document/ExistingFileIdGenerationTests.cs | 9 +++++++ .../Document/ExistingShortFileNameTests.cs | 24 +++++++++++++++++++ .../ShortFileNameGeneratorTests.cs | 12 ++++++++++ 7 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/AddIns/BackendBindings/WixBinding/Project/Src/ShortFileName.cs b/src/AddIns/BackendBindings/WixBinding/Project/Src/ShortFileName.cs index 03ae846cf3..2a8e07835b 100644 --- a/src/AddIns/BackendBindings/WixBinding/Project/Src/ShortFileName.cs +++ b/src/AddIns/BackendBindings/WixBinding/Project/Src/ShortFileName.cs @@ -140,6 +140,23 @@ namespace ICSharpCode.WixBinding return truncatedName; } + /// + /// Determines whether the specified filename is a long name + /// and should be converted into a short name. + /// + public static bool IsLongFileName(string fileName) + { + if (fileName == null) { + return false; + } + + string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); + string extension = Path.GetExtension(fileName); + + return fileNameWithoutExtension.Length > MaximumFileNameWithoutExtensionLength || + extension.Length > MaximumFileNameExtensionLength; + } + /// /// Truncates the filename start and adds "_N" where N produces a filename that /// does not exist. diff --git a/src/AddIns/BackendBindings/WixBinding/Project/Src/WixComponentElement.cs b/src/AddIns/BackendBindings/WixBinding/Project/Src/WixComponentElement.cs index c1765a614b..f7c79e1f21 100644 --- a/src/AddIns/BackendBindings/WixBinding/Project/Src/WixComponentElement.cs +++ b/src/AddIns/BackendBindings/WixBinding/Project/Src/WixComponentElement.cs @@ -95,6 +95,7 @@ namespace ICSharpCode.WixBinding // Add the parent folder to the id. string parentDirectory = WixDirectoryElement.GetLastDirectoryName(Path.GetDirectoryName(fileName)); parentDirectory = FirstCharacterToUpperInvariant(parentDirectory); + parentDirectory = WixFileElement.GenerateId(parentDirectory); id = String.Concat(parentDirectory, id); if (!document.ComponentIdExists(id)) { return id; diff --git a/src/AddIns/BackendBindings/WixBinding/Project/Src/WixFileElement.cs b/src/AddIns/BackendBindings/WixBinding/Project/Src/WixFileElement.cs index 16b0434f42..87a27f7080 100644 --- a/src/AddIns/BackendBindings/WixBinding/Project/Src/WixFileElement.cs +++ b/src/AddIns/BackendBindings/WixBinding/Project/Src/WixFileElement.cs @@ -184,7 +184,7 @@ namespace ICSharpCode.WixBinding string longFileName = null; string shortFileName = Path.GetFileName(sourceFileName); string idFileName = shortFileName; - if (shortFileName.Length > ShortFileName.MaximumFileNameLength) { + if (ShortFileName.IsLongFileName(shortFileName)) { longFileName = shortFileName; shortFileName = ShortFileName.Convert(shortFileName, ShortFileNameExists); idFileName = longFileName; @@ -217,7 +217,7 @@ namespace ICSharpCode.WixBinding // Add the file's parent directory to the id. string parentDirectoryName = WixDirectoryElement.GetLastDirectoryName(parentDirectory); if (parentDirectoryName.Length > 0) { - id = String.Concat(parentDirectoryName, ".", id); + id = String.Concat(WixFileElement.GenerateId(parentDirectoryName), ".", id); if (!document.FileIdExists(id)) { return id; } diff --git a/src/AddIns/BackendBindings/WixBinding/Test/DirectoryImport/DuplicateComponentIdTestFixture.cs b/src/AddIns/BackendBindings/WixBinding/Test/DirectoryImport/DuplicateComponentIdTestFixture.cs index 39a935af52..cf49ea0727 100644 --- a/src/AddIns/BackendBindings/WixBinding/Test/DirectoryImport/DuplicateComponentIdTestFixture.cs +++ b/src/AddIns/BackendBindings/WixBinding/Test/DirectoryImport/DuplicateComponentIdTestFixture.cs @@ -31,17 +31,19 @@ namespace WixBinding.Tests.DirectoryImport WixComponentElement readmeComponentElement; WixComponentElement licenseComponentElement; WixComponentElement exeComponentElement; + WixNamespaceManager nsManager; string directory = @"C:\Projects\Test\MyApp"; + string directory2 = @"C:\Projects\Test\a-app"; string[] files = new string[] {"MyApp.exe", "readme.txt", "license.txt"}; - [TestFixtureSetUp] - public void SetUpFixture() + [SetUp] + public void Init() { base.InitFixture(); editor.AddDirectory(directory); - WixNamespaceManager nsManager = new WixNamespaceManager(editor.Document.NameTable); - appDirectoryElement = (WixDirectoryElement)editor.Document.RootDirectory.SelectSingleNode("w:Directory[@Name='MyApp']", nsManager);; + nsManager = new WixNamespaceManager(editor.Document.NameTable); + appDirectoryElement = (WixDirectoryElement)editor.Document.RootDirectory.SelectSingleNode("w:Directory[@Name='MyApp']", nsManager); readmeComponentElement = (WixComponentElement)appDirectoryElement.SelectSingleNode("w:Component[w:File/@Name='readme.txt']", nsManager); licenseComponentElement = (WixComponentElement)appDirectoryElement.SelectSingleNode("w:Component[w:File/@Name='license.txt']", nsManager); exeComponentElement = (WixComponentElement)appDirectoryElement.SelectSingleNode("w:Component[w:File/@Name='MyApp.exe']", nsManager); @@ -68,10 +70,24 @@ namespace WixBinding.Tests.DirectoryImport Assert.AreEqual("MyAppMyAppExe2", exeComponentElement.Id); } + [Test] + public void AddDirectoryWithHyphen() + { + view.SelectedElement = null; + editor.AddDirectory(directory2); + + WixDirectoryElement directoryElement = (WixDirectoryElement)editor.Document.RootDirectory.SelectSingleNode("w:Directory[@Name='a-app']", nsManager); + WixComponentElement exeComponentElement = (WixComponentElement)directoryElement.SelectSingleNode("w:Component[w:File/@Name='MyApp.exe']", nsManager); + + Assert.AreEqual("A_appMyAppExe", exeComponentElement.Id); + } + public override string[] GetFiles(string path) { if (path == directory) { return files; + } else if (path == directory2) { + return files; } return new string[0]; } diff --git a/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingFileIdGenerationTests.cs b/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingFileIdGenerationTests.cs index 8089b96b1a..35b63f7800 100644 --- a/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingFileIdGenerationTests.cs +++ b/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingFileIdGenerationTests.cs @@ -84,6 +84,14 @@ namespace WixBinding.Tests.Document Assert.IsFalse(doc.FileIdExists("lice'nse.txt")); } + [Test] + public void FileIdAlreadyExistsAndParentDirectoryUsingHyphen() + { + WixFileElement fileElement = new WixFileElement(doc, @"C:/Projects/Setup/a-docs/readme.rtf"); + Assert.AreEqual("a_docs.readme.rtf", fileElement.Id); + } + + string GetWixXml() { return "\r\n" + @@ -104,6 +112,7 @@ namespace WixBinding.Tests.Document "\t\t\t\t\t\t\t\r\n" + "\t\t\t\t\t\t\t\r\n" + "\t\t\t\t\t\t\t\r\n" + + "\t\t\t\t\t\t\t\r\n" + "\t\t\t\t\t\t\r\n" + "\t\t\t\t\t\r\n" + "\t\t\t\r\n" + diff --git a/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingShortFileNameTests.cs b/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingShortFileNameTests.cs index 3248fce911..9f33c37c54 100644 --- a/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingShortFileNameTests.cs +++ b/src/AddIns/BackendBindings/WixBinding/Test/Document/ExistingShortFileNameTests.cs @@ -50,6 +50,30 @@ namespace WixBinding.Tests.Document WixFileElement fileElement = (WixFileElement)myAppComponent.LastChild; Assert.AreEqual("CHANGE_1.XML", fileElement.ShortName); } + + /// + /// Tests that the short name generated is correct when + /// the file extension has only two characters. + /// + [Test] + public void TwoCharacterExtension() + { + myAppComponent.AddFile(@"C:\Projects\Setup\doc\ebcdic-it.so"); + WixFileElement fileElement = (WixFileElement)myAppComponent.LastChild; + Assert.AreEqual("EBCDIC_1.SO", fileElement.ShortName); + } + + /// + /// Tests that the short name generated is correct when + /// the file extension has more than three characters. + /// + [Test] + public void FourCharacterExtension() + { + myAppComponent.AddFile(@"C:\Projects\Setup\doc\abc.text"); + WixFileElement fileElement = (WixFileElement)myAppComponent.LastChild; + Assert.AreEqual("ABC.TEX", fileElement.ShortName); + } string GetWixXml() { diff --git a/src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/ShortFileNameGeneratorTests.cs b/src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/ShortFileNameGeneratorTests.cs index 172d92d900..f32f02f376 100644 --- a/src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/ShortFileNameGeneratorTests.cs +++ b/src/AddIns/BackendBindings/WixBinding/Test/PackageFiles/ShortFileNameGeneratorTests.cs @@ -97,6 +97,18 @@ namespace WixBinding.Tests.PackageFiles string name = "boo.exe.config"; Assert.AreEqual("BOOEXE.CON", ShortFileName.Convert(name)); } + + [Test] + public void NullFileNameIsNotTooLong() + { + Assert.IsFalse(ShortFileName.IsLongFileName(null)); + } + + [Test] + public void EmptyStringIsNotTooLong() + { + Assert.IsFalse(ShortFileName.IsLongFileName(String.Empty)); + } bool GetFileNameExists(string fileName) {