Skip to content

Commit 0c89f07

Browse files
committed
fix compilation errors in tests
1 parent 5b0e85a commit 0c89f07

30 files changed

+437
-668
lines changed

documentation/specs/unified-configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ src/Microsoft.Extensions.Configuration.DotnetCli/
606606
└── Services/
607607
├── DotNetConfiguration.cs
608608
├── DotNetConfigurationRoot.cs
609-
├── IDotNetConfigurationService.cs
609+
├── DotNetConfigurationService.cs
610610
└── DotNetConfigurationService.cs
611611
```
612612

@@ -855,12 +855,12 @@ public sealed class ToolConfiguration
855855
### Strongly-Typed Configuration Service with Source Generator
856856

857857
```csharp
858-
// src/Microsoft.Extensions.Configuration.DotnetCli/Services/IDotNetConfigurationService.cs
858+
// src/Microsoft.Extensions.Configuration.DotnetCli/Services/DotNetConfigurationService.cs
859859
namespace Microsoft.Extensions.Configuration.DotnetCli.Services;
860860

861861
using Microsoft.Extensions.Configuration.DotnetCli.Models;
862862

863-
public interface IDotNetConfigurationService
863+
public interface DotNetConfigurationService
864864
{
865865
IConfiguration RawConfiguration { get; }
866866

@@ -876,7 +876,7 @@ public interface IDotNetConfigurationService
876876
}
877877

878878
// src/Microsoft.Extensions.Configuration.DotnetCli/Services/DotNetConfigurationService.cs
879-
public class DotNetConfigurationService : IDotNetConfigurationService
879+
public class DotNetConfigurationService : DotNetConfigurationService
880880
{
881881
private readonly IConfiguration _configuration;
882882

src/Cli/dotnet/Commands/Pack/PackCommand.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
using System.CommandLine;
55
using Microsoft.DotNet.Cli.Commands.Restore;
6-
using Microsoft.DotNet.Cli.Configuration;
76
using Microsoft.DotNet.Cli.Extensions;
87
using Microsoft.DotNet.Cli.Utils;
8+
using Microsoft.Extensions.Configuration.DotnetCli;
99

1010
namespace Microsoft.DotNet.Cli.Commands.Pack;
1111

@@ -28,13 +28,11 @@ public static PackCommand FromParseResult(ParseResult parseResult, string? msbui
2828

2929
var msbuildArgs = parseResult.OptionValuesToBeForwarded(PackCommandParser.GetCommand()).Concat(parseResult.GetValue(PackCommandParser.SlnOrProjectArgument) ?? []);
3030

31-
var configurationService = DotNetConfigurationFactory.Create();
3231
ReleasePropertyProjectLocator projectLocator = new(parseResult, MSBuildPropertyNames.PACK_RELEASE,
3332
new ReleasePropertyProjectLocator.DependentCommandOptions(
3433
parseResult.GetValue(PackCommandParser.SlnOrProjectArgument),
3534
parseResult.HasOption(PackCommandParser.ConfigurationOption) ? parseResult.GetValue(PackCommandParser.ConfigurationOption) : null
36-
), configurationService
37-
);
35+
));
3836

