Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions MSStore.API/MSStore.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.74.1" />
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.74.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.8" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.76.0" />
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.76.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.0-preview.7.25380.108" />
</ItemGroup>

</Project>
104 changes: 36 additions & 68 deletions MSStore.CLI.UnitTests/BaseCommandLineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
// Licensed under the MIT License.

using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Hosting;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -35,7 +31,6 @@ namespace MSStore.CLI.UnitTests
{
public class BaseCommandLineTest
{
internal MicrosoftStoreCLI Cli { get; private set; } = null!;
internal Mock<IConsoleReader> FakeConsole { get; private set; } = null!;
internal Mock<IConfigurationManager<Configurations>> FakeConfigurationManager { get; private set; } = null!;
internal Mock<IConfigurationManager<TelemetryConfigurations>> FakeTelemetryConfigurationManager { get; private set; } = null!;
Expand Down Expand Up @@ -97,7 +92,7 @@ public class BaseCommandLineTest
Id = new Guid("F3C1CCB6-09C0-4BAB-BABA-C034BFB60EF9")
};

private Parser _parser = null!;
private IHostBuilder _hostBuilder = null!;
protected IAnsiConsole ErrorAnsiConsole { get; private set; } = null!;

protected static string CopyFilesRecursively(string sourcePath, [CallerMemberName] string caller = null!)
Expand Down Expand Up @@ -204,12 +199,8 @@ public void Initialize()
.Setup(fac => fac.CreatePackagedAsync(It.IsAny<Configurations>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(FakeStorePackagedAPI.Object);

Cli = [];

StorePackagedAPI.DefaultSubmissionPollDelay = TimeSpan.Zero;

Cli.AddCommand(new TestCommand(this));

var azureBlobManagerMock = new Mock<IAzureBlobManager>();
azureBlobManagerMock
.Setup(x => x.UploadFileAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IProgress<double>>(), It.IsAny<CancellationToken>()))
Expand Down Expand Up @@ -242,8 +233,7 @@ public void Initialize()

TokenManager = new Mock<ITokenManager>();

var builder = new CommandLineBuilder(Cli);
_parser = builder.UseHost(_ => Host.CreateDefaultBuilder(null), (builder) => builder
_hostBuilder = Host.CreateDefaultBuilder(null)
.UseEnvironment("CLI")
.ConfigureServices((hostContext, services) =>
{
Expand Down Expand Up @@ -279,8 +269,7 @@ public void Initialize()
.AddScoped(sp => PWAAppInfoManager.Object)
.AddScoped<IElectronManifestManager>(sp => ElectronManifestManager.Object)
.AddScoped(sp => NuGetPackageManager.Object)
.AddScoped<IAppXManifestManager>(sp => AppXManifestManager.Object)
.AddSingleton(Cli);
.AddScoped<IAppXManifestManager>(sp => AppXManifestManager.Object);

services.AddLogging(builder =>
{
Expand All @@ -292,39 +281,7 @@ public void Initialize()
.ConfigureLogging((hostContext, logging) =>
{
logging.SetMinimumLevel(LogLevel.Debug);
}))
.AddMiddleware(
async (context, next) =>
{
var ct = context.GetCancellationToken();

var host = context.GetHost();

var configurationManager = host.Services.GetService<IConfigurationManager<Configurations>>()!;
var credentialManager = host.Services.GetService<ICredentialManager>()!;
var consoleReader = host.Services.GetService<IConsoleReader>()!;
var cliConfigurator = host.Services.GetService<ICLIConfigurator>()!;
var logger = host.Services.GetService<ILogger<Program>>()!;

if (context.ParseResult.CommandResult.Command is MicrosoftStoreCLI
|| context.ParseResult.CommandResult.Command is ReconfigureCommand
|| context.ParseResult.CommandResult.Command is TestCommand
|| await MicrosoftStoreCLI.InitAsync(ErrorAnsiConsole, configurationManager, credentialManager, consoleReader, cliConfigurator, logger, ct))
{
await next(context).ConfigureAwait(false);
}
}, MiddlewareOrder.Default)
.UseVersionOption()
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.UseTypoCorrections()
.UseParseErrorReporting()
.UseExceptionHandler()
.UseHelp()
.CancelOnProcessTermination()
.Build();
});
}

protected void FakeLogin(string? publisherDisplayName = null)
Expand Down Expand Up @@ -792,7 +749,7 @@ protected void SetupBasedOnTestDataProjectSubPath(DirectoryInfo dirInfo, string[
}
}

protected Task<(string Output, string Error)> RunTestAsync(Func<InvocationContext, Task>? testCallback)
protected Task<(string Output, string Error)> RunTestAsync(Func<ParseResult, IHost, CancellationToken, Task>? testCallback)
{
_testCallback = testCallback;

Expand All @@ -818,19 +775,39 @@ protected void SetupBasedOnTestDataProjectSubPath(DirectoryInfo dirInfo, string[
AnsiConsole.Profile.Capabilities.Ansi = true;
AnsiConsole.Profile.Capabilities.Unicode = true;

var parseResult = _parser.Parse(args);
IHost host = _hostBuilder.Start();

if (parseResult.Errors.Any())
var storeCLI = host.Services.GetRequiredService<MicrosoftStoreCLI>();
storeCLI.Subcommands.Add(new TestCommand(this, host));
var parseResult = storeCLI.Parse(args);

IHostApplicationLifetime lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();

if (parseResult.CommandResult.Command is not MicrosoftStoreCLI
&& parseResult.CommandResult.Command is not ReconfigureCommand
&& parseResult.CommandResult.Command is not TestCommand
&& !await MicrosoftStoreCLI.InitAsync(ErrorAnsiConsole, host.Services.GetService<IConfigurationManager<Configurations>>()!, host.Services.GetService<ICredentialManager>()!, host.Services.GetService<IConsoleReader>()!, host.Services.GetService<ICLIConfigurator>()!, host.Services.GetService<ILogger<Program>>()!, lifetime.ApplicationStopping))
{
// Initialization failed
await host.StopAsync();
return (Output: string.Empty, Error: string.Empty);
}

if (parseResult.Action is ParseErrorAction parseError)
{
throw new ArgumentException(string.Join(Environment.NewLine, parseResult.Errors.Select(e => e.Message)));
parseError.ShowTypoCorrections = true;
parseError.ShowHelp = true;
}

var testOutputConsole = new TestConsole(outputCapture, errorCapture);
parseResult.InvocationConfiguration.Output = outputCapture;
parseResult.InvocationConfiguration.Error = errorCapture;

var invokeTask = parseResult.InvokeAsync(testOutputConsole);
var invokeTask = parseResult.InvokeAsync(parseResult.InvocationConfiguration);

var result = await invokeTask.ConfigureAwait(false);

await host.StopAsync();

if (expectedResult.HasValue)
{
result.Should().Be(expectedResult.Value);
Expand Down Expand Up @@ -858,29 +835,29 @@ private OutputCapture RefreshAnsiConsole()
return errorCapture;
}

private Func<InvocationContext, Task>? _testCallback;
private Func<ParseResult, IHost, CancellationToken, Task>? _testCallback;

private async Task TestAsync(InvocationContext invocationContext)
private async Task TestAsync(ParseResult parseResult, IHost host, CancellationToken cancellationToken)
{
if (_testCallback != null)
{
await _testCallback(invocationContext);
await _testCallback(parseResult, host, cancellationToken);
}
}

private sealed class TestCommand : Command
{
private readonly BaseCommandLineTest _baseCommandLineTest;

public TestCommand(BaseCommandLineTest baseCommandLineTest)
public TestCommand(BaseCommandLineTest baseCommandLineTest, IHost host)
: base("test")
{
_baseCommandLineTest = baseCommandLineTest;
this.SetHandler(_baseCommandLineTest.TestAsync);
SetAction((parseResult, ct) => _baseCommandLineTest.TestAsync(parseResult, host, ct));
}
}

internal sealed class OutputCapture : TextWriter, IStandardStreamWriter, IDisposable
internal sealed class OutputCapture : TextWriter, IDisposable
{
#pragma warning disable CA2213 // Disposable fields should be disposed
private readonly TextWriter _stdOutWriter;
Expand Down Expand Up @@ -908,15 +885,6 @@ public override void WriteLine(string? value)
}
}

internal sealed class TestConsole(BaseCommandLineTest.OutputCapture outputCapture, BaseCommandLineTest.OutputCapture errorCapture) : IConsole
{
public IStandardStreamWriter Error { get; set; } = errorCapture;
public IStandardStreamWriter Out { get; set; } = outputCapture;
public bool IsOutputRedirected { get; set; }
public bool IsErrorRedirected { get; set; }
public bool IsInputRedirected { get; set; }
}

internal sealed class CustomAnsiConsoleOutput(TextWriter writer) : IAnsiConsoleOutput
{
public TextWriter Writer { get; } = writer ?? throw new ArgumentNullException(nameof(writer));
Expand Down
2 changes: 1 addition & 1 deletion MSStore.CLI.UnitTests/EmptyCommandUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task EmptyCommandShouldReturnZeroIfLoggedIn()

var result = await ParseAndInvokeAsync([]);

result.Error.Should().Contain("CLI tool to automate Microsoft Store Developer tasks.");
result.Output.Should().Contain("CLI tool to automate Microsoft Store Developer tasks.");
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions MSStore.CLI.UnitTests/FlightsSubmissionCommandUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public async Task FlightSubmissionCommandWithNoParameter()
[
"flights",
"submission"
]);
], 1);

result.Error.Should().Contain("Execute flight submissions related tasks.");
result.Output.Should().Contain("Execute flight submissions related tasks.");
}

[TestMethod]
Expand Down
10 changes: 5 additions & 5 deletions MSStore.CLI.UnitTests/MSStore.CLI.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="MSTest.Sdk/3.10.1">
<Project Sdk="MSTest.Sdk/3.10.3">

<PropertyGroup>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">net9.0;net9.0-windows10.0.17763.0</TargetFrameworks>
Expand Down Expand Up @@ -44,22 +44,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.Testing.Extensions.TrxReport" Version="1.8.1" />
<PackageReference Update="Microsoft.Testing.Extensions.TrxReport" Version="1.8.3" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="MSTest.Analyzers" Version="3.10.1">
<PackageReference Update="MSTest.Analyzers" Version="3.10.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<PackageReference Update="MSTest.TestAdapter" Version="3.10.1" />
<PackageReference Update="MSTest.TestAdapter" Version="3.10.3" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="MSTest.TestFramework" Version="3.10.1" />
<PackageReference Update="MSTest.TestFramework" Version="3.10.3" />
</ItemGroup>

</Project>
6 changes: 1 addition & 5 deletions MSStore.CLI.UnitTests/ProjectConfiguratorFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.CommandLine.Hosting;
using Microsoft.Extensions.DependencyInjection;
using MSStore.CLI.ProjectConfigurators;

Expand Down Expand Up @@ -33,11 +32,8 @@ public async Task ProjectConfiguratorFactoryFindsURLProperly(string pathOrUrl, T
pathOrUrl = path;
}

await RunTestAsync(async (context) =>
await RunTestAsync(async (parseResult, host, ct) =>
{
var ct = context.GetCancellationToken();

var host = context.GetHost();
var projectConfiguratorFactory = host.Services.GetService<IProjectConfiguratorFactory>()!;

if (testDataProjectSubPath != null && testDataProjectSubPath.Length != 0 && path != null)
Expand Down
4 changes: 2 additions & 2 deletions MSStore.CLI.UnitTests/SettingsCommandUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public async Task SettingsCommandWithNoParametersShouldReturnZeroAndShowHelp()
"settings"
]);

result.Error.Should().Contain("Usage:");
result.Error.Should().Contain("settings [command] [options]");
result.Output.Should().Contain("Usage:");
result.Output.Should().Contain("settings [command] [options]");
}

[TestMethod]
Expand Down
4 changes: 2 additions & 2 deletions MSStore.CLI.UnitTests/SubmissionCommandPackagedUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public async Task SubmissionCommandWithNoParameter()
var result = await ParseAndInvokeAsync(
[
"submission"
]);
], 1);

result.Error.Should().Contain("Executes commands to a store submission.");
result.Output.Should().Contain("Executes commands to a store submission.");
}

[TestMethod]
Expand Down
34 changes: 0 additions & 34 deletions MSStore.CLI/CommandExtensions.cs

This file was deleted.

Loading
Loading