Skip to content

Commit

Permalink
Merge pull request #241 from NoxOrg/bugfix/powershell-error-message
Browse files Browse the repository at this point in the history
- changed powershell script error reporting
  • Loading branch information
jan-schutte authored Jun 3, 2024
2 parents 323dc0c + 947ffd5 commit 3805df5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx)

var results = _pwsh.Invoke();

if (results.Any(r => (r.ToString() ?? "").Contains("error", StringComparison.OrdinalIgnoreCase)))
if (_pwsh.HadErrors)
{
var errorMessage = "";
foreach (var err in _pwsh.Streams.Error)
{
errorMessage += err.ErrorDetails.Message + Environment.NewLine;
}
ctx.SetErrorMessage(errorMessage);
} else if (results.Any(r => (r.ToString() ?? "").Contains("error", StringComparison.OrdinalIgnoreCase)))
{
var errorMessage = "";
foreach (var result in results)
Expand All @@ -83,7 +91,7 @@ public Task<IDictionary<string, object>> ProcessAsync(INoxWorkflowContext ctx)
{
outputs["result"] = results;

ctx.SetState(ActionState.Success);
ctx.SetState(ActionState.Success);
}

}
Expand Down
30 changes: 30 additions & 0 deletions src/Plugin.Powershell.Tests/Plugin.Powershell.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Nox.Cli.Plugins\Nox.Cli.Plugin.Powershell\Nox.Cli.Plugin.Powershell.csproj" />
<ProjectReference Include="..\Nox.Cli.Variables\Nox.Cli.Variables.csproj" />
<ProjectReference Include="..\Nox.Cli\Nox.Cli.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions src/Plugin.Powershell.Tests/ScriptTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Moq;
using Nox.Cli.Abstractions;
using Nox.Cli.Abstractions.Caching;
using Nox.Cli.Actions;
using Nox.Cli.Configuration;
using Nox.Cli.Plugins.Powershell;
using Nox.Cli.Variables.Secrets;
using Nox.Secrets.Abstractions;
using Nox.Solution;

namespace Plugin.Powershell.Tests;

public class ScriptTests
{
[Fact]
public async Task Can_perform_a_git_pull()
{
Directory.SetCurrentDirectory("/home/jan/Projects/IWG/Fno.MultiDeploy");
var plugin = new PowershellScript_v1();
var inputs = new Dictionary<string, object>
{
{"script", "git pull --rebase iwgplc main"},
};
await plugin.BeginAsync(inputs);
var wfConfig = new WorkflowConfiguration();
var sln = Mock.Of<NoxSolution>();
var orgResolver = Mock.Of<IOrgSecretResolver>();
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
await plugin.ProcessAsync(ctx);
Console.WriteLine(ctx.CurrentAction!.ErrorMessage!);
Assert.Equal(ActionState.Success, ctx.State);
}
}

0 comments on commit 3805df5

Please sign in to comment.