2 // TaskLoggingHelperTest.cs
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (ankit.jain@xamarin.com)
8 // (C) 2005 Marek Sieradzki
9 // (C) 2016 Xamarin, Inc. (http://www.xamarin.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
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;
40 namespace MonoTests.Microsoft.Build.Utilities {
42 class TestTask : Task {
43 public static Action<TaskLoggingHelper> action = null;
46 : base (new ResourceManager("Strings", typeof(TestTask).GetTypeInfo().Assembly))
50 public override bool Execute ()
58 public class TaskLoggingHelperTest {
60 TaskLoggingHelper tlh;
63 public TaskLoggingHelperTest ()
65 task = new TestTask ();
69 public void TestAssignment ()
71 tlh = new TaskLoggingHelper (task);
75 [Category ("NotWorking")]
76 public void TestExtractMessageCode1 ()
78 tlh = new TaskLoggingHelper (task);
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;
85 code = tlh.ExtractMessageCode (message, out messageWithoutCodePrefix);
87 Assert.AreEqual (validCode, code, "#1");
88 Assert.AreEqual (validMessageWithoutCodePrefix, messageWithoutCodePrefix, "#2");
92 [ExpectedException (typeof (ArgumentNullException))]
93 public void TestExtractMessageCode2 ()
95 tlh = new TaskLoggingHelper (task);
97 tlh.ExtractMessageCode (null, out output);
101 [ExpectedException (typeof (ArgumentNullException))]
102 public void TestLogErrorFromResourcesNullMessage ()
104 tlh = new TaskLoggingHelper (task);
105 tlh.LogErrorFromResources (null);
109 [ExpectedException (typeof (ArgumentNullException))]
110 public void TestLogErrorFromResourcesNullMessage2 ()
112 tlh = new TaskLoggingHelper (task);
113 tlh.LogErrorFromResources (null, null, null, null, 0, 0, 0, 0, null);
117 public void TestLogErrorFromResources1 ()
119 RunAndCheckTaskLoggingHelper (
120 (tlh) => tlh.LogErrorFromResources ("MessageResource1", "foo"),
121 (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
126 public void TestLogErrorFromResourcesNonExistantResourceName ()
128 RunAndCheckTaskLoggingHelper (
129 (tlh) => tlh.LogErrorFromResources ("NonExistantResourceName", "foo"),
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");
142 public void TestLogErrorFromResourcesNullSubcategoryResourceName ()
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")
151 [ExpectedException (typeof (ArgumentNullException))]
152 public void TestLogWarningFromResourcesNullMessage ()
154 tlh = new TaskLoggingHelper (task);
155 tlh.LogWarningFromResources (null);
159 [ExpectedException (typeof (ArgumentNullException))]
160 public void TestLogWarningFromResourcesNullMessage2 ()
162 tlh = new TaskLoggingHelper (task);
163 tlh.LogWarningFromResources (null, null, null, null, 0, 0, 0, 0, null);
167 public void TestLogWarningFromResourcesNullSubcategoryResourceName ()
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")
176 public void TestLogWarningFromResources1 ()
178 RunAndCheckTaskLoggingHelper (
179 (tlh) => tlh.LogWarningFromResources ("MessageResource1", "foo"),
180 (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
185 public void TestLogWarningFromResourcesNonExistantResourceName ()
187 RunAndCheckTaskLoggingHelper (
188 (tlh) => tlh.LogWarningFromResources ("NonExistantResourceName", "foo"),
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");
201 [ExpectedException (typeof (ArgumentNullException))]
202 public void TestLogMessageFromResourcesNullMessage ()
204 tlh = new TaskLoggingHelper (task);
205 tlh.LogMessageFromResources (null);
209 [ExpectedException (typeof (ArgumentNullException))]
210 public void TestLogMessageFromResourcesNullMessage2 ()
212 tlh = new TaskLoggingHelper (task);
213 tlh.LogMessageFromResources (MessageImportance.Low, null);
217 public void TestLogMessageFromResources1 ()
219 RunAndCheckTaskLoggingHelper (
220 (tlh) => tlh.LogMessageFromResources ("MessageResource1", "foo"),
221 (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
226 public void TestLogMessageFromResourcesNonExistantResourceName ()
228 RunAndCheckTaskLoggingHelper (
229 (tlh) => tlh.LogMessageFromResources ("NonExistantResourceName", "foo"),
232 if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
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");
242 [ExpectedException (typeof (ArgumentNullException))]
243 public void TestFormatResourceString1 ()
245 tlh = new TaskLoggingHelper (task);
246 tlh.FormatResourceString (null);
250 [ExpectedException (typeof (InvalidOperationException))]
251 public void TestFormatResourceString2 ()
253 tlh = new TaskLoggingHelper (task);
254 tlh.FormatResourceString ("MessageResource1");
258 public void TestFormatResourceString3 ()
260 RunAndCheckTaskLoggingHelper (
261 (tlh) => tlh.FormatResourceString ("NonExistantResourceName"),
264 if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
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");
274 public void TestFormatResourceString4 ()
276 RunAndCheckTaskLoggingHelper (
277 (tlh) => Assert.AreEqual (
278 tlh.FormatResourceString ("MessageResource1", "foo"),
279 "Message from resources with arg 'foo'"),
283 void RunAndCheckTaskLoggingHelper (Action<TaskLoggingHelper> taskAction, Action<TestMessageLogger> loggerAction, Action<Project, TestMessageLogger> projectBuildAction = null)
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 + @"' />
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);
299 TestTask.action = taskAction;
301 if (projectBuildAction == null) {
302 if (!proj.Build ("1")) {
303 logger.DumpMessages ();
304 Assert.Fail ("Build failed");
307 projectBuildAction (proj, logger);
309 if (loggerAction != null)
310 loggerAction (logger);