From b248a36b681f97857710e30b4c3626b8fc4230ec Mon Sep 17 00:00:00 2001 From: Nigusu Solomon Yenework <59111203+Nigusu-Allehu@users.noreply.github.com> Date: Fri, 7 Feb 2025 15:08:55 -0800 Subject: [PATCH] Refactor PluginDiscovererTests.cs (#6257) --- .../Plugins/PluginDiscovererTests.cs | 208 +++++++----------- 1 file changed, 75 insertions(+), 133 deletions(-) diff --git a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Plugins/PluginDiscovererTests.cs b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Plugins/PluginDiscovererTests.cs index 44d148d3b3b..c2e792f67d9 100644 --- a/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Plugins/PluginDiscovererTests.cs +++ b/test/NuGet.Core.Tests/NuGet.Protocol.Tests/Plugins/PluginDiscovererTests.cs @@ -3,7 +3,9 @@ using System; using System.Collections.Generic; -using System.Diagnostics; +# if !NET8_0_OR_GREATER +using Microsoft.Internal.NuGet.Testing.SignedPackages.ChildProcess; +#endif using System.IO; using System.Linq; using System.Threading; @@ -12,11 +14,19 @@ using NuGet.Common; using NuGet.Test.Utility; using Xunit; +using Xunit.Abstractions; namespace NuGet.Protocol.Plugins.Tests { public class PluginDiscovererTests { + private readonly ITestOutputHelper _testOutputHelper; + + public PluginDiscovererTests(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = testOutputHelper; + } + [Theory] [InlineData(null)] [InlineData("")] @@ -199,14 +209,7 @@ public async Task DiscoverAsync_withValidDotNetToolsPluginWindows_FindsThePlugin var result = await discoverer.DiscoverAsync(CancellationToken.None); // Assert - var discovered = false; - - foreach (PluginDiscoveryResult discoveryResult in result) - { - if (myPlugin == discoveryResult.PluginFile.Path) discovered = true; - } - - Assert.True(discovered); + Assert.Contains(result, discoveryResult => myPlugin == discoveryResult.PluginFile.Path); } } } @@ -233,14 +236,7 @@ public async Task DiscoverAsync_WithPluginPathSpecifiedInNuGetPluginPathsEnvVari var result = await discoverer.DiscoverAsync(CancellationToken.None); // Assert - var discovered = false; - - foreach (PluginDiscoveryResult discoveryResult in result) - { - if (myPlugin == discoveryResult.PluginFile.Path) discovered = true; - } - - Assert.True(discovered); + Assert.Contains(result, discoveryResult => myPlugin == discoveryResult.PluginFile.Path); } } } @@ -267,14 +263,7 @@ public async Task DiscoverAsync_withInValidDotNetToolsPluginNameWindows_DoesNotF var result = await discoverer.DiscoverAsync(CancellationToken.None); // Assert - var discovered = false; - - foreach (PluginDiscoveryResult discoveryResult in result) - { - if (myPlugin == discoveryResult.PluginFile.Path) discovered = true; - } - - Assert.False(discovered); + Assert.DoesNotContain(result, discoveryResult => myPlugin == discoveryResult.PluginFile.Path); } } } @@ -291,35 +280,14 @@ public async Task DiscoverAsync_withValidDotNetToolsPluginLinux_FindsThePlugin() File.WriteAllText(myPlugin, string.Empty); Mock environmentalVariableReader = new Mock(); environmentalVariableReader.Setup(env => env.GetEnvironmentVariable(EnvironmentVariableConstants.PluginPaths)).Returns(pluginPath); - - using (var process = new Process()) + SetFileExecutable(myPlugin, true); + using (var discoverer = new PluginDiscoverer(environmentalVariableReader.Object)) { - // Use a shell command to make the file executable - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod +x {myPlugin}\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); - - if (process.ExitCode == 0) - { - using (var discoverer = new PluginDiscoverer(environmentalVariableReader.Object)) - { - // Act - var result = await discoverer.DiscoverAsync(CancellationToken.None); - - // Assert - var discovered = false; - - foreach (PluginDiscoveryResult discoveryResult in result) - { - if (myPlugin == discoveryResult.PluginFile.Path) discovered = true; - } + // Act + var result = await discoverer.DiscoverAsync(CancellationToken.None); - Assert.True(discovered); - } - } + // Assert + Assert.Contains(result, discoveryResult => myPlugin == discoveryResult.PluginFile.Path); } } } @@ -338,34 +306,15 @@ public async Task DiscoverAsync_WithPluginPathSpecifiedInNuGetPluginPathsEnvVari environmentalVariableReader.Setup(env => env.GetEnvironmentVariable(EnvironmentVariableConstants.PluginPaths)).Returns(pluginPath); environmentalVariableReader.Setup(env => env.GetEnvironmentVariable("PATHS")).Returns(""); - using (var process = new Process()) - { - // Use a shell command to make the file executable - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod +x {myPlugin}\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); - - if (process.ExitCode == 0) - { - using (var discoverer = new PluginDiscoverer(environmentalVariableReader.Object)) - { - // Act - var result = await discoverer.DiscoverAsync(CancellationToken.None); - - // Assert - var discovered = false; + SetFileExecutable(myPlugin, true); - foreach (PluginDiscoveryResult discoveryResult in result) - { - if (myPlugin == discoveryResult.PluginFile.Path) discovered = true; - } + using (var discoverer = new PluginDiscoverer(environmentalVariableReader.Object)) + { + // Act + var result = await discoverer.DiscoverAsync(CancellationToken.None); - Assert.True(discovered); - } - } + // Assert + Assert.Contains(result, discoveryResult => myPlugin == discoveryResult.PluginFile.Path); } } } @@ -383,34 +332,15 @@ public async Task DiscoverAsync_withNoExecutableValidDotNetToolsPluginLinux_Does Mock environmentalVariableReader = new Mock(); environmentalVariableReader.Setup(env => env.GetEnvironmentVariable(EnvironmentVariableConstants.PluginPaths)).Returns(pluginPath); - using (var process = new Process()) - { - // Use a shell command to make the file not executable - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod -x {myPlugin}\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); - - if (process.ExitCode == 0) - { - using (var discoverer = new PluginDiscoverer(environmentalVariableReader.Object)) - { - // Act - var result = await discoverer.DiscoverAsync(CancellationToken.None); - - // Assert - var discovered = false; + SetFileExecutable(myPlugin, false); - foreach (PluginDiscoveryResult discoveryResult in result) - { - if (myPlugin == discoveryResult.PluginFile.Path) discovered = true; - } + using (var discoverer = new PluginDiscoverer(environmentalVariableReader.Object)) + { + // Act + var result = await discoverer.DiscoverAsync(CancellationToken.None); - Assert.False(discovered); - } - } + // Assert + Assert.DoesNotContain(result, discoveryResult => myPlugin == discoveryResult.PluginFile.Path); } } } @@ -636,14 +566,7 @@ public void IsValidPluginFile_Unix_ExecutableFile_ReturnsTrue() // Set execute permissions File.SetUnixFileMode(pluginFilePath, UnixFileMode.UserExecute | UnixFileMode.UserRead); #else - // Use chmod to set execute permissions - var process = new Process(); - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod +x {pluginFilePath}\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); + SetFileExecutable(pluginFilePath, true); #endif var fileInfo = new FileInfo(pluginFilePath); @@ -666,13 +589,7 @@ public void IsExecutable_FileIsExecutable_ReturnsTrue() File.Create(pluginFilePath); // Set execute permissions - var process = new Process(); - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod +x {pluginFilePath}\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); + SetFileExecutable(pluginFilePath, true); var fileInfo = new FileInfo(pluginFilePath); @@ -693,13 +610,7 @@ public void IsExecutable_FileIsNotExecutable_ReturnsFalse() File.Create(pluginFilePath); // Remove execute permissions - var process = new Process(); - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod -x {pluginFilePath}\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); + SetFileExecutable(pluginFilePath, false); var fileInfo = new FileInfo(pluginFilePath); @@ -720,13 +631,7 @@ public void IsExecutable_FileWithSpace_ReturnsTrue() File.Create(pluginFilePath).Dispose(); // Set execute permissions - var process = new Process(); - process.StartInfo.FileName = "/bin/bash"; - process.StartInfo.Arguments = $"-c \"chmod +x '{pluginFilePath}'\""; - process.StartInfo.UseShellExecute = false; - process.StartInfo.RedirectStandardOutput = true; - process.Start(); - process.WaitForExit(); + SetFileExecutable(pluginFilePath, true); var fileInfo = new FileInfo(pluginFilePath); @@ -738,5 +643,42 @@ public void IsExecutable_FileWithSpace_ReturnsTrue() } #endif + + private void SetFileExecutable(string filePath, bool executable) + { + if (!File.Exists(filePath)) + { + throw new FileNotFoundException($"File not found: {filePath}"); + } +#if NET8_0_OR_GREATER + try + { + File.SetUnixFileMode(filePath, executable ? + UnixFileMode.UserExecute | UnixFileMode.UserRead | UnixFileMode.UserWrite : + UnixFileMode.UserRead | UnixFileMode.UserWrite); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Error while setting file mode for: {filePath}", ex); + } +#else + try + { + CommandRunnerResult result = CommandRunner.Run( + filename: "/bin/bash", + arguments: $"-c \"chmod {(executable ? "+x" : "-x")} '{filePath}'\"", + testOutputHelper: _testOutputHelper); + + if (!result.Success) + { + throw new InvalidOperationException($"Failed to set execute permissions for {filePath}. Error: {result.Errors}"); + } + } + catch (Exception ex) + { + throw new InvalidOperationException($"Error setting file executable permission for {filePath}", ex); + } +#endif + } } }