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
70 changes: 70 additions & 0 deletions src/winapp-CLI/WinApp.Cli.Tests/MakeAppxToolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation and Contributors. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using WinApp.Cli.Tools;

namespace WinApp.Cli.Tests;

[TestClass]
public sealed class MakeAppxToolTests
{
[TestMethod]
public void ExecutableName_ReturnsMakeAppxExe()
{
Assert.AreEqual("makeappx.exe", new MakeAppxTool().ExecutableName);
}

[TestMethod]
public void PrintErrorText_LogsStdoutFromFirstErrorLineOnly()
{
var logger = new CapturingLogger<MakeAppxTool>();
var stdout = string.Join(Environment.NewLine,
"Processing payload one",
"MakeAppx : error: manifest invalid",
"Details after error",
"MakeAppx : error: package failed");

new MakeAppxTool().PrintErrorText(stdout, stderr: string.Empty, logger);

Assert.AreEqual(3, logger.Entries.Count);
Assert.IsFalse(logger.Entries.Any(e => e.Message.Contains("Processing payload", StringComparison.Ordinal)));
Assert.IsTrue(logger.Has(LogLevel.Error, "manifest invalid"));
Assert.IsTrue(logger.Has(LogLevel.Error, "Details after error"));
Assert.IsTrue(logger.Has(LogLevel.Error, "package failed"));
}

[TestMethod]
public void PrintErrorText_LogsStderrWhenStdoutHasNoErrorLine()
{
var logger = new CapturingLogger<MakeAppxTool>();

new MakeAppxTool().PrintErrorText("Processing payload", "stderr failure", logger);

Assert.AreEqual(1, logger.Entries.Count);
Assert.IsTrue(logger.Has(LogLevel.Error, "stderr failure"));
}

[TestMethod]
public void PrintErrorText_FallsBackToBaseWhenNoStdoutErrorOrStderr()
{
var logger = new CapturingLogger<MakeAppxTool>();

new MakeAppxTool().PrintErrorText("plain stdout", string.Empty, logger);

Assert.IsTrue(logger.Entries.Count > 0, "Base Tool.PrintErrorText should surface available output.");
Assert.IsTrue(logger.Entries.Any(e => e.Message.Contains("plain stdout", StringComparison.Ordinal)));
}

[TestMethod]
public void PrintErrorText_TreatsWhitespaceStdoutAsAbsentAndUsesStderr()
{
var logger = new CapturingLogger<MakeAppxTool>();

new MakeAppxTool().PrintErrorText(" ", "stderr only", logger);

Assert.AreEqual(1, logger.Entries.Count);
Assert.IsTrue(logger.Has(LogLevel.Error, "stderr only"));
}
}

41 changes: 41 additions & 0 deletions src/winapp-CLI/WinApp.Cli.Tests/MakePriToolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation and Contributors. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging;
using WinApp.Cli.Tools;

namespace WinApp.Cli.Tests;

[TestClass]
public sealed class MakePriToolTests
{
[TestMethod]
public void ExecutableName_ReturnsMakePriExe()
{
Assert.AreEqual("makepri.exe", new MakePriTool().ExecutableName);
}

[TestMethod]
public void PrintErrorText_LogsStderrWhenPresent()
{
var logger = new CapturingLogger<MakePriTool>();

new MakePriTool().PrintErrorText("stdout details", "makepri stderr failure", logger);

Assert.AreEqual(1, logger.Entries.Count);
Assert.IsTrue(logger.Has(LogLevel.Error, "makepri stderr failure"));
Assert.IsFalse(logger.Entries.Any(e => e.Message.Contains("stdout details", StringComparison.Ordinal)));
}

[TestMethod]
public void PrintErrorText_FallsBackToBaseWhenStderrIsBlank()
{
var logger = new CapturingLogger<MakePriTool>();

new MakePriTool().PrintErrorText("makepri stdout failure", " ", logger);

Assert.IsTrue(logger.Entries.Count > 0, "Base Tool.PrintErrorText should surface stdout when makepri.exe produced no stderr.");
Assert.IsTrue(logger.Entries.Any(e => e.Message.Contains("makepri stdout failure", StringComparison.Ordinal)));
}
}

243 changes: 243 additions & 0 deletions src/winapp-CLI/WinApp.Cli.Tests/PromptConfirmationTaskTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
// Copyright (c) Microsoft Corporation and Contributors. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Extensions.Logging.Abstractions;
using Spectre.Console;
using Spectre.Console.Rendering;
using Spectre.Console.Testing;
using WinApp.Cli.ConsoleTasks;

namespace WinApp.Cli.Tests;

