📄 standardoutputfromprocesstestfixture.cs
字号:
//
// SharpDevelop NAnt add-in
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Matthew Ward (mrward@users.sourceforge.net)
using ICSharpCode.NAntAddIn;
using NUnit.Framework;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace ICSharpCode.NAntAddIn.Tests
{
/// <summary>
/// Runs a process that returns some text on the standard output.
/// </summary>
[TestFixture]
//[Ignore("Ignoring since need to run ConsoleApp.exe")]
public class StandardOutputFromProcessTestFixture
{
int preTestThreadCount;
/// <summary>
/// Initialises each test.
/// </summary>
[SetUp]
public void Init()
{
GetPreTestThreadCount();
}
[TearDown]
public void TearDown()
{
//If we use Output.ReadLine then ThreadPoolCompletion Port threads are
//used rendering a threadcount test useless.
//CheckPostTestThreadCount();
}
/// <summary>
/// Runs a process expecting a single line of output.
/// </summary>
[Test]
public void SingleLineOfOutput()
{
ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = Path.GetDirectoryName(Config.ConsoleAppFilename);
string echoText = "Test";
string expectedOutput = String.Concat(echoText, "\r\n");
runner.Run(Config.ConsoleAppFilename, String.Concat("-echo:", echoText));
runner.WaitForExit();
Assert.IsFalse(runner.IsRunning, "IsRunning should be false.");
Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect.");
Assert.AreEqual(expectedOutput, runner.StandardOutput, "Should have some output.");
Assert.AreEqual(String.Empty, runner.StandardError, "Should not be any error output.");
}
/// <summary>
/// Runs a process and makes sure it does not return any output.
/// </summary>
[Test]
public void NoOutput()
{
ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = Path.GetDirectoryName(Config.ConsoleAppFilename);
runner.Run(Config.ConsoleAppFilename);
runner.WaitForExit();
Assert.IsFalse(runner.IsRunning, "IsRunning should be false.");
Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect.");
Assert.AreEqual(String.Empty, runner.StandardOutput, "Should not be any output.");
Assert.AreEqual(String.Empty, runner.StandardError, "Should not be any error output.");
}
/// <summary>
/// The process that is run tries to send such a large amount of
/// data that it fills the standard output's buffer.
/// </summary>
[Test]
public void LargeAmountOfOutput()
{
ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = Path.GetDirectoryName(Config.ConsoleAppFilename);
string filename = "test.txt";
string fullFilename = Path.Combine(runner.WorkingDirectory, filename);
try
{
string outputText = GetOutputText();
CreateTextFile(fullFilename, outputText);
runner.Run(Config.ConsoleAppFilename, String.Concat("-file:", filename));
bool exited = runner.WaitForExit(500);
Assert.IsTrue(exited, "App did not exit.");
Assert.IsFalse(runner.IsRunning, "IsRunning should be false.");
Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect.");
Assert.AreEqual(outputText, runner.StandardOutput, "Should have some output.");
Assert.AreEqual(String.Empty, runner.StandardError, "Should not be any error output.");
}
finally
{
if (File.Exists(fullFilename)) {
File.Delete(fullFilename);
}
}
}
/// <summary>
/// The process that is run tries to send such a large amount of
/// data that it fills the standard output's buffer.
/// </summary>
[Test]
public void LargeAmountOfErrorOutput()
{
ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = Path.GetDirectoryName(Config.ConsoleAppFilename);
string filename = "test.txt";
string fullFilename = Path.Combine(runner.WorkingDirectory, filename);
try
{
string outputText = GetOutputText();
CreateTextFile(fullFilename, outputText);
runner.Run(Config.ConsoleAppFilename, String.Concat("-error.file:", filename));
bool exited = runner.WaitForExit(500);
Assert.IsTrue(exited, "App did not exit.");
Assert.IsFalse(runner.IsRunning, "IsRunning should be false.");
Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect.");
Assert.AreEqual(String.Empty, runner.StandardOutput, "Should not be any output.");
Assert.AreEqual(outputText, runner.StandardError, "Should have some error output.");
}
finally
{
if (File.Exists(fullFilename)) {
File.Delete(fullFilename);
}
}
}
/// <summary>
/// Runs a process expecting a single line of output written
/// to standard error.
/// </summary>
[Test]
public void SingleLineOfErrorOutput()
{
ProcessRunner runner = new ProcessRunner();
runner.WorkingDirectory = Path.GetDirectoryName(Config.ConsoleAppFilename);
string echoText = "Test";
string expectedOutput = String.Concat(echoText, "\r\n");
runner.Run(Config.ConsoleAppFilename, String.Concat("-error.echo:", echoText));
runner.WaitForExit();
Assert.AreEqual(0, runner.ExitCode, "Exit code is incorrect.");
Assert.IsFalse(runner.IsRunning, "IsRunning should be false.");
Assert.AreEqual(String.Empty, runner.StandardOutput, "Should not be any output.");
Assert.AreEqual(expectedOutput, runner.StandardError, "Should have some error output.");
}
/// <summary>
/// Creates a UTF8 text file.
/// </summary>
/// <param name="filename">The filename of the text file.</param>
/// <param name="outputText">The text to store in the file.</param>
void CreateTextFile(string filename, string outputText)
{
FileStream stream = File.Create(filename);
byte[] bytes = UnicodeEncoding.UTF8.GetBytes(outputText);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
/// <summary>
/// Gets the output text that the console app will
/// attempt to send us back.
/// </summary>
/// <returns></returns>
string GetOutputText()
{
StringBuilder outputText = new StringBuilder();
for (int i = 0; i < 300; ++i) {
outputText.Append("i=");
outputText.Append(i.ToString());
outputText.Append(" A line of text.\r\n");
}
return outputText.ToString();
}
/// <summary>
/// Reads the number of threads before the test is run.
/// </summary>
void GetPreTestThreadCount()
{
Process process = Process.GetCurrentProcess();
preTestThreadCount = process.Threads.Count;
}
/// <summary>
/// Reads the number of threads after the test is run.
/// </summary>
void CheckPostTestThreadCount()
{
GC.Collect();
int postThreadCount = Process.GetCurrentProcess().Threads.Count;
Assert.AreEqual(preTestThreadCount, postThreadCount, "Thread count mismatch.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -