[xbuild] TaskLoggingHelper: Implement support for correctly using task resources
[mono.git] / mcs / class / Microsoft.Build.Utilities / Test / Microsoft.Build.Utilities / TaskLoggingHelperTest.cs
1 //
2 // TaskLoggingHelperTest.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (ankit.jain@xamarin.com)
7 //
8 // (C) 2005 Marek Sieradzki
9 // (C) 2016 Xamarin, Inc. (http://www.xamarin.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 using System;
31 using System.Collections;
32 using System.Reflection;
33 using System.Resources;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
36 using Microsoft.Build.BuildEngine;
37 using NUnit.Framework;
38 using MonoTests.Microsoft.Build.Tasks;
39
40 namespace MonoTests.Microsoft.Build.Utilities {
41         
42         class TestTask : Task {
43                 public static Action<TaskLoggingHelper> action = null;
44
45                 public TestTask ()
46                         : base (new ResourceManager("Strings", typeof(TestTask).GetTypeInfo().Assembly))
47                 {
48                 }
49
50                 public override bool Execute ()
51                 {
52                         action (Log);
53                         return true; 
54                 }
55         }
56         
57         [TestFixture]
58         public class TaskLoggingHelperTest {
59         
60                 TaskLoggingHelper tlh;
61                 TestTask task;
62
63                 public TaskLoggingHelperTest ()
64                 {
65                         task = new TestTask ();
66                 }
67         
68                 [Test]
69                 public void TestAssignment ()
70                 {
71                         tlh = new TaskLoggingHelper (task);
72                 }
73                 
74                 [Test]
75                 [Category ("NotWorking")]
76                 public void TestExtractMessageCode1 ()
77                 {
78                         tlh = new TaskLoggingHelper (task);
79                         
80                         string message = "MYTASK1001: This is an error message.";
81                         string validCode = "MYTASK1001";
82                         string validMessageWithoutCodePrefix = "This is an error message.";
83                         string code, messageWithoutCodePrefix;
84                         
85                         code = tlh.ExtractMessageCode (message, out messageWithoutCodePrefix);
86                         
87                         Assert.AreEqual (validCode, code, "#1");
88                         Assert.AreEqual (validMessageWithoutCodePrefix, messageWithoutCodePrefix, "#2");
89                 }
90                 
91                 [Test]
92                 [ExpectedException (typeof (ArgumentNullException))]
93                 public void TestExtractMessageCode2 ()
94                 {
95                         tlh = new TaskLoggingHelper (task);
96                         string output;
97                         tlh.ExtractMessageCode (null, out output);
98                 }
99
100                 [Test]
101                 [ExpectedException (typeof (ArgumentNullException))]
102                 public void TestLogErrorFromResourcesNullMessage ()
103                 {
104                         tlh = new TaskLoggingHelper (task);
105                         tlh.LogErrorFromResources (null);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentNullException))]
110                 public void TestLogErrorFromResourcesNullMessage2 ()
111                 {
112                         tlh = new TaskLoggingHelper (task);
113                         tlh.LogErrorFromResources (null, null, null, null, 0, 0, 0, 0, null);
114                 }
115
116                 [Test]
117                 public void TestLogErrorFromResources1 ()
118                 {
119                         RunAndCheckTaskLoggingHelper (
120                                         (tlh) => tlh.LogErrorFromResources ("MessageResource1", "foo"),
121                                         (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
122                                         );
123                 }
124
125                 [Test]
126                 public void TestLogErrorFromResourcesNonExistantResourceName ()
127                 {
128                         RunAndCheckTaskLoggingHelper (
129                                         (tlh) => tlh.LogErrorFromResources ("NonExistantResourceName", "foo"),
130                                         null,
131                                         (p, l) => {
132                                                 Assert.IsFalse (p.Build (), "Build should have failed");
133                                                 Assert.IsTrue (l.CheckFullLog (
134                                                                 "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
135                                                                 "Error not found in the log");
136                                         }
137                                 );
138                 }
139
140
141                 [Test]
142                 public void TestLogErrorFromResourcesNullSubcategoryResourceName ()
143                 {
144                         RunAndCheckTaskLoggingHelper (
145                                         (tlh) => tlh.LogErrorFromResources (null, null, null, null, 0, 0, 0, 0, "MessageResource1", "foo"),
146                                         (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
147                                         );
148                 }
149
150                 [Test]
151                 [ExpectedException (typeof (ArgumentNullException))]
152                 public void TestLogWarningFromResourcesNullMessage ()
153                 {
154                         tlh = new TaskLoggingHelper (task);
155                         tlh.LogWarningFromResources (null);
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (ArgumentNullException))]
160                 public void TestLogWarningFromResourcesNullMessage2 ()
161                 {
162                         tlh = new TaskLoggingHelper (task);
163                         tlh.LogWarningFromResources (null, null, null, null, 0, 0, 0, 0, null);
164                 }
165
166                 [Test]
167                 public void TestLogWarningFromResourcesNullSubcategoryResourceName ()
168                 {
169                         RunAndCheckTaskLoggingHelper (
170                                         (tlh) => tlh.LogWarningFromResources (null, null, null, null, 0, 0, 0, 0, "MessageResource1", "foo"),
171                                         (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
172                                         );
173                 }
174
175                 [Test]
176                 public void TestLogWarningFromResources1 ()
177                 {
178                         RunAndCheckTaskLoggingHelper (
179                                         (tlh) => tlh.LogWarningFromResources ("MessageResource1", "foo"),
180                                         (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
181                                         );
182                 }
183
184                 [Test]
185                 public void TestLogWarningFromResourcesNonExistantResourceName ()
186                 {
187                         RunAndCheckTaskLoggingHelper (
188                                         (tlh) => tlh.LogWarningFromResources ("NonExistantResourceName", "foo"),
189                                         null,
190                                         (p, l) => {
191                                                 if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
192                                                 Assert.IsTrue (l.CheckFullLog (
193                                                                 "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
194                                                                 "Error not found in the log");
195                                         }
196                                 );
197                 }
198
199
200                 [Test]
201                 [ExpectedException (typeof (ArgumentNullException))]
202                 public void TestLogMessageFromResourcesNullMessage ()
203                 {
204                         tlh = new TaskLoggingHelper (task);
205                         tlh.LogMessageFromResources (null);
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof (ArgumentNullException))]
210                 public void TestLogMessageFromResourcesNullMessage2 ()
211                 {
212                         tlh = new TaskLoggingHelper (task);
213                         tlh.LogMessageFromResources (MessageImportance.Low, null);
214                 }
215
216                 [Test]
217                 public void TestLogMessageFromResources1 ()
218                 {
219                         RunAndCheckTaskLoggingHelper (
220                                         (tlh) => tlh.LogMessageFromResources ("MessageResource1", "foo"),
221                                         (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
222                                         );
223                 }
224
225                 [Test]
226                 public void TestLogMessageFromResourcesNonExistantResourceName ()
227                 {
228                         RunAndCheckTaskLoggingHelper (
229                                         (tlh) => tlh.LogMessageFromResources ("NonExistantResourceName", "foo"),
230                                         null,
231                                         (p, l) => {
232                                                 if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
233                                                 l.DumpMessages ();
234                                                 Assert.IsTrue (l.CheckFullLog (
235                                                                 "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
236                                                                 "Error not found in the log");
237                                         }
238                                 );
239                 }
240
241                 [Test]
242                 [ExpectedException (typeof (ArgumentNullException))]
243                 public void TestFormatResourceString1 ()
244                 {
245                         tlh = new TaskLoggingHelper (task);
246                         tlh.FormatResourceString (null);
247                 }
248
249                 [Test]
250                 [ExpectedException (typeof (InvalidOperationException))]
251                 public void TestFormatResourceString2 ()
252                 {
253                         tlh = new TaskLoggingHelper (task);
254                         tlh.FormatResourceString ("MessageResource1");
255                 }
256
257                 [Test]
258                 public void TestFormatResourceString3 ()
259                 {
260                         RunAndCheckTaskLoggingHelper (
261                                         (tlh) => tlh.FormatResourceString ("NonExistantResourceName"),
262                                         null,
263                                         (p, l) => {
264                                                 if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
265                                                 l.DumpMessages ();
266                                                 Assert.IsTrue (l.CheckFullLog (
267                                                                 "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
268                                                                 "Error not found in the log");
269                                         }
270                                 );
271                 }
272
273                 [Test]
274                 public void TestFormatResourceString4 ()
275                 {
276                         RunAndCheckTaskLoggingHelper (
277                                         (tlh) => Assert.AreEqual (
278                                                 tlh.FormatResourceString ("MessageResource1", "foo"),
279                                                 "Message from resources with arg 'foo'"),
280                                         null
281                                 );
282                 }
283                 void RunAndCheckTaskLoggingHelper (Action<TaskLoggingHelper> taskAction, Action<TestMessageLogger> loggerAction, Action<Project, TestMessageLogger> projectBuildAction = null)
284                 {
285                         string asmLocation = typeof (TaskLoggingHelperTest).Assembly.Location;
286                         string project_xml = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
287                         <UsingTask TaskName='MonoTests.Microsoft.Build.Utilities.TestTask' AssemblyFile='" + asmLocation + @"' />
288                         <Target Name=""1"">
289                                 <TestTask />
290                         </Target>
291                         </Project>";
292
293                         Engine engine = new Engine (Consts.BinPath);
294                         Project proj = engine.CreateNewProject ();
295                         proj.LoadXml (project_xml);
296                         TestMessageLogger logger = new TestMessageLogger ();
297                         engine.RegisterLogger (logger);
298
299                         TestTask.action = taskAction;
300
301                         if (projectBuildAction == null) {
302                                 if (!proj.Build ("1")) {
303                                         logger.DumpMessages ();
304                                         Assert.Fail ("Build failed");
305                                 }
306                         } else
307                                 projectBuildAction (proj, logger);
308
309                         if (loggerAction != null)
310                                 loggerAction (logger);
311                 }
312         }
313 }