2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / Microsoft.Build.Tasks / Test / Microsoft.Build.Tasks / FindAppConfigFileTest.cs
1 //
2 // FindAppConfigFileTest.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 FindAppConfigFileTest {
41
42                 [Test]
43                 public void TestExecution1 ()
44                 {
45                         CheckOutput (new string [] {"app.config"}, new string [] {"app.config"}, "AppConfigWithTargetPath: app.config List1 Foo.exe.config");
46                 }
47
48                 [Test]
49                 public void TestExecution2 ()
50                 {
51                         CheckOutput (new string [] {"foobar"}, new string [] {"app.config"}, "AppConfigWithTargetPath: app.config List2 Foo.exe.config");
52                 }
53
54                 [Test]
55                 public void TestExecution3 ()
56                 {
57                         CheckOutput (new string [] {"foo\\app.config"}, new string [] {"app.config"}, "AppConfigWithTargetPath: app.config List2 Foo.exe.config");
58                 }
59
60                 [Category ("NotWorking")]
61                 [Test]
62                 public void TestExecution4 ()
63                 {
64                         CheckOutput (new string[] { "foo\\app.config" }, new string[] { "bar\\app.config" }, "AppConfigWithTargetPath: foo\\app.config List1 Foo.exe.config");
65                 }
66
67                 [Category ("NotWorking")]
68                 [Test]
69                 public void TestExecution5 ()
70                 {
71                         CheckOutput (new string[] { "foobar" }, new string[] { "bar\\app.config" }, "AppConfigWithTargetPath: bar\\app.config List2 Foo.exe.config");
72                 }
73
74                 [Test]
75                 public void TestExecution6 ()
76                 {
77                         CheckOutput (new string[] { "foobar" }, new string[] { "bar\\foo.config" }, "AppConfigWithTargetPath:   ");
78                 }
79
80                 [Test]
81                 public void TestExecution7 () {
82                         CheckOutput (new string[] { ".\\app.config" }, new string[] { "app.config" }, "AppConfigWithTargetPath: app.config List2 Foo.exe.config");
83                 }
84
85                 void CheckOutput (string[] primary_list, string[] secondary_list, string expected) {
86                         StringBuilder sb = new StringBuilder ();
87
88                         sb.Append (@"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"" ToolsVersion=""3.5"">");
89                         sb.Append ("\t<ItemGroup>");
90                         if (primary_list != null)
91                                 foreach (string s in primary_list)
92                                         sb.AppendFormat ("\t\t<List1 Include=\"{0}\"><Source>List1</Source></List1>", s);
93
94                         if (secondary_list != null)
95                                 foreach (string s in secondary_list)
96                                         sb.AppendFormat ("\t\t<List2 Include=\"{0}\"><Source>List2</Source></List2>", s);
97                         sb.Append ("\t</ItemGroup>");
98
99                         sb.Append (@"
100                                         <Target Name='1'>
101                                                 <FindAppConfigFile PrimaryList=""@(List1)"" SecondaryList=""@(List2)"" TargetPath=""Foo.exe.config"">
102                                                         <Output TaskParameter=""AppConfigFile"" ItemName=""AppConfigWithTargetPath""/>
103                                                 </FindAppConfigFile>
104
105                                                 <Message Text=""AppConfigWithTargetPath: %(AppConfigWithTargetPath.Identity) %(AppConfigWithTargetPath.Source) %(AppConfigWithTargetPath.TargetPath)""/>
106                                         </Target>
107                                 </Project>");
108
109                         string projectXml = sb.ToString ();
110                         Engine engine = new Engine (Consts.BinPath);
111                         TestMessageLogger testLogger = new TestMessageLogger ();
112                         engine.RegisterLogger (testLogger);
113
114                         Project project = engine.CreateNewProject ();
115                         project.LoadXml (projectXml);
116                         if (!project.Build ("1")) {
117                                 testLogger.DumpMessages ();
118                                 Assert.Fail ("Build failed");
119                         }
120                         testLogger.DumpMessages ();
121
122                         Assert.AreEqual (1, testLogger.NormalMessageCount, "Expected number of messages");
123                         testLogger.CheckLoggedMessageHead (expected, "A1");
124                 }
125         }
126 }       
127