Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigusu-Allehu committed Oct 9, 2024
1 parent 76db993 commit b958a0c
Showing 1 changed file with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Test.Utility;
using Xunit;

namespace NuGet.Protocol.Plugins.Tests
Expand Down Expand Up @@ -135,6 +138,100 @@ public async Task GetOrCreateAsync_ThrowsIfDisposed()
Assert.Equal(nameof(PluginFactory), exception.ObjectName);
}

[PlatformFact(Platform.Windows)]
public async Task GetOrCreateNetPluginAsync_CreatesPluginAndSendsHandshakeRequest()
{
using TestDirectory testDirectory = TestDirectory.Create();
string pluginPath = Path.Combine(testDirectory.Path, "nuget-plugin-batFile.bat");

string outputFilePath = Path.Combine(testDirectory.Path, "output.txt");
File.WriteAllText(outputFilePath, "");

// Create the .bat file that simulates a plugin
// Simply waits for a json request that looks like`{"RequestId":"ff52f0ae-28c9-4a19-957d-78db5b68f3f2","Type":"Request","Method":"Handshake","Payload":{"ProtocolVersion":"2.0.0","MinimumProtocolVersion":"1.0.0"}} `
// and sends it back with the type changed to `Response`
string batFileContent = $@"
@echo off
setlocal enabledelayedexpansion
set outputFile={outputFilePath}
:: Clear the output file initially (if it exists)
echo. > %outputFile%
:: Initialize the counter for '}}' characters
set closingBraceCount=0
:loop
:: Read from stdin (standard input)
set /p input=""""
:: Count occurrences of '}}' in the input
for /l %%i in (0,1,1023) do (
if ""!input:~%%i,1!""==""}}"" (
set /a closingBraceCount+=1
)
)
:: Write the input to the output file
echo !input! >> %outputFile%
:: If '}}' has appeared twice (even across multiple inputs), process the output file
if !closingBraceCount! geq 2 (
goto processOutput
)
:: Go back to the loop to accept more input
goto loop
:processOutput
for /f ""delims="" %%a in (%outputFile%) do (
set ""singleLine=!singleLine! %%a""
)
:: Perform string replacement (""Request"" -> ""Response"") in the single line
set modifiedLine=!singleLine:""Type"":""Request""=""Type"":""Response""!
echo !modifiedLine!
";
File.WriteAllText(pluginPath, batFileContent);

var args = PluginConstants.PluginArguments;
var reqHandler = new RequestHandlers();
var options = ConnectionOptions.CreateDefault();

var pluginFactory = new PluginFactory(Timeout.InfiniteTimeSpan);

// Act
var plugin = Assert.ThrowsAsync<PluginException>(() => pluginFactory.GetOrCreateNetToolsPluginAsync(pluginPath, args, reqHandler, options, CancellationToken.None));
await Task.Delay(10);

// Assert
Assert.NotNull(plugin);
string jsonOutput = File.ReadAllText(outputFilePath);
var jsonDocument = JsonDocument.Parse(jsonOutput);
var rootElement = jsonDocument.RootElement;
if (rootElement.TryGetProperty("Method", out JsonElement methodElement))
{
Assert.Equal("Handshake", methodElement.GetString());
}
else
{
Assert.Fail("The Method property was not found in the JSON output.");
}

if (rootElement.TryGetProperty("Type", out JsonElement typeElement))
{
Assert.Equal("Request", typeElement.GetString());
}
else
{
Assert.Fail("The Type property was not found in the JSON output.");
}
}

[Fact]
public async Task CreateFromCurrentProcessAsync_ThrowsForNullRequestHandlers()
{
Expand Down

0 comments on commit b958a0c

Please sign in to comment.