/
StdioConnectionTests.cs
129 lines (104 loc) · 4.28 KB
/
StdioConnectionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.App.Connection;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Tests;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Microsoft.DotNet.Interactive.Utility;
using Pocket.For.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Interactive.App.Tests;
[LogToPocketLogger(FileNameEnvironmentVariable = "POCKETLOGGER_LOG_PATH")]
public class StdioConnectionTests : ProxyKernelConnectionTestsBase
{
private readonly StdioConnectionTestConfiguration _configuration;
public StdioConnectionTests(ITestOutputHelper output) : base(output)
{
_configuration = CreateConnectionConfiguration();
}
protected override Func<string, Task<ProxyKernel>> CreateConnector()
{
var command = new List<string> { _configuration.Command };
if (_configuration.Args?.Length > 0)
{
command.AddRange(_configuration.Args);
}
return _ => new StdIoKernelConnector(
command.ToArray(),
rootProxyKernelLocalName: "rootProxy",
kernelHostUri: new Uri("kernel://test-kernel"),
workingDirectory: _configuration.WorkingDirectory).CreateRootProxyKernelAsync();
}
protected StdioConnectionTestConfiguration CreateConnectionConfiguration()
{
var toolPath = new FileInfo(typeof(Program).Assembly.Location);
var args = new List<string>
{
$"\"{toolPath.FullName}\"",
"stdio",
"--default-kernel",
"csharp",
};
#if DEBUG
if (Environment.GetEnvironmentVariable("POCKETLOGGER_LOG_PATH") is { } logPath)
{
var fileInfo = new FileInfo(logPath);
var proxiedLogPath = Path.Combine(fileInfo.DirectoryName, $"proxied.{fileInfo.Name}");
args.Add("--verbose");
args.Add("--log-path");
args.Add(proxiedLogPath);
}
#endif
return new StdioConnectionTestConfiguration
{
Command = Dotnet.Path.FullName,
Args = args.ToArray(),
WorkingDirectory = toolPath.Directory
};
}
[Fact]
public async Task stdio_server_encoding_is_utf_8()
{
using var localCompositeKernel = new CompositeKernel
{
new FakeKernel("fsharp")
};
AddKernelConnector(localCompositeKernel);
localCompositeKernel.DefaultKernelName = "fsharp";
var connectToRemoteKernel = new SubmitCode(
$"#!connect stdio --kernel-name newKernelName --command \"{_configuration.Command}\" {string.Join(" ", _configuration.Args)}");
await localCompositeKernel.SendAsync(connectToRemoteKernel);
var result = await localCompositeKernel.SendAsync(new SubmitCode("System.Console.InputEncoding.EncodingName + \"/\" + System.Console.OutputEncoding.EncodingName",
"newKernelName"));
var expected = Encoding.UTF8.EncodingName + "/" + Encoding.UTF8.EncodingName;
result.Events
.Should()
.ContainSingle<DisplayEvent>(
where: d => d.FormattedValues.Any(FormattedValue => FormattedValue.Value == expected));
}
protected override SubmitCode CreateConnectCommand(string localKernelName)
{
return new SubmitCode(
$"#!connect stdio --kernel-name {localKernelName} --command \"{_configuration.Command}\" {string.Join(" ", _configuration.Args)}");
}
protected override void AddKernelConnector(CompositeKernel compositeKernel)
{
compositeKernel.AddKernelConnector(new ConnectStdIoCommand(new Uri("kernel://test-kernel")));
}
}
public class StdioConnectionTestConfiguration
{
public string Command { get; set; }
public string[] Args { get; set; }
public DirectoryInfo WorkingDirectory { get; set; }
}