@@ -13,18 +13,37 @@ public class ProcessRunnerCancellationTests
1313 private const string LinuxFixtureSkipReason = "The detached stderr-retention fixture requires Linux setsid." ;
1414 private const string StderrClosureFailure = "stderr stream closed" ;
1515 private const string WindowsLongRunningCommand = "Write-Output $PID; Start-Sleep -Seconds 30" ;
16+ private const string SystemRootVariableName = "SystemRoot" ;
17+ private const string WindowsDirectoryVariableName = "WINDIR" ;
18+ private const string PathVariableName = "PATH" ;
19+ private const string PathExtensionsVariableName = "PATHEXT" ;
20+ private const string TestInput = "test" ;
21+ private const string MissingExecutableNamePrefix = "missing-claude-cli-" ;
22+ private const string ScriptFileNamePrefix = "claude-cli-" ;
23+ private const string ShellScriptExtension = ".sh" ;
24+ private const string PosixShellPath = "/bin/sh" ;
25+ private const string PosixShellCommandFlag = "-c" ;
26+ private const string PosixLongRunningCommand = "echo $$; exec /bin/sleep 30" ;
27+ private const string WindowsPowerShellPath = "powershell.exe" ;
28+ private const string PowerShellNoProfileFlag = "-NoProfile" ;
29+ private const string PowerShellNonInteractiveFlag = "-NonInteractive" ;
30+ private const string PowerShellCommandFlag = "-Command" ;
31+ private const string EmptyStandardInput = "" ;
32+ private const string TestsDirectoryName = "tests" ;
33+ private const string SandboxDirectoryName = ".sandbox" ;
34+ private const string FixtureDirectoryName = "ProcessRunnerCancellationTests" ;
1635
1736 [ Test ]
1837 public async Task PublicExec_PreCanceledTokenDoesNotStartCliProcess ( )
1938 {
20- var executablePath = Path . Combine ( GetTestSandboxDirectory ( ) , $ "missing-claude-cli- { Guid . NewGuid ( ) : N} ") ;
39+ var executablePath = Path . Combine ( GetTestSandboxDirectory ( ) , $ "{ MissingExecutableNamePrefix } { Guid . NewGuid ( ) : N} ") ;
2140 using var cancellation = new CancellationTokenSource ( ) ;
2241 cancellation . Cancel ( ) ;
2342 var exec = new ClaudeExec ( false , TimeSpan . FromSeconds ( 5 ) , executablePath , CreateEnvironment ( ) ) ;
2443
2544 await using var enumerator = exec . RunAsync ( new ClaudeExecArgs
2645 {
27- Input = "test" ,
46+ Input = TestInput ,
2847 CancellationToken = cancellation . Token ,
2948 } ) . GetAsyncEnumerator ( cancellation . Token ) ;
3049
@@ -45,9 +64,9 @@ public async Task RootExitWithDescendantHoldingStderr_FailsWithinConfiguredBound
4564
4665 const int terminationTimeoutMilliseconds = 250 ;
4766 var invocation = new ClaudeProcessInvocation (
48- "/bin/sh" ,
67+ PosixShellPath ,
4968 Environment . CurrentDirectory ,
50- [ "-c" , DescendantHoldingStderrScript ] ,
69+ [ PosixShellCommandFlag , DescendantHoldingStderrScript ] ,
5170 CreateEnvironment ( ) ,
5271 string . Empty ,
5372 TimeSpan . FromMilliseconds ( terminationTimeoutMilliseconds ) ) ;
@@ -105,7 +124,7 @@ public async Task PublicExec_CancellationBetweenYieldedLinesIsNotReportedAsSucce
105124 {
106125 await using var enumerator = exec . RunAsync ( new ClaudeExecArgs
107126 {
108- Input = "test" ,
127+ Input = TestInput ,
109128 CancellationToken = cancellation . Token ,
110129 } ) . GetAsyncEnumerator ( cancellation . Token ) ;
111130
@@ -131,8 +150,8 @@ public async Task Cancellation_WaitsForStartedProcessToExit()
131150 {
132151 using var cancellation = new CancellationTokenSource ( ) ;
133152 var invocation = OperatingSystem . IsWindows ( )
134- ? new ClaudeProcessInvocation ( "powershell.exe" , Environment . CurrentDirectory , [ "-NoProfile" , "-NonInteractive" , "-Command" , WindowsLongRunningCommand ] , CreateWindowsProcessEnvironment ( ) , string . Empty , TimeSpan . FromSeconds ( 5 ) )
135- : new ClaudeProcessInvocation ( "/bin/sh" , Environment . CurrentDirectory , [ "-c" , "echo $$; exec /bin/sleep 30" ] , CreateEnvironment ( ) , string . Empty , TimeSpan . FromSeconds ( 5 ) ) ;
153+ ? new ClaudeProcessInvocation ( WindowsPowerShellPath , Environment . CurrentDirectory , [ PowerShellNoProfileFlag , PowerShellNonInteractiveFlag , PowerShellCommandFlag , WindowsLongRunningCommand ] , CreateWindowsProcessEnvironment ( ) , EmptyStandardInput , TimeSpan . FromSeconds ( 5 ) )
154+ : new ClaudeProcessInvocation ( PosixShellPath , Environment . CurrentDirectory , [ PosixShellCommandFlag , PosixLongRunningCommand ] , CreateEnvironment ( ) , EmptyStandardInput , TimeSpan . FromSeconds ( 5 ) ) ;
136155 var runner = new DefaultClaudeProcessRunner ( ) ;
137156
138157 await using var enumerator = runner . RunAsync ( invocation , NullLogger . Instance , cancellation . Token )
@@ -159,7 +178,13 @@ private static Dictionary<string, string> CreateEnvironment()
159178 private static Dictionary < string , string > CreateWindowsProcessEnvironment ( )
160179 {
161180 var environment = CreateEnvironment ( ) ;
162- foreach ( var variableName in new [ ] { "SystemRoot" , "WINDIR" , "PATH" , "PATHEXT" } )
181+ foreach ( var variableName in new [ ]
182+ {
183+ SystemRootVariableName ,
184+ WindowsDirectoryVariableName ,
185+ PathVariableName ,
186+ PathExtensionsVariableName ,
187+ } )
163188 {
164189 var value = Environment . GetEnvironmentVariable ( variableName ) ;
165190 if ( ! string . IsNullOrEmpty ( value ) )
@@ -173,7 +198,7 @@ private static Dictionary<string, string> CreateWindowsProcessEnvironment()
173198
174199 private static string CreateLongRunningCliScript ( )
175200 {
176- var scriptPath = Path . Combine ( GetTestSandboxDirectory ( ) , $ "claude-cli- { Guid . NewGuid ( ) : N} .sh ") ;
201+ var scriptPath = Path . Combine ( GetTestSandboxDirectory ( ) , $ "{ ScriptFileNamePrefix } { Guid . NewGuid ( ) : N} { ShellScriptExtension } ") ;
177202 File . WriteAllText ( scriptPath , LongRunningScript ) ;
178203 if ( ! OperatingSystem . IsWindows ( ) )
179204 {
@@ -184,7 +209,7 @@ private static string CreateLongRunningCliScript()
184209
185210 private static string GetTestSandboxDirectory ( )
186211 {
187- var path = Path . Combine ( Environment . CurrentDirectory , "tests" , ".sandbox" , "ProcessRunnerCancellationTests" ) ;
212+ var path = Path . Combine ( Environment . CurrentDirectory , TestsDirectoryName , SandboxDirectoryName , FixtureDirectoryName ) ;
188213 Directory . CreateDirectory ( path ) ;
189214 return path ;
190215 }
0 commit comments