Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.16.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.25.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.25.1" />
<PackageReference Include="MimeTypes" Version="2.5.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Spectre.Console.Testing" Version="0.51.1" />
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="22.0.16" />
<PackageReference Include="Verify.NUnit" Version="30.13.0" />
<PackageReference Include="Verify.NUnit" Version="30.19.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Verify.NUnit" Version="30.13.0" />
<PackageReference Include="Verify.NUnit" Version="30.19.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DecSm.Atom.Tests/DecSm.Atom.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Shouldly" Version="4.3.0" />
<PackageReference Include="Spectre.Console.Testing" Version="0.51.1" />
<PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="22.0.16" />
<PackageReference Include="Verify.NUnit" Version="30.13.0" />
<PackageReference Include="Verify.NUnit" Version="30.19.1" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions DecSm.Atom/Process/ProcessRunOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,17 @@ public ProcessRunOptions(string Name, string[] Args) : this(Name, string.Join("
/// </code>
/// </example>
public bool AllowFailedResult { get; init; }

/// <summary>
/// Gets or sets the environment variables to be used by the process during execution.
/// </summary>
/// <value>
/// A dictionary where keys represent the names of the environment variables and values represent their corresponding values.
/// A value of <c>null</c> for any variable indicates that it should be removed from the environment.
/// </value>
/// <remarks>
/// These environment variables are passed to the process when it is executed, potentially overriding inherited variables
/// from the current environment. If not set, the process will inherit the current environment's variables by default.
/// </remarks>
public Dictionary<string, string?> EnvironmentVariables { get; init; } = [];
}
86 changes: 70 additions & 16 deletions DecSm.Atom/Process/ProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,38 @@ public sealed class ProcessRunner(ILogger<ProcessRunner> logger) : IProcessRunne
/// </remarks>
public ProcessRunResult Run(ProcessRunOptions options)
{
if (options.WorkingDirectory is { Length: > 0 })
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} in {WorkingDirectory}",
options.Name,
options.Args,
options.WorkingDirectory);
else
logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args);
switch (options)
{
case { WorkingDirectory.Length: > 0, EnvironmentVariables.Count: > 0 }:
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} in {WorkingDirectory} with env {EnvironmentVariables}",
options.Name,
options.Args,
options.WorkingDirectory,
string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}")));

break;
case { WorkingDirectory.Length: > 0 }:
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} in {WorkingDirectory}",
options.Name,
options.Args,
options.WorkingDirectory);

break;
case { EnvironmentVariables.Count: > 0 }:
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} with env {EnvironmentVariables}",
options.Name,
options.Args,
string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}")));

break;
default:
logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args);

break;
}

using var process = new System.Diagnostics.Process();

Expand All @@ -170,6 +194,9 @@ public ProcessRunResult Run(ProcessRunOptions options)
RedirectStandardError = true,
};

foreach (var environmentVariable in options.EnvironmentVariables)
process.StartInfo.Environment[environmentVariable.Key] = environmentVariable.Value;

var outputBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();

Expand Down Expand Up @@ -278,14 +305,38 @@ void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e)
/// </example>
public async Task<ProcessRunResult> RunAsync(ProcessRunOptions options, CancellationToken cancellationToken = default)
{
if (options.WorkingDirectory is { Length: > 0 })
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} in {WorkingDirectory}",
options.Name,
options.Args,
options.WorkingDirectory);
else
logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args);
switch (options)
{
case { WorkingDirectory.Length: > 0, EnvironmentVariables.Count: > 0 }:
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} in {WorkingDirectory} with env {EnvironmentVariables}",
options.Name,
options.Args,
options.WorkingDirectory,
string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}")));

break;
case { WorkingDirectory.Length: > 0 }:
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} in {WorkingDirectory}",
options.Name,
options.Args,
options.WorkingDirectory);

break;
case { EnvironmentVariables.Count: > 0 }:
logger.Log(options.InvocationLogLevel,
"Run: {Name} {Args} with env {EnvironmentVariables}",
options.Name,
options.Args,
string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}")));

break;
default:
logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args);

break;
}

using var process = new System.Diagnostics.Process();

Expand All @@ -298,6 +349,9 @@ public async Task<ProcessRunResult> RunAsync(ProcessRunOptions options, Cancella
RedirectStandardError = true,
};

foreach (var environmentVariable in options.EnvironmentVariables)
process.StartInfo.Environment[environmentVariable.Key] = environmentVariable.Value;

var outputBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();

Expand Down
Loading