Exemplo n.º 1
0
        TestMessageLogger CreateLogger(string projectXml)
        {
            var logger = new TestMessageLogger ();
            var result = Build (projectXml, logger);

            if (!result) {
                logger.DumpMessages ();
                Assert.Fail ("Build failed");
            }

            return logger;
        }
        public static ReadOnlyCollection<Message> AnalyzeAssemblies(string referenceAssemblyFile, string newAssemblyFile)
        {
            using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceAssemblyFile)))
            {
                using (PEReader newAssembly = new PEReader(File.OpenRead(newAssemblyFile)))
                {
                    TestMessageLogger logger = new TestMessageLogger();
                    Analyzer analyzer = new Analyzer(referenceAssembly, newAssembly, logger);
                    analyzer.Run();

                    return logger.RawMessages;
                }
            }
        }
Exemplo n.º 3
0
        public static ReadOnlyCollection <Message> AnalyzeAssemblies(string referenceAssemblyFile, string newAssemblyFile)
        {
            using (PEReader referenceAssembly = new PEReader(File.OpenRead(referenceAssemblyFile)))
            {
                using (PEReader newAssembly = new PEReader(File.OpenRead(newAssemblyFile)))
                {
                    TestMessageLogger logger   = new TestMessageLogger();
                    Analyzer          analyzer = new Analyzer(referenceAssembly, newAssembly, logger);
                    analyzer.Run();

                    return(logger.RawMessages);
                }
            }
        }
Exemplo n.º 4
0
        public void TestRunTargetTwice()
        {
            string documentString = @"<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
			<Target Name=""Foo"">
				<Message Text=""Foo ran""/>
			</Target>
			<Target Name=""Main"">
				<MSBuild Projects=""$(MSBuildProjectFile)"" Targets=""Foo;Foo"" />
			</Target>

		</Project>"        ;

            var filepath = Path.GetTempFileName();

            try {
                File.WriteAllText(filepath, documentString);

                var engine  = new Engine(Consts.BinPath);
                var project = engine.CreateNewProject();
                project.Load(filepath);

                var logger = new TestMessageLogger();
                engine.RegisterLogger(logger);

                var result = project.Build("Main");
                if (!result)
                {
                    logger.DumpMessages();
                    Assert.Fail("Build failed, see the logs");
                }

                Assert.AreEqual(1, logger.NormalMessageCount, "Expected number of messages");
                logger.CheckLoggedMessageHead("Foo ran", "A1");

                Assert.AreEqual(0, logger.NormalMessageCount, "Extra messages found");
                Assert.AreEqual(0, logger.WarningMessageCount, "Extra warning messages found");

                Assert.AreEqual(2, logger.TargetStarted, "TargetStarted count");
                Assert.AreEqual(2, logger.TargetFinished, "TargetFinished count");

                Assert.IsTrue(result);
            } finally {
                File.Delete(filepath);
            }
        }
Exemplo n.º 5
0
        public void TestTargetReturns()
        {
            engine = new Engine (Consts.BinPath);
            project = engine.CreateNewProject ();
            project.Load (Path.Combine ("Test", Path.Combine ("resources", "TestReturns.csproj")));

            var logger = new TestMessageLogger ();
            engine.RegisterLogger (logger);

            bool result = project.Build ("Main");
            if (!result) {
                logger.DumpMessages ();
                Assert.Fail ("Build failed");
            }

            logger.CheckLoggedMessageHead ("Result: Bar", "A1");

            Assert.AreEqual (0, logger.NormalMessageCount, "Unexpected extra messages found");
        }
        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);
            }
        }
