Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 21, 2025

  • Explore repository structure and understand the issue
  • Review CliHostEnvironment.cs implementation
  • Review existing tests in CliHostEnvironmentTests.cs
  • Run existing tests to verify they pass
  • Update CliHostEnvironment to detect console redirection
    • Added Console.IsInputRedirected check in DetectInteractiveInput
    • Added Console.IsOutputRedirected check in DetectInteractiveOutput
    • Added Console.IsOutputRedirected check in DetectAnsiSupport
  • Address feedback: Disable ANSI colors when output is redirected
  • Make console redirection state testable
    • Added constructor overload that accepts isInputRedirected and isOutputRedirected parameters
    • Existing constructor delegates to new constructor with Console.IsInputRedirected and Console.IsOutputRedirected
    • Updated all detection methods to use parameters instead of static Console properties
    • Updated all tests to use new constructor with explicit values
    • Added 3 new tests for redirection scenarios
  • Fix test failures in ConsoleInteractionServiceTests
    • Updated TestHelpers.CreateInteractiveHostEnvironment() to explicitly set isInputRedirected=false and isOutputRedirected=false
    • Updated TestHelpers.CreateNonInteractiveHostEnvironment() to explicitly set redirection state
    • This ensures prompting tests simulate an interactive environment correctly
  • Verify all tests pass (495 total Aspire.Cli tests passing)

Summary

Successfully refactored CliHostEnvironment to make console redirection state testable and fixed test failures:

Changes Made:

  1. Added new constructor overload accepting isInputRedirected and isOutputRedirected parameters
  2. Existing constructor delegates to new constructor with actual Console.IsInputRedirected and Console.IsOutputRedirected values
  3. Updated DetectInteractiveInput(), DetectInteractiveOutput(), and DetectAnsiSupport() to accept parameters instead of directly accessing Console
  4. Updated all existing tests to use new constructor with explicit values (no longer depends on actual console state)
  5. Added 3 new tests that specifically test redirection scenarios
  6. Fixed TestHelpers to explicitly set redirection state to false for interactive environment simulation

Benefits:

  • Tests are now deterministic and don't depend on actual console redirection state during test execution
  • Tests can explicitly verify behavior when input/output is redirected
  • Production code still uses actual Console state via default constructor
  • Better separation of concerns and testability
  • Prompting tests now work correctly by simulating interactive environment

Testing:

  • All 495 Aspire.Cli tests pass
  • ConsoleInteractionServiceTests now pass correctly
  • Tests now properly verify all redirection scenarios with explicit values
Original prompt

In @dotnet/aspire/files/src/Aspire.Cli/Utils/CliHostEnvironment.cs Handle console output being redirected as well. If the input and output being redirected as well.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Handle redirected console input and output Handle console input/output redirection in CliHostEnvironment Oct 21, 2025
@Copilot Copilot AI requested a review from davidfowl October 21, 2025 06:57
Copilot finished work on behalf of davidfowl October 21, 2025 06:57
@davidfowl
Copy link
Member

@copilot We also don't want ansi colors when the output is redirected.

@Copilot
Copy link
Contributor Author

Copilot AI commented Oct 21, 2025

@copilot We also don't want ansi colors when the output is redirected.

Done. ANSI colors are now disabled when console output is redirected. Updated DetectAnsiSupport() to check Console.IsOutputRedirected and adjusted the related tests. (commit e1c7842)

Copilot finished work on behalf of davidfowl October 21, 2025 07:09
@davidfowl davidfowl marked this pull request as ready for review October 21, 2025 07:15
@Copilot Copilot AI review requested due to automatic review settings October 21, 2025 07:15
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds detection for console stream redirection to the CliHostEnvironment class, ensuring the CLI behaves appropriately when input/output is piped or redirected. The changes prevent interactive features (prompts, spinners, ANSI colors) from being used inappropriately in non-interactive contexts like automation scripts or log files.

Key Changes:

  • Enhanced DetectInteractiveInput() to check Console.IsInputRedirected
  • Enhanced DetectInteractiveOutput() to check Console.IsOutputRedirected
  • Enhanced DetectAnsiSupport() to check Console.IsOutputRedirected

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Aspire.Cli/Utils/CliHostEnvironment.cs Added console redirection detection for input, output, and ANSI support
tests/Aspire.Cli.Tests/Utils/CliHostEnvironmentTests.cs Updated test assertions to account for console redirection during test execution

