Skip to content

Commit 2f8edd9

Browse files
authored
More 'dotnet test' cleanups (#50596)
1 parent afd06de commit 2f8edd9

19 files changed

+114
-31
lines changed

src/Cli/dotnet/Commands/CliCommandStrings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2663,4 +2663,7 @@ Proceed?</value>
26632663
<data name="DotnetTestPipeFailureWithoutHandshake" xml:space="preserve">
26642664
<value>Error disposing 'NamedPipeServer', and no handshake was found.</value>
26652665
</data>
2666-
</root>
2666+
<data name="DotnetTestIncompatibleHandshakeVersion" xml:space="preserve">
2667+
<value>Supported protocol versions sent by Microsoft.Testing.Platform are '{0}'. The SDK supports '{1}', which is incompatible.</value>
2668+
</data>
2669+
</root>

src/Cli/dotnet/Commands/Test/CliConstants.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ internal static class HandshakeMessagePropertyNames
6565

6666
internal static class ProtocolConstants
6767
{
68-
internal const string Version = "1.0.0";
68+
/// <summary>
69+
/// The protocol versions that are supported by the current SDK. Multiple versions can be present and be semicolon separated.
70+
/// </summary>
71+
internal const string SupportedVersions = "1.0.0";
6972
}
7073

7174
internal static class ProjectProperties

src/Cli/dotnet/Commands/Test/CustomEventArgs.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55

66
namespace Microsoft.DotNet.Cli.Commands.Test;
77

8-
internal class HandshakeArgs : EventArgs
8+
internal sealed class HandshakeArgs : EventArgs
99
{
1010
public Handshake Handshake { get; set; }
11+
public bool GotSupportedVersion { get; set; }
1112
}
1213

13-
internal class HelpEventArgs : EventArgs
14+
internal sealed class HelpEventArgs : EventArgs
1415
{
1516
public string ModulePath { get; set; }
1617

1718
public CommandLineOption[] CommandLineOptions { get; set; }
1819
}
1920

20-
internal class DiscoveredTestEventArgs : EventArgs
21+
internal sealed class DiscoveredTestEventArgs : EventArgs
2122
{
2223
public string ExecutionId { get; set; }
2324

@@ -26,7 +27,7 @@ internal class DiscoveredTestEventArgs : EventArgs
2627
public DiscoveredTest[] DiscoveredTests { get; set; }
2728
}
2829

29-
internal class TestResultEventArgs : EventArgs
30+
internal sealed class TestResultEventArgs : EventArgs
3031
{
3132
public string ExecutionId { get; set; }
3233

@@ -37,7 +38,7 @@ internal class TestResultEventArgs : EventArgs
3738
public FailedTestResult[] FailedTestResults { get; set; }
3839
}
3940

40-
internal class FileArtifactEventArgs : EventArgs
41+
internal sealed class FileArtifactEventArgs : EventArgs
4142
{
4243
public string ExecutionId { get; set; }
4344

@@ -46,25 +47,19 @@ internal class FileArtifactEventArgs : EventArgs
4647
public FileArtifact[] FileArtifacts { get; set; }
4748
}
4849

49-
internal class SessionEventArgs : EventArgs
50+
internal sealed class SessionEventArgs : EventArgs
5051
{
5152
public TestSession SessionEvent { get; set; }
5253
}
5354

54-
internal class ErrorEventArgs : EventArgs
55+
internal sealed class ErrorEventArgs : EventArgs
5556
{
5657
public string ErrorMessage { get; set; }
5758
}
5859

59-
internal class TestProcessExitEventArgs : EventArgs
60+
internal sealed class TestProcessExitEventArgs : EventArgs
6061
{
6162
public List<string> OutputData { get; set; }
6263
public List<string> ErrorData { get; set; }
6364
public int ExitCode { get; set; }
6465
}
65-
66-
internal class ExecutionEventArgs : EventArgs
67-
{
68-
public string ModulePath { get; set; }
69-
public string ExecutionId { get; set; }
70-
}

src/Cli/dotnet/Commands/Test/IPC/NamedPipeServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public NamedPipeServer(
3030
CancellationToken cancellationToken,
3131
bool skipUnknownMessages)
3232
{
33-
_namedPipeServerStream = new((PipeName = pipeNameDescription).Name, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
33+
_namedPipeServerStream = new((PipeName = pipeNameDescription).Name, PipeDirection.InOut, maxNumberOfServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly);
3434
_callback = callback;
3535
_cancellationToken = cancellationToken;
3636
_skipUnknownMessages = skipUnknownMessages;

src/Cli/dotnet/Commands/Test/TestApplication.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ private Task<IResponse> OnRequest(NamedPipeServer server, IRequest request)
184184
{
185185
case HandshakeMessage handshakeMessage:
186186
_handshakes.Add(server, handshakeMessage);
187-
OnHandshakeMessage(handshakeMessage);
188-
return Task.FromResult((IResponse)CreateHandshakeMessage(GetSupportedProtocolVersion(handshakeMessage)));
187+
string negotiatedVersion = GetSupportedProtocolVersion(handshakeMessage);
188+
OnHandshakeMessage(handshakeMessage, negotiatedVersion.Length > 0);
189+
return Task.FromResult((IResponse)CreateHandshakeMessage(negotiatedVersion));
189190

190191
case CommandLineOptionMessages commandLineOptionMessages:
191192
OnCommandLineOptionMessages(commandLineOptionMessages);
@@ -235,15 +236,26 @@ private Task<IResponse> OnRequest(NamedPipeServer server, IRequest request)
235236

236237
private static string GetSupportedProtocolVersion(HandshakeMessage handshakeMessage)
237238
{
238-
handshakeMessage.Properties.TryGetValue(HandshakeMessagePropertyNames.SupportedProtocolVersions, out string protocolVersions);
239+
if (!handshakeMessage.Properties.TryGetValue(HandshakeMessagePropertyNames.SupportedProtocolVersions, out string protocolVersions) ||
240+
protocolVersions is null)
241+
{
242+
// It's not expected we hit this.
243+
// TODO: Maybe we should fail more hard?
244+
return string.Empty;
245+
}
239246

240-
string version = string.Empty;
241-
if (protocolVersions is not null && protocolVersions.Split(";").Contains(ProtocolConstants.Version))
247+
// NOTE: Today, ProtocolConstants.Version is only 1.0.0 (i.e, SDK supports only a single version).
248+
// Whenever we support multiple versions in SDK, we should do intersection
249+
// between protocolVersions given by MTP, and the versions supported by SDK.
250+
// Then we return the "highest" version from the intersection.
251+
// The current logic **assumes** that ProtocolConstants.SupportedVersions is a single version.
252+
if (protocolVersions.Split(";").Contains(ProtocolConstants.SupportedVersions))
242253
{
243-
version = ProtocolConstants.Version;
254+
return ProtocolConstants.SupportedVersions;
244255
}
245256

246-
return version;
257+
// The version given by MTP is not supported by SDK.
258+
return string.Empty;
247259
}
248260

249261
private static HandshakeMessage CreateHandshakeMessage(string version) =>
@@ -304,9 +316,9 @@ private bool ModulePathExists()
304316
return true;
305317
}
306318

307-
public void OnHandshakeMessage(HandshakeMessage handshakeMessage)
319+
public void OnHandshakeMessage(HandshakeMessage handshakeMessage, bool gotSupportedVersion)
308320
{
309-
HandshakeReceived?.Invoke(this, new HandshakeArgs { Handshake = new Handshake(handshakeMessage.Properties) });
321+
HandshakeReceived?.Invoke(this, new HandshakeArgs { Handshake = new Handshake(handshakeMessage.Properties), GotSupportedVersion = gotSupportedVersion });
310322
}
311323

312324
public void OnCommandLineOptionMessages(CommandLineOptionMessages commandLineOptionMessages)

src/Cli/dotnet/Commands/Test/TestApplicationEventHandlers.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ internal sealed class TestApplicationsEventHandlers(TerminalTestReporter output)
1818
public void OnHandshakeReceived(object sender, HandshakeArgs args)
1919
{
2020
var testApplication = (TestApplication)sender;
21-
// Today, it's 1.0.0 in MTP.
22-
// https://github.com/microsoft/testfx/blob/516eebb3c9b7e81eb2677c00b3d0b7867d8acb33/src/Platform/Microsoft.Testing.Platform/ServerMode/DotnetTest/IPC/Constants.cs#L40
23-
var supportedProtocolVersions = args.Handshake.Properties[HandshakeMessagePropertyNames.SupportedProtocolVersions];
24-
if (supportedProtocolVersions != "1.0.0" && !supportedProtocolVersions.Split(';').Contains("1.0.0"))
21+
if (!args.GotSupportedVersion)
2522
{
26-
_output.HandshakeFailure(testApplication.Module.TargetPath, string.Empty, ExitCode.GenericFailure, $"Supported protocol versions '{supportedProtocolVersions}' doesn't include '1.0.0' which is not supported by the current .NET SDK.", string.Empty);
23+
_output.HandshakeFailure(
24+
testApplication.Module.TargetPath,
25+
string.Empty,
26+
ExitCode.GenericFailure,
27+
string.Format(
28+
CliCommandStrings.DotnetTestIncompatibleHandshakeVersion,
29+
args.Handshake.Properties[HandshakeMessagePropertyNames.SupportedProtocolVersions],
30+
ProtocolConstants.SupportedVersions),
31+
string.Empty);
2732
}
2833

2934
var hostType = args.Handshake.Properties[HandshakeMessagePropertyNames.HostType];

src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)