// NAnt - A .NET build tool
// Copyright (C) 2001-2002 Gerry Shaw
//
// 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
//
// Gerry Shaw (gerry_shaw@yahoo.com)
using System;
using System.IO;
using System.Text;
using System.Xml;
using NUnit.Framework;
using NAnt.Core;
using Tests.NAnt.Core.Util;
namespace Tests.NAnt.Core {
///
/// Base class for running build files and checking results.
///
///
/// Provides support for quickly running a build and capturing the output.
///
public abstract class BuildTestBase {
#region Private Instance Fields
private string _tempDirName = null;
#endregion Private Instance Fields
#region Public Instance Properties
///
/// Gets the temporary directory name for this test case.
///
///
/// The temporary directory name for this test case.
///
///
/// Should be in the form %temp%\ClassName (ex. c:\temp\Tests.NAnt.Core.BuildTestBase).
///
public string TempDirName {
get { return _tempDirName; }
}
///
/// Gets the temporary directory for this test case.
///
///
/// The temporary directory for this test case.
///
///
/// Should be in the form %temp%\ClassName (ex. c:\temp\Tests.NAnt.Core.BuildTestBase).
///
public DirectoryInfo TempDirectory {
get { return new DirectoryInfo(_tempDirName); }
}
#endregion Public Instance Properties
#region Public Instance Methods
///
/// Runs the XML as a NAnt project and returns the console output as a
/// string.
///
/// XML representing the build file contents.
///
/// The console output.
///
public string RunBuild(string xml) {
return RunBuild(xml, Level.Info);
}
///
/// Runs the XML as a NAnt project and returns the console output as a
/// string.
///
/// XML representing the build file contents.
/// A to which all build events will be dispatched.
///
/// The console output.
///
public string RunBuild(string xml, IBuildListener listener) {
return RunBuild(xml, Level.Info, listener);
}
///
/// Runs the XML as NAnt project and returns the console output as a
/// string.
///
/// XML representing the build file contents.
/// The build output threshold.
///
/// The console output.
///
public string RunBuild(string xml, Level level) {
Project project = CreateFilebasedProject(xml, level);
return ExecuteProject(project);
}
///
/// Runs the XML as NAnt project and returns the console output as a
/// string.
///
/// XML representing the build file contents.
/// The build output threshold.
/// A to which all build events will be dispatched.
///
/// The console output.
///
public string RunBuild(string xml, Level level, IBuildListener listener) {
Project project = CreateFilebasedProject(xml, level);
//use Project.AttachBuildListeners to attach.
IBuildListener[] listners = {listener};
project.AttachBuildListeners(new BuildListenerCollection(listners));
// execute the project
return ExecuteProject(project);
}
///
/// Executes the project and returns the console output as a string.
///
/// The project to execute.
///
/// The console output.
///
///
/// Any exception that is thrown as part of the execution of the
/// is wrapped in a .
///
public static string ExecuteProject(Project p) {
using (ConsoleCapture c = new ConsoleCapture()) {
string output = null;
try {
p.Execute();
} catch (BuildException e) {
output = c.Close();
throw new TestBuildException("Error Executing Project", output, e);
} finally {
if (output == null) {
output = c.Close();
}
}
return output;
}
}
///
/// Executes a task and returns the console output as a string.
///
/// The task to execute.
///
/// The console output.
///
///
/// Any exception that is thrown as part of the execution of the
/// is wrapped in a .
///
public static string ExecuteTask(Task task) {
using (ConsoleCapture c = new ConsoleCapture()) {
string output = null;
try {
task.Execute();
} catch (Exception e) {
output = c.Close();
throw new TestBuildException("Error Executing Task", output, e);
} finally {
if (output == null) {
output = c.Close();
}
}
return output;
}
}
///
/// Creates a new with output level .
///
/// The XML of the build file
///
/// A new with output level .
///
public Project CreateFilebasedProject(string xml) {
return CreateFilebasedProject(xml, Level.Info);
}
///
/// Creates a new with the given output level.
///
/// The XML of the build file
/// The build output level.
///
/// A new with the specified output level.
///
public Project CreateFilebasedProject(string xml, Level level) {
// create the build file in the temp folder
string buildFileName = Path.Combine(TempDirName, "test.build");
TempFile.CreateWithContents(xml, buildFileName);
return new Project(buildFileName, level, 0);
}
///
/// Creates an empty project and loads it
/// with a new project.
///
///
/// The new .
///
protected Project CreateEmptyProject() {
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.AppendChild(doc.CreateElement("project"));
return new Project(doc, Level.Info, 0);
}
///
/// Creates a temporary file in the temporary directory of the test.
///
/// The filename, should not be absolute.
///
/// The full path to the temporary file.
///
///
/// The file is created and existance is checked.
///
public string CreateTempFile(string name) {
return CreateTempFile(name, null);
}
///
/// Creates a tempfile in the test temp directory.
///
/// The filename, should not be absolute.
/// The content of the file.
///
/// The full path to the new file.
///
///
/// The file is created and existance is checked.
///
public string CreateTempFile(string name, string contents) {
string filename = Path.Combine(TempDirName, name);
if (Path.IsPathRooted(name)) {
filename = name;
}
if (contents == null) {
return TempFile.Create(filename);
}
return TempFile.CreateWithContents(contents, filename);
}
///
/// Creates a temporary directory.
///
/// The name of the directory to create (name only, no path info).
///
/// The full path to the temp directory.
///
///
/// The directory is created and existance is checked.
///
public string CreateTempDir(string name) {
return TempDir.Create(Path.Combine(TempDirName, name));
}
#endregion Public Instance Methods
#region Protected Instance Methods
///
/// No need to add SetUp attribute to overriden method.
/// Super classes that override SetUp must call the base class first.
///
protected virtual void SetUp() {
_tempDirName = TempDir.Create(this.GetType().FullName);
}
///
/// This method will be called by NUnit for setup.
///
[SetUp]
protected void NUnitSetUp() {
SetUp();
}
///
/// Classes that derive from and override
/// must call this method on the base class last.
///
[TearDown]
protected virtual void TearDown() {
TempDir.Delete(TempDirName);
}
#endregion Protected Instance Methods
}
}