Skip to content

Commit

Permalink
#36 - put package list and version parse under tests
Browse files Browse the repository at this point in the history
Added some basic tests around building the list of installed packages and put some logic and a custom exception in for the version parsing from chocolately /? output, then switched to a regex match
  • Loading branch information
tarwn committed Feb 14, 2013
1 parent 74843b9 commit b1cdb8b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Chocolatey Explorer/Chocolatey.Explorer.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -121,6 +121,7 @@
<Compile Include="Powershell\IRunAsync.cs" />
<Compile Include="Powershell\IRunSync.cs" />
<Compile Include="Services\ChocolateyLibDirHelper.cs" />
<Compile Include="Services\ChocolateyVersionUnknownException.cs" />
<Compile Include="Services\FileStorageService\LocalFileSystemStorageService.cs" />
<Compile Include="Services\FileStorageService\IFileStorageService.cs" />
<Compile Include="Services\ICacheable.cs" />
Expand Down
15 changes: 9 additions & 6 deletions Chocolatey Explorer/Services/ChocolateyLibDirHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Chocolatey.Explorer.Services
/// chocolatey lib directory, specified by the setting
/// "ChocolateyLibDirectory".
/// </summary>
class ChocolateyLibDirHelper
public class ChocolateyLibDirHelper
{
private static readonly ILog log = LogManager.GetLogger(typeof(ChocolateyLibDirHelper));

Expand All @@ -24,7 +24,7 @@ class ChocolateyLibDirHelper
private List<Package> _instaledPackages;
private readonly IChocolateyService _chocolateyService;
private readonly IFileStorageService _fileStorageService;
private string chocoVersion;
private string _chocoVersion;

public ChocolateyLibDirHelper() : this(new ChocolateyService(), new LocalFileSystemStorageService()) { }

Expand Down Expand Up @@ -55,16 +55,19 @@ public IList<Package> ReloadFromDir()
}
//add chocolatey by default because else this won't work anyway
_chocolateyService.Help();
var chocoPackage = new Package { Name = "chocolatey", InstalledVersion = chocoVersion };
var chocoPackage = new Package { Name = "chocolatey", InstalledVersion = _chocoVersion };
_instaledPackages.Add(chocoPackage);
return _instaledPackages;
}

private void VersionChangeFinished(string version)
{
var versionseparator = new string[] {"Version:"};
var installseparator = new string[] { "Install" };
chocoVersion = version.Split(versionseparator, StringSplitOptions.None)[1].Split(installseparator, StringSplitOptions.None)[0].Trim().Replace("'", "");
var match = Regex.Match(version, "Version:[ ]+'([0-9\\.]+)'");

if (!match.Success)
throw new ChocolateyVersionUnknownException(version);

_chocoVersion = match.Groups[1].Value;
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions Chocolatey Explorer/Services/ChocolateyVersionUnknownException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chocolatey.Explorer.Services
{
public class ChocolateyVersionUnknownException : Exception
{
public ChocolateyVersionUnknownException(string versionString) : base("Unknown input received for version: " + versionString) { }
}
}
1 change: 1 addition & 0 deletions Chocolatey.Explorer.Test/Chocolatey.Explorer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="Model\TestPackageVersion.cs" />
<Compile Include="Model\TestSource.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\TestChocolateyLibDirHelper.cs" />
<Compile Include="Services\TestPackageVersionService.cs" />
<Compile Include="Services\TestPackagesService.cs" />
<Compile Include="Services\TestChocolateyService.cs" />
Expand Down
105 changes: 105 additions & 0 deletions Chocolatey.Explorer.Test/Services/TestChocolateyLibDirHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chocolatey.Explorer.Services;
using Rhino.Mocks;
using Chocolatey.Explorer.Services.FileStorageService;
using System.IO;

namespace Chocolatey.Explorer.Test.Services
{
[TestFixture]
public class TestChocolateyLibDirHelper
{
[Test]
[ExpectedException()]
public void IfReloadFromDirWithInvalidDirectoryThenThrowsException()
{
var fileStorageService = MockRepository.GenerateMock<IFileStorageService>();
fileStorageService.Stub(fss => fss.GetDirectories(Arg<string>.Is.Anything)).Throw(new DirectoryNotFoundException());
var helper = new ChocolateyLibDirHelper(MockRepository.GenerateMock<IChocolateyService>(), fileStorageService);

var result = helper.ReloadFromDir();

// expects some sort of exception to match current behavior - test should be latered if this behavior is not actually expected :)
}

[Test]
[ExpectedException()]
public void IfReloadFromDirWithInvalidEnvironmentVariableThenThrowsException()
{
var fileStorageService = MockRepository.GenerateMock<IFileStorageService>();
fileStorageService.Stub(fss => fss.GetDirectories(Arg<string>.Is.Anything)).Throw(new Exception("Posing as HREsult-based exception that environment would throw, per the way the logic is currently written"));
var helper = new ChocolateyLibDirHelper(MockRepository.GenerateMock<IChocolateyService>(), fileStorageService);

var result = helper.ReloadFromDir();

// expects some sort of exception to match current behavior - test should be latered if this behavior is not actually expected :)
}

[Test]
public void IfReloadFromDirWithEmptyDirectoryThenReturnsListWithChocolatelyOnly()
{
var fileStorageService = MockRepository.GenerateMock<IFileStorageService>();
fileStorageService.Stub(fss => fss.GetDirectories(Arg<string>.Is.Anything)).Return(new string[] { });
var helper = new ChocolateyLibDirHelper(MockRepository.GenerateMock<IChocolateyService>(), fileStorageService);

var result = helper.ReloadFromDir();

Assert.AreEqual("chocolatey", result.Single().Name);
}

[Test]
[ExpectedException(typeof(ChocolateyVersionUnknownException))]
public void IfReloadFromDirAndHelpTextIsUnrecognizedThenThrowsChocoVersionUnknownException()
{
var fileStorageService = MockRepository.GenerateMock<IFileStorageService>();
fileStorageService.Stub(fss => fss.GetDirectories(Arg<string>.Is.Anything)).Return(new string[] { });
var chocolatelyService = new FakeChocolateyService() {
ExpectedOutputFromHelp = "not a valid chocolatey version string"
};
var helper = new ChocolateyLibDirHelper(chocolatelyService, fileStorageService);

var result = helper.ReloadFromDir();

// expect the version exception
}

[Test]
public void IfReloadFromDirAndHelpTextIsCorrectPatternThenChocolateyPackageContainsProperVersion()
{
var fileStorageService = MockRepository.GenerateMock<IFileStorageService>();
fileStorageService.Stub(fss => fss.GetDirectories(Arg<string>.Is.Anything)).Return(new string[] { });
var chocolatelyService = new FakeChocolateyService() {
ExpectedOutputFromHelp = "Version: '0.9.8.20'\nInstall Directory: 'C:\\Chocolatey'"
};
var helper = new ChocolateyLibDirHelper(chocolatelyService, fileStorageService);

var result = helper.ReloadFromDir();

Assert.AreEqual("0.9.8.20", result.Single().InstalledVersion);
}

private class FakeChocolateyService : IChocolateyService
{

public event ChocolateyService.OutputDelegate OutputChanged;
public event ChocolateyService.RunFinishedDelegate RunFinished;

public string ExpectedOutputFromHelp { get; set; }

public void LatestVersion()
{
throw new NotImplementedException();
}

public void Help()
{
if (OutputChanged != null)
OutputChanged(ExpectedOutputFromHelp);
}
}
}
}

0 comments on commit b1cdb8b

Please sign in to comment.