Пример #1
0
        /// <summary>
        /// This is the common function that executes the verification that the build
        /// messages are correctly written or omitted from the output according with
        /// the verbosity level.
        /// It takes as argument a function that set the verbosity for the logger. This
        /// delegate is needed because there are at least two different ways to set the
        /// verbosity: one reading it from the registry, the other setting it directly
        /// on the logger.
        /// </summary>
        private static void VerifyLoggingByVerbosity(LoggerVerbositySetter verbositySetter)
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                using (TaskProvider task = new TaskProvider(new ServiceProvider(provider)))
                {
                    IVsHierarchy hierarchy  = MockFactories.HierarchyForLogger(provider);
                    BaseMock     mockOutput = MockFactories.OutputWindowPaneFactory.GetInstance();

                    // Create the logger and make sure that it points to the verbosity
                    // registry key that we have deleted at the beginning of the test.
                    IDELoggerProxy logger = new IDELoggerProxy(mockOutput as IVsOutputWindowPane, task, hierarchy);
                    logger.SetBuildVerbosityRegistryRoot(TestVerbosityRoot);

                    // Create the IEventSource that will be used to send messages to the logger
                    BaseMock mockSource = MockFactories.CreateMSBuildEventSource();

                    // Now initialize the logger.
                    logger.Initialize(mockSource as IEventSource);
                    // Verify that the logger has installed an event handler for messages.
                    Assert.IsNotNull(mockSource["MessageRaised"]);
                    BuildMessageEventHandler messageHandler = (BuildMessageEventHandler)mockSource["MessageRaised"];

                    foreach (VerbosityImportancePair expected in expectedVerbosityImportancePair)
                    {
                        // Because the IDEBuildLogger caches its Verbosity setting after reading it
                        // from the registry, we need to clear the cached value before calling the
                        // verbositySetter, which may set verbosity by changing the registry value.
                        logger.ClearVerbosityCache();

                        // Set the verbosity of the logger.
                        verbositySetter(logger, expected.Verbosity);
                        // Create a message of the expected importance.
                        BuildMessageEventArgs message = new BuildMessageEventArgs("message", "help", "sender", expected.Importance);

                        // Reset the counter of the calls to the output window pane.
                        mockOutput.ResetAllFunctionCalls();

                        // Execute the message event handler
                        messageHandler.Invoke(mockSource, message);
                        System.Windows.Forms.Application.DoEvents();

                        // Verify that the output pane was used only if the expected result is that
                        // something should be printed on the output.
                        if (expected.ShouldPrint)
                        {
                            Assert.IsTrue(mockOutput.TotalCallsAllFunctions() > 0);
                        }
                        else
                        {
                            Assert.AreEqual <int>(mockOutput.TotalCallsAllFunctions(), 0);
                        }
                    }
                }
            }
        }
Пример #2
0
 public void LoggerInitializeBadParam()
 {
     using (OleServiceProvider provider = new OleServiceProvider())
     {
         using (TaskProvider task = new TaskProvider(new ServiceProvider(provider)))
         {
             IVsHierarchy   hierarchy = MockFactories.HierarchyForLogger(provider);
             IDELoggerProxy logger    = new IDELoggerProxy(null, task, hierarchy);
             logger.Initialize(null);
         }
     }
 }
Пример #3
0
        public void LoggerConstructorParameter()
        {
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                Assert.IsTrue(HasConstructorThrown <ArgumentNullException>(null, null, null));
                using (TaskProvider task = new TaskProvider(null))
                {
                    IVsHierarchy        hierarchy = MockFactories.HierarchyForLogger(provider);
                    IVsOutputWindowPane output    = MockFactories.OutputWindowPaneFactory.GetInstance() as IVsOutputWindowPane;

                    Assert.IsTrue(HasConstructorThrown <ArgumentNullException>(null, task, null));
                    Assert.IsFalse(HasConstructorThrown <ArgumentNullException>(null, task, hierarchy));
                    Assert.IsTrue(HasConstructorThrown <ArgumentNullException>(null, null, hierarchy));
                    Assert.IsFalse(HasConstructorThrown <ArgumentNullException>(output, task, hierarchy));
                    Assert.IsTrue(HasConstructorThrown <ArgumentNullException>(output, null, hierarchy));
                    Assert.IsTrue(HasConstructorThrown <ArgumentNullException>(output, task, null));
                }
            }
        }
