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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal partial class CollectLinuxCommandHandler
private LineRewriter rewriter;
private long statusUpdateTimestamp;
private Version minRuntimeSupportingUserEventsIPCCommand = new(10, 0, 0);
private readonly bool cancelOnEnter;
private readonly bool printStatusOverTime;

internal sealed record CollectLinuxArgs(
CancellationToken Ct,
Expand All @@ -41,6 +43,8 @@ public CollectLinuxCommandHandler(IConsole console = null)
{
Console = console ?? new DefaultConsole();
rewriter = new LineRewriter(Console);
cancelOnEnter = !Console.IsInputRedirected;
printStatusOverTime = !Console.IsOutputRedirected;
}

internal static bool IsSupported()
Expand Down Expand Up @@ -491,12 +495,12 @@ private int OutputHandler(uint type, IntPtr data, UIntPtr dataLen)
}
}

if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
if (cancelOnEnter && Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
{
stopTracing = true;
}

if (ot == OutputType.Progress)
if (printStatusOverTime && ot == OutputType.Progress)
{
long currentTimestamp = Stopwatch.GetTimestamp();
if (statusUpdateTimestamp != 0 && currentTimestamp < statusUpdateTimestamp)
Expand Down
10 changes: 6 additions & 4 deletions src/tests/Common/MockConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public MockConsole(int width, int height, ITestOutputHelper outputHelper = null)

public bool IsOutputRedirected { get; set; }

public bool IsInputRedirected { get; private set; }
public bool IsInputRedirected { get; set; }

public bool KeyAvailable { get; private set; }
public bool KeyAvailable { get; set; }

public TextWriter Out => this;

Expand Down Expand Up @@ -186,9 +186,11 @@ public void FlushTestLogging()

public string GetLineText(int row) => new string(_chars[row]).TrimEnd();

public ConsoleKeyInfo ReadKey() => Console.ReadKey();
public ConsoleKeyInfo NextKeyInfo { get; set; } = new ConsoleKeyInfo('\0', ConsoleKey.Enter, false, false, false);

public ConsoleKeyInfo ReadKey(bool intercept) => Console.ReadKey(intercept);
public ConsoleKeyInfo ReadKey() => NextKeyInfo;

public ConsoleKeyInfo ReadKey(bool intercept) => NextKeyInfo;

public string[] Lines
{
Expand Down
53 changes: 52 additions & 1 deletion src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CollectLinuxCommandFunctionalTests
{
public static bool IsCollectLinuxSupported => CollectLinuxCommandHandler.IsSupported();
public static bool IsCollectLinuxNotSupported => !CollectLinuxCommandHandler.IsSupported();

private readonly ITestOutputHelper _outputHelper;

public CollectLinuxCommandFunctionalTests(ITestOutputHelper outputHelper)
Expand Down Expand Up @@ -269,6 +269,57 @@ public void CollectLinuxCommand_DoesNotChangeCursorVisibility_WhenOutputIsRedire
Assert.Equal(initialCursorVisible, console.CursorVisible);
}

[ConditionalFact(nameof(IsCollectLinuxSupported))]
public void CollectLinuxCommand_DoesNotPrintStatusUpdates_WhenOutputIsRedirected()
{
MockConsole console = new(200, 30, _outputHelper);
console.IsOutputRedirected = true;

var handler = new CollectLinuxCommandHandler(console);
handler.RecordTraceInvoker = (cmd, len, cb) => {
// Send progress output type.
cb((uint)3, IntPtr.Zero, UIntPtr.Zero);
return 0;
};

int exitCode = handler.CollectLinux(TestArgs());
Assert.Equal((int)ReturnCode.Ok, exitCode);

string[] lines = console.Lines;
Assert.DoesNotContain(lines, l => l.Contains("Recording trace", StringComparison.OrdinalIgnoreCase));
Assert.DoesNotContain(lines, l => l.Contains("Press <Enter>", StringComparison.OrdinalIgnoreCase));
}

[ConditionalFact(nameof(IsCollectLinuxSupported))]
public void CollectLinuxCommand_DoesNotReadKey_WhenInputIsRedirected()
{
MockConsole console = new(200, 30, _outputHelper);
console.IsInputRedirected = true;
console.KeyAvailable = true;
console.NextKeyInfo = new ConsoleKeyInfo('\r', ConsoleKey.Enter, false, false, false);

var handler = new CollectLinuxCommandHandler(console);
bool callbackInvoked = false;
handler.RecordTraceInvoker = (cmd, len, cb) => {
// Send progress output type.
int result = cb((uint)3, IntPtr.Zero, UIntPtr.Zero);

// It should return 0, i.e., continue tracing, even though
// enter was pressed.
Assert.Equal(0, result);

callbackInvoked = true;

return 0;
};

int exitCode = handler.CollectLinux(TestArgs());
Assert.Equal((int)ReturnCode.Ok, exitCode);

// The important assertion is in the callback so make sure it was called.
Assert.True(callbackInvoked);
}

private static int Run(object args, MockConsole console)
{
var handler = new CollectLinuxCommandHandler(console);
Expand Down