Exemplo n.º 1
0
 public TextUI(ExtendedTextWriter writer, ConsoleOptions options)
 {
     _options   = options;
     _outWriter = writer ?? new ColorConsoleWriter(!options.NoColor);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Execute the tests in the assembly, passing in
        /// a list of arguments.
        /// </summary>
        /// <param name="args">Execution options</param>
        public int Execute(string[] args)
        {
            var options         = new NUnitLiteOptions(args);
            var callingAssembly = Assembly.GetCallingAssembly();

            var level = (InternalTraceLevel)Enum.Parse(typeof(InternalTraceLevel), options.InternalTraceLevel ?? "Off", true);

#if NETCF  // NETCF: Try to unify
            InitializeInternalTrace(callingAssembly.GetName().CodeBase, level);
#else
            InitializeInternalTrace(callingAssembly.Location, level);
#endif

            ExtendedTextWriter outWriter = null;
            if (options.OutFile != null)
            {
                outWriter = new ExtendedTextWrapper(new StreamWriter(Path.Combine(options.WorkDirectory, options.OutFile)));
                Console.SetOut(outWriter);
            }

            TextWriter errWriter = null;
            if (options.ErrFile != null)
            {
                errWriter = new StreamWriter(Path.Combine(options.WorkDirectory, options.ErrFile));
                Console.SetError(errWriter);
            }

            var _textUI = new TextUI(outWriter, options);

            if (!options.NoHeader)
            {
                _textUI.DisplayHeader();
            }

            if (options.ShowHelp)
            {
                _textUI.DisplayHelp();
                return(TextRunner.OK);
            }

            if (options.ErrorMessages.Count > 0)
            {
                _textUI.DisplayErrors(options.ErrorMessages);
                _textUI.DisplayHelp();

                return(TextRunner.INVALID_ARG);
            }

#if !PORTABLE
            if (options.InputFiles.Count > 0)
            {
                _textUI.DisplayError("Input assemblies may not be specified when using the NUnitLite AutoRunner");
                return(TextRunner.INVALID_ARG);
            }
#endif

            _textUI.DisplayTestFiles(new string[] { callingAssembly.GetName().Name });
            _textUI.DisplayRuntimeEnvironment();
            _textUI.DisplayRequestedOptions();

            if (options.WaitBeforeExit && options.OutFile != null)
            {
                _textUI.DisplayWarning("Ignoring /wait option - only valid for Console");
            }

            try
            {
                return(new TextRunner(_textUI, options).Execute(callingAssembly));
            }
            finally
            {
                if (options.WaitBeforeExit)
                {
                    _textUI.WaitForUser("Press Enter key to continue . . .");
                }

                if (outWriter != null)
                {
                    outWriter.Close();
                }

                if (errWriter != null)
                {
                    errWriter.Close();
                }
            }
        }
Exemplo n.º 3
0
 public TextUI(ExtendedTextWriter writer)
 {
     _outWriter = writer;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Execute a test run based on the aruments passed
        /// from Main.
        /// </summary>
        /// <param name="args">An array of arguments</param>
        public int Execute(string[] args)
        {
            // NOTE: Execute must be directly called from the
            // test assembly in order for the mechanism to work.

            _options = new ConsoleOptions(args);

            _workDirectory = _options.WorkDirectory;
            if (_workDirectory == null)
            {
                _workDirectory = NUnit.Env.DefaultWorkDirectory;
            }
            else if (!Directory.Exists(_workDirectory))
            {
                Directory.CreateDirectory(_workDirectory);
            }

#if !SILVERLIGHT
#if !NETCF
            if (_options.TeamCity)
            {
                _teamCity = new TeamCityEventListener();
            }
#endif

            if (_options.OutFile != null)
            {
                _outWriter = new ExtendedTextWriter(new StreamWriter(Path.Combine(_workDirectory, _options.OutFile)));
                Console.SetOut(_outWriter);
                ColorConsole.Enabled = false;
            }

            if (_options.ErrFile != null)
            {
                _errWriter = new StreamWriter(Path.Combine(_workDirectory, _options.ErrFile));
                Console.SetError(_errWriter);
            }
#endif
            if (_options.NoColor)
            {
                ColorConsole.Enabled = false;
            }

            if (!_options.NoHeader)
            {
                WriteHeader(_outWriter);
            }

            if (_options.ShowHelp)
            {
                WriteHelpText();
                return(OK);
            }

            if (_options.ErrorMessages.Count > 0)
            {
                foreach (string line in _options.ErrorMessages)
                {
                    _outWriter.WriteLine(line);
                }

                _options.WriteOptionDescriptions(_outWriter);

                return(INVALID_ARG);
            }
            Assembly callingAssembly = Assembly.GetCallingAssembly();

            // We must call this before creating the runner so that any internal logging is initialized
            var level = (InternalTraceLevel)Enum.Parse(typeof(InternalTraceLevel), _options.InternalTraceLevel ?? "Off", true);
#if NETCF  // NETCF: Try to unify
            InitializeInternalTrace(callingAssembly.GetName().CodeBase, level);
#else
            InitializeInternalTrace(callingAssembly.Location, level);
#endif

            _runner = new NUnitTestAssemblyRunner(new DefaultTestAssemblyBuilder());

            _outWriter.WriteLine(ColorStyle.SectionHeader, "Test Files:");

            DisplayRequestedOptions(_outWriter);

            WriteRuntimeEnvironment(_outWriter);

            if (_options.WaitBeforeExit && _options.OutFile != null)
            {
                _outWriter.WriteLine("Ignoring /wait option - only valid for Console");
            }

            var runSettings = MakeRunSettings(_options);

            TestFilter filter = CreateTestFilter(_options);

            try
            {
                foreach (string name in _options.InputFiles)
#if NETCF
                { _assemblies.Add(name.IndexOf(',') != -1 || (name.IndexOf('\\') == -1 && !Path.HasExtension(name)) ? Assembly.Load(name) : Assembly.LoadFrom(name)); }
#else
                { _assemblies.Add(Assembly.Load(name)); }
#endif

                if (_assemblies.Count == 0)
                {
                    _assemblies.Add(callingAssembly);
                }

                // TODO: For now, ignore all but first assembly
                Assembly assembly = _assemblies[0];

                // Randomizer.InitialSeed = _commandLineOptions.InitialSeed;

                if (_runner.Load(assembly, runSettings) != null)
                {
                    return(_options.Explore ? ExploreTests() : RunTests(filter));
                }

                var assemblyName = AssemblyHelper.GetAssemblyName(assembly);
                Console.WriteLine("No tests found in assembly {0}", assemblyName.Name);
                return(OK);
            }
            catch (FileNotFoundException ex)
            {
                _outWriter.WriteLine(ColorStyle.Error, ex.Message);
                return(FILE_NOT_FOUND);
            }
            catch (Exception ex)
            {
                _outWriter.WriteLine(ColorStyle.Error, ex.ToString());
                return(UNEXPECTED_ERROR);
            }
            finally
            {
                if (_options.OutFile == null)
                {
                    if (_options.WaitBeforeExit)
                    {
                        _outWriter.WriteLine(ColorStyle.Label, "Press Enter key to continue . . .");
                        Console.ReadLine();
                    }
                }
                else
                {
                    _outWriter.Close();
                }

                if (_options.ErrFile != null)
                {
                    _errWriter.Close();
                }
            }
        }