Пример #4
0
        public void EventsOutOfSequence()
        {
            // Make sure that the registry key is no present so that we can set the verbosity.
            DeleteVerbosityKey();
            using (OleServiceProvider provider = new OleServiceProvider())
            {
                using (TaskProvider task = new TaskProvider(new ServiceProvider(provider)))
                {
                    IVsHierarchy        hierarchy  = MockFactories.HierarchyForLogger(provider);
                    IVsOutputWindowPane output     = MockFactories.OutputPaneWithStringFunctions();
                    BaseMock            mockOutput = (BaseMock)output;

                    // Create the logger and make sure that it points to the verbosity
                    IDELoggerProxy logger = new IDELoggerProxy(output, task, hierarchy);
                    logger.SetBuildVerbosityRegistryRoot(TestVerbosityRoot);

                    // Create the IEventSource that will be used to send messages to the logger
                    BaseMock mockSource = MockFactories.CreateMSBuildEventSource();

                    // Now initialize the logger.
                    logger.Initialize(mockSource as IEventSource);

                    // Verify that the logger has installed an event handler for messages and
                    // build events.
                    Assert.IsNotNull(mockSource["MessageRaised"]);
                    BuildMessageEventHandler messageHandler = (BuildMessageEventHandler)mockSource["MessageRaised"];
                    Assert.IsNotNull(mockSource["BuildStarted"]);
                    BuildStartedEventHandler buildStartedHandler = (BuildStartedEventHandler)mockSource["BuildStarted"];
                    Assert.IsNotNull(mockSource["BuildFinished"]);
                    BuildFinishedEventHandler buildFinishedHandler = (BuildFinishedEventHandler)mockSource["BuildFinished"];
                    Assert.IsNotNull(mockSource["TaskStarted"]);
                    TaskStartedEventHandler taskStartedHandler = (TaskStartedEventHandler)mockSource["TaskStarted"];
                    Assert.IsNotNull(mockSource["TaskFinished"]);
                    TaskFinishedEventHandler taskFinishedHandler = (TaskFinishedEventHandler)mockSource["TaskFinished"];

                    // Create the arguments for the events and the delegates.
                    BuildMessageEventArgs messageArgs = new BuildMessageEventArgs("Message", "help", "sender", MessageImportance.Normal);
                    SendEventFunction     sendMessage = delegate { messageHandler.Invoke(mockSource, messageArgs); };
                    //
                    BuildStartedEventArgs startBuildArgs = new BuildStartedEventArgs("Start Build", "help");
                    SendEventFunction     sendStartBuild = delegate { buildStartedHandler.Invoke(mockSource, startBuildArgs); };
                    //
                    BuildFinishedEventArgs finishBuildArgs = new BuildFinishedEventArgs("End Build", "help", true);
                    SendEventFunction      sendEndBuild    = delegate { buildFinishedHandler.Invoke(mockSource, finishBuildArgs); };
                    //
                    TaskStartedEventArgs startTaskArgs = new TaskStartedEventArgs("Start Task", null, null, null, null);
                    SendEventFunction    sendStartTask = delegate { taskStartedHandler.Invoke(mockSource, startTaskArgs); };
                    //
                    TaskFinishedEventArgs endTaskArgs = new TaskFinishedEventArgs("End Task", null, null, null, null, true);
                    SendEventFunction     sendEndTask = delegate { taskFinishedHandler.Invoke(mockSource, endTaskArgs); };

                    // Set the verbosity to Diagnostic so that all the messages are written.
                    logger.Verbosity = LoggerVerbosity.Diagnostic;

                    List <SendEventFunction>[] events = new List <SendEventFunction>[] {
                        new List <SendEventFunction>(new SendEventFunction[] { sendMessage, sendEndBuild, sendStartBuild }),
                        new List <SendEventFunction>(new SendEventFunction[] { sendStartBuild, sendEndTask, sendEndBuild, sendStartTask }),
                        new List <SendEventFunction>(new SendEventFunction[] { sendStartTask, sendStartBuild, sendEndTask, sendEndBuild }),
                        new List <SendEventFunction>(new SendEventFunction[] { sendEndBuild, sendStartTask, sendEndTask, sendStartBuild }),
                        new List <SendEventFunction>(new SendEventFunction[] { sendEndBuild, sendEndTask, sendStartTask, sendStartBuild }),
                        new List <SendEventFunction>(new SendEventFunction[] { sendEndTask, sendEndBuild, sendStartTask, sendStartBuild }),
                    };

                    // Call the functions. Note that here we don't check for the actual text printed on the output,
                    // but we simply verify that the logger can handle the events without exceptions.
                    foreach (List <SendEventFunction> sendFunctions in events)
                    {
                        output.Clear();
                        foreach (SendEventFunction func in sendFunctions)
                        {
                            func();
                        }
                    }
                }
            }
        }
