HasPartialMessage() public method

Determines if a partial matching message exists in the log.
public HasPartialMessage ( LogLevel level, string message ) : bool
level LogLevel Log level.
message string Log message.
return bool
Beispiel #1
0
        public void ArrangeInvalidConfigurationTest()
        {
            string testConfiguration = Path.GetTempFileName();

            File.WriteAllText(testConfiguration, "<xml");

            try
            {
                TestLogger   logger       = new TestLogger();
                FileArranger fileArranger = new FileArranger(testConfiguration, logger);

                bool success = fileArranger.Arrange(_testValidSourceFile1, null);

                Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
                Assert.IsTrue(logger.HasPartialMessage(LogLevel.Error, "Unable to load configuration file"));
            }
            finally
            {
                try
                {
                    File.Delete(testConfiguration);
                }
                catch
                {
                }
            }
        }
Beispiel #2
0
        public void ArrangeEmptyProjectTest()
        {
            TestLogger   logger       = new TestLogger();
            FileArranger fileArranger = new FileArranger(null, logger);

            string emptyProjectFile = Path.Combine(
                Path.GetTempPath(),
                Guid.NewGuid().ToString().Replace('-', '_') + ".csproj");

            File.WriteAllText(emptyProjectFile, "<Project></Project>");

            try
            {
                bool success = fileArranger.Arrange(emptyProjectFile, null);

                Assert.IsTrue(success, "Expected file to be arranged succesfully.");
                Assert.IsTrue(
                    logger.HasPartialMessage(LogLevel.Warning, "does not contain any supported source files"));
            }
            finally
            {
                try
                {
                    File.Delete(emptyProjectFile);
                }
                catch
                {
                }
            }
        }
Beispiel #3
0
        public void ArrangeNonExistantConfigurationTest()
        {
            TestLogger   logger       = new TestLogger();
            FileArranger fileArranger = new FileArranger("blahblahblahblah.xml", logger);

            bool success = fileArranger.Arrange(_testValidSourceFile1, null);

            Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
            Assert.IsTrue(logger.HasPartialMessage(LogLevel.Error, "Unable to load configuration file"));
        }
Beispiel #4
0
        public void ArrangeInvalidExtensionTest()
        {
            TestLogger   logger       = new TestLogger();
            FileArranger fileArranger = new FileArranger(null, logger);

            bool success = fileArranger.Arrange(_testInvalidExtensionFile, null);

            Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
            Assert.IsTrue(logger.HasPartialMessage(LogLevel.Warning, "No assembly is registered to handle file"));
        }
