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

pull/865/head
Mark McDowall 7 years ago committed by ta264
parent b9d240924f
commit 91082b2903

@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Processes;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Music;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.CustomScript
@ -31,6 +30,8 @@ namespace NzbDrone.Core.Notifications.CustomScript
public override string Link => "https://github.com/Lidarr/Lidarr/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 artist = message.Artist;
@ -162,51 +163,37 @@ namespace NzbDrone.Core.Notifications.CustomScript
failures.Add(new NzbDroneValidationFailure("Path", "File does not exist"));
}
foreach (var systemFolder in SystemFolders.GetSystemFolders())
{
if (systemFolder.IsParentPath(Settings.Path))
{
failures.Add(new NzbDroneValidationFailure("Path", $"Must not be a descendant of '{systemFolder}'"));
}
}
if (failures.Empty())
try
{
try
{
var environmentVariables = new StringDictionary();
environmentVariables.Add("Sonarr_EventType", "Test");
var environmentVariables = new StringDictionary();
environmentVariables.Add("Lidarr_EventType", "Test");
var processOutput = ExecuteScript(environmentVariables);
var processOutput = ExecuteScript(environmentVariables);
if (processOutput.ExitCode != 0)
{
failures.Add(new NzbDroneValidationFailure(string.Empty, $"Script exited with code: {processOutput.ExitCode}"));
}
}
catch (Exception ex)
if (processOutput.ExitCode != 0)
{
_logger.Error(ex);
failures.Add(new NzbDroneValidationFailure(string.Empty, ex.Message));
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: {System.Environment.NewLine}{string.Join(System.Environment.NewLine, process.Lines)}");
}
_logger.Debug("Executed external script: {0} - Status: {1}", Settings.Path, processOutput.ExitCode);
_logger.Debug($"Script Output: {System.Environment.NewLine}{string.Join(System.Environment.NewLine, processOutput.Lines)}");
private bool ValidatePathParent(string possibleParent, string path)
{
return possibleParent.IsParentPath(path);
return processOutput;
}
}
}

Loading…
Cancel
Save