Пример #5
0
        public void MessageFormattingTest()
        {
            // Make sure that the registry key is no present so that we can set the verbosity.
            DeleteVerbosityKey();

            using (OleServiceProvider provider = new OleServiceProvider())
            {
                using (TaskProvider task = new TaskProvider(new ServiceProvider(provider)))
                {
                    IVsHierarchy        hierarchy  = MockFactories.HierarchyForLogger(provider);
                    IVsOutputWindowPane output     = MockFactories.OutputPaneWithStringFunctions();
                    BaseMock            mockOutput = (BaseMock)output;

                    // Create the logger and make sure that it points to the verbosity
                    IDELoggerProxy logger = new IDELoggerProxy(output, task, hierarchy);
                    logger.SetBuildVerbosityRegistryRoot(TestVerbosityRoot);

                    // Create the IEventSource that will be used to send messages to the logger
                    BaseMock mockSource = MockFactories.CreateMSBuildEventSource();

                    // Now initialize the logger.
                    logger.Initialize(mockSource as IEventSource);

                    // Verify that the logger has installed an event handler for messages and
                    // build events.
                    Assert.IsNotNull(mockSource["MessageRaised"]);
                    BuildMessageEventHandler messageHandler = (BuildMessageEventHandler)mockSource["MessageRaised"];
                    Assert.IsNotNull(mockSource["BuildStarted"]);
                    BuildStartedEventHandler buildStartedHandler = (BuildStartedEventHandler)mockSource["BuildStarted"];
                    Assert.IsNotNull(mockSource["BuildFinished"]);
                    BuildFinishedEventHandler buildFinishedHandler = (BuildFinishedEventHandler)mockSource["BuildFinished"];
                    Assert.IsNotNull(mockSource["TaskStarted"]);
                    TaskStartedEventHandler taskStartedHandler = (TaskStartedEventHandler)mockSource["TaskStarted"];
                    Assert.IsNotNull(mockSource["TaskFinished"]);
                    TaskFinishedEventHandler taskFinishedHandler = (TaskFinishedEventHandler)mockSource["TaskFinished"];

                    // Set the verbosity to Diagnostic so that all the messages are written.
                    logger.Verbosity = LoggerVerbosity.Diagnostic;

                    // Create a message of the expected importance.
                    BuildMessageEventArgs message = new BuildMessageEventArgs("message", "help", "sender", MessageImportance.Normal);
                    messageHandler.Invoke(mockSource, message);

                    // Add a second message with null text.
                    message = new BuildMessageEventArgs(null, "help", "sender", MessageImportance.Normal);
                    messageHandler.Invoke(mockSource, message);

                    // Add another message with emty text.
                    message = new BuildMessageEventArgs(string.Empty, "help", "sender", MessageImportance.Normal);
                    messageHandler.Invoke(mockSource, message);
                    System.Windows.Forms.Application.DoEvents();

                    string expected = "message" + Environment.NewLine;
                    System.Text.StringBuilder builder = (System.Text.StringBuilder)mockOutput["StringBuilder"];
                    Assert.AreEqual(expected, builder.ToString(), false);

                    // Clean up the text.
                    output.Clear();

                    // Now verify the identation in case of start and end build events.
                    string startText   = "Test Started";
                    string messageText = "build message";
                    string endText     = "Test Finished";
                    BuildStartedEventArgs startBuildArgs = new BuildStartedEventArgs(startText, "help");
                    buildStartedHandler.Invoke(mockSource, startBuildArgs);
                    message = new BuildMessageEventArgs(messageText, "help", "sender", MessageImportance.Normal);
                    messageHandler.Invoke(mockSource, message);
                    BuildFinishedEventArgs finishBuildArgs = new BuildFinishedEventArgs(endText, "help", true);
                    buildFinishedHandler.Invoke(mockSource, finishBuildArgs);
                    System.Windows.Forms.Application.DoEvents();

                    // Get the text in the output pane.
                    builder  = (System.Text.StringBuilder)mockOutput["StringBuilder"];
                    expected = string.Format("{0}{1}{2}{1}{1}{3}{1}", startText, Environment.NewLine, messageText, endText);
                    Assert.AreEqual(expected, builder.ToString(), false);

                    // Clear the output and test the identation in case of start and end task.
                    output.Clear();
                    TaskStartedEventArgs startTaskArgs = new TaskStartedEventArgs(startText, null, null, null, null);
                    taskStartedHandler.Invoke(mockSource, startTaskArgs);
                    message = new BuildMessageEventArgs(messageText, "help", "sender", MessageImportance.Normal);
                    messageHandler.Invoke(mockSource, message);
                    TaskFinishedEventArgs endTaskArgs = new TaskFinishedEventArgs(endText, null, null, null, null, true);
                    taskFinishedHandler.Invoke(mockSource, endTaskArgs);
                    System.Windows.Forms.Application.DoEvents();

                    // Verify the text in the output pane.
                    expected = string.Format("{0}{1}\t{2}{1}{3}{1}", startText, Environment.NewLine, messageText, endText);
                    builder  = (System.Text.StringBuilder)mockOutput["StringBuilder"];
                    Assert.AreEqual(expected, builder.ToString(), false);
                }
            }
        }