//-------------------------------------------------------------------------------------
// <copyright file='NunitProjectTests.cs' company='Jonno'>
// Copyright (c) Paul Johnson 2009 (email:paulmichael.johnson@gmail.com)
// Code is released under The Code Project Open License (CPOL).
// </copyright>
//-------------------------------------------------------------------------------------
namespace Jonno.AddIns.Entities.UnitTests
{
using System.Collections.Generic;
using System.IO;
using Jonno.Utilities;
using NUnit.Framework;
using Rhino.Mocks;
public class NunitProjectTests
{
private const string NunitLocation = @"C:\Program Files\NUnit 2.5.2\bin\net-2.0\nunit.exe";
private const string TestFolder = @"C:\LogicTestsUnlikelyFolderNameToExistAAAAAAAAA\";
private const string NonTestFolder = @"D:\LogicTestsUnlikelyFolderNameToExistAAAAAAAAA\";
private TestProjectFactory factory;
[SetUp]
public void Setup()
{
this.CreateTestFolder();
this.DeleteAnyFileFromTestFolder();
factory = new TestProjectFactory();
}
[TearDown]
public void TearDown()
{
this.DeleteAnyFileFromTestFolder();
try
{
Directory.Delete(TestFolder);
}
catch
{
}
}
[Test]
public void CreateNunitProjectFileAndRunIt()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
list.Add(TestFolder + @"Jonno.AddIns.ugh\bin\Debug\Jonno.AddIns.ugh.dll");
list.Add(TestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
list.Add(TestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
Expect.Call(() => processMock.Kill("nunit"));
Expect.Call(() => processMock.Start(
NunitLocation,
"\"" + TestFolder + sut.FileName + "\" /run"));
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there now is a file
var files = Directory.GetFiles(TestFolder);
Assert.That(files.GetLength(0), Is.EqualTo(1));
Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));
using (var sr = File.OpenText(files[0]))
{
Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Settings activeconfig=\"NunitHelper\" />"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));
foreach (var path in list)
{
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", path)));
}
Assert.That(sr.ReadLine(), Is.EqualTo(" </Config>"));
Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));
sr.Close();
}
}
[Test]
public void CreateNunitProjectFileAndRunItDifferentDirectoriesShouldRetrunThosebeforeTheDifferentOne()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
list.Add(TestFolder + @"Jonno.AddIns.ugh\bin\Debug\Jonno.AddIns.ugh.dll");
list.Add(NonTestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
list.Add(TestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
Expect.Call(() => processMock.Kill("nunit"));
Expect.Call(() => processMock.Start(
NunitLocation,
"\"" + TestFolder + sut.FileName + "\" /run"));
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there now is a file
var files = Directory.GetFiles(TestFolder);
Assert.That(files.GetLength(0), Is.EqualTo(1));
Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));
using (var sr = File.OpenText(files[0]))
{
Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Settings activeconfig=\"NunitHelper\" />"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[0])));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[1])));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[3])));
Assert.That(sr.ReadLine(), Is.EqualTo(" </Config>"));
Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));
sr.Close();
}
}
[Test]
public void CreateNunitProjectFileAndRunItDifferentDirectoriesShouldRetrunThoseWithCommonPaths()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
list.Add(TestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.ugh.dll");
list.Add(NonTestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
list.Add(TestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
Expect.Call(() => processMock.Kill("nunit"));
Expect.Call(() => processMock.Start(
NunitLocation,
"\"" + TestFolder + sut.FileName + "\" /run"));
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there now is a file
var files = Directory.GetFiles(TestFolder);
Assert.That(files.GetLength(0), Is.EqualTo(1));
Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));
using (var sr = File.OpenText(files[0]))
{
Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Settings activeconfig=\"NunitHelper\" />"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[0])));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[1])));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[3])));
Assert.That(sr.ReadLine(), Is.EqualTo(" </Config>"));
Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));
sr.Close();
}
}
[Test]
public void CreateNunitProjectFileAndRunItCommonPathDoesNotExistShouldDoNothing()
{
// check that the folder is at first empty
Assert.That(Directory.Exists(NonTestFolder), Is.EqualTo(false));
var list = new List<string>();
list.Add(NonTestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.Meh.dll");
list.Add(NonTestFolder + @"Jonno.AddIns.Blah\bin\Debug\Jonno.AddIns.ugh.dll");
list.Add(NonTestFolder + @"Jonno.AddIns.Msss\bin\Debug\Jonno.AddIns.Msss.dll");
list.Add(NonTestFolder + @"Jonno.AddIns.XXX\bin\Debug\Jonno.AddIns.xxx.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// recheck
Assert.That(Directory.Exists(NonTestFolder), Is.EqualTo(false));
}
[Test]
public void CreateNunitProjectFileAndRunItWithOneEntry()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
list.Add(TestFolder + @"Jonno.AddIns.Meh.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
Expect.Call(() => processMock.Kill("nunit"));
Expect.Call(() => processMock.Start(
NunitLocation,
"\"" + TestFolder + sut.FileName + "\" /run"));
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there now is a file
var files = Directory.GetFiles(TestFolder);
Assert.That(files.GetLength(0), Is.EqualTo(1));
Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));
using (var sr = File.OpenText(files[0]))
{
Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Settings activeconfig=\"NunitHelper\" />"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));
foreach (var path in list)
{
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", path)));
}
Assert.That(sr.ReadLine(), Is.EqualTo(" </Config>"));
Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));
sr.Close();
}
}
[Test]
public void CreateNunitProjectFileAndRunItWithTwoEntrys()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
list.Add(TestFolder + @"Jonno.AddIns.Meh.dll");
list.Add(TestFolder + @"Jonno.AddIns.Blah.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
Expect.Call(() => processMock.Kill("nunit"));
Expect.Call(() => processMock.Start(
NunitLocation,
"\"" + TestFolder + sut.FileName + "\" /run"));
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there now is a file
var files = Directory.GetFiles(TestFolder);
Assert.That(files.GetLength(0), Is.EqualTo(1));
Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));
using (var sr = File.OpenText(files[0]))
{
Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Settings activeconfig=\"NunitHelper\" />"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));
foreach (var path in list)
{
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", path)));
}
Assert.That(sr.ReadLine(), Is.EqualTo(" </Config>"));
Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));
sr.Close();
}
}
[Test]
public void CreateNunitProjectFileAndRunItWithTwoDifferentEntrys()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
list.Add(TestFolder + @"Jonno.AddIns.Meh.dll");
list.Add(NonTestFolder + @"Jonno.AddIns.Blah.dll");
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
Expect.Call(() => processMock.Kill("nunit"));
Expect.Call(() => processMock.Start(
NunitLocation,
"\"" + TestFolder + sut.FileName + "\" /run"));
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there now is a file
var files = Directory.GetFiles(TestFolder);
Assert.That(files.GetLength(0), Is.EqualTo(1));
Assert.That(files[0], Is.EqualTo(TestFolder + sut.FileName));
using (var sr = File.OpenText(files[0]))
{
Assert.That(sr.ReadLine(), Is.EqualTo("<NUnitProject>"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Settings activeconfig=\"NunitHelper\" />"));
Assert.That(sr.ReadLine(), Is.EqualTo(" <Config name=\"NunitHelper\" binpathtype=\"Auto\">"));
Assert.That(sr.ReadLine(), Is.EqualTo(string.Format(" <assembly path=\"{0}\" />", list[0])));
Assert.That(sr.ReadLine(), Is.EqualTo(" </Config>"));
Assert.That(sr.ReadLine(), Is.EqualTo("</NUnitProject>"));
sr.Close();
}
}
[Test]
public void CreateNunitProjectFileAndRunIt_NullListDoesNothing()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
List<string> list = null;
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there is not a file
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
}
[Test]
public void CreateNunitProjectFileAndRunIt_EmptyListDoesNothing()
{
// check that the folder is at first empty
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
var list = new List<string>();
var mocks = new MockRepository();
var processMock = mocks.StrictMock<Processes>();
var sut = this.factory.Create("nunit.framework");
sut.Process = processMock;
mocks.ReplayAll();
sut.CreateAndRun(list, NunitLocation);
mocks.VerifyAll();
// check that there is not a file
Assert.That(Directory.GetFiles(TestFolder).GetLength(0), Is.EqualTo(0));
}
private void CreateTestFolder()
{
try
{
Directory.CreateDirectory(TestFolder);
}
catch
{
}
}
private void DeleteAnyFileFromTestFolder()
{
var files = Directory.GetFiles(TestFolder);
foreach (string file in files)
{
File.Delete(file);
}
}
}
}