11using System ;
2+ using System . Diagnostics ;
23using System . IO ;
34using FlaUI . Core ;
45using FlaUI . Core . AutomationElements ;
@@ -8,7 +9,8 @@ namespace CodeShellManager.UITests;
89
910/// <summary>
1011/// Launches and tears down the CodeShellManager process once per test class.
11- /// Sets CSM_STATE_PATH to a temp file so tests never read the developer's real sessions.
12+ /// Sets CSM_STATE_PATH on the child process's environment directly (via ProcessStartInfo)
13+ /// so the env var is scoped to that process only — no test-runner-level mutation.
1214/// </summary>
1315public sealed class AppFixture : IDisposable
1416{
@@ -21,11 +23,13 @@ public AppFixture()
2123 {
2224 _statePath = Path . GetTempFileName ( ) ;
2325
24- // Child process inherits this env var — StateService uses it instead of %AppData%
25- Environment . SetEnvironmentVariable ( "CSM_STATE_PATH" , _statePath ) ;
26+ // Scope CSM_STATE_PATH to the child process only — avoids env var races when
27+ // xUnit constructs multiple AppFixture instances concurrently.
28+ var psi = new ProcessStartInfo ( GetExePath ( ) ) ;
29+ psi . EnvironmentVariables [ "CSM_STATE_PATH" ] = _statePath ;
2630
2731 Automation = new UIA3Automation ( ) ;
28- App = Application . Launch ( GetExePath ( ) ) ;
32+ App = Application . Launch ( psi ) ;
2933 MainWindow = App . GetMainWindow ( Automation , TimeSpan . FromSeconds ( 15 ) ) ;
3034 }
3135
@@ -34,7 +38,6 @@ public void Dispose()
3438 try { App . Close ( ) ; } catch { /* ignore if already closed */ }
3539 Automation . Dispose ( ) ;
3640 try { File . Delete ( _statePath ) ; } catch { }
37- Environment . SetEnvironmentVariable ( "CSM_STATE_PATH" , null ) ;
3841 }
3942
4043 private static string GetExePath ( )
0 commit comments