From a4500606a9568770195329513a3ebbd2684aa4d3 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 24 Jul 2014 23:37:32 -0700 Subject: [PATCH] Fixed: mono version check will check running mono version instead running another version --- src/NzbDrone.Common/NzbDrone.Common.csproj | 1 + .../Processes/IRuntimeProvider.cs | 9 ++++ .../Checks/MonoVersionCheckFixture.cs | 39 +++++------------- .../HealthCheck/Checks/MonoVersionCheck.cs | 23 ++++------- src/NzbDrone.Mono/MonoRuntimeProvider.cs | 41 +++++++++++++++++++ src/NzbDrone.Mono/NzbDrone.Mono.csproj | 1 + 6 files changed, 72 insertions(+), 42 deletions(-) create mode 100644 src/NzbDrone.Common/Processes/IRuntimeProvider.cs create mode 100644 src/NzbDrone.Mono/MonoRuntimeProvider.cs diff --git a/src/NzbDrone.Common/NzbDrone.Common.csproj b/src/NzbDrone.Common/NzbDrone.Common.csproj index c5ebd8d90..e5ef0d4e7 100644 --- a/src/NzbDrone.Common/NzbDrone.Common.csproj +++ b/src/NzbDrone.Common/NzbDrone.Common.csproj @@ -112,6 +112,7 @@ + diff --git a/src/NzbDrone.Common/Processes/IRuntimeProvider.cs b/src/NzbDrone.Common/Processes/IRuntimeProvider.cs new file mode 100644 index 000000000..96f7a1e62 --- /dev/null +++ b/src/NzbDrone.Common/Processes/IRuntimeProvider.cs @@ -0,0 +1,9 @@ +using System; + +namespace NzbDrone.Common.Processes +{ + public interface IRuntimeProvider + { + String GetVersion(); + } +} diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/MonoVersionCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/MonoVersionCheckFixture.cs index 8a0bbf7da..3184496ef 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/Checks/MonoVersionCheckFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/MonoVersionCheckFixture.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using NUnit.Framework; using NzbDrone.Common.Processes; using NzbDrone.Core.HealthCheck.Checks; @@ -18,16 +17,9 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks private void GivenOutput(string version) { - Mocker.GetMock() - .Setup(s => s.StartAndCapture("mono", "--version")) - .Returns(new ProcessOutput - { - Standard = new List - { - String.Format("Mono JIT compiler version {0} (Debian {0}-8)", version), - "Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com" - } - }); + Mocker.GetMock() + .Setup(s => s.GetVersion()) + .Returns(String.Format("{0} (tarball Wed Sep 25 16:35:44 CDT 2013)", version)); } [Test] @@ -46,6 +38,14 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks Subject.Check().ShouldBeWarning(); } + [Test] + public void should_return_warning_when_mono_2_10_2() + { + GivenOutput("2.10.2"); + + Subject.Check().ShouldBeWarning(); + } + [Test] public void should_return_ok_when_mono_3_2() { @@ -85,22 +85,5 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks Subject.Check().ShouldBeOk(); } - - [Test] - public void should_return_ok_when_mono_3_6_1_with_custom_output() - { - Mocker.GetMock() - .Setup(s => s.StartAndCapture("mono", "--version")) - .Returns(new ProcessOutput - { - Standard = new List - { - "Mono JIT compiler version 3.6.1 (master/fce3972 Fri Jul 4 01:12:43 CEST 2014)", - "Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com" - } - }); - - Subject.Check().ShouldBeOk(); - } } } diff --git a/src/NzbDrone.Core/HealthCheck/Checks/MonoVersionCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/MonoVersionCheck.cs index 733d5e61a..52944faa1 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/MonoVersionCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/MonoVersionCheck.cs @@ -8,13 +8,13 @@ namespace NzbDrone.Core.HealthCheck.Checks { public class MonoVersionCheck : HealthCheckBase { - private readonly IProcessProvider _processProvider; + private readonly IRuntimeProvider _runtimeProvider; private readonly Logger _logger; private static readonly Regex VersionRegex = new Regex(@"(?<=\W)(?\d+\.\d+\.\d+(\.\d+)?)(?=\W)", RegexOptions.Compiled | RegexOptions.IgnoreCase); - public MonoVersionCheck(IProcessProvider processProvider, Logger logger) + public MonoVersionCheck(IRuntimeProvider runtimeProvider, Logger logger) { - _processProvider = processProvider; + _runtimeProvider = runtimeProvider; _logger = logger; } @@ -25,21 +25,16 @@ namespace NzbDrone.Core.HealthCheck.Checks return new HealthCheck(GetType()); } - var output = _processProvider.StartAndCapture("mono", "--version"); + var versionMatch = VersionRegex.Match(_runtimeProvider.GetVersion()); - foreach (var line in output.Standard) + if (versionMatch.Success) { - var versionMatch = VersionRegex.Match(line); + var version = new Version(versionMatch.Groups["version"].Value); - if (versionMatch.Success) + if (version >= new Version(3, 2)) { - var version = new Version(versionMatch.Groups["version"].Value); - - if (version >= new Version(3, 2)) - { - _logger.Debug("mono version is 3.2 or better: {0}", version.ToString()); - return new HealthCheck(GetType()); - } + _logger.Debug("mono version is 3.2 or better: {0}", version.ToString()); + return new HealthCheck(GetType()); } } diff --git a/src/NzbDrone.Mono/MonoRuntimeProvider.cs b/src/NzbDrone.Mono/MonoRuntimeProvider.cs new file mode 100644 index 000000000..af0fe4115 --- /dev/null +++ b/src/NzbDrone.Mono/MonoRuntimeProvider.cs @@ -0,0 +1,41 @@ +using System; +using System.Reflection; +using NLog; +using NzbDrone.Common.Processes; + +namespace NzbDrone.Mono +{ + public class MonoRuntimeProvider : IRuntimeProvider + { + private readonly Logger _logger; + + public MonoRuntimeProvider(Logger logger) + { + _logger = logger; + } + + public String GetVersion() + { + try + { + var type = Type.GetType("Mono.Runtime"); + + if (type != null) + { + var displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); + + if (displayName != null) + { + return displayName.Invoke(null, null).ToString(); + } + } + } + catch (Exception ex) + { + _logger.ErrorException("Unable to get mono version: " + ex.Message, ex); + } + + return String.Empty; + } + } +} diff --git a/src/NzbDrone.Mono/NzbDrone.Mono.csproj b/src/NzbDrone.Mono/NzbDrone.Mono.csproj index 91d0efeb3..fa37ac15e 100644 --- a/src/NzbDrone.Mono/NzbDrone.Mono.csproj +++ b/src/NzbDrone.Mono/NzbDrone.Mono.csproj @@ -70,6 +70,7 @@ +