@github-actions
Copy link
Contributor

github-actions bot commented Oct 21, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 12242

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 12242"

@davidfowl davidfowl requested a review from mitchdenny October 21, 2025 07:21
@davidfowl
Copy link
Member

@copilot We need to make this testable so these values need to be passed in as input to the constructor

@Copilot
Copy link
Contributor Author

Copilot AI commented Oct 21, 2025

@copilot We need to make this testable so these values need to be passed in as input to the constructor

Made testable by adding a constructor overload that accepts isInputRedirected and isOutputRedirected parameters. The existing constructor delegates to the new one with actual Console.IsInputRedirected and Console.IsOutputRedirected values. Updated all tests to use explicit values and added 3 new tests for redirection scenarios. (commit 43bbab0)

Copilot finished work on behalf of davidfowl October 21, 2025 14:32
@davidfowl
Copy link
Member

@copilot Any test, testing prompting needs to set isInputRedirected to true.

🔴 Aspire.Cli.Tests.Interaction.ConsoleInteractionServiceTests.PromptForSelectionsAsync_EmptyChoices_ThrowsEmptyChoicesException
Assert.Throws() Failure: Exception type was not an exact match
Expected: typeof(Aspire.Cli.Interaction.EmptyChoicesException)
Actual: typeof(System.InvalidOperationException)
---- System.InvalidOperationException : Interactive input is not supported in this environment. Use the --non-interactive flag or ensure the CLI is running in an interactive terminal. at Aspire.Cli.Tests.Interaction.ConsoleInteractionServiceTests.PromptForSelectionsAsync_EmptyChoices_ThrowsEmptyChoicesException() in /home/runner/work/aspire/aspire/tests/Aspire.Cli.Tests/Interaction/ConsoleInteractionServiceTests.cs:line 34
--- End of stack trace from previous location ---
----- Inner Stack Trace -----
at Aspire.Cli.Interaction.ConsoleInteractionService.PromptForSelectionsAsync[T](String promptText, IEnumerable1 choices, Func2 choiceFormatter, CancellationToken cancellationToken) in //src/Aspire.Cli/Interaction/ConsoleInteractionService.cs:line 127
🔴 Aspire.Cli.Tests.Interaction.ConsoleInteractionServiceTests.PromptForSelectionAsync_EmptyChoices_ThrowsEmptyChoicesException
Assert.Throws() Failure: Exception type was not an exact match
Expected: typeof(Aspire.Cli.Interaction.EmptyChoicesException)
Actual: typeof(System.InvalidOperationException)
---- System.InvalidOperationException : Interactive input is not supported in this environment. Use the --non-interactive flag or ensure the CLI is running in an interactive terminal. at Aspire.Cli.Tests.Interaction.ConsoleInteractionServiceTests.PromptForSelectionAsync_EmptyChoices_ThrowsEmptyChoicesException() in /home/runner/work/aspire/aspire/tests/Aspire.Cli.Tests/Interaction/ConsoleInteractionServiceTests.cs:line 21
--- End of stack trace from previous location ---
----- Inner Stack Trace -----
at Aspire.Cli.Interaction.ConsoleInteractionService.PromptForSelectionAsync[T](String promptText, IEnumerable1 choices, Func2 choiceFormatter, CancellationToken cancellationToken) in /
/src/Aspire.Cli/Interaction/ConsoleInteractionService.cs:line 100

@Copilot
Copy link
Contributor Author

Copilot AI commented Oct 21, 2025

@copilot Any test, testing prompting needs to set isInputRedirected to true.

🔴 Aspire.Cli.Tests.Interaction.ConsoleInteractionServiceTests.PromptForSelectionsAsync_EmptyChoices_ThrowsEmptyChoicesE...

Fixed by updating TestHelpers to explicitly set isInputRedirected: false and isOutputRedirected: false when creating interactive host environments. This ensures prompting tests simulate an interactive environment correctly. All tests now pass. (commit 2e8a5f3)

Copilot finished work on behalf of davidfowl October 21, 2025 14:51
@mitchdenny
Copy link
Member

I'm not sure this is working?

image

I downloaded the build and added app containers they did aspire deploy. I expected it to bomb out because the environment variables were not provided so I thought it would exit with the error, but looking at what was captured in the output it was just waiting for input.

@davidfowl
Copy link
Member

@mitchdenny are you sure you used this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants