public void ResetConsoleLoggerState_PerformanceCounters()
        {
            for (int i = 1; i <= 2; i++)
            {
                EventSourceSink es = new EventSourceSink();
                //Create a simulated console
                SimulatedConsole sc = new SimulatedConsole();
                // Create a ConsoleLogger with Normal verbosity
                ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Normal, sc.Write, sc.SetColor, sc.ResetColor);
                // Initialize ConsoleLogger
                L.Parameters = "Performancesummary";
                L.Initialize(es, i);
                // prjPerfString = Project Performance Summary:
                string prjPerfString = ResourceUtilities.FormatResourceString("ProjectPerformanceSummary", null);
                // targetPerfString = Target Performance Summary:
                string targetPerfString = ResourceUtilities.FormatResourceString("TargetPerformanceSummary", null);
                // taskPerfString = Task Performance Summary:
                string taskPerfString = ResourceUtilities.FormatResourceString("TaskPerformanceSummary", null);

                // BuildStarted Event
                es.Consume(new BuildStartedEventArgs("bs", null));
                //Project Started Event
                ProjectStartedEventArgs project1Started = new ProjectStartedEventArgs(1, null, null, "p", "t", null, null, new BuildEventContext(BuildEventContext.InvalidNodeId, BuildEventContext.InvalidTargetId, BuildEventContext.InvalidProjectContextId, BuildEventContext.InvalidTaskId));
                project1Started.BuildEventContext = new BuildEventContext(1, 1, 1, 1);
                es.Consume(project1Started);
                TargetStartedEventArgs targetStarted1 = new TargetStartedEventArgs(null, null, "t", null, null);
                targetStarted1.BuildEventContext = project1Started.BuildEventContext;
                // TargetStarted Event
                es.Consume(targetStarted1);

                TaskStartedEventArgs taskStarted1 = new TaskStartedEventArgs(null, null, null, null, "task");
                taskStarted1.BuildEventContext = project1Started.BuildEventContext;
                // TaskStarted Event 
                es.Consume(taskStarted1);

                BuildMessageEventArgs messsage1 = new BuildMessageEventArgs(null, null, null, MessageImportance.High);
                messsage1.BuildEventContext = project1Started.BuildEventContext;
                // Message Event
                es.Consume(messsage1);
                TaskFinishedEventArgs taskFinished1 = new TaskFinishedEventArgs(null, null, null, null, "task", true);
                taskFinished1.BuildEventContext = project1Started.BuildEventContext;
                // TaskFinished Event
                es.Consume(taskFinished1);

                TargetFinishedEventArgs targetFinished1 = new TargetFinishedEventArgs(null, null, "t", null, null, true);
                targetFinished1.BuildEventContext = project1Started.BuildEventContext;
                // TargetFinished Event
                es.Consume(targetFinished1);

                ProjectStartedEventArgs project2Started = new ProjectStartedEventArgs(2, null, null, "p2", "t2", null, null, project1Started.BuildEventContext);
                //Project Started Event
                project2Started.BuildEventContext = new BuildEventContext(2, 2, 2, 2);
                es.Consume(project2Started);
                TargetStartedEventArgs targetStarted2 = new TargetStartedEventArgs(null, null, "t2", null, null);
                targetStarted2.BuildEventContext = project2Started.BuildEventContext;
                // TargetStarted Event
                es.Consume(targetStarted2);

                TaskStartedEventArgs taskStarted2 = new TaskStartedEventArgs(null, null, null, null, "task2");
                taskStarted2.BuildEventContext = project2Started.BuildEventContext;
                // TaskStarted Event 
                es.Consume(taskStarted2);

                BuildMessageEventArgs messsage2 = new BuildMessageEventArgs(null, null, null, MessageImportance.High);
                messsage2.BuildEventContext = project2Started.BuildEventContext;
                // Message Event
                es.Consume(messsage2);
                TaskFinishedEventArgs taskFinished2 = new TaskFinishedEventArgs(null, null, null, null, "task2", true);
                taskFinished2.BuildEventContext = project2Started.BuildEventContext;
                // TaskFinished Event
                es.Consume(taskFinished2);

                TargetFinishedEventArgs targetFinished2 = new TargetFinishedEventArgs(null, null, "t2", null, null, true);
                targetFinished2.BuildEventContext = project2Started.BuildEventContext;
                // TargetFinished Event
                es.Consume(targetFinished2);

                ProjectFinishedEventArgs finished2 = new ProjectFinishedEventArgs(null, null, "p2", true);
                finished2.BuildEventContext = project2Started.BuildEventContext;
                // ProjectFinished Event
                es.Consume(finished2);            // BuildFinished Event

                ProjectFinishedEventArgs finished1 = new ProjectFinishedEventArgs(null, null, "p", true);
                finished1.BuildEventContext = project1Started.BuildEventContext;
                // ProjectFinished Event
                es.Consume(finished1);            // BuildFinished Event
                es.Consume(new BuildFinishedEventArgs("bf",
                                                         null, true));
                // Log so far
                string actualLog = sc.ToString();

                Console.WriteLine("==");
                Console.WriteLine(sc.ToString());
                Console.WriteLine("==");

                // Verify that the log has perf summary
                // Project perf summary
                Assert.IsTrue(actualLog.Contains(prjPerfString));
                // Target perf summary
                Assert.IsTrue(actualLog.Contains(targetPerfString));
                // Task Perf summary
                Assert.IsTrue(actualLog.Contains(taskPerfString));

                // Clear the log obtained so far
                sc.Clear();

                // BuildStarted event
                es.Consume(new BuildStartedEventArgs("bs", null));
                // BuildFinished 
                es.Consume(new BuildFinishedEventArgs("bf",
                                                         null, true));
                // Log so far
                actualLog = sc.ToString();

                Console.WriteLine("==");
                Console.WriteLine(sc.ToString());
                Console.WriteLine("==");

                // Verify that the log doesn't have perf summary
                Assert.IsFalse(actualLog.Contains(prjPerfString));
                Assert.IsFalse(actualLog.Contains(targetPerfString));
                Assert.IsFalse(actualLog.Contains(taskPerfString));
            }
        }
        public void ResetConsoleLoggerState_Initialize()
        {
            // Create an event source
            EventSourceSink es = new EventSourceSink();
            //Create a simulated console
            SimulatedConsole sc = new SimulatedConsole();

            // error and warning string for 1 error and 1 warning
            // errorString = 1 Error(s)
            // warningString = 1 Warning(s)
            string errorString = ResourceUtilities.FormatResourceString("ErrorCount", 1);
            string warningString = ResourceUtilities.FormatResourceString("WarningCount", 1);

            // Create a ConsoleLogger with Normal verbosity
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Normal,
                                                sc.Write, sc.SetColor, sc.ResetColor);
            // Initialize ConsoleLogger
            L.Initialize(es);

            // BuildStarted Event
            es.Consume(new BuildStartedEventArgs("bs", null));

            // Introduce a warning
            BuildWarningEventArgs bwea = new BuildWarningEventArgs("VBC",
                            "31415", "file.vb", 42, 0, 0, 0,
                            "Some long message", "help", "sender");

            es.Consume(bwea);

            // Introduce an error
            BuildErrorEventArgs beea = new BuildErrorEventArgs("VBC",
                        "31415", "file.vb", 42, 0, 0, 0,
                        "Some long message", "help", "sender");

            es.Consume(beea);

            // NOTE: We don't call the es.RaiseBuildFinishedEvent(...) here as this 
            // would call ResetConsoleLoggerState and we will fail to detect if Initialize() 
            // is not calling it.

            // Log so far
            string actualLog = sc.ToString();

            Console.WriteLine("==");
            Console.WriteLine(sc.ToString());
            Console.WriteLine("==");

            // Verify that the log has correct error and warning string
            Assert.IsTrue(actualLog.Contains("<red>"));
            Assert.IsTrue(actualLog.Contains("<yellow>"));

            // Clear the log obtained so far
            sc.Clear();

            //Initilialize (This should call ResetConsoleLoggerState(...))
            L.Initialize(es);

            // BuildStarted event
            es.Consume(new BuildStartedEventArgs("bs", null));

            // BuildFinished 
            es.Consume(new BuildFinishedEventArgs("bf",
                                                     null, true));
            // Log so far
            actualLog = sc.ToString();

            Console.WriteLine("==");
            Console.WriteLine(sc.ToString());
            Console.WriteLine("==");

            // Verify that the error and warning from the previous build is not
            // reported in the subsequent build
            Assert.IsFalse(actualLog.Contains("<red>"));
            Assert.IsFalse(actualLog.Contains("<yellow>"));

            // errorString = 0 Error(s)
            errorString = ResourceUtilities.FormatResourceString("ErrorCount", 0);
            // warningString = 0 Warning(s)
            warningString = ResourceUtilities.FormatResourceString("WarningCount", 0);

            // Verify that the log has correct error and warning string
            Assert.IsTrue(actualLog.Contains(errorString));
            Assert.IsTrue(actualLog.Contains(warningString));
        }
        public void NestedProjectMinimal()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Minimal,
                                                sc.Write, sc.SetColor, sc.ResetColor);
            L.Initialize(es, 1);

            es.Consume(new BuildStartedEventArgs("bs", null));

            //Clear time dependant build started message
            sc.Clear();

            es.Consume(new ProjectStartedEventArgs("ps1", null, "fname1", "", null, null));

            es.Consume(new TargetStartedEventArgs("ts", null,
                                                     "trname", "fname", "tfile"));

            es.Consume(new ProjectStartedEventArgs("ps2", null, "fname2", "", null, null));

            Assert.AreEqual(string.Empty, sc.ToString());

            BuildErrorEventArgs beea = new BuildErrorEventArgs("VBC",
                        "31415", "file.vb", 42, 0, 0, 0,
                        "Some long message", "help", "sender");

            es.Consume(beea);

            Assert.AreEqual(
                "<cyan>" + BaseConsoleLogger.projectSeparatorLine + Environment.NewLine +
                ResourceUtilities.FormatResourceString("ProjectStartedPrefixForTopLevelProjectWithDefaultTargets", "fname1") + Environment.NewLine +
                                        Environment.NewLine + "<reset color>" +
                "<cyan>" + BaseConsoleLogger.projectSeparatorLine + Environment.NewLine +
                ResourceUtilities.FormatResourceString("ProjectStartedPrefixForNestedProjectWithDefaultTargets", "fname1", "fname2") + Environment.NewLine +
                                                      Environment.NewLine + "<reset color>" +
                "<red>" + "file.vb(42): VBC error 31415: Some long message" +
                                                      Environment.NewLine + "<reset color>",
                sc.ToString());
        }
        public void NestedProjectNormal()
        {
            EventSourceSink es = new EventSourceSink();
            SimulatedConsole sc = new SimulatedConsole();
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Normal,
                                                sc.Write, sc.SetColor, sc.ResetColor);
            L.Initialize(es);

            es.Consume(new BuildStartedEventArgs("bs", null));


            //Clear time dependant build started message
            string expectedOutput = null;
            string actualOutput = null;
            sc.Clear();

            es.Consume(new ProjectStartedEventArgs("ps1", null, "fname1", "", null, null));

            #region Check
            expectedOutput =
                        "<cyan>" + BaseConsoleLogger.projectSeparatorLine + Environment.NewLine +
                        ResourceUtilities.FormatResourceString("ProjectStartedPrefixForTopLevelProjectWithDefaultTargets", "fname1") + Environment.NewLine +
                        Environment.NewLine + "<reset color>";
            actualOutput = sc.ToString();

            Assert.AreEqual(expectedOutput, actualOutput);
            Console.WriteLine("1 [" + expectedOutput + "] [" + actualOutput + "]");
            sc.Clear();
            #endregion

            es.Consume(new TargetStartedEventArgs("ts", null,
                                                     "tarname", "fname", "tfile"));
            #region Check
            expectedOutput = String.Empty;
            actualOutput = sc.ToString();

            Console.WriteLine("2 [" + expectedOutput + "] [" + actualOutput + "]");
            Assert.AreEqual(expectedOutput, actualOutput);
            sc.Clear();
            #endregion

            es.Consume(new TaskStartedEventArgs("", "", "", "", "Exec"));
            es.Consume(new ProjectStartedEventArgs("ps2", null, "fname2", "", null, null));

            #region Check
            expectedOutput =
                "<cyan>" + ResourceUtilities.FormatResourceString("TargetStartedPrefix", "tarname") + Environment.NewLine + "<reset color>"
                + "<cyan>" + "    " + BaseConsoleLogger.projectSeparatorLine
                                          + Environment.NewLine +
                "    " + ResourceUtilities.FormatResourceString("ProjectStartedPrefixForNestedProjectWithDefaultTargets", "fname1", "fname2") + Environment.NewLine +
                Environment.NewLine + "<reset color>";
            actualOutput = sc.ToString();

            Console.WriteLine("3 [" + expectedOutput + "] [" + actualOutput + "]");
            Assert.AreEqual(expectedOutput, actualOutput);
            sc.Clear();
            #endregion

            es.Consume(new ProjectFinishedEventArgs("pf2", null, "fname2", true));
            es.Consume(new TaskFinishedEventArgs("", "", "", "", "Exec", true));

            #region Check
            expectedOutput = String.Empty;
            actualOutput = sc.ToString();

            Console.WriteLine("4 [" + expectedOutput + "] [" + actualOutput + "]");
            Assert.AreEqual(expectedOutput, actualOutput);
            sc.Clear();
            #endregion

            es.Consume(new TargetFinishedEventArgs("tf", null, "tarname", "fname", "tfile", true));

            #region Check
            expectedOutput = String.Empty;
            actualOutput = sc.ToString();

            Console.WriteLine("5 [" + expectedOutput + "] [" + actualOutput + "]");
            Assert.AreEqual(expectedOutput, actualOutput);
            sc.Clear();
            #endregion

            es.Consume(new ProjectFinishedEventArgs("pf1", null, "fname1", true));

            #region Check
            expectedOutput = String.Empty;
            actualOutput = sc.ToString();

            Console.WriteLine("6 [" + expectedOutput + "] [" + actualOutput + "]");
            Assert.AreEqual(expectedOutput, actualOutput);
            sc.Clear();
            #endregion

            es.Consume(new BuildFinishedEventArgs("bf", null, true));

            #region Check
            expectedOutput = "<green>" + Environment.NewLine + "bf" +
                        Environment.NewLine + "<reset color>" +
                "    " + ResourceUtilities.FormatResourceString("WarningCount", 0) +
                        Environment.NewLine + "<reset color>" +
                "    " + ResourceUtilities.FormatResourceString("ErrorCount", 0) +
                        Environment.NewLine + "<reset color>" +
                        Environment.NewLine;

            // Would like to add...
            //    + ResourceUtilities.FormatResourceString("TimeElapsed", String.Empty);
            // ...but this assumes that the time goes on the far right in every locale.

            actualOutput = sc.ToString().Substring(0, expectedOutput.Length);

            Console.WriteLine("7 [" + expectedOutput + "] [" + actualOutput + "]");
            Assert.AreEqual(expectedOutput, actualOutput);
            sc.Clear();
            #endregion

        }