Beispiel #5
0
        public void ArrangeInvalidExtensionAssemblyTest()
        {
            string xml =
                @"<CodeConfiguration xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
                    <Handlers>
                        <ProjectHandler Parser='NArrange.Core.MSBuildProjectParser'>
                            <ProjectExtensions>
                                <Extension Name='csproj'/>
                            </ProjectExtensions>
                        </ProjectHandler>
                        <SourceHandler Assembly='NArrange.BlahBlahBlahBlah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'>
                            <SourceExtensions>
                                <Extension Name='cs'/>
                            </SourceExtensions>
                        </SourceHandler>
                    </Handlers>
                </CodeConfiguration>";

            string testConfiguration = Path.GetTempFileName();

            File.WriteAllText(testConfiguration, xml);

            try
            {
                TestLogger logger = new TestLogger();

                FileArranger fileArranger = new FileArranger(testConfiguration, logger);

                bool success = fileArranger.Arrange(_testValidSourceFile1, null);

                Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
                Assert.IsTrue(logger.HasPartialMessage(LogLevel.Error, "Unable to load configuration file"));
            }
            finally
            {
                try
                {
                    File.Delete(testConfiguration);
                }
                catch
                {
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Tests the files.
        /// </summary>
        /// <param name="inputDir">The input dir.</param>
        /// <param name="logger">The logger.</param>
        private static void TestFiles(string inputDir, ILogger logger)
        {
            if (!Directory.Exists(inputDir))
            {
                logger.LogMessage(LogLevel.Error, "Test directory {0} does not exist", inputDir);
                Environment.Exit(Fail);
            }

            string arrangedDir = Path.Combine(inputDir, "Arranged");
            if (Directory.Exists(arrangedDir))
            {
                Directory.Delete(arrangedDir, true);
            }
            Directory.CreateDirectory(arrangedDir);

            FileInfo[] allSourceFiles = GetSourceFileNames(inputDir);
            logger.LogMessage(LogLevel.Info, "Testing with {0} source files...", allSourceFiles.Length);

            int preprocessorCount = 0;
            int uncompiledCount = 0;
            int successCount = 0;
            int failedCount = 0;

            TestLogger testLogger = new TestLogger();
            FileArranger fileArranger = new FileArranger(null, testLogger);

            foreach (FileInfo sourceFile in allSourceFiles)
            {
                string initialSource = File.ReadAllText(sourceFile.FullName, Encoding.Default);

                CompilerResults initialResults = CompileSourceFile(sourceFile, initialSource);

                CompilerError error = TestUtilities.GetCompilerError(initialResults);
                if (error == null)
                {
                    logger.LogMessage(LogLevel.Trace, "Succesfully compiled {0}", sourceFile.FullName);

                    //
                    // Arrange the source code file
                    //
                    testLogger.Clear();
                    string outputFile = Path.Combine(arrangedDir, sourceFile.Name);
                    bool success = false;
                    try
                    {
                        success = fileArranger.Arrange(sourceFile.FullName, outputFile);
                    }
                    catch (Exception ex)
                    {
                        logger.LogMessage(
                            LogLevel.Error,
                            "Unable to arrange {0}.  {1}",
                            sourceFile.Name,
                            ex.Message);
                        failedCount++;
                    }

                    if (success)
                    {
                        logger.LogMessage(LogLevel.Info, "Arrange successful.");
                    }
                    else if (testLogger.HasPartialMessage(LogLevel.Warning, "preprocessor"))
                    {
                        logger.LogMessage(LogLevel.Trace, "File is unhandled.");
                        preprocessorCount++;
                    }
                    else
                    {
                        foreach (TestLogger.TestLogEvent logEvent in testLogger.Events)
                        {
                            logger.LogMessage(logEvent.Level, logEvent.Message);
                        }

                        logger.LogMessage(LogLevel.Error, "Unable to arrange {0}.", sourceFile.Name);
                        failedCount++;
                    }

                    if (success)
                    {
                        string arrangedSource = File.ReadAllText(outputFile, Encoding.Default);
                        CompilerResults arrangedResults = CompileSourceFile(
                            new FileInfo(outputFile), arrangedSource);

                        CompilerError arrangedError = TestUtilities.GetCompilerError(arrangedResults);
                        if (arrangedError == null)
                        {
                            logger.LogMessage(LogLevel.Trace, "Succesfully compiled arranged file {0}", outputFile);
                            try
                            {
                                bool assembliesMatch = CompareAssemblies(
                                    initialResults.CompiledAssembly, arrangedResults.CompiledAssembly, logger);
                                if (assembliesMatch)
                                {
                                    successCount++;
                                }
                                else
                                {
                                    logger.LogMessage(LogLevel.Error, "Arranged assembly differs.");
                                    failedCount++;
                                }
                            }
                            catch (ReflectionTypeLoadException ex)
                            {
                                logger.LogMessage(
                                    LogLevel.Error,
                                    "Failed to load one or more types. {0}, {1}",
                                    outputFile,
                                    ex.ToString());
                                failedCount++;
                            }
                        }
                        else
                        {
                            logger.LogMessage(
                                LogLevel.Error,
                                "Failed to compile arranged file {0}, {1}",
                                outputFile,
                                arrangedError.ToString());
                            failedCount++;
                        }
                    }
                }
                else
                {
                    logger.LogMessage(LogLevel.Error, "Failed to compile {0}", sourceFile.FullName);
                    uncompiledCount++;
                }
            }

            logger.LogMessage(LogLevel.Info, "Unsupported - preprocessor: " + preprocessorCount.ToString());
            logger.LogMessage(LogLevel.Info, "Uncompiled: " + uncompiledCount.ToString());
            logger.LogMessage(LogLevel.Info, "Success: " + successCount.ToString());
            logger.LogMessage(LogLevel.Info, "Failed: " + failedCount.ToString());
        }
        public void ArrangeNonExistantConfigurationTest()
        {
            TestLogger logger = new TestLogger();
            FileArranger fileArranger = new FileArranger("blahblahblahblah.xml", logger);

            bool success = fileArranger.Arrange(_testValidSourceFile1, null);

            Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
            Assert.IsTrue(logger.HasPartialMessage(LogLevel.Error, "Unable to load configuration file"));
        }
        public void ArrangeInvalidExtensionTest()
        {
            TestLogger logger = new TestLogger();
            FileArranger fileArranger = new FileArranger(null, logger);

            bool success = fileArranger.Arrange(_testInvalidExtensionFile, null);

            Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
            Assert.IsTrue(logger.HasPartialMessage(LogLevel.Warning, "No assembly is registered to handle file"));
        }
        public void ArrangeInvalidExtensionAssemblyTest()
        {
            string xml =
                @"<CodeConfiguration xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
                    <Handlers>
                        <ProjectHandler Parser='NArrange.Core.MSBuildProjectParser'>
                            <ProjectExtensions>
                                <Extension Name='csproj'/>
                            </ProjectExtensions>
                        </ProjectHandler>
                        <SourceHandler Assembly='NArrange.BlahBlahBlahBlah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'>
                            <SourceExtensions>
                                <Extension Name='cs'/>
                            </SourceExtensions>
                        </SourceHandler>
                    </Handlers>
                </CodeConfiguration>";

            string testConfiguration = Path.GetTempFileName();
            File.WriteAllText(testConfiguration, xml);

            try
            {
                TestLogger logger = new TestLogger();

                FileArranger fileArranger = new FileArranger(testConfiguration, logger);

                bool success = fileArranger.Arrange(_testValidSourceFile1, null);

                Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
                Assert.IsTrue(logger.HasPartialMessage(LogLevel.Error, "Unable to load configuration file"));
            }
            finally
            {
                try
                {
                    File.Delete(testConfiguration);
                }
                catch
                {
                }
            }
        }
Beispiel #10
0
        public void ArrangeInvalidConfigurationTest()
        {
            string testConfiguration = Path.GetTempFileName();
            File.WriteAllText(testConfiguration, "<xml");

            try
            {
                TestLogger logger = new TestLogger();
                FileArranger fileArranger = new FileArranger(testConfiguration, logger);

                bool success = fileArranger.Arrange(_testValidSourceFile1, null);

                Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
                Assert.IsTrue(logger.HasPartialMessage(LogLevel.Error, "Unable to load configuration file"));
            }
            finally
            {
                try
                {
                    File.Delete(testConfiguration);
                }
                catch
                {
                }
            }
        }
Beispiel #11
0
        public void ArrangeEmptyProjectTest()
        {
            TestLogger logger = new TestLogger();
            FileArranger fileArranger = new FileArranger(null, logger);

            string emptyProjectFile = Path.Combine(
                Path.GetTempPath(),
                Guid.NewGuid().ToString().Replace('-', '_') + ".csproj");
            File.WriteAllText(emptyProjectFile, "<Project></Project>");

            try
            {
                bool success = fileArranger.Arrange(emptyProjectFile, null);

                Assert.IsTrue(success, "Expected file to be arranged succesfully.");
                Assert.IsTrue(
                    logger.HasPartialMessage(LogLevel.Warning, "does not contain any supported source files"));
            }
            finally
            {
                try
                {
                    File.Delete(emptyProjectFile);
                }
                catch
                {
                }
            }
        }
Beispiel #12
0
        public void ArrangeReadOnlySourceFileTest()
        {
            TestLogger logger = new TestLogger();

            File.SetAttributes(_testValidSourceFile1, FileAttributes.ReadOnly);

            try
            {
                bool success = Arrange(logger, _testValidSourceFile1);

                Assert.IsFalse(success, "Expected file to not be arranged succesfully.");
                Assert.IsTrue(logger.HasPartialMessage(LogLevel.Warning, "is read-only"));
                Assert.IsTrue(logger.HasMessage(LogLevel.Verbose, "0 files written."));
            }
            finally
            {
                File.SetAttributes(_testValidSourceFile1, FileAttributes.Normal);
            }
        }