public void FilesToScanTest()
        {
            ScannerTask target = new ScannerTask();

            SetAndGetFilesToScan(target, null);
            SetAndGetFilesToScan(target, new ITaskItem[] { });
            SetAndGetFilesToScan(target, new ITaskItem[] { new MockTaskItem("foo"), new MockTaskItem("bar") });
        }
        public void TermTablesTest()
        {
            ScannerTask target = new ScannerTask();

            SetAndGetTermTables(target, null);
            SetAndGetTermTables(target, "");
            SetAndGetTermTables(target, "z:\fileName.ext");
            SetAndGetTermTables(target, "first;second;third");
        }
        public void ExecuteWithHost()
        {
            ScannerTask target = new ScannerTask();

            // Create the term tables and target files.
            string termFile1 = Utilities.CreateTempFile(CreateTermTableXml("countries", 2, "Geopolitical", "comment"));
            string termFile2 = Utilities.CreateTempFile(CreateTermTableXml("shoot", 3, "Profanity", "comment"));

            string scanFile1 = Utilities.CreateTempTxtFile("the word 'countries' should produce a hit");
            string scanFile2 = Utilities.CreateTempTxtFile("the word 'shoot' should produce a hit");

            // Create the project that will execute the task.
            Microsoft.Build.Evaluation.Project project = Utilities.SetupMSBuildProject(new string[] { scanFile1, scanFile2 }, new string[] { termFile1, termFile2 });

            // Set up a custom logger to capture the output.
            MockLogger logger = new MockLogger();

            project.ProjectCollection.RegisterLogger(logger);
            int errors   = 0;
            int warnings = 0;
            int messages = 0;

            logger.OnError += delegate(object sender, BuildErrorEventArgs args)
            {
                ++errors;
            };
            logger.OnWarning += delegate(object sender, BuildWarningEventArgs args)
            {
                ++warnings;
            };
            logger.OnMessage += delegate(object sender, BuildMessageEventArgs args)
            {
                ++messages;
            };

            Microsoft.Build.Construction.ProjectTaskElement task = Utilities.GetScannerTask(project);

            // Set the host object for the task.
            int            hostUpdates = 0;
            MockHostObject host        = new MockHostObject();

            host.OnAddResult += delegate(object sender, MockHostObject.AddResultArgs args)
            {
                ++hostUpdates;
            };
            logger.OnBuildStart += delegate(object sender, BuildStartedEventArgs args)
            {
                project.ProjectCollection.HostServices.RegisterHostObject(project.FullPath, "AfterBuild", "ScannerTask", host);
            };

            project.Build("AfterBuild");

            Assert.AreEqual(0, errors, "Build did not log expected number of errors.");
            Assert.AreEqual(0, warnings, "Build did not log expected number of warnings.");
            Assert.AreEqual(4, messages, "Build did not log expected number of messages.");
            Assert.AreEqual(2, hostUpdates, "Build did not send expected number of results to host.");
        }
 private void SetAndGetFilesToScan(ScannerTask target, ITaskItem[] val)
 {
     target.FilesToScan = val;
     if (val == null)
     {
         Assert.IsNull(target.FilesToScan, "FilesToScan did not return expected value (null).");
     }
     else
     {
         Assert.AreEqual(val.Length, target.FilesToScan.Length, "FilesToScan returned array of wrong length.");
         for (int i = 0; i < val.Length; ++i)
         {
             Assert.AreEqual(val[i], target.FilesToScan[i], "Item " + i.ToString() + " of FilesToScan is incorrect.");
         }
     }
 }
        public void ExecuteWithoutHost()
        {
            ScannerTask target = new ScannerTask();

            // Create the term tables and target files.
            string termFile1 = Utilities.CreateTempFile(CreateTermTableXml("countries", 2, "Geopolitical", "comment"));
            string termFile2 = Utilities.CreateTempFile(CreateTermTableXml("shoot", 3, "Profanity", "comment"));

            string scanFile1 = Utilities.CreateTempTxtFile("the word 'countries' should produce a hit");
            string scanFile2 = Utilities.CreateTempTxtFile("the word 'shoot' should produce a hit");

            // Create the project that will execute the task.
            Microsoft.Build.Evaluation.Project project = Utilities.SetupMSBuildProject(new string[] { scanFile1, scanFile2 }, new string[] { termFile1, termFile2 });

            // Set up a custom logger to capture the output.
            MockLogger logger = new MockLogger();

            project.ProjectCollection.RegisterLogger(logger);
            int errors   = 0;
            int warnings = 0;
            int messages = 0;

            logger.OnError += delegate(object sender, BuildErrorEventArgs args)
            {
                ++errors;
            };
            logger.OnWarning += delegate(object sender, BuildWarningEventArgs args)
            {
                ++warnings;
            };
            logger.OnMessage += delegate(object sender, BuildMessageEventArgs args)
            {
                ++messages;
            };

            project.Build("AfterBuild");

            Assert.AreEqual(0, errors, "Build did not log expected number of errors.");
            Assert.AreEqual(2, warnings, "Build did not log expected number of warnings.");
            Assert.AreEqual(2, messages, "Build did not log expected number of messages.");
        }
 private void SetAndGetTermTables(ScannerTask target, string val)
 {
     target.TermTables = val;
     Assert.AreEqual(val, target.TermTables, "TermTables did not return expected value (" + val + ").");
 }