Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<StartupObject>AutoGeneratedProgram</StartupObject>
<!-- Ensure that an XML doc file is emitted to supply command line help -->
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<LangVersion>10</LangVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Xunit;
using Xunit.Abstractions;
using System.CommandLine.Rendering.Views;
using System.CommandLine.Tests;
using System.CommandLine.Tests.Utility;
using static System.Environment;

Expand Down
91 changes: 15 additions & 76 deletions src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;
using static System.Environment;
using Process = System.CommandLine.Tests.Utility.Process;

namespace System.CommandLine.Suggest.Tests
{
Expand Down Expand Up @@ -72,11 +72,11 @@ private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome()
}

[ReleaseBuildOnlyFact]
public async Task Test_app_supplies_suggestions()
public void Test_app_supplies_suggestions()
{
var stdOut = new StringBuilder();

await ExecuteAsync(
Process.RunToCompletion(
_endToEndTestApp.FullName,
"[suggest:1] \"a\"",
stdOut: value => stdOut.AppendLine(value),
Expand All @@ -88,22 +88,22 @@ await ExecuteAsync(
}

[ReleaseBuildOnlyFact]
public async Task Dotnet_suggest_provides_suggestions_for_app()
public void Dotnet_suggest_provides_suggestions_for_app()
{
// run once to trigger a call to dotnet-suggest register
await ExecuteAsync(
Process.RunToCompletion(
_endToEndTestApp.FullName,
"-h",
stdOut: s => _output.WriteLine(s),
stdErr: s => _output.WriteLine(s),
environmentVariables: _environmentVariables);
environmentVariables: _environmentVariables).Should().Be(0);

var stdOut = new StringBuilder();
var stdErr = new StringBuilder();

var commandLineToComplete = "a";

await ExecuteAsync(
Process.RunToCompletion(
_dotnetSuggest.FullName,
$"get -e \"{_endToEndTestApp.FullName}\" --position {commandLineToComplete.Length} -- \"{commandLineToComplete}\"",
stdOut: value => stdOut.AppendLine(value),
Expand All @@ -123,22 +123,22 @@ await ExecuteAsync(
}

[ReleaseBuildOnlyFact]
public async Task Dotnet_suggest_provides_suggestions_for_app_with_only_commandname()
public void Dotnet_suggest_provides_suggestions_for_app_with_only_commandname()
{
// run once to trigger a call to dotnet-suggest register
await ExecuteAsync(
Process.RunToCompletion(
_endToEndTestApp.FullName,
"-h",
stdOut: s => _output.WriteLine(s),
stdErr: s => _output.WriteLine(s),
environmentVariables: _environmentVariables);
environmentVariables: _environmentVariables).Should().Be(0);

var stdOut = new StringBuilder();
var stdErr = new StringBuilder();

var commandLineToComplete = "a ";

await ExecuteAsync(
Process.RunToCompletion(
_dotnetSuggest.FullName,
$"get -e \"{_endToEndTestApp.FullName}\" --position {commandLineToComplete.Length} -- \"{commandLineToComplete}\"",
stdOut: value => stdOut.AppendLine(value),
Expand All @@ -149,73 +149,12 @@ await ExecuteAsync(
_output.WriteLine($"stdErr:{NewLine}{stdErr}{NewLine}");

stdErr.ToString()
.Should()
.BeEmpty();
.Should()
.BeEmpty();

stdOut.ToString()
.Should()
.Be($"--apple{NewLine}--banana{NewLine}--cherry{NewLine}--durian{NewLine}--help{NewLine}--version{NewLine}-?{NewLine}-h{NewLine}/?{NewLine}/h{NewLine}");
}

private static async Task ExecuteAsync(
string command,
string args,
Action<string> stdOut = null,
Action<string> stdErr = null,
params (string key, string value)[] environmentVariables)
{
args ??= "";

var process = new Diagnostics.Process
{
StartInfo =
{
Arguments = args,
FileName = command,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false
}
};

if (environmentVariables.Length > 0)
{
for (var i = 0; i < environmentVariables.Length; i++)
{
var (key, value) = environmentVariables[i];
process.StartInfo.Environment.Add(key, value);
}
}

if (stdOut != null)
{
process.OutputDataReceived += (sender, eventArgs) =>
{
if (eventArgs.Data != null)
{
stdOut(eventArgs.Data);
}
};
}

if (stdErr != null)
{
process.ErrorDataReceived += (sender, eventArgs) =>
{
if (eventArgs.Data != null)
{
stdErr(eventArgs.Data);
}
};
}

process.Start();

process.BeginOutputReadLine();
process.BeginErrorReadLine();

await process.WaitForExitAsync();
.Should()
.Be($"--apple{NewLine}--banana{NewLine}--cherry{NewLine}--durian{NewLine}--help{NewLine}--version{NewLine}-?{NewLine}-h{NewLine}/?{NewLine}/h{NewLine}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
69 changes: 35 additions & 34 deletions src/System.CommandLine.Suggest/DotnetMuxer.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
// 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.

#if NET6_0_OR_GREATER

using System.IO;
using System.Runtime.InteropServices;

namespace System.CommandLine.Suggest
namespace System.CommandLine.Suggest;

internal static class DotnetMuxer
{
internal static class DotnetMuxer
public static FileInfo Path { get; }

static DotnetMuxer()
{
public static FileInfo Path { get; }
var muxerFileName = ExecutableName("dotnet");
var fxDepsFile = GetDataFromAppDomain("FX_DEPS_FILE");

static DotnetMuxer()
if (string.IsNullOrEmpty(fxDepsFile))
{
var muxerFileName = ExecutableName("dotnet");
var fxDepsFile = GetDataFromAppDomain("FX_DEPS_FILE");

if (string.IsNullOrEmpty(fxDepsFile))

{
return;
}

var muxerDir = new FileInfo(fxDepsFile).Directory?.Parent?.Parent?.Parent;
return;
}

if (muxerDir == null)
{
return;
var muxerDir = new FileInfo(fxDepsFile).Directory?.Parent?.Parent?.Parent;

}
if (muxerDir is null)
{
return;
}

var muxerCandidate = new FileInfo(System.IO.Path.Combine(muxerDir.FullName, muxerFileName));
var muxerCandidate = new FileInfo(System.IO.Path.Combine(muxerDir.FullName, muxerFileName));

if (muxerCandidate.Exists)
{
Path = muxerCandidate;
}
else
{
throw new InvalidOperationException("no muxer!");
}
if (muxerCandidate.Exists)
{
Path = muxerCandidate;
}

public static string GetDataFromAppDomain(string propertyName)
else
{
return AppContext.GetData(propertyName) as string;
throw new InvalidOperationException("no muxer!");
}
}

public static string ExecutableName(this string withoutExtension) =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? withoutExtension + ".exe"
: withoutExtension;
public static string GetDataFromAppDomain(string propertyName)
{
return AppContext.GetData(propertyName) as string;
}

public static string ExecutableName(this string withoutExtension) =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? withoutExtension + ".exe"
: withoutExtension;
}

#endif
2 changes: 1 addition & 1 deletion src/System.CommandLine.Suggest/dotnet-suggest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>true</IsPackable>
<PackAsTool>true</PackAsTool>
<PackageId>dotnet-suggest</PackageId>
Expand Down
Loading