2 // AssignProjectConfigurationTest.cs
5 // Ankit Jain (jankit@novell.com)
7 // Copyright 2009 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
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;
38 namespace MonoTests.Microsoft.Build.Tasks
41 public class AssignProjectConfigurationTest
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}",
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}"
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 : "},
76 public void TestNoGuidAndNoAbsolutePathFound()
78 string[] guids = new string[] {
82 string[] project_ref_guids = new string[] {
83 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
88 CreateAndCheckProject (guids, new bool[]{false},
91 "AssignedProjects : : SetConfig: ",
92 "AssignedProjects : : SetPlatform: ",
93 "UnassignedProjects : foo0.csproj;foo1.csproj;foo2.csproj"
99 public void TestInvalidProjectGuidWithAbsolutePath ()
101 string[] guids = new string[] {
103 "another invalid guid", // has AbsPath
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
113 CreateAndCheckProject (guids, new bool[]{false, true},
116 "AssignedProjects : foo1.csproj: SetConfig: Configuration=Release",
117 "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU1",
118 "UnassignedProjects : foo0.csproj;foo2.csproj;foo3.csproj"
124 public void TestNoGuidWithAbsolutePath ()
126 string[] guids = new string[] {
131 string[] project_ref_guids = new string[] {
132 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
133 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
137 CreateAndCheckProject (guids, new bool[]{true, false},
140 "AssignedProjects : foo0.csproj: SetConfig: Configuration=Release",
141 "AssignedProjects : foo0.csproj: SetPlatform: Platform=AnyCPU0",
142 "UnassignedProjects : foo1.csproj;foo2.csproj"
148 public void TestInvalidProjectGuidInSolutionConfigContents () {
149 string[] guids = new string[] {
150 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}",
154 string[] project_ref_guids = new string[] {
155 "{DAE34193-B5C7-4488-A911-29EE15C84CB8}",
156 "{23F291D9-78DF-4133-8CF2-78CE104DDE63}"
159 CreateAndCheckProject (guids, new bool[]{false, true},
162 "AssignedProjects : foo1.csproj: SetConfig: Configuration=Release",
163 "AssignedProjects : foo1.csproj: SetPlatform: Platform=AnyCPU0",
164 "UnassignedProjects : foo0.csproj"
169 void CreateAndCheckProject (string[] guids, bool[] set_project_paths, string[] project_ref_guids, string[] messages, bool build_result, string prefix)
171 Engine engine = new Engine (Consts.BinPath);
172 Project project = engine.CreateNewProject ();
173 TestMessageLogger testLogger = new TestMessageLogger ();
174 engine.RegisterLogger (testLogger);
176 string projectString = CreateProject (guids, set_project_paths, project_ref_guids);
177 project.LoadXml (projectString);
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
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 ();
194 string CreateProject (string[] guids, bool[] set_project_paths, string[] project_ref_guids)
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));
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>");
214 return sb.ToString ();
217 string CreateSolutionConfigurationProperty (string[] guids, bool[] set_project_paths, string config_str)
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);
232 sb.Append ("\t</foo>\n");
234 sb.Append ("</CurrentSolutionConfigurationContents>\n");
235 return sb.ToString ();
238 string CreateProjectReferencesItemGroup (string[] guids)
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");
248 sb.Append ("</ItemGroup>\n");
249 return sb.ToString ();
252 string GetUsingTask (string taskName)
254 return "<UsingTask TaskName='Microsoft.Build.Tasks." + taskName + "' AssemblyFile='" + Consts.GetTasksAsmPath () + "' />";