3937
bool noRestore = parseResult.HasOption(PackCommandParser.NoRestoreOption) || parseResult.HasOption(PackCommandParser.NoBuildOption);
4038
var parsedMSBuildArgs = MSBuildArgs.AnalyzeMSBuildArguments(

src/Cli/dotnet/Commands/Package/Add/PackageAddCommandParser.cs

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,18 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using System.CommandLine.Completions;
86
using System.CommandLine.Parsing;
9-
using Microsoft.DotNet.Cli.Configuration;
107
using Microsoft.DotNet.Cli.Extensions;
11-
using Microsoft.Extensions.Configuration.DotnetCli.Services;
8+
using Microsoft.Extensions.Configuration.DotnetCli;
129
using Microsoft.Extensions.EnvironmentAbstractions;
1310
using NuGet.Versioning;
1411

1512
namespace Microsoft.DotNet.Cli.Commands.Package.Add;
1613

1714
public static class PackageAddCommandParser
1815
{
19-
public static readonly Argument<PackageIdentityWithRange> CmdPackageArgument = CommonArguments.RequiredPackageIdentityArgument()
20-
.AddCompletions((context) =>
21-
{
22-
// we should take --prerelease flags into account for version completion
23-
var allowPrerelease = context.ParseResult.GetValue(PrereleaseOption);
24-
return QueryNuGet(context.WordToComplete, allowPrerelease, CancellationToken.None).Result.Select(packageId => new CompletionItem(packageId));
25-
});
26-
27-
public static readonly Option<string> VersionOption = new DynamicForwardedOption<string>("--version", "-v")
28-
{
29-
Description = CliCommandStrings.CmdVersionDescription,
30-
HelpName = CliCommandStrings.CmdVersion
31-
}.ForwardAsSingle(o => $"--version {o}")
32-
.AddCompletions((context) =>
33-
{
34-
// we can only do version completion if we have a package id
35-
if (context.ParseResult.GetValue(CmdPackageArgument) is { HasVersion: false } packageId)
36-
{
37-
// we should take --prerelease flags into account for version completion
38-
var allowPrerelease = context.ParseResult.GetValue(PrereleaseOption);
39-
return QueryVersionsForPackage(packageId.Id, context.WordToComplete, allowPrerelease, CancellationToken.None)
40-
.Result
41-
.Select(version => new CompletionItem(version.ToNormalizedString()));
42-
}
43-
else
44-
{
45-
return [];
46-
}
47-
});
48-
4916
public static readonly Option<string> FrameworkOption = new ForwardedOption<string>("--framework", "-f")
5017
{
5118
Description = CliCommandStrings.PackageAddCmdFrameworkDescription,
@@ -78,6 +45,36 @@ public static class PackageAddCommandParser
7845
Arity = ArgumentArity.Zero
7946
}.ForwardAs("--prerelease");
8047

48+
public static readonly Argument<PackageIdentityWithRange> CmdPackageArgument = CommonArguments.RequiredPackageIdentityArgument()
49+
.AddCompletions((context) =>
50+
{
51+
// we should take --prerelease flags into account for version completion
52+
var allowPrerelease = context.ParseResult.GetValue(PrereleaseOption);
53+
return QueryNuGet(context.WordToComplete, allowPrerelease, CancellationToken.None).Result.Select(packageId => new CompletionItem(packageId));
54+
});
55+
56+
public static readonly Option<string> VersionOption = new DynamicForwardedOption<string>("--version", "-v")
57+
{
58+
Description = CliCommandStrings.CmdVersionDescription,
59+
HelpName = CliCommandStrings.CmdVersion
60+
}.ForwardAsSingle(o => $"--version {o}")
61+
.AddCompletions((context) =>
62+
{
63+
// we can only do version completion if we have a package id
64+
if (context.ParseResult.GetValue(CmdPackageArgument) is { HasVersion: false } packageId)
65+
{
66+
// we should take --prerelease flags into account for version completion
67+
var allowPrerelease = context.ParseResult.GetValue(PrereleaseOption);
68+
return QueryVersionsForPackage(packageId.Id, context.WordToComplete, allowPrerelease, CancellationToken.None)
69+
.Result
70+
.Select(version => new CompletionItem(version.ToNormalizedString()));
71+
}
72+
else
73+
{
74+
return [];
75+
}
76+
});
77+
8178
private static readonly Command Command = ConstructCommand();
8279

8380
public static Command GetCommand()
@@ -107,17 +104,18 @@ private static Command ConstructCommand()
107104

108105
private static void DisallowVersionIfPackageIdentityHasVersionValidator(OptionResult result)
109106
{
110-
if (result.Parent.GetValue(CmdPackageArgument).HasVersion)
107+
if (result.Parent!.GetValue(CmdPackageArgument).HasVersion)
111108
{
112109
result.AddError(CliCommandStrings.ValidationFailedDuplicateVersion);
113110
}
114111
}
115112

116-
public static async Task<IEnumerable<string>> QueryNuGet(string packageStem, bool allowPrerelease, CancellationToken cancellationToken)
113+
// Only called during tab-completions, so this is allowed can hack/create singleton members like the configuration/downloader
114+
internal static async Task<IEnumerable<string>> QueryNuGet(string packageStem, bool allowPrerelease, CancellationToken cancellationToken)
117115
{
118116
try
119117
{
120-
var downloader = new NuGetPackageDownloader.NuGetPackageDownloader(packageInstallDir: new DirectoryPath(), configurationService: DotNetConfigurationFactory.Create());
118+
var downloader = new NuGetPackageDownloader.NuGetPackageDownloader(packageInstallDir: new DirectoryPath());
121119
var versions = await downloader.GetPackageIdsAsync(packageStem, allowPrerelease, cancellationToken: cancellationToken);
122120
return versions;
123121
}
@@ -127,11 +125,12 @@ public static async Task<IEnumerable<string>> QueryNuGet(string packageStem, boo
127125
}
128126
}
129127

128+
// Only called during tab-completions, so this is allowed can hack/create singleton members like the configuration/downloader
130129
internal static async Task<IEnumerable<NuGetVersion>> QueryVersionsForPackage(string packageId, string versionFragment, bool allowPrerelease, CancellationToken cancellationToken)
131130
{
132131
try
133132
{
134-
var downloader = new NuGetPackageDownloader.NuGetPackageDownloader(packageInstallDir: new DirectoryPath(), configurationService: DotNetConfigurationFactory.Create());
133+
var downloader = new NuGetPackageDownloader.NuGetPackageDownloader(packageInstallDir: new DirectoryPath());
135134
var versions = await downloader.GetPackageVersionsAsync(new(packageId), versionFragment, allowPrerelease, cancellationToken: cancellationToken);
136135
return versions;
137136
}

src/Cli/dotnet/Commands/Publish/PublishCommand.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System.CommandLine;
55
using Microsoft.DotNet.Cli.Commands.Restore;
66
using Microsoft.DotNet.Cli.Commands.Run;
7-
using Microsoft.DotNet.Cli.Configuration;
87
using Microsoft.DotNet.Cli.Extensions;
98
using Microsoft.DotNet.Cli.Utils;
9+
using Microsoft.Extensions.Configuration.DotnetCli;
1010

1111
namespace Microsoft.DotNet.Cli.Commands.Publish;
1212

@@ -57,13 +57,12 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
5757
NoCache = true,
5858
},
5959
(msbuildArgs, msbuildPath) => {
60-
var configurationService = DotNetConfigurationFactory.Create();
6160
var options = new ReleasePropertyProjectLocator.DependentCommandOptions(
6261
nonBinLogArgs,
6362
parseResult.HasOption(PublishCommandParser.ConfigurationOption) ? parseResult.GetValue(PublishCommandParser.ConfigurationOption) : null,
6463
parseResult.HasOption(PublishCommandParser.FrameworkOption) ? parseResult.GetValue(PublishCommandParser.FrameworkOption) : null
6564
);
66-
var projectLocator = new ReleasePropertyProjectLocator(parseResult, MSBuildPropertyNames.PUBLISH_RELEASE, options, configurationService);
65+
var projectLocator = new ReleasePropertyProjectLocator(parseResult, MSBuildPropertyNames.PUBLISH_RELEASE, options);
6766
var releaseModeProperties = projectLocator.GetCustomDefaultConfigurationValueIfSpecified();
6867
return new PublishCommand(
6968
msbuildArgs: msbuildArgs.CloneWithAdditionalProperties(releaseModeProperties),

src/Cli/dotnet/Commands/Test/TestCommandParser.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
using System.Diagnostics;
66
using Microsoft.DotNet.Cli.Extensions;
77
using Microsoft.Extensions.Configuration;
8-
using Microsoft.DotNet.Cli.Configuration;
9-
using Microsoft.Extensions.Configuration.DotnetCli.Services;
8+
using Microsoft.Extensions.Configuration.DotnetCli;
109

1110
namespace Microsoft.DotNet.Cli.Commands.Test;
1211

@@ -166,11 +165,7 @@ public static Command GetCommand()
166165
return Command;
167166
}
168167

169-
public static string GetTestRunnerName()
170-
{
171-
var configurationService = DotNetConfigurationFactory.Create();
172-
return configurationService.Test.RunnerName;
173-
}
168+
public static string GetTestRunnerName() => DotNetConfigurationFactory.Create().Test.RunnerName;
174169

175170
private static Command ConstructCommand()
176171
{

0 commit comments

Comments
 (0)