From efb9c0791f534942994d26aa21d5c4146ce6b8bb Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 1 Mar 2017 21:28:48 -0800 Subject: [PATCH] New: Testing Custom Script executes the script and verifies the exit code --- .../CustomScript/CustomScript.cs | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index 8e027a8c0..00255095b 100644 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; @@ -6,6 +7,7 @@ using FluentValidation.Results; using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.Processes; +using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; using NzbDrone.Core.Validation; @@ -28,6 +30,8 @@ namespace NzbDrone.Core.Notifications.CustomScript public override string Link => "https://github.com/Sonarr/Sonarr/wiki/Custom-Post-Processing-Scripts"; + public override ProviderMessage Message => new ProviderMessage("Testing will execute the script with the EventType set to Test, ensure your script handles this correctly", ProviderMessageType.Warning); + public override void OnGrab(GrabMessage message) { var series = message.Series; @@ -120,7 +124,6 @@ namespace NzbDrone.Core.Notifications.CustomScript ExecuteScript(environmentVariables); } - public override ValidationResult Test() { var failures = new List(); @@ -130,17 +133,37 @@ namespace NzbDrone.Core.Notifications.CustomScript failures.Add(new NzbDroneValidationFailure("Path", "File does not exist")); } + try + { + var environmentVariables = new StringDictionary(); + environmentVariables.Add("Sonarr_EventType", "Test"); + + var processOutput = ExecuteScript(environmentVariables); + + if (processOutput.ExitCode != 0) + { + failures.Add(new NzbDroneValidationFailure(string.Empty, $"Script exited with code: {processOutput.ExitCode}")); + } + } + catch (Exception ex) + { + _logger.Error(ex); + failures.Add(new NzbDroneValidationFailure(string.Empty, ex.Message)); + } + return new ValidationResult(failures); } - private void ExecuteScript(StringDictionary environmentVariables) + private ProcessOutput ExecuteScript(StringDictionary environmentVariables) { _logger.Debug("Executing external script: {0}", Settings.Path); - var process = _processProvider.StartAndCapture(Settings.Path, Settings.Arguments, environmentVariables); + var processOutput = _processProvider.StartAndCapture(Settings.Path, Settings.Arguments, environmentVariables); - _logger.Debug("Executed external script: {0} - Status: {1}", Settings.Path, process.ExitCode); - _logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", process.Lines)); + _logger.Debug("Executed external script: {0} - Status: {1}", Settings.Path, processOutput.ExitCode); + _logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", processOutput.Lines)); + + return processOutput; } } }