1 // 2 // ProjectTargetInstanceTest.cs 3 // 4 // Author: 5 // Atsushi Enomoto (atsushi@xamarin.com) 6 // 7 // Copyright (C) 2013 Xamarin Inc. (http://www.xamarin.com) 8 // 9 // Permission is hereby granted, free of charge, to any person obtaining 10 // a copy of this software and associated documentation files (the 11 // "Software"), to deal in the Software without restriction, including 12 // without limitation the rights to use, copy, modify, merge, publish, 13 // distribute, sublicense, and/or sell copies of the Software, and to 14 // permit persons to whom the Software is furnished to do so, subject to 15 // the following conditions: 16 // 17 // The above copyright notice and this permission notice shall be 18 // included in all copies or substantial portions of the Software. 19 // 20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 // 28 using System; 29 using System.IO; 30 using System.Linq; 31 using System.Xml; 32 using Microsoft.Build.Construction; 33 using Microsoft.Build.Execution; 34 using NUnit.Framework; 35 using Microsoft.Build.Logging; 36 using Microsoft.Build.Framework; 37 38 namespace MonoTests.Microsoft.Build.Execution 39 { 40 [TestFixture] 41 public class ProjectTargetInstanceTest 42 { 43 [Test] DefaultTargetsEmpty()44 public void DefaultTargetsEmpty () 45 { 46 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 47 </Project>"; 48 var xml = XmlReader.Create (new StringReader (project_xml)); 49 var root = ProjectRootElement.Create (xml); 50 var proj = new ProjectInstance (root); 51 Assert.AreEqual (new string [0], proj.DefaultTargets, "#1"); 52 } 53 54 [Test] DefaultTargetsFromAttribute()55 public void DefaultTargetsFromAttribute () 56 { 57 string project_xml = @"<Project DefaultTargets='Foo Bar Baz;Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 58 </Project>"; 59 var xml = XmlReader.Create (new StringReader (project_xml)); 60 var root = ProjectRootElement.Create (xml); 61 var proj = new ProjectInstance (root); 62 string [] expected = {"Foo Bar Baz", "Foo"}; 63 Assert.AreEqual (expected, proj.DefaultTargets, "#1"); 64 } 65 66 [Test] DefaultTargetsFromElements()67 public void DefaultTargetsFromElements () 68 { 69 string [] defaultTargetAtts = {string.Empty, "DefaultTargets=''"}; 70 71 for (int i = 0; i < defaultTargetAtts.Length; i++) { 72 string project_xml = string.Format (@"<Project {0} xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 73 <Target Name='Foo' /> 74 <Target Name='Bar' /> 75 </Project>", defaultTargetAtts [i]); 76 var xml = XmlReader.Create (new StringReader (project_xml)); 77 var root = ProjectRootElement.Create (xml); 78 var proj = new ProjectInstance (root); 79 string [] expected = {"Foo"}; // Bar is not included 80 Assert.AreEqual (expected, proj.DefaultTargets, "#1-" + i); 81 } 82 } 83 84 [Test] MicrosoftCommonTargets()85 public void MicrosoftCommonTargets () 86 { 87 string [] defaultTargetAtts = { string.Empty, "DefaultTargets=''" }; 88 89 for (int i = 0; i < defaultTargetAtts.Length; i++) { 90 string project_xml = string.Format (@"<Project {0} xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 91 <Import Project='$(MSBuildToolsPath)\Microsoft.Common.targets' /> 92 </Project>", defaultTargetAtts [i]); 93 var xml = XmlReader.Create (new StringReader (project_xml)); 94 var root = ProjectRootElement.Create (xml); 95 var proj = new ProjectInstance (root); 96 Assert.AreEqual ("Build", proj.DefaultTargets.FirstOrDefault (), "#1-" + i); 97 } 98 } 99 100 [Test] DefaultTargetsOverride()101 public void DefaultTargetsOverride () 102 { 103 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 104 <Import Project='$(MSBuildToolsPath)\Microsoft.Common.targets' /> 105 </Project>"; 106 var xml = XmlReader.Create (new StringReader (project_xml)); 107 var root = ProjectRootElement.Create (xml); 108 var proj = new ProjectInstance (root); 109 Assert.AreEqual ("Foo", proj.DefaultTargets.FirstOrDefault (), "#1"); 110 } 111 112 [Test] MultipleDefaultTargets()113 public void MultipleDefaultTargets () 114 { 115 bool[] expected = { true, false, true }; 116 string [] defaultTargets = {"Foo", "Foo;Bar", "Foo;Bar"}; 117 string [] targets = { string.Empty, string.Empty, "<Target Name='Bar' />" }; 118 for (int i = 0; i < expected.Length; i++) { 119 string project_xml = string.Format (@"<Project DefaultTargets='{0}' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 120 <Import Project='$(MSBuildToolsPath)\Microsoft.Common.targets' /> 121 <Target Name='Foo' /> 122 {1} 123 </Project>", defaultTargets [i], targets [i]); 124 var xml = XmlReader.Create (new StringReader (project_xml)); 125 var root = ProjectRootElement.Create (xml); 126 root.FullPath = string.Format ("ProjectInstanceTest.MultipleDefaultTargets.{0}.proj", i); 127 var proj = new ProjectInstance (root); 128 Assert.AreEqual ("Foo", proj.DefaultTargets.FirstOrDefault (), "#1-" + i); 129 Assert.AreEqual (expected [i], proj.Build (), "#2-" + i); 130 } 131 } 132 133 [Test] DependsOnTargets()134 public void DependsOnTargets () 135 { 136 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 137 <Target Name='Bar' DependsOnTargets='Foo' /> 138 <Target Name='Foo'> 139 <Error Text='expected error' /> 140 </Target> 141 </Project>"; 142 var xml = XmlReader.Create (new StringReader (project_xml)); 143 var root = ProjectRootElement.Create (xml); 144 root.FullPath = "ProjectInstanceTest.DependsOnTargets.proj"; 145 var proj = new ProjectInstance (root); 146 Assert.AreEqual (2, proj.Targets.Count, "#1"); 147 Assert.IsFalse (proj.Build ("Bar", new ILogger [0]), "#2"); 148 } 149 150 [Test] InputsAndOutputs()151 public void InputsAndOutputs () 152 { 153 string project_xml = @"<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 154 <Target Name='Foo' Inputs='inputsandoutputstest.txt' Outputs='inputsandoutputstest.txt'> 155 <Error Text='error' /> 156 </Target> 157 </Project>"; 158 try { 159 if (!File.Exists ("inputsandoutputstest.txt")) 160 File.CreateText ("inputsandoutputstest.txt").Close (); 161 var xml = XmlReader.Create (new StringReader (project_xml)); 162 var root = ProjectRootElement.Create (xml); 163 root.FullPath = "ProjectTargetInstanceTest.InputsAndOutputs.proj"; 164 var proj = new ProjectInstance (root); 165 Assert.IsTrue (proj.Build (), "#1"); // if it does not skip Foo, it results in an error. 166 } finally { 167 if (File.Exists ("inputsandoutputstest.txt")) 168 File.Delete ("inputsandoutputstest.txt"); 169 } 170 } 171 172 [Test] PropertiesInTarget()173 public void PropertiesInTarget () 174 { 175 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 176 <Target Name='Foo' DependsOnTargets='Bar'> 177 <Error Text='error' Condition='$(X)!=x' /> 178 </Target> 179 <Target Name='Bar'> 180 <PropertyGroup> 181 <X>x</X> 182 </PropertyGroup> 183 </Target> 184 </Project>"; 185 var xml = XmlReader.Create (new StringReader (project_xml)); 186 var root = ProjectRootElement.Create (xml); 187 root.FullPath = "ProjectTargetInstanceTest.PropertiesInTarget.proj"; 188 var proj = new ProjectInstance (root); 189 Assert.IsTrue (proj.Build (), "#1"); // if it skips Bar or does not persist property X, it results in an error. 190 } 191 192 [Test] PropertiesInTarget2()193 public void PropertiesInTarget2 () 194 { 195 string project_xml = @"<Project DefaultTargets='Foo' xmlns='http://schemas.microsoft.com/developer/msbuild/2003'> 196 <Target Name='Foo'> 197 <Error Text='error' Condition='$(X)!=x' /> 198 <!-- defined later, means it does not affect Condition above --> 199 <PropertyGroup> 200 <X>x</X> 201 </PropertyGroup> 202 </Target> 203 </Project>"; 204 var xml = XmlReader.Create (new StringReader (project_xml)); 205 var root = ProjectRootElement.Create (xml); 206 root.FullPath = "ProjectTargetInstanceTest.PropertiesInTarget.proj"; 207 var proj = new ProjectInstance (root); 208 Assert.IsFalse (proj.Build (), "#1"); 209 } 210 } 211 } 212