Skip to content

Commit

Permalink
# This is a combination of 25 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

Read unix executable plugins

# This is the commit message #2:

Add tests

# This is the commit message #3:

Use GetUnixFileMode

# This is the commit message #4:

Cleanup testing

# This is the commit message #5:

Add NUGET_PLUGIN_PATHS

# This is the commit message #6:

use empty body constructor

# This is the commit message #7:

NuGetPluginPaths Env variable used by dotnet tools only

# This is the commit message #8:

Plugins path env

# This is the commit message #9:

fix tests

# This is the commit message #10:

fix linux bash call

# This is the commit message #11:

remove some apis

# This is the commit message #12:

cleanup

# This is the commit message #13:

cleanup

# This is the commit message #14:

cleanup
# This is the commit message #15:

Use GetUnixFileMode

# This is the commit message #16:

Cleanup testing

# This is the commit message #17:

remove some apis

# This is the commit message #18:

stub

# This is the commit message #19:

Add test

# This is the commit message #20:

cleanup
# This is the commit message #21:

cleanup

# This is the commit message #22:

cleanup

# This is the commit message #23:

oops
# This is the commit message #24:

Clean up
# This is the commit message #25:

undo
  • Loading branch information
Nigusu-Allehu committed Oct 22, 2024
1 parent edc0101 commit 0380e40
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#nullable enable
~NuGet.Protocol.Plugins.IPluginFactory.GetOrCreateNetToolsPluginAsync(string filePath, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
~NuGet.Protocol.Plugins.PluginFactory.GetOrCreateNetToolsPluginAsync(string filePath, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Protocol/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
[assembly: SuppressMessage("Build", "CA1031:Modify 'FireBeforeClose' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.Plugin.FireBeforeClose")]
[assembly: SuppressMessage("Build", "CA1031:Modify 'FireClosed' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.Plugin.FireClosed")]
[assembly: SuppressMessage("Build", "CA2000:Call System.IDisposable.Dispose on object created by 'new MonitorNuGetProcessExitRequestHandler(plugin)' before all references to it are out of scope.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.PluginFactory.CreateFromCurrentProcessAsync(NuGet.Protocol.Plugins.IRequestHandlers,NuGet.Protocol.Plugins.ConnectionOptions,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Protocol.Plugins.IPlugin}")]
[assembly: SuppressMessage("Build", "CA2000:Use recommended dispose pattern to ensure that object created by 'new PluginProcess(startInfo)' is disposed on all paths. If possible, wrap the creation within a 'using' statement or a 'using' declaration. Otherwise, use a try-finally pattern, with a dedicated local variable declared before the try region and an unconditional Dispose invocation on non-null value in the 'finally' region, say 'x?.Dispose()'. If the object is explicitly disposed within the try region or the dispose ownership is transfered to another object or method, assign 'null' to the local variable just after such an operation to prevent double dispose in 'finally'.", Justification = "The responsibility to dispose the object is transferred to another object or wrapper that's created in the method and returned to the caller", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.PluginFactory.CreatePluginAsync(System.String,System.Collections.Generic.IEnumerable{System.String},NuGet.Protocol.Plugins.IRequestHandlers,NuGet.Protocol.Plugins.ConnectionOptions,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Protocol.Plugins.IPlugin}")]
[assembly: SuppressMessage("Build", "CA1031:Modify 'SendCloseRequest' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.PluginFactory.SendCloseRequest(NuGet.Protocol.Plugins.IPlugin)")]
[assembly: SuppressMessage("Build", "CA1822:Member GetPluginOperationClaimsAsync does not access instance data and can be marked as static (Shared in VisualBasic)", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.PluginManager.GetPluginOperationClaimsAsync(NuGet.Protocol.Plugins.IPlugin,System.String,Newtonsoft.Json.Linq.JObject,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Collections.Generic.IReadOnlyList{NuGet.Protocol.Plugins.OperationClaim}}")]
[assembly: SuppressMessage("Build", "CA1031:Modify 'TryCreatePluginAsync' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.Protocol.Plugins.PluginManager.TryCreatePluginAsync(NuGet.Protocol.Plugins.PluginDiscoveryResult,NuGet.Protocol.Plugins.OperationClaim,NuGet.Protocol.Plugins.PluginManager.PluginRequestKey,System.String,Newtonsoft.Json.Linq.JObject,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Tuple{System.Boolean,NuGet.Protocol.Plugins.PluginCreationResult}}")]
Expand Down
2 changes: 1 addition & 1 deletion src/NuGet.Core/NuGet.Protocol/NuGet.Protocol.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(TargetFrameworksLibraryForSigning);</TargetFrameworks>
<TargetFrameworks>$(TargetFrameworksLibraryForSigning)</TargetFrameworks>
<TargetFramework />
<NoWarn>$(NoWarn);CS1591;CS1573;CS0012;RS0041</NoWarn>
<PackageTags>nuget protocol</PackageTags>
Expand Down
3 changes: 0 additions & 3 deletions src/NuGet.Core/NuGet.Protocol/Plugins/PluginDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ internal PluginDiscoverer(IEnvironmentVariableReader environmentVariableReader)
}

_semaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1);
_environmentVariableReader = environmentVariableReader;
}

/// <summary>
Expand Down Expand Up @@ -186,8 +185,6 @@ internal List<PluginFile> GetPluginsInNuGetPluginPaths()
var pluginFiles = new List<PluginFile>();
string[] paths = _nuGetPluginPaths?.Split(Path.PathSeparator) ?? Array.Empty<string>();

paths = nugetPluginPaths?.Split(Path.PathSeparator) ?? Array.Empty<string>();

foreach (var path in paths)
{
if (PathValidator.IsValidLocalPath(path) || PathValidator.IsValidUncPath(path))
Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/Plugins/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task<IEnumerable<PluginCreationResult>> CreatePluginsAsync(
var pluginCreationResults = new List<PluginCreationResult>();

// Fast path
if (source.PackageSource.IsHttp)
if (source.PackageSource.IsHttp && IsPluginPossiblyAvailable())
{
var serviceIndex = await source.GetResourceAsync<ServiceIndexResourceV3>(cancellationToken);

Expand Down Expand Up @@ -325,6 +325,7 @@ private void Initialize(IEnvironmentVariableReader reader,
{
throw new ArgumentNullException(nameof(pluginFactoryCreator));
}

_connectionOptions = ConnectionOptions.CreateDefault(reader);

var idleTimeoutInSeconds = EnvironmentVariableReader.GetEnvironmentVariable(EnvironmentVariableConstants.IdleTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,43 +245,6 @@ public async Task DiscoverAsync_WithPluginPathSpecifiedInNuGetPluginPathsEnvVari
}
}

[PlatformTheory(Platform.Windows)]
[InlineData("nuget-plugin-myPlugin.exe")]
[InlineData("nuget-plugin-myPlugin.bat")]
public async Task DiscoverAsync_WithPluginPathSpecifiedInNuGetPluginPathsEnvVariableWindows_FindsThePlugin(string fileName)
{
using (var testDirectory = TestDirectory.Create())
{
// Arrange
var pluginPath = Path.Combine(testDirectory.Path, "myPlugin");
Directory.CreateDirectory(pluginPath);
var myPlugin = Path.Combine(pluginPath, fileName);
Mock<IEnvironmentVariableReader> environmentalVariableReader = new Mock<IEnvironmentVariableReader>();
environmentalVariableReader.Setup(env => env.GetEnvironmentVariable("NUGET_PLUGIN_PATHS")).Returns(pluginPath);
environmentalVariableReader.Setup(env => env.GetEnvironmentVariable("PATHS")).Returns("");
File.WriteAllText(myPlugin, string.Empty);
var verifierSpy = new Mock<EmbeddedSignatureVerifier>();
verifierSpy.Setup(spy => spy.IsValid(It.IsAny<string>()))
.Returns(true);

using (var discoverer = new PluginDiscoverer("", verifierSpy.Object, 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;
}

Assert.True(discovered);
}
}
}

[PlatformTheory(Platform.Windows)]
[InlineData("nugetplugin-myPlugin.exe")]
[InlineData("nugetplugin-myPlugin.bat")]
Expand Down Expand Up @@ -407,56 +370,6 @@ public async Task DiscoverAsync_WithPluginPathSpecifiedInNuGetPluginPathsEnvVari
}
}

[PlatformFact(Platform.Linux)]
public async Task DiscoverAsync_WithPluginPathSpecifiedInNuGetPluginPathsEnvVariableLinux_FindsThePlugin()
{
using (var testDirectory = TestDirectory.Create())
{
// Arrange
var pluginPath = Path.Combine(testDirectory.Path, "myPlugins");
Directory.CreateDirectory(pluginPath);
var myPlugin = Path.Combine(pluginPath, "nuget-plugin-MyPlugin");
File.WriteAllText(myPlugin, string.Empty);
Mock<IEnvironmentVariableReader> environmentalVariableReader = new Mock<IEnvironmentVariableReader>();
environmentalVariableReader.Setup(env => env.GetEnvironmentVariable("NUGET_PLUGIN_PATHS")).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)
{
var verifierSpy = new Mock<EmbeddedSignatureVerifier>();
verifierSpy.Setup(spy => spy.IsValid(It.IsAny<string>()))
.Returns(true);

using (var discoverer = new PluginDiscoverer("", verifierSpy.Object, 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;
}

Assert.True(discovered);
}
}
}
}
}

[PlatformFact(Platform.Linux)]
public async Task DiscoverAsync_withNoExecutableValidDotNetToolsPluginLinux_DoesNotFindThePlugin()
{
Expand Down Expand Up @@ -526,28 +439,6 @@ public void GetPluginsInNuGetPluginPaths_WithNuGetPluginPathsSet_ReturnsPluginsI
Assert.True(plugins[0].IsDotnetToolsPlugin);
}

[PlatformFact(Platform.Windows)]
public void GetPluginsInNuGetPluginPathsAndPath_WithoutNuGetPluginPaths_FallsBackToPath()
{
// Arrange
var pathDirectory = TestDirectory.Create();
var pluginFilePath = Path.Combine(pathDirectory.Path, "nuget-plugin-fallback.exe");
File.Create(pluginFilePath);

var environmentalVariableReader = new Mock<IEnvironmentVariableReader>();
environmentalVariableReader.Setup(env => env.GetEnvironmentVariable(EnvironmentVariableConstants.PluginPaths)).Returns(string.Empty);
environmentalVariableReader.Setup(env => env.GetEnvironmentVariable("PATH")).Returns(pathDirectory.Path);

var pluginDiscoverer = new PluginDiscoverer("", Mock.Of<EmbeddedSignatureVerifier>(), environmentalVariableReader.Object);

// Act
var plugins = pluginDiscoverer.GetPluginsInNuGetPluginPathsAndPath();

// Assert
Assert.Single(plugins);
Assert.Equal(pluginFilePath, plugins[0].Path);
}

[PlatformFact(Platform.Windows)]
public void GetPluginsInNuGetPluginPaths_WithoutNuGetPluginPaths_ReturnsEmpty()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ internal PluginResourceProviderNegativeTest(string serviceIndexJson, string sour

_pluginDiscoverer.Setup(x => x.DiscoverAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(pluginDiscoveryResults);
_pluginDiscoverer.Setup(x => x.Dispose());

_testDirectory = TestDirectory.Create();

Expand Down

0 comments on commit 0380e40

Please sign in to comment.