Skip to content

Commit c3e3416

Browse files
authored
Merge pull request #759 from adamralph/refactor
refactor private naming, qualification, and captured value type modification
2 parents 165b700 + 4acaae9 commit c3e3416

File tree

7 files changed

+44
-43
lines changed

7 files changed

+44
-43
lines changed

.editorconfig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ csharp_style_namespace_declarations = file
2121
csharp_style_var_elsewhere = true
2222
csharp_style_var_for_built_in_types = true
2323
csharp_style_var_when_type_is_apparent = true
24-
dotnet_style_qualification_for_event = true
25-
dotnet_style_qualification_for_field = true
26-
dotnet_style_qualification_for_method = true
27-
dotnet_style_qualification_for_property = true
24+
dotnet_style_qualification_for_event = false:warning
25+
dotnet_style_qualification_for_field = false:warning
26+
dotnet_style_qualification_for_method = false:warning
27+
dotnet_style_qualification_for_property = false:warning
2828
file_header_template = unset
2929
indent_size = 4
3030

SimpleExec/Command.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace SimpleExec;
1010
/// </summary>
1111
public static class Command
1212
{
13-
private static readonly Action<IDictionary<string, string?>> defaultAction = _ => { };
14-
private static readonly string defaultEchoPrefix = Assembly.GetEntryAssembly()?.GetName().Name ?? "SimpleExec";
13+
private static readonly Action<IDictionary<string, string?>> DefaultAction = _ => { };
14+
private static readonly string DefaultEchoPrefix = Assembly.GetEntryAssembly()?.GetName().Name ?? "SimpleExec";
1515

1616
/// <summary>
1717
/// Runs a command without redirecting standard output (stdout) and standard error (stderr) and without writing to standard input (stdin).
@@ -58,9 +58,9 @@ public static void Run(
5858
[],
5959
workingDirectory,
6060
false,
61-
configureEnvironment ?? defaultAction,
61+
configureEnvironment ?? DefaultAction,
6262
createNoWindow)
63-
.Run(noEcho, echoPrefix ?? defaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
63+
.Run(noEcho, echoPrefix ?? DefaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
6464

6565
/// <summary>
6666
/// Runs a command without redirecting standard output (stdout) and standard error (stderr) and without writing to standard input (stdin).
@@ -106,9 +106,9 @@ public static void Run(
106106
args ?? throw new ArgumentNullException(nameof(args)),
107107
workingDirectory,
108108
false,
109-
configureEnvironment ?? defaultAction,
109+
configureEnvironment ?? DefaultAction,
110110
createNoWindow)
111-
.Run(noEcho, echoPrefix ?? defaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
111+
.Run(noEcho, echoPrefix ?? DefaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
112112

113113
private static void Run(
114114
this System.Diagnostics.ProcessStartInfo startInfo,
@@ -175,9 +175,9 @@ public static Task RunAsync(
175175
[],
176176
workingDirectory,
177177
false,
178-
configureEnvironment ?? defaultAction,
178+
configureEnvironment ?? DefaultAction,
179179
createNoWindow)
180-
.RunAsync(noEcho, echoPrefix ?? defaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
180+
.RunAsync(noEcho, echoPrefix ?? DefaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
181181

182182
/// <summary>
183183
/// Runs a command asynchronously without redirecting standard output (stdout) and standard error (stderr) and without writing to standard input (stdin).
@@ -224,9 +224,9 @@ public static Task RunAsync(
224224
args ?? throw new ArgumentNullException(nameof(args)),
225225
workingDirectory,
226226
false,
227-
configureEnvironment ?? defaultAction,
227+
configureEnvironment ?? DefaultAction,
228228
createNoWindow)
229-
.RunAsync(noEcho, echoPrefix ?? defaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
229+
.RunAsync(noEcho, echoPrefix ?? DefaultEchoPrefix, handleExitCode, cancellationIgnoresProcessTree, cancellationToken);
230230

231231
private static async Task RunAsync(
232232
this System.Diagnostics.ProcessStartInfo startInfo,
@@ -291,7 +291,7 @@ private static async Task RunAsync(
291291
[],
292292
workingDirectory,
293293
true,
294-
configureEnvironment ?? defaultAction,
294+
configureEnvironment ?? DefaultAction,
295295
true,
296296
encoding)
297297
.ReadAsync(
@@ -347,7 +347,7 @@ private static async Task RunAsync(
347347
args ?? throw new ArgumentNullException(nameof(args)),
348348
workingDirectory,
349349
true,
350-
configureEnvironment ?? defaultAction,
350+
configureEnvironment ?? DefaultAction,
351351
true,
352352
encoding)
353353
.ReadAsync(
@@ -392,7 +392,7 @@ private static async Task RunAsync(
392392
#pragma warning disable CA1849 // Call async methods when in an async method
393393
var output = readOutput.Result;
394394
var error = readError.Result;
395-
#pragma warning restore CA1849 // Call async methods when in an async method
395+
#pragma warning restore CA1849
396396

397397
return (handleExitCode?.Invoke(process.ExitCode) ?? false) || process.ExitCode == 0
398398
? (output, error)

SimpleExec/ExitCodeException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace SimpleExec;
66
/// <param name="exitCode">The exit code of the command.</param>
77
#pragma warning disable CA1032 // Implement standard exception constructors
88
public class ExitCodeException(int exitCode) : Exception
9-
#pragma warning restore CA1032 // Implement standard exception constructors
9+
#pragma warning restore CA1032
1010
{
1111
/// <summary>
1212
/// Gets the exit code of the command.
1313
/// </summary>
1414
public int ExitCode { get; } = exitCode;
1515

1616
/// <inheritdoc/>
17-
public override string Message => $"The command exited with code {this.ExitCode}.";
17+
public override string Message => $"The command exited with code {ExitCode}.";
1818
}

SimpleExec/ExitCodeReadException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ namespace SimpleExec;
55
/// </summary>
66
#pragma warning disable CA1032 // Implement standard exception constructors
77
public class ExitCodeReadException : ExitCodeException
8-
#pragma warning restore CA1032 // Implement standard exception constructors
8+
#pragma warning restore CA1032
99
{
10-
private static readonly string twoNewLines = $"{Environment.NewLine}{Environment.NewLine}";
10+
private static readonly string TwoNewLines = $"{Environment.NewLine}{Environment.NewLine}";
1111

1212
/// <summary>
1313
/// Constructs an instance of a <see cref="ExitCodeReadException"/>.
1414
/// </summary>
1515
/// <param name="exitCode">The exit code of the command.</param>
1616
/// <param name="standardOutput">The contents of standard output (stdout).</param>
1717
/// <param name="standardError">The contents of standard error (stderr).</param>
18-
public ExitCodeReadException(int exitCode, string standardOutput, string standardError) : base(exitCode) => (this.StandardOutput, this.StandardError) = (standardOutput, standardError);
18+
public ExitCodeReadException(int exitCode, string standardOutput, string standardError) : base(exitCode) => (StandardOutput, StandardError) = (standardOutput, standardError);
1919

2020
/// <summary>
2121
/// Gets the contents of standard output (stdout).
@@ -29,5 +29,5 @@ public class ExitCodeReadException : ExitCodeException
2929

3030
/// <inheritdoc/>
3131
public override string Message =>
32-
$"{base.Message}{twoNewLines}Standard output (stdout):{twoNewLines}{this.StandardOutput}{twoNewLines}Standard error (stderr):{twoNewLines}{this.StandardError}";
32+
$"{base.Message}{TwoNewLines}Standard output (stdout):{TwoNewLines}{StandardOutput}{TwoNewLines}Standard error (stderr):{TwoNewLines}{StandardError}";
3333
}

SimpleExec/ProcessExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.Globalization;
3+
using System.Runtime.CompilerServices;
34
using System.Text;
45

56
namespace SimpleExec;
@@ -8,7 +9,7 @@ internal static class ProcessExtensions
89
{
910
public static void Run(this Process process, bool noEcho, string echoPrefix, bool cancellationIgnoresProcessTree, CancellationToken cancellationToken)
1011
{
11-
var cancelled = 0L;
12+
var cancelled = new StrongBox<long>(0);
1213

1314
if (!noEcho)
1415
{
@@ -22,14 +23,14 @@ public static void Run(this Process process, bool noEcho, string echoPrefix, boo
2223
{
2324
if (process.TryKill(cancellationIgnoresProcessTree))
2425
{
25-
_ = Interlocked.Increment(ref cancelled);
26+
_ = Interlocked.Increment(ref cancelled.Value);
2627
}
2728
},
2829
useSynchronizationContext: false);
2930

3031
process.WaitForExit();
3132

32-
if (Interlocked.Read(ref cancelled) == 1)
33+
if (Interlocked.Read(ref cancelled.Value) == 1)
3334
{
3435
cancellationToken.ThrowIfCancellationRequested();
3536
}
@@ -102,7 +103,7 @@ private static bool TryKill(this Process process, bool ignoreProcessTree)
102103
}
103104
#pragma warning disable CA1031 // Do not catch general exception types
104105
catch (Exception)
105-
#pragma warning restore CA1031 // Do not catch general exception types
106+
#pragma warning restore CA1031
106107
{
107108
return false;
108109
}

SimpleExecTests/Infra/Capture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace SimpleExecTests.Infra;
22

33
internal static class Capture
44
{
5-
private static readonly Lazy<TextWriter> @out = new(() => new StringWriter());
5+
private static readonly Lazy<TextWriter> LazyOut = new(() => new StringWriter());
66

7-
public static TextWriter Out => @out.Value;
7+
public static TextWriter Out => LazyOut.Value;
88
}

SimpleExecTests/RunningCommands.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SimpleExecTests;
99

1010
public static class RunningCommands
1111
{
12-
private static readonly string command = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "hello-world.cmd" : "ls";
12+
private static readonly string Command = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "hello-world.cmd" : "ls";
1313
private static CancellationToken Ct => TestContext.Current.CancellationToken;
1414

1515
public static bool OsPlatformIsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
@@ -18,7 +18,7 @@ public static class RunningCommands
1818
public static void RunningASucceedingCommand()
1919
{
2020
// act
21-
var exception = Record.Exception(() => Command.Run(command, cancellationToken: Ct));
21+
var exception = Record.Exception(() => SimpleExec.Command.Run(Command, cancellationToken: Ct));
2222

2323
// assert
2424
Assert.Null(exception);
@@ -28,7 +28,7 @@ public static void RunningASucceedingCommand()
2828
public static void RunningASucceedingCommandWithArgs()
2929
{
3030
// act
31-
var exception = Record.Exception(() => Command.Run("dotnet", $"exec {Tester.Path} hello world", cancellationToken: Ct));
31+
var exception = Record.Exception(() => SimpleExec.Command.Run("dotnet", $"exec {Tester.Path} hello world", cancellationToken: Ct));
3232

3333
// assert
3434
Assert.Null(exception);
@@ -38,7 +38,7 @@ public static void RunningASucceedingCommandWithArgs()
3838
public static async Task RunningASucceedingCommandAsync()
3939
{
4040
// act
41-
var exception = await Record.ExceptionAsync(() => Command.RunAsync(command, cancellationToken: Ct));
41+
var exception = await Record.ExceptionAsync(() => SimpleExec.Command.RunAsync(Command, cancellationToken: Ct));
4242

4343
// assert
4444
Assert.Null(exception);
@@ -48,7 +48,7 @@ public static async Task RunningASucceedingCommandAsync()
4848
public static void RunningAFailingCommand()
4949
{
5050
// act
51-
var exception = Record.Exception(() => Command.Run("dotnet", $"exec {Tester.Path} 1 hello world", cancellationToken: Ct));
51+
var exception = Record.Exception(() => SimpleExec.Command.Run("dotnet", $"exec {Tester.Path} 1 hello world", cancellationToken: Ct));
5252

5353
// assert
5454
Assert.Equal(1, Assert.IsType<ExitCodeException>(exception).ExitCode);
@@ -58,7 +58,7 @@ public static void RunningAFailingCommand()
5858
public static async Task RunningAFailingCommandAsync()
5959
{
6060
// act
61-
var exception = await Record.ExceptionAsync(() => Command.RunAsync("dotnet", $"exec {Tester.Path} 1 hello world", cancellationToken: Ct));
61+
var exception = await Record.ExceptionAsync(() => SimpleExec.Command.RunAsync("dotnet", $"exec {Tester.Path} 1 hello world", cancellationToken: Ct));
6262

6363
// assert
6464
Assert.Equal(1, Assert.IsType<ExitCodeException>(exception).ExitCode);
@@ -68,7 +68,7 @@ public static async Task RunningAFailingCommandAsync()
6868
public static void RunningACommandInANonExistentWorkDirectory()
6969
{
7070
// act
71-
var exception = Record.Exception(() => Command.Run("dotnet", $"exec {Tester.Path}", "non-existent-working-directory", cancellationToken: Ct));
71+
var exception = Record.Exception(() => SimpleExec.Command.Run("dotnet", $"exec {Tester.Path}", "non-existent-working-directory", cancellationToken: Ct));
7272

7373
// assert
7474
_ = Assert.IsType<Win32Exception>(exception);
@@ -78,7 +78,7 @@ public static void RunningACommandInANonExistentWorkDirectory()
7878
public static async Task RunningACommandAsyncInANonExistentWorkDirectory()
7979
{
8080
// act
81-
var exception = await Record.ExceptionAsync(() => Command.RunAsync("dotnet", $"exec {Tester.Path}", "non-existent-working-directory", cancellationToken: Ct));
81+
var exception = await Record.ExceptionAsync(() => SimpleExec.Command.RunAsync("dotnet", $"exec {Tester.Path}", "non-existent-working-directory", cancellationToken: Ct));
8282

8383
// assert
8484
_ = Assert.IsType<Win32Exception>(exception);
@@ -88,7 +88,7 @@ public static async Task RunningACommandAsyncInANonExistentWorkDirectory()
8888
public static void RunningANonExistentCommand()
8989
{
9090
// act
91-
var exception = Record.Exception(() => Command.Run("simple-exec-tests-non-existent-command", cancellationToken: Ct));
91+
var exception = Record.Exception(() => SimpleExec.Command.Run("simple-exec-tests-non-existent-command", cancellationToken: Ct));
9292

9393
// assert
9494
_ = Assert.IsType<Win32Exception>(exception);
@@ -98,7 +98,7 @@ public static void RunningANonExistentCommand()
9898
public static async Task RunningANonExistentCommandAsync()
9999
{
100100
// act
101-
var exception = await Record.ExceptionAsync(() => Command.RunAsync("simple-exec-tests-non-existent-command", cancellationToken: Ct));
101+
var exception = await Record.ExceptionAsync(() => SimpleExec.Command.RunAsync("simple-exec-tests-non-existent-command", cancellationToken: Ct));
102102

103103
// assert
104104
_ = Assert.IsType<Win32Exception>(exception);
@@ -110,7 +110,7 @@ public static async Task RunningANonExistentCommandAsync()
110110
public static void RunningNoCommand(string name)
111111
{
112112
// act
113-
var exception = Record.Exception(() => Command.Run(name, cancellationToken: Ct));
113+
var exception = Record.Exception(() => SimpleExec.Command.Run(name, cancellationToken: Ct));
114114

115115
// assert
116116
Assert.Equal(nameof(name), Assert.IsType<ArgumentException>(exception).ParamName);
@@ -122,7 +122,7 @@ public static void RunningNoCommand(string name)
122122
public static async Task RunningNoCommandAsync(string name)
123123
{
124124
// act
125-
var exception = await Record.ExceptionAsync(() => Command.RunAsync(name, cancellationToken: Ct));
125+
var exception = await Record.ExceptionAsync(() => SimpleExec.Command.RunAsync(name, cancellationToken: Ct));
126126

127127
// assert
128128
Assert.Equal(nameof(name), Assert.IsType<ArgumentException>(exception).ParamName);
@@ -155,7 +155,7 @@ public static async Task RunningCommandsInPathOnWindows()
155155
EnvironmentVariableTarget.Process);
156156

157157
// act
158-
var exception = Record.Exception(() => Command.Run(name, cancellationToken: Ct));
158+
var exception = Record.Exception(() => SimpleExec.Command.Run(name, cancellationToken: Ct));
159159

160160
// assert
161161
Assert.Null(exception);
@@ -199,7 +199,7 @@ public static async Task RunningCommandsInPathOnWindowsWithSpecificPathExt(
199199
EnvironmentVariableTarget.Process);
200200

201201
// act
202-
var actual = (await Command.ReadAsync(name, cancellationToken: Ct)).StandardOutput.Trim();
202+
var actual = (await SimpleExec.Command.ReadAsync(name, cancellationToken: Ct)).StandardOutput.Trim();
203203

204204
// assert
205205
Assert.Equal(expected, actual);

0 commit comments

Comments
 (0)