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
140 changes: 140 additions & 0 deletions MSStore.CLI.UnitTests/InitCommandUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,145 @@ public async Task InitCommandShouldOpenBrowserIfNotRegistered()

BrowserLauncher.Verify(x => x.OpenBrowserAsync("https://partner.microsoft.com/dashboard/registration", true, It.IsAny<CancellationToken>()), Times.Once);
}

[TestMethod]
public async Task InitCommandShouldFailIfAppIdIsNotFound()
{
AddDefaultFakeAccount();
AddFakeApps();

FakeStorePackagedAPI
.Setup(x => x.GetApplicationAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new InvalidOperationException("Not found"));

var result = await ParseAndInvokeAsync(
[
"init",
"https://www.microsoft.com/",
"--publish",
"--appId",
"9PN3ABCDEFGZ",
"--verbose"
], -1);

result.Error.Should().Contain("Could not retrieve your application. Please make sure you have the correct AppId.");

FakeStorePackagedAPI.Verify(x => x.GetApplicationsAsync(It.IsAny<CancellationToken>()), Times.Never);
FakeConsole.Verify(
x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()),
Times.Never);
}

[TestMethod]
public async Task InitCommandShouldFailOnCIIfNoAppIdIsProvided()
Comment thread
azchohfi marked this conversation as resolved.
{
AddDefaultFakeAccount();
AddFakeApps();

EnvironmentInformationService
.Setup(x => x.IsRunningOnCI)
.Returns(true);

var result = await ParseAndInvokeAsync(
[
"init",
"https://www.microsoft.com/",
"--publish",
"--verbose"
], -1);

result.Error.Should().Contain("Could not select an application because the current environment is not interactive.");
result.Error.Should().Contain("--appId");

FakeConsole.Verify(
x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()),
Times.Never);
}

[TestMethod]
public async Task InitCommandShouldAutoSelectOnCIIfAccountHasASingleApp()
{
AddDefaultFakeAccount();
AddFakeApps();

EnvironmentInformationService
.Setup(x => x.IsRunningOnCI)
.Returns(true);

FakeStorePackagedAPI
.Setup(x => x.GetApplicationsAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync([FakeApps[0]]);

var result = await ParseAndInvokeAsync(
[
"init",
"https://www.microsoft.com/",
"--output",
Path.GetTempPath(),
"--verbose"
]);

// Asserted piecewise because the app name and id are wrapped in markup,
// which becomes ANSI escape sequences when the console supports them.
result.Error.Should().Contain(FakeApps[0].PrimaryName!);
result.Error.Should().Contain(FakeApps[0].Id!);
result.Error.Should().Contain(", the only application registered in your account.");
result.Error.Should().NotContain("--appId");

FakeConsole.Verify(
x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()),
Times.Never);
}

[TestMethod]
public async Task InitCommandShouldFailIfNotOnCIAndPromptIsNotSupported()
{
AddDefaultFakeAccount();
AddFakeApps();

FakeConsole
.Setup(x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()))
.ThrowsAsync(new NotSupportedException("Cannot show selection prompt since the current terminal isn't interactive."));

var result = await ParseAndInvokeAsync(
[
"init",
"https://www.microsoft.com/",
"--publish",
"--verbose"
], -1);

result.Error.Should().Contain("Could not select an application because the current environment is not interactive.");
result.Error.Should().Contain("--appId");

FakeConsole.Verify(
x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()),
Times.Once);
}
}
}
83 changes: 78 additions & 5 deletions MSStore.CLI.UnitTests/ProjectConfiguratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Text.RegularExpressions;

using MSStore.CLI.Services.PWABuilder;

namespace MSStore.CLI.UnitTests
Expand All @@ -20,12 +22,13 @@ public void Init()

private static (string Output, string Error) CleanResult((string Output, string Error) result)
{
static string Clean(string value) => Regex.Replace(value, @"\x1B\[[0-9;]*[A-Za-z]", string.Empty)
.Replace(Environment.NewLine, " ")
.Replace(" ", " ");

return (
Output: result.Output
.Replace(Environment.NewLine, " ")
.Replace(" ", " "),
Error: result.Error.Replace(Environment.NewLine, " ")
.Replace(" ", " "));
Output: Clean(result.Output),
Error: Clean(result.Error));
}

[TestMethod]
Expand Down Expand Up @@ -660,6 +663,8 @@ public async Task ProjectConfiguratorParsesPWASuccessfullyIfOnCIIfPublish()
"init",
"https://microsoft.com",
"--publish",
"--appId",
FakeApps[0].Id!,
"--verbose"
]);

Expand Down Expand Up @@ -724,5 +729,73 @@ public async Task ProjectConfiguratorParserPWAShouldNotCallPartnerCenterAPIIfPub
result.Error.Should().Contain("You've provided a URL, so we'll use");
result.Error.Should().Contain("Submission commit success!");
}

[TestMethod]
public async Task ProjectConfiguratorParsesPWASuccessfullyOnCIIfAppIdIsProvided()
{
SetupSuccessfullPWA(true);

var result = await ParseAndInvokeAsync(
[
"init",
"https://microsoft.com",
"--publisherDisplayName",
"FAKE_PUBLISHER_DISPLAY_NAME",
"--publish",
"-id",
FakeApps[1].Id!,
"--verbose"
]);

result = CleanResult(result);

result.Error.Should().Contain("You've provided a URL, so we'll use");
result.Error.Should().Contain($"AppId: {FakeApps[1].Id}");
result.Error.Should().Contain("Submission commit success!");

FakeStorePackagedAPI.Verify(x => x.GetApplicationsAsync(It.IsAny<CancellationToken>()), Times.Never);
FakeConsole.Verify(
x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()),
Times.Never);
}

[TestMethod]
public async Task ProjectConfiguratorParsesPWASuccessfullyOnCIIfAccountHasASingleApp()
{
SetupSuccessfullPWA(true);

FakeStorePackagedAPI
.Setup(x => x.GetApplicationsAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync([FakeApps[0]]);

var result = await ParseAndInvokeAsync(
[
"init",
"https://microsoft.com",
"--publisherDisplayName",
"FAKE_PUBLISHER_DISPLAY_NAME",
"--publish",
"--verbose"
]);

result = CleanResult(result);

result.Error.Should().Contain($"Using {FakeApps[0].PrimaryName} ({FakeApps[0].Id}), the only application registered in your account.");
result.Error.Should().Contain("Submission commit success!");

FakeConsole.Verify(
x => x.SelectionPromptAsync(
It.Is<string>(s => s == "Which application should we use to configure your project?"),
It.IsAny<IEnumerable<string>>(),
It.IsAny<int>(),
It.IsAny<Func<string, string>>(),
It.IsAny<CancellationToken>()),
Times.Never);
}
}
}
Loading