diff options
-rw-r--r-- | main/src/core/MonoDevelop.Core/MonoDevelop.Projects.MSBuild/MSBuildImport.cs | 4 | ||||
-rw-r--r-- | main/tests/UnitTests/MonoDevelop.Projects/MSBuildProjectTests.cs | 44 |
2 files changed, 47 insertions, 1 deletions
diff --git a/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.MSBuild/MSBuildImport.cs b/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.MSBuild/MSBuildImport.cs index bc821dad2e..c64ab829ce 100644 --- a/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.MSBuild/MSBuildImport.cs +++ b/main/src/core/MonoDevelop.Core/MonoDevelop.Projects.MSBuild/MSBuildImport.cs @@ -79,7 +79,7 @@ namespace MonoDevelop.Projects.MSBuild base.Write (writer, context); } - void WritePatchedImport (XmlWriter writer, string newTarget) + internal void WritePatchedImport (XmlWriter writer, string newTarget) { /* If an import redirect exists, add a fake import to the project which will be used only if the original import doesn't exist. That is, the following import: @@ -102,6 +102,7 @@ namespace MonoDevelop.Projects.MSBuild writer.WriteStartElement ("Import", MSBuildProject.Schema); writer.WriteAttributeString ("Project", target); writer.WriteAttributeString ("Condition", cond); + writer.WriteEndElement (); // Now add the fake import, with a condition so that it will be used only if the original // import does not exist. @@ -113,6 +114,7 @@ namespace MonoDevelop.Projects.MSBuild writer.WriteStartElement ("Import", MSBuildProject.Schema); writer.WriteAttributeString ("Project", MSBuildProjectService.ToMSBuildPath (null, newTarget)); writer.WriteAttributeString ("Condition", cond); + writer.WriteEndElement (); } } diff --git a/main/tests/UnitTests/MonoDevelop.Projects/MSBuildProjectTests.cs b/main/tests/UnitTests/MonoDevelop.Projects/MSBuildProjectTests.cs index 2e87dee4e9..668a698619 100644 --- a/main/tests/UnitTests/MonoDevelop.Projects/MSBuildProjectTests.cs +++ b/main/tests/UnitTests/MonoDevelop.Projects/MSBuildProjectTests.cs @@ -28,6 +28,7 @@ using NUnit.Framework; using UnitTests; using MonoDevelop.Core.Serialization; using MonoDevelop.Projects.MSBuild; +using System.IO; using System.Linq; using System.Xml; using ValueSet = MonoDevelop.Projects.ConditionedPropertyCollection.ValueSet; @@ -921,6 +922,49 @@ namespace MonoDevelop.Projects Assert.IsFalse (test2Element.HasAttribute ("xmlns")); Assert.IsFalse (test3Element.HasAttribute ("xmlns")); } + + [TestCase ("ToolsVersion=\"15.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\"")] + public void PatchedImport (string projectElementAttributes) + { + string projectXml = + "<Project " + projectElementAttributes + ">\r\n" + + " <PropertyGroup>\r\n" + + " <TargetFramework>netcoreapp1.0</TargetFramework>\r\n" + + " <Test1></Test1>\r\n" + + " </PropertyGroup>\r\n" + + " <Import Project=\"Original.targets\" />\r\n" + + "</Project>"; + + var p = new MSBuildProject (); + p.LoadXml (projectXml); + + var import = p.Imports.Single (); + + var sw = new StringWriter (); + var xw = XmlWriter.Create (sw, new XmlWriterSettings { + OmitXmlDeclaration = true, + NewLineChars = "\r\n", + NewLineHandling = NewLineHandling.Replace + }); + + xw.WriteStartElement (string.Empty, "Root", p.Namespace); + import.WritePatchedImport (xw, "Updated.targets"); + xw.WriteEndElement (); + xw.Dispose (); + + var doc = new XmlDocument (); + doc.LoadXml (sw.ToString ()); + + var import1 = (XmlElement)doc.DocumentElement.ChildNodes [0]; + var import2 = (XmlElement)doc.DocumentElement.ChildNodes [1]; + + Assert.AreEqual ("Original.targets", import1.GetAttribute ("Project")); + Assert.AreEqual ("Exists('Original.targets')", import1.GetAttribute ("Condition")); + Assert.AreEqual ("Updated.targets", import2.GetAttribute ("Project")); + Assert.AreEqual ("!Exists('Original.targets')", import2.GetAttribute ("Condition")); + Assert.IsFalse (import1.HasAttribute ("xmlns")); + Assert.IsFalse (import2.HasAttribute ("xmlns")); + } } } |