[xbuild] Fix #40094, part 2/2: AssignProjectConfiguration - Fallback to
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / AssignProjectConfigurationTest.cs
1 //
2 // AssignProjectConfigurationTest.cs
3 //
4 // Author:
5 //   Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2009 Novell, Inc (http://www.novell.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.Collections;
30 using Microsoft.Build.BuildEngine;
31 using Microsoft.Build.Framework;
32 using Microsoft.Build.Tasks;
33 using Microsoft.Build.Utilities;
34 using NUnit.Framework;
35 using System.IO;
36 using System.Text;
37
38 namespace MonoTests.Microsoft.Build.Tasks
39 {
40         [TestFixture]
41         public class AssignProjectConfigurationTest
42         {
43                 [Test]
44                 public void TestValidCase () {
45                         string[] guids = new string[] {
46                                 "{88932AF5-A0AF-44F3-A202-5C88152F25CA}",
47                                 "{88932AF5-A0AF-44F3-A202-5C88152FABC1}",
48                                 "{3653C4D3-60C0-4657-8289-3922D0DFB933}",
49                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
50                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
51                                 "asd"
52                         };
53
54                         string[] project_ref_guids = new string[] {
55                                 "{88932AF5-A0AF-44F3-A202-5C88152F25CA}",
56                                 "{88932AF5-A0AF-44F3-A202-5C88152faBC1}",
57                                 "{3653C4D3-60C0-4657-8289-3922D0DFB933}",
58                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
59                                 "{DAE34193-B5C7-4488-A911-29EE15C84CBE}"
60                         };
61
62                         CreateAndCheckProject (guids, new bool[] {true, true, true, true, true, true},
63                                         project_ref_guids, new string[] {
64                                         "AssignedProjects : foo0.csproj;foo1.csproj;foo2.csproj;foo3.csproj;foo4.csproj: SetConfig: Configuration=Release",
65                                         "AssignedProjects : foo0.csproj: SetPlatform: Platform=AnyCPU0",
66                                         "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU1",
67                                         "AssignedProjects : foo2.csproj: SetPlatform: Platform=AnyCPU2",
68                                         "AssignedProjects : foo3.csproj: SetPlatform: Platform=AnyCPU3",
69                                         "AssignedProjects : foo4.csproj: SetPlatform: Platform=AnyCPU4",
70                                         "UnassignedProjects : "},
71                                         true,
72                                          "A1#");
73                 }
74
75                 [Test]
76                 public void TestNoGuidAndNoAbsolutePathFound()
77                 {
78                         string[] guids = new string[] {
79                                 "asd"
80                         };
81
82                         string[] project_ref_guids = new string[] {
83                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
84                                 "invalid guid",
85                                 ""
86                         };
87
88                         CreateAndCheckProject (guids, new bool[]{false},
89                                         project_ref_guids,
90                                         new string[] {
91                                                 "AssignedProjects : : SetConfig: ",
92                                                 "AssignedProjects : : SetPlatform: ",
93                                                 "UnassignedProjects : foo0.csproj;foo1.csproj;foo2.csproj"
94                                         },
95                                         true, "A1#");
96                 }
97
98                 [Test]
99                 public void TestInvalidProjectGuidWithAbsolutePath ()
100                 {
101                         string[] guids = new string[] {
102                                 null, // no AbsPath
103                                 "another invalid guid", // has AbsPath
104                         };
105
106                         string[] project_ref_guids = new string[] {
107                                 "1234zxc", // this won't match because no AbsPath
108                                 "xzxoiu",  // match with the second project, foo1.csproj
109                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
110                                 "badref"   // no corresponding project at all
111                         };
112
113                         CreateAndCheckProject (guids, new bool[]{false, true},
114                                         project_ref_guids,
115                                         new string[] {
116                                                 "AssignedProjects : foo1.csproj: SetConfig: Configuration=Release",
117                                                 "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU1",
118                                                 "UnassignedProjects : foo0.csproj;foo2.csproj;foo3.csproj"
119                                         },
120                                         true, "A1#");
121                 }
122
123                 [Test]
124                 public void TestNoGuidWithAbsolutePath ()
125                 {
126                         string[] guids = new string[] {
127                                 "",
128                                 null
129                         };
130
131                         string[] project_ref_guids = new string[] {
132                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
133                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
134                                 "invalid guid"
135                         };
136
137                         CreateAndCheckProject (guids, new bool[]{true, false},
138                                         project_ref_guids,
139                                         new string[] {
140                                                 "AssignedProjects : foo0.csproj: SetConfig: Configuration=Release",
141                                                 "AssignedProjects : foo0.csproj: SetPlatform: Platform=AnyCPU0",
142                                                 "UnassignedProjects : foo1.csproj;foo2.csproj"
143                                         },
144                                         true, "A1#");
145                 }
146
147                 [Test]
148                 public void TestInvalidProjectGuidInSolutionConfigContents () {
149                         string[] guids = new string[] {
150                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
151                                 "invalid guid"
152                         };
153
154                         string[] project_ref_guids = new string[] {
155                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
156                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}"
157                         };
158
159                         CreateAndCheckProject (guids, new bool[]{false, true},
160                                 project_ref_guids,
161                                 new string [] {
162                                         "AssignedProjects : foo1.csproj: SetConfig: Configuration=Release",
163                                         "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU0",
164                                         "UnassignedProjects : foo0.csproj"
165                                 }, true, "A1#");
166                 }
167
168
169                 void CreateAndCheckProject (string[] guids, bool[] set_project_paths, string[] project_ref_guids, string[] messages, bool build_result, string prefix)
170                 {
171                         Engine engine = new Engine (Consts.BinPath);
172                         Project project = engine.CreateNewProject ();
173                         TestMessageLogger testLogger = new TestMessageLogger ();
174                         engine.RegisterLogger (testLogger);
175
176                         string projectString = CreateProject (guids, set_project_paths, project_ref_guids);
177                         project.LoadXml (projectString);
178
179                         try {
180                                 Assert.AreEqual (build_result, project.Build (), "Build " + (build_result ? "failed" : "should've failed"));
181                                 if (!build_result || messages == null)
182                                         // build failed as expected, don't check outputs
183                                         return;
184                                 for (int i = 0; i < messages.Length; i++)
185                                         testLogger.CheckLoggedMessageHead (messages [i], prefix + i.ToString ());
186                                 Assert.AreEqual (0, testLogger.NormalMessageCount);
187                         } catch (AssertionException) {
188                                 Console.WriteLine (projectString);
189                                 testLogger.DumpMessages ();
190                                 throw;
191                         }
192                 }
193
194                 string CreateProject (string[] guids, bool[] set_project_paths, string[] project_ref_guids)
195                 {
196                         StringBuilder sb = new StringBuilder ();
197                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">");
198                         sb.Append ("\n" + GetUsingTask ("AssignProjectConfiguration"));
199                         sb.AppendFormat (@"<PropertyGroup>{0}</PropertyGroup>", CreateSolutionConfigurationProperty (guids, set_project_paths, "Release|AnyCPU"));
200                         sb.Append (CreateProjectReferencesItemGroup (project_ref_guids));
201
202                         sb.Append ("\n\t<Target Name=\"1\">\n");
203                         sb.Append ("\t\t<AssignProjectConfiguration ProjectReferences=\"@(ProjectReference)\" " +
204                                         " SolutionConfigurationContents=\"$(CurrentSolutionConfigurationContents)\">\n");
205                         sb.Append ("\t\t\t<Output TaskParameter=\"AssignedProjects\" ItemName = \"AssignedProjects\" />\n");
206                         sb.Append ("\t\t\t<Output TaskParameter=\"UnassignedProjects\" ItemName = \"UnassignedProjects\" />\n");
207                         sb.Append ("\t\t</AssignProjectConfiguration>\n");
208                         sb.Append ("<Message Text=\"AssignedProjects : @(AssignedProjects): SetConfig: %(AssignedProjects.SetConfiguration)\"/>\n");
209                         sb.Append ("<Message Text=\"AssignedProjects : @(AssignedProjects): SetPlatform: %(AssignedProjects.SetPlatform)\"/>\n");
210                         sb.Append ("<Message Text=\"UnassignedProjects : @(UnassignedProjects)\"/>\n");
211                         sb.Append ("</Target>\n");
212                         sb.Append ("</Project>");
213
214                         return sb.ToString ();
215                 }
216
217                 string CreateSolutionConfigurationProperty (string[] guids, bool[] set_project_paths, string config_str)
218                 {
219                         string abs_proj_path_prefix = Path.GetFullPath ("foo");
220                         StringBuilder sb = new StringBuilder ();
221                         sb.Append ("\n<CurrentSolutionConfigurationContents>\n");
222                                 sb.Append ("\t<foo xmlns=\"\">\n");
223                                 for (int i = 0; i < guids.Length; i++) {
224                                         sb.Append ("\t\t<bar");
225                                         if (guids[i] != null)
226                                                 sb.AppendFormat (" Project=\"{0}\"", guids[i]);
227                                         if (set_project_paths[i])
228                                                 sb.AppendFormat (" AbsolutePath=\"{0}{1}.csproj\" ", abs_proj_path_prefix, i);
229                                         sb.AppendFormat (">{1}{2}</bar>\n",
230                                                 guids[i], config_str, i);
231                                 }
232                                 sb.Append ("\t</foo>\n");
233
234                         sb.Append ("</CurrentSolutionConfigurationContents>\n");
235                         return sb.ToString ();
236                 }
237
238                 string CreateProjectReferencesItemGroup (string[] guids)
239                 {
240                         StringBuilder sb = new StringBuilder ();
241                         sb.Append ("\n<ItemGroup>\n");
242                         for (int i = 0; i < guids.Length; i ++) {
243                                 sb.AppendFormat ("\t<ProjectReference Include=\"foo{0}.csproj\">", i);
244                                 if (guids[i] != null)
245                                         sb.AppendFormat ("<Project>{0}</Project>", guids[i]);
246                                 sb.Append ("</ProjectReference>\n");
247                         }
248                         sb.Append ("</ItemGroup>\n");
249                         return sb.ToString ();
250                 }
251                 
252                 string GetUsingTask (string taskName)
253                 {
254                         return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";
255                 }
256
257         }
258 }