diff --git a/.editorconfig b/.editorconfig index 2551c98a21..7388677c1a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -206,6 +206,9 @@ dotnet_diagnostic.CA1840.severity = warning # not default, increased severity to # CA1041: Provide ObsoleteAttribute message dotnet_diagnostic.CA1041.severity = warning # not default, increased severity to ensure it is applied +# CA1837: Use Environment.ProcessId instead of Process.GetCurrentProcess().Id +dotnet_diagnostic.CA1837.severity = warning # not default, increased severity to ensure it is applied + #### C# Coding Conventions #### # var preferences diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTests.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTests.cs index 86aa86fbe7..9673e2cfc5 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTests.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/BaseRunTests.cs @@ -411,7 +411,11 @@ private bool RunTestInternalWithExecutors(IEnumerable> execut EqtTrace.Verbose("Attaching to default test host."); attachedToTestHost = true; +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else var pid = Process.GetCurrentProcess().Id; +#endif if (!FrameworkHandle.AttachDebuggerToProcess(pid)) { EqtTrace.Warning( diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs index 93a201f16c..485cb1630d 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/PostProcessing/ArtifactProcessingManager.cs @@ -65,7 +65,14 @@ public ArtifactProcessingManager(string? testSessionCorrelationId, { _testSessionCorrelationId = testSessionCorrelationId; _processArtifactFolder = Path.Combine(_fileHelper.GetTempPath(), _testSessionCorrelationId); - _testSessionProcessArtifactFolder = Path.Combine(_processArtifactFolder, $"{Process.GetCurrentProcess().Id}_{Guid.NewGuid()}"); +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif + _testSessionProcessArtifactFolder = Path.Combine(_processArtifactFolder, $"{pid}_{Guid.NewGuid()}"); } } diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs index 5e76f3adcd..7f9aff9aef 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs @@ -3,7 +3,9 @@ using System; using System.Collections.Generic; +#if !NET5_0_OR_GREATER using System.Diagnostics; +#endif using System.Globalization; using System.Linq; using System.Threading; @@ -150,7 +152,12 @@ public void StartSession() if (port > 0) { // Fill the parameters - _consoleParameters.ParentProcessId = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + _consoleParameters.ParentProcessId = Environment.ProcessId; +#else + using (var process = Process.GetCurrentProcess()) + _consoleParameters.ParentProcessId = process.Id; +#endif _consoleParameters.PortNumber = port; // Start vstest.console.exe process @@ -576,7 +583,12 @@ public async Task StartSessionAsync() if (port > 0) { // Fill the parameters - _consoleParameters.ParentProcessId = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + _consoleParameters.ParentProcessId = Environment.ProcessId; +#else + using (var process = Process.GetCurrentProcess()) + _consoleParameters.ParentProcessId = process.Id; +#endif _consoleParameters.PortNumber = port; // Start vstest.console.exe process diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs index 32a3e7d9d3..f64cc5c876 100644 --- a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/ProxyOperationManagerTests.cs @@ -109,12 +109,20 @@ public void SetupChannelShouldAddRunnerProcessIdForTestHost() _testOperationManager.SetupChannel(Enumerable.Empty(), DefaultRunSettings); +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif + _mockTestHostManager.Verify( th => th.GetTestHostProcessStartInfo( It.IsAny>(), It.IsAny>(), - It.Is(t => t.RunnerProcessId.Equals(Process.GetCurrentProcess().Id)))); + It.Is(t => t.RunnerProcessId.Equals(pid)))); } [TestMethod] diff --git a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs index 1aa1c3e350..ccbd93865e 100644 --- a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs +++ b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DefaultTestHostManagerTests.cs @@ -341,7 +341,14 @@ public void LaunchTestHostShouldReturnTestHostProcessId() Assert.IsTrue(processId.Result); - Assert.AreEqual(Process.GetCurrentProcess().Id, _testHostId); +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif + Assert.AreEqual(pid, _testHostId); } [TestMethod] @@ -471,7 +478,13 @@ public async Task ProcessExitedButNoErrorMessageIfNoDataWritten(int exitCode) [TestMethod] public async Task CleanTestHostAsyncShouldKillTestHostProcess() { - var pid = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif bool isVerified = false; _mockProcessHelper.Setup(ph => ph.TerminateProcess(It.IsAny())) .Callback(p => isVerified = ((Process)p).Id == pid); @@ -486,7 +499,13 @@ public async Task CleanTestHostAsyncShouldKillTestHostProcess() [TestMethod] public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted() { - var pid = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif bool isVerified = false; _mockProcessHelper.Setup(ph => ph.TerminateProcess(It.IsAny())).Callback(p => isVerified = ((Process)p).Id == pid).Throws(); diff --git a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs index 7fb0d2f91f..2dd103de21 100644 --- a/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs +++ b/test/Microsoft.TestPlatform.TestHostProvider.UnitTests/Hosting/DotnetTestHostManagerTests.cs @@ -90,13 +90,21 @@ public DotnetTestHostManagerTests() _mockFileHelper.Setup(ph => ph.Exists(_defaultTestHostPath)).Returns(true); _mockFileHelper.Setup(ph => ph.Exists(DefaultDotnetPath)).Returns(true); +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif + _mockTestHostLauncher .Setup(th => th.LaunchTestHost(It.IsAny(), It.IsAny())) - .Returns(Process.GetCurrentProcess().Id); + .Returns(pid); _mockTestHostLauncher .Setup(th => th.LaunchTestHost(It.IsAny())) - .Returns(Process.GetCurrentProcess().Id); + .Returns(pid); _defaultTestProcessStartInfo = _dotnetHostManager.GetTestHostProcessStartInfo(new[] { defaultSourcePath }, null, _defaultConnectionInfo); } @@ -430,7 +438,13 @@ public void GetTestHostProcessStartInfoShouldUseTestHostX86ExeFromNugetIfNotFoun [TestMethod] public void LaunchTestHostShouldLaunchProcessWithNullEnvironmentVariablesOrArgs() { - var expectedProcessId = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var expectedProcessId = Environment.ProcessId; +#else + int expectedProcessId; + using (var p = Process.GetCurrentProcess()) + expectedProcessId = p.Id; +#endif _mockTestHostLauncher.Setup(thl => thl.LaunchTestHost(It.IsAny(), It.IsAny())).Returns(expectedProcessId); _mockFileHelper.Setup(ph => ph.Exists("testhost.dll")).Returns(true); var startInfo = GetDefaultStartInfo(); @@ -448,7 +462,13 @@ public void LaunchTestHostShouldLaunchProcessWithNullEnvironmentVariablesOrArgs( [TestMethod] public void LaunchTestHostAsyncShouldNotStartHostProcessIfCancellationTokenIsSet() { - var expectedProcessId = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var expectedProcessId = Environment.ProcessId; +#else + int expectedProcessId; + using (var p = Process.GetCurrentProcess()) + expectedProcessId = p.Id; +#endif _mockTestHostLauncher.Setup(thl => thl.LaunchTestHost(It.IsAny())).Returns(expectedProcessId); _mockFileHelper.Setup(ph => ph.Exists("testhost.dll")).Returns(true); var startInfo = GetDefaultStartInfo(); @@ -577,7 +597,13 @@ public async Task LaunchTestHostShouldLaunchProcessWithConnectionInfo() [TestMethod] public void LaunchTestHostShouldSetExitCallBackInCaseCustomHost() { - var expectedProcessId = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var expectedProcessId = Environment.ProcessId; +#else + int expectedProcessId; + using (var p = Process.GetCurrentProcess()) + expectedProcessId = p.Id; +#endif _mockTestHostLauncher.Setup(thl => thl.LaunchTestHost(It.IsAny(), It.IsAny())).Returns(expectedProcessId); _mockFileHelper.Setup(ph => ph.Exists("testhost.dll")).Returns(true); @@ -959,7 +985,13 @@ public async Task DotNetCoreProcessExitedButNoErrorMessageIfNoDataWritten(int ex [TestMethod] public async Task CleanTestHostAsyncShouldKillTestHostProcess() { - var pid = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif bool isVerified = false; _mockProcessHelper.Setup(ph => ph.TerminateProcess(It.IsAny())) .Callback(p => isVerified = ((Process)p).Id == pid); @@ -975,7 +1007,13 @@ public async Task CleanTestHostAsyncShouldKillTestHostProcess() [TestMethod] public async Task CleanTestHostAsyncShouldNotThrowIfTestHostIsNotStarted() { - var pid = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif bool isVerified = false; _mockProcessHelper.Setup(ph => ph.TerminateProcess(It.IsAny())).Callback(p => isVerified = ((Process)p).Id == pid).Throws(); diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs index 27cb906d03..4dbd851556 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperAsyncTests.cs @@ -4,6 +4,9 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +#if !NET5_0_OR_GREATER +using System.Diagnostics; +#endif using System.Threading; using System.Threading.Tasks; @@ -57,7 +60,13 @@ public VsTestConsoleWrapperAsyncTests() public async Task StartSessionAsyncShouldStartVsTestConsoleWithCorrectArguments() { var inputPort = 123; - int expectedParentProcessId = System.Diagnostics.Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var expectedParentProcessId = Environment.ProcessId; +#else + int expectedParentProcessId; + using (var p = Process.GetCurrentProcess()) + expectedParentProcessId = p.Id; +#endif _mockRequestSender.Setup(rs => rs.InitializeCommunicationAsync(It.IsAny())).Returns(Task.FromResult(inputPort)); await _consoleWrapper.StartSessionAsync(); diff --git a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs index f6301485e4..a9f58cd2b3 100644 --- a/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs +++ b/test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs @@ -58,7 +58,13 @@ public VsTestConsoleWrapperTests() public void StartSessionShouldStartVsTestConsoleWithCorrectArguments() { var inputPort = 123; - int expectedParentProcessId = Process.GetCurrentProcess().Id; +#if NET5_0_OR_GREATER + var expectedParentProcessId = Environment.ProcessId; +#else + int expectedParentProcessId; + using (var p = Process.GetCurrentProcess()) + expectedParentProcessId = p.Id; +#endif _mockRequestSender.Setup(rs => rs.InitializeCommunication()).Returns(inputPort); _consoleWrapper.StartSession(); diff --git a/test/vstest.console.UnitTests/Processors/PortArgumentProcessorTests.cs b/test/vstest.console.UnitTests/Processors/PortArgumentProcessorTests.cs index c1e3979ca1..612028845b 100644 --- a/test/vstest.console.UnitTests/Processors/PortArgumentProcessorTests.cs +++ b/test/vstest.console.UnitTests/Processors/PortArgumentProcessorTests.cs @@ -2,7 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +#if !NET5_0_OR_GREATER using System.Diagnostics; +#endif using Microsoft.VisualStudio.TestPlatform.Client.DesignMode; using Microsoft.VisualStudio.TestPlatform.Client.RequestHelper; @@ -120,12 +122,18 @@ public void ExecutorInitializeShouldSetProcessExitCallback() { _executor = new PortArgumentExecutor(CommandLineOptions.Instance, _testRequestManager.Object, _mockProcessHelper.Object); int port = 2345; - int processId = Process.GetCurrentProcess().Id; - CommandLineOptions.Instance.ParentProcessId = processId; +#if NET5_0_OR_GREATER + var pid = Environment.ProcessId; +#else + int pid; + using (var p = Process.GetCurrentProcess()) + pid = p.Id; +#endif + CommandLineOptions.Instance.ParentProcessId = pid; _executor.Initialize(port.ToString()); - _mockProcessHelper.Verify(ph => ph.SetExitCallback(processId, It.IsAny>()), Times.Once); + _mockProcessHelper.Verify(ph => ph.SetExitCallback(pid, It.IsAny>()), Times.Once); } [TestMethod]