From b1cdb8bc3369f32eca09039f16c6c8da5cf1c90d Mon Sep 17 00:00:00 2001 From: Eli Weinstock-Herman Date: Wed, 13 Feb 2013 22:18:47 -0500 Subject: [PATCH] #36 - put package list and version parse under tests 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 --- .../Chocolatey.Explorer.csproj | 3 +- .../Services/ChocolateyLibDirHelper.cs | 15 ++- .../ChocolateyVersionUnknownException.cs | 12 ++ .../Chocolatey.Explorer.Test.csproj | 1 + .../Services/TestChocolateyLibDirHelper.cs | 105 ++++++++++++++++++ 5 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 Chocolatey Explorer/Services/ChocolateyVersionUnknownException.cs create mode 100644 Chocolatey.Explorer.Test/Services/TestChocolateyLibDirHelper.cs diff --git a/Chocolatey Explorer/Chocolatey.Explorer.csproj b/Chocolatey Explorer/Chocolatey.Explorer.csproj index 54a0b50f5..e15801c2d 100644 --- a/Chocolatey Explorer/Chocolatey.Explorer.csproj +++ b/Chocolatey Explorer/Chocolatey.Explorer.csproj @@ -1,4 +1,4 @@ - + Debug @@ -121,6 +121,7 @@ + diff --git a/Chocolatey Explorer/Services/ChocolateyLibDirHelper.cs b/Chocolatey Explorer/Services/ChocolateyLibDirHelper.cs index 4d726d8a8..833e433ea 100644 --- a/Chocolatey Explorer/Services/ChocolateyLibDirHelper.cs +++ b/Chocolatey Explorer/Services/ChocolateyLibDirHelper.cs @@ -14,7 +14,7 @@ namespace Chocolatey.Explorer.Services /// chocolatey lib directory, specified by the setting /// "ChocolateyLibDirectory". /// - class ChocolateyLibDirHelper + public class ChocolateyLibDirHelper { private static readonly ILog log = LogManager.GetLogger(typeof(ChocolateyLibDirHelper)); @@ -24,7 +24,7 @@ class ChocolateyLibDirHelper private List _instaledPackages; private readonly IChocolateyService _chocolateyService; private readonly IFileStorageService _fileStorageService; - private string chocoVersion; + private string _chocoVersion; public ChocolateyLibDirHelper() : this(new ChocolateyService(), new LocalFileSystemStorageService()) { } @@ -55,16 +55,19 @@ public IList 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; } /// diff --git a/Chocolatey Explorer/Services/ChocolateyVersionUnknownException.cs b/Chocolatey Explorer/Services/ChocolateyVersionUnknownException.cs new file mode 100644 index 000000000..79ad8ec35 --- /dev/null +++ b/Chocolatey Explorer/Services/ChocolateyVersionUnknownException.cs @@ -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) { } + } +} diff --git a/Chocolatey.Explorer.Test/Chocolatey.Explorer.Test.csproj b/Chocolatey.Explorer.Test/Chocolatey.Explorer.Test.csproj index 753016b70..99e0fe44d 100644 --- a/Chocolatey.Explorer.Test/Chocolatey.Explorer.Test.csproj +++ b/Chocolatey.Explorer.Test/Chocolatey.Explorer.Test.csproj @@ -99,6 +99,7 @@ + diff --git a/Chocolatey.Explorer.Test/Services/TestChocolateyLibDirHelper.cs b/Chocolatey.Explorer.Test/Services/TestChocolateyLibDirHelper.cs new file mode 100644 index 000000000..403199180 --- /dev/null +++ b/Chocolatey.Explorer.Test/Services/TestChocolateyLibDirHelper.cs @@ -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(); + fileStorageService.Stub(fss => fss.GetDirectories(Arg.Is.Anything)).Throw(new DirectoryNotFoundException()); + var helper = new ChocolateyLibDirHelper(MockRepository.GenerateMock(), 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(); + fileStorageService.Stub(fss => fss.GetDirectories(Arg.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(), 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(); + fileStorageService.Stub(fss => fss.GetDirectories(Arg.Is.Anything)).Return(new string[] { }); + var helper = new ChocolateyLibDirHelper(MockRepository.GenerateMock(), fileStorageService); + + var result = helper.ReloadFromDir(); + + Assert.AreEqual("chocolatey", result.Single().Name); + } + + [Test] + [ExpectedException(typeof(ChocolateyVersionUnknownException))] + public void IfReloadFromDirAndHelpTextIsUnrecognizedThenThrowsChocoVersionUnknownException() + { + var fileStorageService = MockRepository.GenerateMock(); + fileStorageService.Stub(fss => fss.GetDirectories(Arg.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(); + fileStorageService.Stub(fss => fss.GetDirectories(Arg.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); + } + } + } +}