Skip to content

Commit

Permalink
Refactor PluginDiscovererTests.cs (#6257)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigusu-Allehu authored Feb 7, 2025
1 parent 75a3792 commit b248a36
Showing 1 changed file with 75 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("")]
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -291,35 +280,14 @@ public async Task DiscoverAsync_withValidDotNetToolsPluginLinux_FindsThePlugin()
File.WriteAllText(myPlugin, string.Empty);
Mock<IEnvironmentVariableReader> environmentalVariableReader = new Mock<IEnvironmentVariableReader>();
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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -383,34 +332,15 @@ public async Task DiscoverAsync_withNoExecutableValidDotNetToolsPluginLinux_Does
Mock<IEnvironmentVariableReader> environmentalVariableReader = new Mock<IEnvironmentVariableReader>();
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);
}
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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
}
}
}

0 comments on commit b248a36

Please sign in to comment.