Example #5
0
        public void ResetConsoleLoggerStateTestBasic()
        {
            // Create an event source
            EventSource es = new EventSource();
            //Create a simulated console
            SimulatedConsole sc = new SimulatedConsole();

            // error and warning string for 1 error and 1 warning
            // errorString = 1 Error(s)
            // warningString = 1 Warning(s)
            string errorString = ResourceUtilities.FormatResourceString("ErrorCount", 1);
            string warningString = ResourceUtilities.FormatResourceString("WarningCount", 1);

            // Create a ConsoleLogger with Normal verbosity
            ConsoleLogger L = new ConsoleLogger(LoggerVerbosity.Normal,
                                                sc.Write, sc.SetColor, sc.ResetColor);
            // Initialize ConsoleLogger
            L.Initialize(es);

            // BuildStarted Event
            es.RaiseBuildStartedEvent(null,
                          new BuildStartedEventArgs("bs", null));

            // Introduce a warning
            BuildWarningEventArgs bwea = new BuildWarningEventArgs("VBC",
                            "31415", "file.vb", 42, 0, 0, 0,
                            "Some long message", "help", "sender");

            es.RaiseWarningEvent(null, bwea);

            // Introduce an error
            BuildErrorEventArgs beea = new BuildErrorEventArgs("VBC",
                        "31415", "file.vb", 42, 0, 0, 0,
                        "Some long message", "help", "sender");

            es.RaiseErrorEvent(null, beea);

            // BuildFinished Event
            es.RaiseBuildFinishedEvent(null,
                          new BuildFinishedEventArgs("bf",
                                                     null, true));

            // Log so far
            string actualLog = sc.ToString();

            Console.WriteLine("==");
            Console.WriteLine(sc.ToString());
            Console.WriteLine("==");

            // Verify that the log has correct error and warning string
            Assertion.Assert(actualLog.Contains(errorString));
            Assertion.Assert(actualLog.Contains(warningString));
            Assertion.Assert(actualLog.Contains("<red>"));
            Assertion.Assert(actualLog.Contains("<yellow>"));

            // Clear the log obtained so far
            sc.Clear();

            // BuildStarted event
            es.RaiseBuildStartedEvent(null,
                         new BuildStartedEventArgs("bs", null));

            // BuildFinished 
            es.RaiseBuildFinishedEvent(null,
                          new BuildFinishedEventArgs("bf",
                                                     null, true));
            // Log so far
            actualLog = sc.ToString();

            Console.WriteLine("==");
            Console.WriteLine(sc.ToString());
            Console.WriteLine("==");

            // Verify that the error and warning from the previous build is not
            // reported in the subsequent build
            Assertion.Assert(!actualLog.Contains(errorString));
            Assertion.Assert(!actualLog.Contains(warningString));
            Assertion.Assert(!actualLog.Contains("<red>"));
            Assertion.Assert(!actualLog.Contains("<yellow>"));
            
            // errorString = 0 Error(s)
            // warningString = 0 Warning(s)
            errorString = ResourceUtilities.FormatResourceString("ErrorCount", 0);
            warningString = ResourceUtilities.FormatResourceString("WarningCount", 0);
            
            // Verify that the log has correct error and warning string
            Assertion.Assert(actualLog.Contains(errorString));
            Assertion.Assert(actualLog.Contains(warningString));
        }