[xbuild] Clean up test logs.
[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.Text;
36
37 namespace MonoTests.Microsoft.Build.Tasks
38 {
39         [TestFixture]
40         public class AssignProjectConfigurationTest
41         {
42                 [Test]
43                 public void TestValidCase () {
44                         string[] guids = new string[] {
45                                 "{88932AF5-A0AF-44F3-A202-5C88152F25CA}",
46                                 "{88932AF5-A0AF-44F3-A202-5C88152FABC1}",
47                                 "{3653C4D3-60C0-4657-8289-3922D0DFB933}",
48                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
49                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
50                                 "asd"
51                         };
52
53                         string[] project_ref_guids = new string[] {
54                                 "{88932AF5-A0AF-44F3-A202-5C88152F25CA}",
55                                 "{88932AF5-A0AF-44F3-A202-5C88152faBC1}",
56                                 "{3653C4D3-60C0-4657-8289-3922D0DFB933}",
57                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
58                                 "{DAE34193-B5C7-4488-A911-29EE15C84CBE}"
59                         };
60
61                         CreateAndCheckProject (guids, project_ref_guids, new string[] {
62                                         "AssignedProjects : foo0.csproj;foo1.csproj;foo2.csproj;foo3.csproj: SetConfig: Configuration=Release",
63                                         "AssignedProjects : foo0.csproj: SetPlatform: Platform=AnyCPU0",
64                                         "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU1",
65                                         "AssignedProjects : foo2.csproj: SetPlatform: Platform=AnyCPU2",
66                                         "AssignedProjects : foo3.csproj: SetPlatform: Platform=AnyCPU3",
67                                         "UnassignedProjects : foo4.csproj"},
68                                         true,
69                                          "A1#");
70                 }
71
72                 [Test]
73                 public void TestInvalidProjectGuid ()
74                 {
75                         string[] guids = new string[] {
76                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
77                         };
78
79                         string[] project_ref_guids = new string[] {
80                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
81                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
82                                 "invalid guid"
83                         };
84
85                         CreateAndCheckProject (guids, project_ref_guids, null, false, "A1#");
86                 }
87
88                 [Test]
89                 public void TestInvalidProjectGuidInSolutionConfigContents () {
90                         string[] guids = new string[] {
91                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
92                                 "invalid guid"
93                         };
94
95                         string[] project_ref_guids = new string[] {
96                                 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
97                                 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}"
98                         };
99
100                         CreateAndCheckProject (guids, project_ref_guids,
101                                 new string [] {
102                                         "AssignedProjects : foo1.csproj: SetConfig: Configuration=Release",
103                                         "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU0",
104                                         "UnassignedProjects : foo0.csproj"
105                                 }, true, "A1#");
106                 }
107
108
109                 void CreateAndCheckProject (string[] guids, string[] project_ref_guids, string[] messages, bool build_result, string prefix)
110                 {
111                         Engine engine = new Engine (Consts.BinPath);
112                         Project project = engine.CreateNewProject ();
113                         TestMessageLogger testLogger = new TestMessageLogger ();
114                         engine.RegisterLogger (testLogger);
115
116                         string projectString = CreateProject (guids, project_ref_guids);
117                         project.LoadXml (projectString);
118
119                         try {
120                                 Assert.AreEqual (build_result, project.Build (), "Build " + (build_result ? "failed" : "should've failed"));
121                                 if (!build_result || messages == null)
122                                         // build failed as expected, don't check outputs
123                                         return;
124                                 for (int i = 0; i < messages.Length; i++)
125                                         testLogger.CheckLoggedMessageHead (messages [i], prefix + i.ToString ());
126                                 Assert.AreEqual (0, testLogger.NormalMessageCount);
127                         } catch (AssertionException) {
128                                 Console.WriteLine (projectString);
129                                 testLogger.DumpMessages ();
130                                 throw;
131                         }
132                 }
133
134                 string CreateProject (string[] guids, string[] project_ref_guids)
135                 {
136                         StringBuilder sb = new StringBuilder ();
137                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">");
138                         sb.Append ("\n" + GetUsingTask ("AssignProjectConfiguration"));
139                         sb.AppendFormat (@"<PropertyGroup>{0}</PropertyGroup>", CreateSolutionConfigurationProperty (guids, "Release|AnyCPU"));
140                         sb.Append (CreateProjectReferencesItemGroup (project_ref_guids));
141
142                         sb.Append ("\n\t<Target Name=\"1\">\n");
143                         sb.Append ("\t\t<AssignProjectConfiguration ProjectReferences=\"@(ProjectReference)\" " +
144                                         " SolutionConfigurationContents=\"$(CurrentSolutionConfigurationContents)\">\n");
145                         sb.Append ("\t\t\t<Output TaskParameter=\"AssignedProjects\" ItemName = \"AssignedProjects\" />\n");
146                         sb.Append ("\t\t\t<Output TaskParameter=\"UnassignedProjects\" ItemName = \"UnassignedProjects\" />\n");
147                         sb.Append ("\t\t</AssignProjectConfiguration>\n");
148                         sb.Append ("<Message Text=\"AssignedProjects : @(AssignedProjects): SetConfig: %(AssignedProjects.SetConfiguration)\"/>\n");
149                         sb.Append ("<Message Text=\"AssignedProjects : @(AssignedProjects): SetPlatform: %(AssignedProjects.SetPlatform)\"/>\n");
150                         sb.Append ("<Message Text=\"UnassignedProjects : @(UnassignedProjects)\"/>\n");
151                         sb.Append ("</Target>\n");
152                         sb.Append ("</Project>");
153
154                         return sb.ToString ();
155                 }
156
157                 string CreateSolutionConfigurationProperty (string[] guids, string config_str)
158                 {
159                         StringBuilder sb = new StringBuilder ();
160                         sb.Append ("\n<CurrentSolutionConfigurationContents>\n");
161                                 sb.Append ("\t<foo xmlns=\"\">\n");
162                                 for (int i = 0; i < guids.Length; i++) {
163                                         sb.AppendFormat ("\t\t<bar Project=\"{0}\">{1}{2}</bar>\n",
164                                                 guids[i], config_str, i);
165                                 }
166                                 sb.Append ("\t</foo>\n");
167
168                         sb.Append ("</CurrentSolutionConfigurationContents>\n");
169                         return sb.ToString ();
170                 }
171
172                 string CreateProjectReferencesItemGroup (string[] guids)
173                 {
174                         StringBuilder sb = new StringBuilder ();
175                         sb.Append ("\n<ItemGroup>\n");
176                         for (int i = 0; i < guids.Length; i ++)
177                                 sb.AppendFormat ("\t<ProjectReference Include=\"foo{1}.csproj\"><Project>{0}</Project></ProjectReference>\n", guids [i], i);
178                         sb.Append ("</ItemGroup>\n");
179                         return sb.ToString ();
180                 }
181                 
182                 string GetUsingTask (string taskName)
183                 {
184                         return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";
185                 }
186
187         }
188 }