Skip to content

Commit 1e3234c

Browse files
authored
Add support for running xUnit tests in VS Test Explorer (#642)
Fix issue with running xUnit tests over and over. Have a better way to detect when the session details file has been written. Fix issue with ServiceReturnsPowerShellVersionDetails test, where it only worked when you ran the tests using x86 arch. It now runs correctly using x64.
1 parent 781f573 commit 1e3234c

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,9 @@ await this.SendRequest(
825825
Assert.StartsWith("5.", versionDetails.Version);
826826
Assert.StartsWith("5.", versionDetails.DisplayVersion);
827827
Assert.Equal("Desktop", versionDetails.Edition);
828-
Assert.Equal("x86", versionDetails.Architecture);
828+
829+
string expectedArchitecture = (IntPtr.Size == 8) ? "x64" : "x86";
830+
Assert.Equal(expectedArchitecture, versionDetails.Architecture);
829831
}
830832

831833
private async Task SendOpenFileEvent(string filePath, bool waitForDiagnostics = true)

test/PowerShellEditorServices.Test.Host/PowerShellEditorServices.Test.Host.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<PackageReference Include="Microsoft.PowerShell.SDK">
2323
<Version>6.0.0-alpha13</Version>
2424
</PackageReference>
25+
<PackageReference Include="more.xunit.runner.visualstudio" Version="2.3.1" />
2526
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
2627
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
2728
</ItemGroup>

test/PowerShellEditorServices.Test.Host/ServerTestsBase.cs

+31-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ protected async Task<Tuple<int, int>> LaunchService(
5151
Path.Combine(
5252
Path.GetDirectoryName(assemblyPath), $"session-{++sessionCounter}.json");
5353

54+
if (File.Exists(sessionPath))
55+
{
56+
File.Delete(sessionPath);
57+
}
58+
5459
string editorServicesModuleVersion =
5560
string.Format(
5661
"{0}.{1}.{2}",
@@ -106,20 +111,42 @@ protected async Task<Tuple<int, int>> LaunchService(
106111
// Start the process
107112
this.serviceProcess.Start();
108113

109-
// Wait for the server to finish initializing
110-
while (!File.Exists(sessionPath) || (new FileInfo(sessionPath).Length == 0))
114+
string sessionDetailsText = string.Empty;
115+
116+
// Wait up to ~5 seconds for the server to finish initializing
117+
var maxRetryAttempts = 10;
118+
while (maxRetryAttempts-- > 0)
111119
{
112-
Thread.Sleep(100);
120+
try
121+
{
122+
using (var stream = new FileStream(sessionPath, FileMode.Open, FileAccess.Read, FileShare.None))
123+
using (var reader = new StreamReader(stream))
124+
{
125+
sessionDetailsText = reader.ReadToEnd();
126+
break;
127+
}
128+
}
129+
catch (FileNotFoundException)
130+
{
131+
}
132+
catch (Exception ex)
133+
{
134+
Debug.WriteLine($"Session details at '{sessionPath}' not available: {ex.Message}");
135+
}
136+
137+
Thread.Sleep(500);
113138
}
114139

115-
JObject result = JObject.Parse(File.ReadAllText(sessionPath));
140+
JObject result = JObject.Parse(sessionDetailsText);
116141
if (result["status"].Value<string>() == "started")
117142
{
118143
return new Tuple<int, int>(
119144
result["languageServicePort"].Value<int>(),
120145
result["debugServicePort"].Value<int>());
121146
}
122147

148+
Debug.WriteLine($"Failed to read session details from '{sessionPath}'");
149+
123150
return null;
124151
}
125152

test/PowerShellEditorServices.Test.Protocol/PowerShellEditorServices.Test.Protocol.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="more.xunit.runner.visualstudio" Version="2.3.1" />
1516
<PackageReference Include="Newtonsoft.Json">
1617
<Version>9.0.1</Version>
1718
</PackageReference>

test/PowerShellEditorServices.Test/PowerShellEditorServices.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PackageReference Include="Microsoft.PowerShell.SDK">
1616
<Version>6.0.0-alpha13</Version>
1717
</PackageReference>
18+
<PackageReference Include="more.xunit.runner.visualstudio" Version="2.3.1" />
1819
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
1920
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
2021
</ItemGroup>

0 commit comments

Comments
 (0)