[TestClass]
public sealed class PromptConfirmationTaskTests
{
[TestMethod]
public async Task WaitForInputAsync_YThenEnter_ConfirmsAndShowsYesPrompt()
{
var (task, console, updates) = CreateTask("Install package?");
console.Input.PushKey(ConsoleKey.Y);
console.Input.PushKey(ConsoleKey.Enter);

var result = await task.WaitForInputAsync(CancellationToken.None);

Assert.IsTrue(result);
Assert.AreEqual(PromptState.Confirmed, task.State);
Assert.AreEqual(true, task.CompletedMessage);
Assert.AreEqual(true, task.SuccessfullyCompleted);
StringAssert.Contains(task.InProgressMessage, "Install package?");
StringAssert.Contains(task.InProgressMessage, "Yes");
Assert.IsTrue(updates.Any(message => message.Contains("[green](y)[/]: Y", StringComparison.Ordinal)), string.Join("|", updates));
}

[TestMethod]
public async Task WaitForInputAsync_NThenEnter_DeclinesAndShowsNoPrompt()
{
var (task, console, updates) = CreateTask("Continue?");
console.Input.PushKey(ConsoleKey.N);
console.Input.PushKey(ConsoleKey.Enter);

var result = await task.WaitForInputAsync(CancellationToken.None);

Assert.IsFalse(result);
Assert.AreEqual(PromptState.Declined, task.State);
Assert.AreEqual(false, task.CompletedMessage);
Assert.AreEqual(true, task.SuccessfullyCompleted);
StringAssert.Contains(task.InProgressMessage, "Continue?");
StringAssert.Contains(task.InProgressMessage, "No");
Assert.IsTrue(updates.Any(message => message.Contains("[green](y)[/]: N", StringComparison.Ordinal)), string.Join("|", updates));
}

[TestMethod]
public async Task WaitForInputAsync_EnterWithNoTypedInput_UsesDefaultYes()
{
var (task, console, updates) = CreateTask("Use defaults?");
console.Input.PushKey(ConsoleKey.Enter);

var result = await task.WaitForInputAsync(CancellationToken.None);

Assert.IsTrue(result);
Assert.AreEqual(PromptState.Confirmed, task.State);
Assert.IsTrue(updates.First().Contains("Use defaults? [blue][[Y/n]][/] [green](y)[/]: ", StringComparison.Ordinal));
StringAssert.Contains(task.InProgressMessage, "Yes");
}

[TestMethod]
public async Task WaitForInputAsync_InvalidThenValid_IgnoresInvalidKeyAndAcceptsValidAnswer()
{
var (task, console, updates) = CreateTask("Overwrite?");
console.Input.PushKey(ConsoleKey.X);
console.Input.PushKey(ConsoleKey.N);
console.Input.PushKey(ConsoleKey.Enter);

var result = await task.WaitForInputAsync(CancellationToken.None);

Assert.IsFalse(result);
Assert.AreEqual(PromptState.Declined, task.State);
Assert.IsFalse(updates.Any(message => message.Contains(": X", StringComparison.Ordinal)), string.Join("|", updates));
Assert.IsTrue(updates.Any(message => message.Contains(": N", StringComparison.Ordinal)), string.Join("|", updates));
}

[TestMethod]
public async Task WaitForInputAsync_BackspaceRemovesTypedAnswerBeforeDefaultEnter()
{
var (task, console, updates) = CreateTask("Proceed?");
console.Input.PushKey(ConsoleKey.N);
console.Input.PushKey(ConsoleKey.Backspace);
console.Input.PushKey(ConsoleKey.Enter);

var result = await task.WaitForInputAsync(CancellationToken.None);

Assert.IsTrue(result);
Assert.AreEqual(PromptState.Confirmed, task.State);
Assert.IsTrue(updates.Any(message => message.EndsWith(": N", StringComparison.Ordinal)), string.Join("|", updates));
Assert.IsTrue(updates.Any(message => message.EndsWith(": ", StringComparison.Ordinal)), string.Join("|", updates));
StringAssert.Contains(task.InProgressMessage, "Yes");
}

[TestMethod]
public async Task WaitForInputAsync_EscapeDeclinesImmediately()
{
var (task, console, _) = CreateTask("Cancel?");
console.Input.PushKey(ConsoleKey.Escape);
console.Input.PushKey(ConsoleKey.Y);
console.Input.PushKey(ConsoleKey.Enter);

var result = await task.WaitForInputAsync(CancellationToken.None);

Assert.IsFalse(result);
Assert.AreEqual(PromptState.Declined, task.State);
StringAssert.Contains(task.InProgressMessage, "No");
}

[TestMethod]
public async Task WaitForInputAsync_CancellationBeforeInput_ReturnsFalseAndShowsCancelledPrompt()
{
var (task, _, updates) = CreateTask("Wait forever?");
using var cts = new CancellationTokenSource();
cts.Cancel();

var result = await task.WaitForInputAsync(cts.Token);

Assert.IsFalse(result);
Assert.AreEqual(PromptState.Cancelled, task.State);
Assert.AreEqual(false, task.CompletedMessage);
StringAssert.Contains(task.InProgressMessage, "Wait forever?");
StringAssert.Contains(task.InProgressMessage, "(cancelled)");
Assert.IsTrue(updates.Last().Contains("(cancelled)", StringComparison.Ordinal), string.Join("|", updates));
}

[TestMethod]
public async Task WaitForInputAsync_NoInputAvailable_TreatedAsCancelled()
{
// An empty console reader reports EOF as InvalidOperationException; the prompt treats it as cancelled.
var (task, _, updates) = CreateTask("Still waiting?");
using var cts = new CancellationTokenSource();

var result = await task.WaitForInputAsync(cts.Token);

Assert.IsFalse(result);
Assert.AreEqual(PromptState.Cancelled, task.State);
Assert.IsTrue(updates.Last().Contains("(cancelled)", StringComparison.Ordinal), string.Join("|", updates));
}

[TestMethod]
public async Task WaitForInputAsync_CancelledWhileWaitingForKey_ReturnsFalseAndShowsCancelledPrompt()
{
// The console blocks in ReadKeyAsync until the token fires, so this genuinely exercises the
// OperationCanceledException path rather than the empty-console EOF fast-path.
var (task, updates) = CreateBlockingTask("Still waiting?");
using var cts = new CancellationTokenSource();

var pending = task.WaitForInputAsync(cts.Token);
Assert.IsFalse(pending.IsCompleted, "The prompt should block while the reader is waiting for a key.");

cts.Cancel();
var result = await pending;

Assert.IsFalse(result);
Assert.AreEqual(PromptState.Cancelled, task.State);
Assert.IsTrue(updates.Last().Contains("(cancelled)", StringComparison.Ordinal), string.Join("|", updates));
}

[TestMethod]
public void FormatPromptMessage_UnknownStateFallsBackToPromptText()
{
var method = typeof(PromptConfirmationTask).GetMethod("FormatPromptMessage", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

var result = method!.Invoke(null, ["Raw prompt", (PromptState)999, "ignored"]);

Assert.AreEqual("Raw prompt", result);
}

[TestMethod]
public async Task ExecuteAsync_IsNotInputDriverAndReturnsFalseWithoutChangingState()
{
var (task, _, _) = CreateTask("Execute?");

var result = await task.ExecuteAsync(null, CancellationToken.None);

Assert.IsFalse(result);
Assert.AreEqual(PromptState.WaitingForInput, task.State);
Assert.IsNull(task.SuccessfullyCompleted);
}

private static (PromptConfirmationTask Task, TestConsole Console, List<string> Updates) CreateTask(string promptText)
{
var console = new TestConsole();
var updates = new List<string>();
PromptConfirmationTask? task = null;
task = new PromptConfirmationTask(
promptText,
parent: null,
ansiConsole: console,
logger: NullLogger.Instance,
renderLock: new Lock(),
onUpdate: () => updates.Add(task!.InProgressMessage));
updates.Add(task.InProgressMessage);
return (task, console, updates);
}

private static (PromptConfirmationTask Task, List<string> Updates) CreateBlockingTask(string promptText)
{
var console = new BlockingInputConsole(new TestConsole());
var updates = new List<string>();
PromptConfirmationTask? task = null;
task = new PromptConfirmationTask(
promptText,
parent: null,
ansiConsole: console,
logger: NullLogger.Instance,
renderLock: new Lock(),
onUpdate: () => updates.Add(task!.InProgressMessage));
updates.Add(task.InProgressMessage);
return (task, updates);
}

// A console whose input blocks until the cancellation token fires, so tests can exercise the real
// "cancelled while waiting for a key" path (ReadKeyAsync throws OperationCanceledException) rather
// than the empty-console EOF fast-path.
private sealed class BlockingInputConsole(IAnsiConsole inner) : IAnsiConsole
{
public Profile Profile => inner.Profile;
public IAnsiConsoleCursor Cursor => inner.Cursor;
public IAnsiConsoleInput Input { get; } = new BlockingConsoleInput();
public IExclusivityMode ExclusivityMode => inner.ExclusivityMode;
public RenderPipeline Pipeline => inner.Pipeline;
public void Clear(bool home) => inner.Clear(home);
public void Write(IRenderable renderable) => inner.Write(renderable);
}

private sealed class BlockingConsoleInput : IAnsiConsoleInput
{
public bool IsKeyAvailable() => false;

public ConsoleKeyInfo? ReadKey(bool intercept) => throw new NotSupportedException();

public async Task<ConsoleKeyInfo?> ReadKeyAsync(bool intercept, CancellationToken cancellationToken)
{
await Task.Delay(Timeout.Infinite, cancellationToken).ConfigureAwait(false);
return null;
}
}
}
Loading
Loading