Exemplo n.º 7
0
        private void Verify(
            IBinarySkimmer skimmer,
            IEnumerable<string> additionalTestFiles,
            bool useDefaultPolicy,
            bool expectToPass)
        {
            var targets = new List<string>();
            string ruleName = skimmer.GetType().Name;
            string testFilesDirectory = ruleName;
            testFilesDirectory = Path.Combine(Environment.CurrentDirectory, "FunctionalTestsData", testFilesDirectory);
            testFilesDirectory = Path.Combine(testFilesDirectory, expectToPass ? "Pass" : "Fail");

            Assert.True(Directory.Exists(testFilesDirectory));

            foreach (string target in Directory.GetFiles(testFilesDirectory, "*", SearchOption.AllDirectories))
            {
                if (AnalyzeCommand.ValidAnalysisFileExtensions.Contains(Path.GetExtension(target)))
                {
                    targets.Add(target);
                }
            }

            if (additionalTestFiles != null)
            {
                foreach (string additionalTestFile in additionalTestFiles)
                {
                    targets.Add(additionalTestFile);
                }
            }

            var context = new BinaryAnalyzerContext();
            var logger = new TestMessageLogger();
            context.Logger = logger;
            PropertyBag policy = null;

            if (useDefaultPolicy)
            {
                policy = new PropertyBag();
            }
            context.Policy = policy;

            skimmer.Initialize(context);

            foreach (string target in targets)
            {
                PE pe = new PE(target);
                if (!pe.IsPEFile) { continue; }

                context = CreateContext(logger, policy, target);

                context.Rule = skimmer;

                string reasonForNotAnalyzing;
                if (skimmer.CanAnalyze(context, out reasonForNotAnalyzing) != AnalysisApplicability.ApplicableToSpecifiedTarget)
                {
                    continue;
                }

                skimmer.Analyze(context);
            }

            HashSet<string> expected = expectToPass ? logger.PassTargets : logger.FailTargets;
            HashSet<string> other = expectToPass ? logger.FailTargets : logger.PassTargets;
            HashSet<string> configErrors = logger.ConfigurationErrorTargets;

            string expectedText = expectToPass ? "success" : "failure";
            string actualText = expectToPass ? "failed" : "succeeded";
            var sb = new StringBuilder();

            foreach (string target in targets)
            {
                if (expected.Contains(target))
                {
                    expected.Remove(target);
                    continue;
                }
                bool missingEntirely = !other.Contains(target);

                if (missingEntirely && !expectToPass && target.Contains("Pdb"))
                {
                    // Missing pdbs provoke configuration errors;
                    if (configErrors.Contains(target))
                    {
                        missingEntirely = false;
                        configErrors.Remove(target);
                        continue;
                    }
                }

                if (missingEntirely)
                {
                    sb.AppendLine("Expected '" + ruleName + "' " + expectedText + " but saw no result at all for file: " + Path.GetFileName(target));
                }
                else
                {
                    other.Remove(target);
                    sb.AppendLine("Expected '" + ruleName + "' " + expectedText + " but check " + actualText + " for: " + Path.GetFileName(target));
                }
            }

            if (sb.Length > 0)
            {
                _testOutputHelper.WriteLine(sb.ToString());
            }

            Assert.Equal(0, sb.Length);
            Assert.Equal(0, expected.Count);
            Assert.Equal(0, other.Count);
        }
Exemplo n.º 8
0
        private void VerifyNotApplicable(
            IBinarySkimmer skimmer,
            HashSet<string> notApplicableConditions,
            AnalysisApplicability expectedApplicability = AnalysisApplicability.NotApplicableToSpecifiedTarget)
        {
            string ruleName = skimmer.GetType().Name;
            string testFilesDirectory = ruleName;
            testFilesDirectory = Path.Combine(Environment.CurrentDirectory, "FunctionalTestsData", testFilesDirectory);
            testFilesDirectory = Path.Combine(testFilesDirectory, "NotApplicable");

            var context = new BinaryAnalyzerContext();

            HashSet<string> targets = GetTestFilesMatchingConditions(notApplicableConditions);

            if (Directory.Exists(testFilesDirectory))
            {
                foreach (string target in Directory.GetFiles(testFilesDirectory, "*", SearchOption.AllDirectories))
                {
                    if (AnalyzeCommand.ValidAnalysisFileExtensions.Contains(Path.GetExtension(target)))
                    {
                        targets.Add(target);
                    }
                }
            }

            var logger = new TestMessageLogger();
            context.Logger = logger;

            var sb = new StringBuilder();

            foreach (string target in targets)
            {
                string extension = Path.GetExtension(target);
                if (!AnalyzeCommand.ValidAnalysisFileExtensions.Contains(extension))
                {
                    Assert.True(false, "Test file with unexpected extension encountered: " + target);
                }

                context = CreateContext(logger, null, target);
                if (!context.PE.IsPEFile) { continue; }

                context.Rule = skimmer;

                string reasonForNotAnalyzing;
                AnalysisApplicability applicability;
                applicability = skimmer.CanAnalyze(context, out reasonForNotAnalyzing);
                if (applicability != expectedApplicability)
                {
                    sb.AppendLine("CanAnalyze did not indicate target was invalid for analysis (return was " +
                        applicability + "): " +
                        Path.GetFileName(target));
                    continue;
                }
            }

            if (sb.Length > 0)
            {
                _testOutputHelper.WriteLine(sb.ToString());
            }

            Assert.Equal(0, sb.Length);
        }
Exemplo n.º 9
0
        private BinaryAnalyzerContext CreateContext(TestMessageLogger logger, PropertyBag policy, string target)
        {
            var context = new BinaryAnalyzerContext();
            context.Logger = logger;
            context.Policy = policy;

            if (target != null)
            {
                context.TargetUri = new Uri(target);
            }

            return context;
        }