New: Testing Custom Script executes the script and verifies the exit code

This commit is contained in:
Mark McDowall 2017-03-01 21:28:48 -08:00 committed by Taloth Saldono
parent acc901455b
commit efb9c0791f
1 changed files with 28 additions and 5 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
@ -6,6 +7,7 @@ using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Processes; using NzbDrone.Common.Processes;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using NzbDrone.Core.Validation; 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 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) public override void OnGrab(GrabMessage message)
{ {
var series = message.Series; var series = message.Series;
@ -120,7 +124,6 @@ namespace NzbDrone.Core.Notifications.CustomScript
ExecuteScript(environmentVariables); ExecuteScript(environmentVariables);
} }
public override ValidationResult Test() public override ValidationResult Test()
{ {
var failures = new List<ValidationFailure>(); var failures = new List<ValidationFailure>();
@ -130,17 +133,37 @@ namespace NzbDrone.Core.Notifications.CustomScript
failures.Add(new NzbDroneValidationFailure("Path", "File does not exist")); 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); return new ValidationResult(failures);
} }
private void ExecuteScript(StringDictionary environmentVariables) private ProcessOutput ExecuteScript(StringDictionary environmentVariables)
{ {
_logger.Debug("Executing external script: {0}", Settings.Path); _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("Executed external script: {0} - Status: {1}", Settings.Path, processOutput.ExitCode);
_logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", process.Lines)); _logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", processOutput.Lines));
return processOutput;
} }
} }
} }