[xbuild] TaskLoggingHelper: Implement support for correctly using task resources
[mono.git] / mcs / class / Microsoft.Build.Utilities / Test / Microsoft.Build.Utilities / TaskLoggingHelperTest.cs
index 8ba85db6fafef033bf08370a71a48bb3ed3f1e11..80174bfd454f5aff558c49715e79b307b4912a7f 100644 (file)
@@ -3,8 +3,10 @@
 //
 // Author:
 //   Marek Sieradzki (marek.sieradzki@gmail.com)
+//   Ankit Jain (ankit.jain@xamarin.com)
 //
 // (C) 2005 Marek Sieradzki
+// (C) 2016 Xamarin, Inc. (http://www.xamarin.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 
 using System;
 using System.Collections;
+using System.Reflection;
+using System.Resources;
 using Microsoft.Build.Framework;
 using Microsoft.Build.Utilities;
+using Microsoft.Build.BuildEngine;
 using NUnit.Framework;
+using MonoTests.Microsoft.Build.Tasks;
 
 namespace MonoTests.Microsoft.Build.Utilities {
        
        class TestTask : Task {
+               public static Action<TaskLoggingHelper> action = null;
+
+               public TestTask ()
+                       : base (new ResourceManager("Strings", typeof(TestTask).GetTypeInfo().Assembly))
+               {
+               }
+
                public override bool Execute ()
                {
+                       action (Log);
                        return true; 
                }
        }
@@ -45,6 +59,11 @@ namespace MonoTests.Microsoft.Build.Utilities {
        
                TaskLoggingHelper tlh;
                TestTask task;
+
+               public TaskLoggingHelperTest ()
+               {
+                       task = new TestTask ();
+               }
        
                [Test]
                public void TestAssignment ()
@@ -77,6 +96,218 @@ namespace MonoTests.Microsoft.Build.Utilities {
                        string output;
                        tlh.ExtractMessageCode (null, out output);
                }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestLogErrorFromResourcesNullMessage ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.LogErrorFromResources (null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestLogErrorFromResourcesNullMessage2 ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.LogErrorFromResources (null, null, null, null, 0, 0, 0, 0, null);
+               }
+
+               [Test]
+               public void TestLogErrorFromResources1 ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogErrorFromResources ("MessageResource1", "foo"),
+                                       (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
+                                       );
+               }
+
+               [Test]
+               public void TestLogErrorFromResourcesNonExistantResourceName ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogErrorFromResources ("NonExistantResourceName", "foo"),
+                                       null,
+                                       (p, l) => {
+                                               Assert.IsFalse (p.Build (), "Build should have failed");
+                                               Assert.IsTrue (l.CheckFullLog (
+                                                               "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
+                                                               "Error not found in the log");
+                                       }
+                               );
+               }
+
+
+               [Test]
+               public void TestLogErrorFromResourcesNullSubcategoryResourceName ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogErrorFromResources (null, null, null, null, 0, 0, 0, 0, "MessageResource1", "foo"),
+                                       (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
+                                       );
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestLogWarningFromResourcesNullMessage ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.LogWarningFromResources (null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestLogWarningFromResourcesNullMessage2 ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.LogWarningFromResources (null, null, null, null, 0, 0, 0, 0, null);
+               }
+
+               [Test]
+               public void TestLogWarningFromResourcesNullSubcategoryResourceName ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogWarningFromResources (null, null, null, null, 0, 0, 0, 0, "MessageResource1", "foo"),
+                                       (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
+                                       );
+               }
+
+               [Test]
+               public void TestLogWarningFromResources1 ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogWarningFromResources ("MessageResource1", "foo"),
+                                       (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
+                                       );
+               }
+
+               [Test]
+               public void TestLogWarningFromResourcesNonExistantResourceName ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogWarningFromResources ("NonExistantResourceName", "foo"),
+                                       null,
+                                       (p, l) => {
+                                               if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
+                                               Assert.IsTrue (l.CheckFullLog (
+                                                               "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
+                                                               "Error not found in the log");
+                                       }
+                               );
+               }
+
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestLogMessageFromResourcesNullMessage ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.LogMessageFromResources (null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestLogMessageFromResourcesNullMessage2 ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.LogMessageFromResources (MessageImportance.Low, null);
+               }
+
+               [Test]
+               public void TestLogMessageFromResources1 ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogMessageFromResources ("MessageResource1", "foo"),
+                                       (l) => Assert.IsTrue (l.CheckFullLog ("Message from resources with arg 'foo'") == 0, "Message not found")
+                                       );
+               }
+
+               [Test]
+               public void TestLogMessageFromResourcesNonExistantResourceName ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.LogMessageFromResources ("NonExistantResourceName", "foo"),
+                                       null,
+                                       (p, l) => {
+                                               if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
+                                               l.DumpMessages ();
+                                               Assert.IsTrue (l.CheckFullLog (
+                                                               "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
+                                                               "Error not found in the log");
+                                       }
+                               );
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TestFormatResourceString1 ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.FormatResourceString (null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void TestFormatResourceString2 ()
+               {
+                       tlh = new TaskLoggingHelper (task);
+                       tlh.FormatResourceString ("MessageResource1");
+               }
+
+               [Test]
+               public void TestFormatResourceString3 ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => tlh.FormatResourceString ("NonExistantResourceName"),
+                                       null,
+                                       (p, l) => {
+                                               if (p.Build ()) { l.DumpMessages (); Assert.Fail ("Build should have failed"); }
+                                               l.DumpMessages ();
+                                               Assert.IsTrue (l.CheckFullLog (
+                                                               "Error executing task TestTask: No resource string found for resource named NonExistantResourceName") == 0,
+                                                               "Error not found in the log");
+                                       }
+                               );
+               }
+
+               [Test]
+               public void TestFormatResourceString4 ()
+               {
+                       RunAndCheckTaskLoggingHelper (
+                                       (tlh) => Assert.AreEqual (
+                                               tlh.FormatResourceString ("MessageResource1", "foo"),
+                                               "Message from resources with arg 'foo'"),
+                                       null
+                               );
+               }
+               void RunAndCheckTaskLoggingHelper (Action<TaskLoggingHelper> taskAction, Action<TestMessageLogger> loggerAction, Action<Project, TestMessageLogger> projectBuildAction = null)
+               {
+                       string asmLocation = typeof (TaskLoggingHelperTest).Assembly.Location;
+                       string project_xml = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
+                       <UsingTask TaskName='MonoTests.Microsoft.Build.Utilities.TestTask' AssemblyFile='" + asmLocation + @"' />
+                       <Target Name=""1"">
+                               <TestTask />
+                       </Target>
+                       </Project>";
+
+                       Engine engine = new Engine (Consts.BinPath);
+                       Project proj = engine.CreateNewProject ();
+                       proj.LoadXml (project_xml);
+                       TestMessageLogger logger = new TestMessageLogger ();
+                       engine.RegisterLogger (logger);
+
+                       TestTask.action = taskAction;
+
+                       if (projectBuildAction == null) {
+                               if (!proj.Build ("1")) {
+                                       logger.DumpMessages ();
+                                       Assert.Fail ("Build failed");
+                               }
+                       } else
+                               projectBuildAction (proj, logger);
+
+                       if (loggerAction != null)
+                               loggerAction (logger);
+               }
        }
-       
 }