From b809ee5969cf4cf9d1e69c8e207f2c5b7965b844 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Thu, 12 Nov 2020 23:16:52 +0000 Subject: [PATCH 1/6] Enable CA1816: Dispose methods should call SuppressFinalize https://docs.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1816 --- .globalconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.globalconfig b/.globalconfig index 32bea9725fd..4590b0704f8 100644 --- a/.globalconfig +++ b/.globalconfig @@ -266,7 +266,7 @@ dotnet_diagnostic.CA1814.severity = none dotnet_diagnostic.CA1815.severity = none # CA1816: Dispose methods should call SuppressFinalize -dotnet_diagnostic.CA1816.severity = suggestion +dotnet_diagnostic.CA1816.severity = warning # CA1819: Properties should not return arrays dotnet_diagnostic.CA1819.severity = none From 56285e94066a5f933d123863ee8604a6ed95f7bb Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Thu, 12 Nov 2020 23:54:44 +0000 Subject: [PATCH 2/6] Fix CommandLineCmdletBase --- .../commands/management/ComputerUnix.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 023fc1d0501..0789f0d008d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -105,7 +105,28 @@ public class CommandLineCmdletBase : PSCmdlet, IDisposable /// public void Dispose() { - _process?.Dispose(); + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// + /// Dispose(bool disposing) executes in two distinct scenarios. + /// If disposing equals true, the method has been called directly + /// or indirectly by a user's code. Managed and unmanaged resources + /// can be disposed. + /// If disposing equals false, the method has been called by the + /// runtime from inside the finalizer and you should not reference + /// other objects. Only unmanaged resources can be disposed. + /// + /// + /// Whether it is directly called. + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + _process?.Dispose(); + } } #endregion "IDisposable Members" From 62941ecd5e6753c4a1ae7d57f6dfd8f7f3bd48df Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 13 Nov 2020 03:15:47 +0000 Subject: [PATCH 3/6] Fix tests --- test/xUnit/csharp/test_FileSystemProvider.cs | 13 ++++++++-- test/xUnit/csharp/test_PSConfiguration.cs | 27 +++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/test/xUnit/csharp/test_FileSystemProvider.cs b/test/xUnit/csharp/test_FileSystemProvider.cs index ef702d47bd6..23702755e77 100644 --- a/test/xUnit/csharp/test_FileSystemProvider.cs +++ b/test/xUnit/csharp/test_FileSystemProvider.cs @@ -39,9 +39,18 @@ public FileSystemProviderTests() File.AppendAllText(testPath, testContent); } - void IDisposable.Dispose() + public void Dispose() { - File.Delete(testPath); + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + File.Delete(testPath); + } } private ExecutionContext GetExecutionContext() diff --git a/test/xUnit/csharp/test_PSConfiguration.cs b/test/xUnit/csharp/test_PSConfiguration.cs index 6237f453335..ef1cf21715d 100644 --- a/test/xUnit/csharp/test_PSConfiguration.cs +++ b/test/xUnit/csharp/test_PSConfiguration.cs @@ -97,18 +97,27 @@ public PowerShellPolicyFixture() public void Dispose() { - CleanupConfigFiles(); - if (systemWideConfigBackupFile != null) - { - File.Move(systemWideConfigBackupFile, systemWideConfigFile); - } + Dispose(true); + GC.SuppressFinalize(this); + } - if (currentUserConfigBackupFile != null) + protected virtual void Dispose(bool disposing) + { + if (disposing) { - File.Move(currentUserConfigBackupFile, currentUserConfigFile); - } + CleanupConfigFiles(); + if (systemWideConfigBackupFile != null) + { + File.Move(systemWideConfigBackupFile, systemWideConfigFile); + } - InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue; + if (currentUserConfigBackupFile != null) + { + File.Move(currentUserConfigBackupFile, currentUserConfigFile); + } + + InternalTestHooks.BypassGroupPolicyCaching = originalTestHookValue; + } } internal PowerShellPolicies SystemWidePolicies From a312899c60d3c83c6981adb9e861f0f343271d1a Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Sat, 21 Nov 2020 23:56:08 +0000 Subject: [PATCH 4/6] Remove obvious comments --- .../commands/management/ComputerUnix.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 0789f0d008d..28e5d5b6caf 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -100,27 +100,12 @@ public class CommandLineCmdletBase : PSCmdlet, IDisposable #region "IDisposable Members" - /// - /// Dispose Method. - /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } - /// - /// - /// Dispose(bool disposing) executes in two distinct scenarios. - /// If disposing equals true, the method has been called directly - /// or indirectly by a user's code. Managed and unmanaged resources - /// can be disposed. - /// If disposing equals false, the method has been called by the - /// runtime from inside the finalizer and you should not reference - /// other objects. Only unmanaged resources can be disposed. - /// - /// - /// Whether it is directly called. protected virtual void Dispose(bool disposing) { if (disposing) From 0a6628ee5c85359253159f4e27f0e72b38e5fdbd Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Sun, 22 Nov 2020 08:00:47 +0000 Subject: [PATCH 5/6] Update comments --- .../commands/management/ComputerUnix.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index 28e5d5b6caf..b55183a9288 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -100,12 +100,23 @@ public class CommandLineCmdletBase : PSCmdlet, IDisposable #region "IDisposable Members" + /// + /// Releases all resources used by the . + /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } + /// + /// Releases the unmanaged resources used by the + /// and optionally releases the managed resources. + /// + /// + /// true to release both managed and unmanaged resources; + /// false to release only unmanaged resources. + /// protected virtual void Dispose(bool disposing) { if (disposing) From 16f615627f098fc10c4409b707e9f6d2f88f2975 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sun, 22 Nov 2020 09:38:49 +0000 Subject: [PATCH 6/6] Fix DOC104UseSeeLangword --- .../commands/management/ComputerUnix.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs index b55183a9288..23894ed9f63 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ComputerUnix.cs @@ -114,8 +114,8 @@ public void Dispose() /// and optionally releases the managed resources. /// /// - /// true to release both managed and unmanaged resources; - /// false to release only unmanaged resources. + /// to release both managed and unmanaged resources; + /// to release only unmanaged resources. /// protected virtual void Dispose(bool disposing) {