Skip to content

Commit f23b08d

Browse files
committed
fix: scope CSM_STATE_PATH to child process, add UITests CollectionDefinition
1 parent 30129c6 commit f23b08d

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

tests/CodeShellManager.UITests/AppFixture.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using FlaUI.Core;
45
using 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>
1315
public 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()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Xunit;
2+
3+
namespace CodeShellManager.UITests;
4+
5+
/// <summary>
6+
/// Declares the "UITests" xUnit collection with parallelization disabled.
7+
/// All test classes decorated with [Collection("UITests")] run sequentially,
8+
/// preventing multiple AppFixture instances from driving the WPF app simultaneously.
9+
/// </summary>
10+
[CollectionDefinition("UITests", DisableParallelization = true)]
11+
public sealed class UITestsCollection { }

0 commit comments

Comments
 (0)