Skip to content

Commit f8ba3fa

Browse files
authored
add .NET 6 target framework for System.CommandLine and add support for trimming (#1572)
* add support for trimming * use FormatterServices.GetUninitializedObject for default value types * update tests to .NET 6; add trimming test * add net6.0 tfm, reorganize some conversion tests * fix bool? conversion * consolidate logic for instantiating arrays and lists * update dotnet-suggest test app to net6.0
1 parent 4628979 commit f8ba3fa

File tree

50 files changed

+900
-673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+900
-673
lines changed

src/System.CommandLine.DragonFruit.Tests/System.CommandLine.DragonFruit.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net5.0</TargetFramework>
3+
<TargetFramework>net6.0</TargetFramework>
44
<StartupObject>AutoGeneratedProgram</StartupObject>
55
<!-- Ensure that an XML doc file is emitted to supply command line help -->
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>

src/System.CommandLine.Generator.Tests/System.CommandLine.Generator.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net5.0</TargetFrameworks>
3+
<TargetFrameworks>net6.0</TargetFrameworks>
44
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
55
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
66
<ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>

src/System.CommandLine.Hosting.Tests/System.CommandLine.Hosting.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net5.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0</TargetFrameworks>
55
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

src/System.CommandLine.NamingConventionBinder.Tests/System.CommandLine.NamingConventionBinder.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net5.0</TargetFrameworks>
3+
<TargetFrameworks>net6.0</TargetFrameworks>
44
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
55
<LangVersion>10</LangVersion>
66
</PropertyGroup>

src/System.CommandLine.Rendering.Tests/System.CommandLine.Rendering.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

src/System.CommandLine.Rendering.Tests/TableRenderingTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Xunit;
99
using Xunit.Abstractions;
1010
using System.CommandLine.Rendering.Views;
11-
using System.CommandLine.Tests;
1211
using System.CommandLine.Tests.Utility;
1312
using static System.Environment;
1413

src/System.CommandLine.Suggest.Tests/DotnetSuggestEndToEndTests.cs

Lines changed: 15 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.IO;
77
using System.Linq;
88
using System.Text;
9-
using System.Threading.Tasks;
109
using Xunit.Abstractions;
1110
using static System.Environment;
11+
using Process = System.CommandLine.Tests.Utility.Process;
1212

1313
namespace System.CommandLine.Suggest.Tests
1414
{
@@ -72,11 +72,11 @@ private static void PrepareTestHomeDirectoryToAvoidPolluteBuildMachineHome()
7272
}
7373

7474
[ReleaseBuildOnlyFact]
75-
public async Task Test_app_supplies_suggestions()
75+
public void Test_app_supplies_suggestions()
7676
{
7777
var stdOut = new StringBuilder();
7878

79-
await ExecuteAsync(
79+
Process.RunToCompletion(
8080
_endToEndTestApp.FullName,
8181
"[suggest:1] \"a\"",
8282
stdOut: value => stdOut.AppendLine(value),
@@ -88,22 +88,22 @@ await ExecuteAsync(
8888
}
8989

9090
[ReleaseBuildOnlyFact]
91-
public async Task Dotnet_suggest_provides_suggestions_for_app()
91+
public void Dotnet_suggest_provides_suggestions_for_app()
9292
{
9393
// run once to trigger a call to dotnet-suggest register
94-
await ExecuteAsync(
94+
Process.RunToCompletion(
9595
_endToEndTestApp.FullName,
9696
"-h",
9797
stdOut: s => _output.WriteLine(s),
9898
stdErr: s => _output.WriteLine(s),
99-
environmentVariables: _environmentVariables);
99+
environmentVariables: _environmentVariables).Should().Be(0);
100100

101101
var stdOut = new StringBuilder();
102102
var stdErr = new StringBuilder();
103103

104104
var commandLineToComplete = "a";
105105

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

125125
[ReleaseBuildOnlyFact]
126-
public async Task Dotnet_suggest_provides_suggestions_for_app_with_only_commandname()
126+
public void Dotnet_suggest_provides_suggestions_for_app_with_only_commandname()
127127
{
128128
// run once to trigger a call to dotnet-suggest register
129-
await ExecuteAsync(
129+
Process.RunToCompletion(
130130
_endToEndTestApp.FullName,
131131
"-h",
132132
stdOut: s => _output.WriteLine(s),
133133
stdErr: s => _output.WriteLine(s),
134-
environmentVariables: _environmentVariables);
134+
environmentVariables: _environmentVariables).Should().Be(0);
135135

136136
var stdOut = new StringBuilder();
137137
var stdErr = new StringBuilder();
138138

139139
var commandLineToComplete = "a ";
140140

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

151151
stdErr.ToString()
152-
.Should()
153-
.BeEmpty();
152+
.Should()
153+
.BeEmpty();
154154

155155
stdOut.ToString()
156-
.Should()
157-
.Be($"--apple{NewLine}--banana{NewLine}--cherry{NewLine}--durian{NewLine}--help{NewLine}--version{NewLine}-?{NewLine}-h{NewLine}/?{NewLine}/h{NewLine}");
158-
}
159-
160-
private static async Task ExecuteAsync(
161-
string command,
162-
string args,
163-
Action<string> stdOut = null,
164-
Action<string> stdErr = null,
165-
params (string key, string value)[] environmentVariables)
166-
{
167-
args ??= "";
168-
169-
var process = new Diagnostics.Process
170-
{
171-
StartInfo =
172-
{
173-
Arguments = args,
174-
FileName = command,
175-
RedirectStandardError = true,
176-
RedirectStandardOutput = true,
177-
RedirectStandardInput = true,
178-
UseShellExecute = false
179-
}
180-
};
181-
182-
if (environmentVariables.Length > 0)
183-
{
184-
for (var i = 0; i < environmentVariables.Length; i++)
185-
{
186-
var (key, value) = environmentVariables[i];
187-
process.StartInfo.Environment.Add(key, value);
188-
}
189-
}
190-
191-
if (stdOut != null)
192-
{
193-
process.OutputDataReceived += (sender, eventArgs) =>
194-
{
195-
if (eventArgs.Data != null)
196-
{
197-
stdOut(eventArgs.Data);
198-
}
199-
};
200-
}
201-
202-
if (stdErr != null)
203-
{
204-
process.ErrorDataReceived += (sender, eventArgs) =>
205-
{
206-
if (eventArgs.Data != null)
207-
{
208-
stdErr(eventArgs.Data);
209-
}
210-
};
211-
}
212-
213-
process.Start();
214-
215-
process.BeginOutputReadLine();
216-
process.BeginErrorReadLine();
217-
218-
await process.WaitForExitAsync();
156+
.Should()
157+
.Be($"--apple{NewLine}--banana{NewLine}--cherry{NewLine}--durian{NewLine}--help{NewLine}--version{NewLine}-?{NewLine}-h{NewLine}/?{NewLine}/h{NewLine}");
219158
}
220159
}
221160
}

src/System.CommandLine.Suggest.Tests/EndToEndTestApp/EndToEndTestApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<PropertyGroup>
88
<OutputType>Exe</OutputType>
9-
<TargetFramework>net5.0</TargetFramework>
9+
<TargetFramework>net6.0</TargetFramework>
1010
</PropertyGroup>
1111

1212
</Project>

src/System.CommandLine.Suggest.Tests/dotnet-suggest.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,55 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
#if NET6_0_OR_GREATER
5+
46
using System.IO;
57
using System.Runtime.InteropServices;
68

7-
namespace System.CommandLine.Suggest
9+
namespace System.CommandLine.Suggest;
10+
11+
internal static class DotnetMuxer
812
{
9-
internal static class DotnetMuxer
13+
public static FileInfo Path { get; }
14+
15+
static DotnetMuxer()
1016
{
11-
public static FileInfo Path { get; }
17+
var muxerFileName = ExecutableName("dotnet");
18+
var fxDepsFile = GetDataFromAppDomain("FX_DEPS_FILE");
1219

13-
static DotnetMuxer()
20+
if (string.IsNullOrEmpty(fxDepsFile))
1421
{
15-
var muxerFileName = ExecutableName("dotnet");
16-
var fxDepsFile = GetDataFromAppDomain("FX_DEPS_FILE");
17-
18-
if (string.IsNullOrEmpty(fxDepsFile))
19-
20-
{
21-
return;
22-
}
23-
24-
var muxerDir = new FileInfo(fxDepsFile).Directory?.Parent?.Parent?.Parent;
22+
return;
23+
}
2524

26-
if (muxerDir == null)
27-
{
28-
return;
25+
var muxerDir = new FileInfo(fxDepsFile).Directory?.Parent?.Parent?.Parent;
2926

30-
}
27+
if (muxerDir is null)
28+
{
29+
return;
30+
}
3131

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

34-
if (muxerCandidate.Exists)
35-
{
36-
Path = muxerCandidate;
37-
}
38-
else
39-
{
40-
throw new InvalidOperationException("no muxer!");
41-
}
34+
if (muxerCandidate.Exists)
35+
{
36+
Path = muxerCandidate;
4237
}
43-
44-
public static string GetDataFromAppDomain(string propertyName)
38+
else
4539
{
46-
return AppContext.GetData(propertyName) as string;
40+
throw new InvalidOperationException("no muxer!");
4741
}
42+
}
4843

49-
public static string ExecutableName(this string withoutExtension) =>
50-
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
51-
? withoutExtension + ".exe"
52-
: withoutExtension;
44+
public static string GetDataFromAppDomain(string propertyName)
45+
{
46+
return AppContext.GetData(propertyName) as string;
5347
}
48+
49+
public static string ExecutableName(this string withoutExtension) =>
50+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
51+
? withoutExtension + ".exe"
52+
: withoutExtension;
5453
}
54+
55+
#endif

0 commit comments

Comments
 (0)