<commit_before>﻿// ***********************************************************************
// Copyright (c) 2009-2014 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace NUnit.Framework.TestHarness
{
    /// <summary>
    /// ConsoleRunner provides a text-based user interface to the
    /// test harness, similar to that of nunit-console.
    /// </summary>
    public class ConsoleRunner
    {
        #region Console Runner Return Codes

        public static readonly int OK = 0;
        public static readonly int INVALID_ARG = -1;
        public static readonly int FILE_NOT_FOUND = -2;
        public static readonly int FIXTURE_NOT_FOUND = -3;
        public static readonly int UNEXPECTED_ERROR = -100;

        #endregion

        #region Instance Fields

        private FrameworkDriver driver;
        private CommandLineOptions options;

        private TextWriter outWriter = Console.Out;
        private TextWriter errorWriter = Console.Error;

        private string workDirectory;

        #endregion

        #region Constructor

        public ConsoleRunner(CommandLineOptions commandlineOptions)
        {
            this.options = commandlineOptions;

            string assemblyPath = Path.GetFullPath(options.AssemblyName);
            AppDomain testDomain = AppDomain.CurrentDomain;
            if (options.RunInSeparateAppDomain)
                testDomain = CreateDomain(Path.GetDirectoryName(assemblyPath));

            var driverSettings = options.CreateDriverSettings();
            this.driver = new FrameworkDriver(assemblyPath, testDomain, driverSettings);

            this.workDirectory = options.WorkDirectory;
            if (this.workDirectory == null)
                this.workDirectory = Environment.CurrentDirectory;
            else if (!Directory.Exists(this.workDirectory))
                Directory.CreateDirectory(this.workDirectory);
        }

        #endregion

        #region Execute Method

        public int Execute()
        {
            DisplayRequestedOptions();

            string filter = TestFilterBuilder.CreateTestFilter(options);

            if (options.Explore)
                return ExploreTests(driver, filter);
            else
                return RunTests(driver, filter);
        }

        #endregion

        #region Helper Methods

        private int ExploreTests(FrameworkDriver driver, string filter)
        {
            IDictionary settings = new Dictionary<string, object>();

            XmlNode testNode = driver.ExploreTests(filter);

            if (testNode.Name == "error")
            {
                DisplayUnexpectedErrorMessage(testNode);
                return ConsoleRunner.UNEXPECTED_ERROR;
            }

            if (testNode.Attributes["runstate"].Value == "NotRunnable")
            {
                DisplayNotRunnableMessage(testNode);
                return ConsoleRunner.FILE_NOT_FOUND;
            }

            string exploreFile = options.ExploreFile;
            XmlTextWriter testWriter = exploreFile != null && exploreFile.Length > 0
                ? new XmlTextWriter(Path.Combine(workDirectory, exploreFile), System.Text.Encoding.UTF8)
                : new XmlTextWriter(Console.Out);
            testWriter.Formatting = Formatting.Indented;
            testNode.WriteTo(testWriter);
            testWriter.Close();

            if (exploreFile != null)
                Console.WriteLine("Tests saved as {0}", options.ExploreFile);

            return ConsoleRunner.OK;
        }

        private int RunTests(FrameworkDriver driver, string testFilter)
        {
            XmlNode loadReport = driver.Load();
            if (loadReport.Name == "error")
            {
                DisplayUnexpectedErrorMessage(loadReport);
                return ConsoleRunner.UNEXPECTED_ERROR;
            }

            TextWriter savedOut = Console.Out;
            TextWriter savedError = Console.Error;
            XmlNode resultNode;

            try
            {
                if (options.OutFile != null)
                    Console.SetOut(new StreamWriter(Path.Combine(workDirectory, options.OutFile)));

                if (options.ErrFile != null)
                    Console.SetError(new StreamWriter(Path.Combine(workDirectory, options.ErrFile)));

                resultNode = driver.Run(testFilter);
            }
            finally
            {
                Console.Out.Flush();
                Console.SetOut(savedOut);
                Console.Error.Flush();
                Console.SetError(savedError);
            }

            if (resultNode.Name == "error")
            {
                DisplayUnexpectedErrorMessage(resultNode);
                return ConsoleRunner.UNEXPECTED_ERROR;
            }

            if (resultNode.Attributes["runstate"].Value == "NotRunnable")
            {
                DisplayNotRunnableMessage(resultNode);
                return ConsoleRunner.FILE_NOT_FOUND;
            }

            string v3ResultFile = Path.Combine(workDirectory, options.V3ResultFile);
            NUnit3TestResultWriter nunit3ResultWriter = new NUnit3TestResultWriter();
            nunit3ResultWriter.WriteResultFile(resultNode, v3ResultFile);
            //XmlTextWriter nunit3ResultWriter = new XmlTextWriter(v3ResultFile, System.Text.Encoding.UTF8);
            //nunit3ResultWriter.Formatting = Formatting.Indented;
            //resultNode.WriteTo(nunit3ResultWriter);
            //nunit3ResultWriter.Close();

            string v2ResultFile = Path.Combine(workDirectory, options.V2ResultFile);
            NUnit2TestResultWriter nunit2ResultWriter = new NUnit2TestResultWriter();
            nunit2ResultWriter.WriteResultFile(resultNode, v2ResultFile);

            if (!options.DisplayTeamCityServiceMessages)
                new ResultReporter(resultNode).ReportResults();

            if (options.OutFile != null)
                Console.WriteLine("Test standard output saved as {0}", Path.Combine(workDirectory, options.OutFile));
            if (options.ErrFile != null)
                Console.WriteLine("Test error output saved as {0}", Path.Combine(workDirectory, options.ErrFile));
            Console.WriteLine("NUnit3 Result File Saved as {0}", v3ResultFile);
            Console.WriteLine("NUnit2 Result File Saved as {0}", v2ResultFile);

            return OK;
        }

        private static void DisplayNotRunnableMessage(XmlNode resultNode)
        {
            var messageText = "Not Runnable: ";

            var messageNode = resultNode.SelectSingleNode("reason/message");
            if (messageNode != null) // First look for a message element
            {
                messageText += messageNode.InnerText;
            }
            else // No message Element, so try property value
            {
                var propNode = resultNode.SelectSingleNode("properties/property[@name='_SKIPREASON']");
                if (propNode != null)<commit_msg>Return number of failed tests as exit code<commit_after>﻿// ***********************************************************************
// Copyright (c) 2009-2014 <NAME>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace NUnit.Framework.TestHarness
{
    /// <summary>
    /// ConsoleRunner provides a text-based user interface to the
    /// test harness, similar to that of nunit-console.
    /// </summary>
    public class ConsoleRunner
    {
        #region Console Runner Return Codes

        public static readonly int OK = 0;
        public static readonly int INVALID_ARG = -1;
        public static readonly int FILE_NOT_FOUND = -2;
        public static readonly int FIXTURE_NOT_FOUND = -3;
        public static readonly int UNEXPECTED_ERROR = -100;

        #endregion

        #region Instance Fields

        private FrameworkDriver driver;
        private CommandLineOptions options;

        private TextWriter outWriter = Console.Out;
        private TextWriter errorWriter = Console.Error;

        private string workDirectory;

        #endregion

        #region Constructor

        public ConsoleRunner(CommandLineOptions commandlineOptions)
        {
            this.options = commandlineOptions;

            string assemblyPath = Path.GetFullPath(options.AssemblyName);
            AppDomain testDomain = AppDomain.CurrentDomain;
            if (options.RunInSeparateAppDomain)
                testDomain = CreateDomain(Path.GetDirectoryName(assemblyPath));

            var driverSettings = options.CreateDriverSettings();
            this.driver = new FrameworkDriver(assemblyPath, testDomain, driverSettings);

            this.workDirectory = options.WorkDirectory;
            if (this.workDirectory == null)
                this.workDirectory = Environment.CurrentDirectory;
            else if (!Directory.Exists(this.workDirectory))
                Directory.CreateDirectory(this.workDirectory);
        }

        #endregion

        #region Execute Method

        public int Execute()
        {
            DisplayRequestedOptions();

            string filter = TestFilterBuilder.CreateTestFilter(options);

            if (options.Explore)
                return ExploreTests(driver, filter);
            else
                return RunTests(driver, filter);
        }

        #endregion

        #region Helper Methods

        private int ExploreTests(FrameworkDriver driver, string filter)
        {
            IDictionary settings = new Dictionary<string, object>();

            XmlNode testNode = driver.ExploreTests(filter);

            if (testNode.Name == "error")
            {
                DisplayUnexpectedErrorMessage(testNode);
                return ConsoleRunner.UNEXPECTED_ERROR;
            }

            if (testNode.Attributes["runstate"].Value == "NotRunnable")
            {
                DisplayNotRunnableMessage(testNode);
                return ConsoleRunner.FILE_NOT_FOUND;
            }

            string exploreFile = options.ExploreFile;
            XmlTextWriter testWriter = exploreFile != null && exploreFile.Length > 0
                ? new XmlTextWriter(Path.Combine(workDirectory, exploreFile), System.Text.Encoding.UTF8)
                : new XmlTextWriter(Console.Out);
            testWriter.Formatting = Formatting.Indented;
            testNode.WriteTo(testWriter);
            testWriter.Close();

            if (exploreFile != null)
                Console.WriteLine("Tests saved as {0}", options.ExploreFile);

            return ConsoleRunner.OK;
        }

        private int RunTests(FrameworkDriver driver, string testFilter)
        {
            XmlNode loadReport = driver.Load();
            if (loadReport.Name == "error")
            {
                DisplayUnexpectedErrorMessage(loadReport);
                return ConsoleRunner.UNEXPECTED_ERROR;
            }

            TextWriter savedOut = Console.Out;
            TextWriter savedError = Console.Error;
            XmlNode resultNode;

            try
            {
                if (options.OutFile != null)
                    Console.SetOut(new StreamWriter(Path.Combine(workDirectory, options.OutFile)));

                if (options.ErrFile != null)
                    Console.SetError(new StreamWriter(Path.Combine(workDirectory, options.ErrFile)));

                resultNode = driver.Run(testFilter);
            }
            finally
            {
                Console.Out.Flush();
                Console.SetOut(savedOut);
                Console.Error.Flush();
                Console.SetError(savedError);
            }

            if (resultNode.Name == "error")
            {
                DisplayUnexpectedErrorMessage(resultNode);
                return ConsoleRunner.UNEXPECTED_ERROR;
            }

            if (resultNode.Attributes["runstate"].Value == "NotRunnable")
            {
                DisplayNotRunnableMessage(resultNode);
                return ConsoleRunner.FILE_NOT_FOUND;
            }

            string v3ResultFile = Path.Combine(workDirectory, options.V3ResultFile);
            NUnit3TestResultWriter nunit3ResultWriter = new NUnit3TestResultWriter();
            nunit3ResultWriter.WriteResultFile(resultNode, v3ResultFile);
            //XmlTextWriter nunit3ResultWriter = new XmlTextWriter(v3ResultFile, System.Text.Encoding.UTF8);
            //nunit3ResultWriter.Formatting = Formatting.Indented;
            //resultNode.WriteTo(nunit3ResultWriter);
            //nunit3ResultWriter.Close();

            string v2ResultFile = Path.Combine(workDirectory, options.V2ResultFile);
            NUnit2TestResultWriter nunit2ResultWriter = new NUnit2TestResultWriter();
            nunit2ResultWriter.WriteResultFile(resultNode, v2ResultFile);

            if (!options.DisplayTeamCityServiceMessages)
                new ResultReporter(resultNode).ReportResults();

            if (options.OutFile != null)
                Console.WriteLine("Test standard output saved as {0}", Path.Combine(workDirectory, options.OutFile));
            if (options.ErrFile != null)
                Console.WriteLine("Test error output saved as {0}", Path.Combine(workDirectory, options.ErrFile));
            Console.WriteLine("NUnit3 Result File Saved as {0}", v3ResultFile);
            Console.WriteLine("NUnit2 Result File Saved as {0}", v2ResultFile);

            return int.Parse(resultNode.Attributes["failed"].Value);
        }

        private static void DisplayNotRunnableMessage(XmlNode resultNode)
        {
            var messageText = "Not Runnable: ";

            var messageNode = resultNode.SelectSingleNode("reason/message");
            if (messageNode != null) // First look for a message element
            {
                messageText += messageNode.InnerText;
            }
            else // No message Element, so try property value
            {
                var propNode = resultNode.SelectSingleNode("properties/property[@name='_SKIPREASON']");
                if (propNode != null)
{'fraction_non_alphanumeric': 0.09611053624688051, 'fraction_numerical': 0.005599853916854343, 'mean_word_length': 2.232343104465867, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2985946', 'n_tokens_mistral': 4037, 'n_tokens_neox': 3795, 'n_words': 1074}