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
22 changes: 22 additions & 0 deletions TrayApp.Test/StatusReportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ public void Build_WithAStatusAndDetails_AppendsThemInOrder()
Assert.Contains("systemd-inhibit", report, StringComparison.Ordinal);
}

[TestMethod]
public void Build_WithAThrowingDetail_ReportsTheErrorInItsRowAndKeepsTheRest()
{
string report = StatusReport.Build(
"demo",
"holding",
[
new StatusDetail("Inhibitor", () => throw new InvalidOperationException("systemd-inhibit was not found on PATH")),
new StatusDetail("Helper", () => "present"),
]);

Assert.Contains("Platform:", report, StringComparison.Ordinal);
Assert.Contains("Tray icon:", report, StringComparison.Ordinal);
Assert.Contains("Inhibitor: error: systemd-inhibit was not found on PATH", report, StringComparison.Ordinal);
Assert.Contains("Helper:", report, StringComparison.Ordinal);
Assert.Contains("present", report, StringComparison.Ordinal);
}

[TestMethod]
public void Read_WithAThrowingGetter_ReturnsTheError() =>
Assert.AreEqual("error: boom", StatusReport.Read(() => throw new InvalidOperationException("boom")));

[TestMethod]
public void Build_EndsWithoutATrailingNewline()
{
Expand Down
31 changes: 31 additions & 0 deletions TrayApp.Test/TrayAppBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using ktsu.Essentials.PersistenceProviders.InMemory;
using ktsu.TrayApp;
Expand Down Expand Up @@ -161,7 +162,7 @@
}

[TestMethod]
public void Preferences_WithTheLongestDebounce_IsAcceptedAndATimerTakesIt()

Check warning on line 165 in TrayApp.Test/TrayAppBuilderTests.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Add at least one assertion to this test case.

Check warning on line 165 in TrayApp.Test/TrayAppBuilderTests.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Add at least one assertion to this test case.
{
InMemoryPersistenceProvider<string> provider = new();
TrayAppDefinition app = TrayAppBuilder.Create("demo")
Expand Down Expand Up @@ -351,6 +352,36 @@
Assert.IsFalse(started);
}

[TestMethod]
[DoNotParallelize]
public async Task RunAsync_WithStatusAndThrowingGetters_PrintsEveryRowAndExitsZero()
{
TextWriter original = Console.Out;
using StringWriter output = new();
Console.SetOut(output);
int exitCode;
try
{
exitCode = await TrayAppBuilder.Create("demo")
.Status(() => throw new InvalidOperationException("state unknown"))
.Status("Inhibitor", () => throw new InvalidOperationException("systemd-inhibit was not found on PATH"))
.Status("Helper", () => "present")
.RunAsync(["--no-tray", "--status"])
.ConfigureAwait(false);
}
finally
{
Console.SetOut(original);
}

string report = output.ToString();
Assert.AreEqual(0, exitCode);
Assert.Contains("Platform:", report, StringComparison.Ordinal);
Assert.Contains("State: error: state unknown", report, StringComparison.Ordinal);
Assert.Contains("Inhibitor: error: systemd-inhibit was not found on PATH", report, StringComparison.Ordinal);
Assert.Contains("Helper:", report, StringComparison.Ordinal);
}

[TestMethod]
public async Task RunAsync_WithStatusAndARememberedToggle_RunsNoSetterAndReportsTheLiveState()
{
Expand Down
24 changes: 23 additions & 1 deletion TrayApp/StatusReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,36 @@ public static string Build(string name, string? status, IReadOnlyList<StatusDeta

foreach (StatusDetail detail in details)
{
AppendRow(report, detail.Label, detail.Value());
AppendRow(report, detail.Label, Read(detail.Value));
}

// Every row above appended a newline; the report is quoted and compared in tests, so it ends where
// the last row's text ends.
return report.ToString().TrimEnd('\r', '\n');
}

/// <summary>
/// Reads one of the tool's status getters for the report.
/// </summary>
/// <remarks>
/// The report exists to explain a tool that cannot do its job, and a getter is likely to throw for the
/// same reason (a missing helper, say), so a throwing getter becomes its own row's text rather than
/// losing every row the library and the other getters would have printed.
/// </remarks>
/// <param name="getter">The tool's getter.</param>
/// <returns>What the getter returned, or <c>error: </c> and the message of what it threw.</returns>
internal static string Read(Func<string> getter)
{
try
{
return getter();
}
catch (Exception ex) when (FatalError.IsNotFatal(ex))
{
return "error: " + ex.Message;
}
}

private static void AppendRow(StringBuilder report, string label, string value) =>
report.AppendLine($"{(label + ':').PadRight(LabelWidth)}{value}");
}
2 changes: 1 addition & 1 deletion TrayApp/TrayAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public Task<int> RunAsync(string[] args)

if (options.ShowStatus)
{
Console.WriteLine(StatusReport.Build(app.Name, app.Status?.Invoke(), app.StatusDetails));
Console.WriteLine(StatusReport.Build(app.Name, app.Status is null ? null : StatusReport.Read(app.Status), app.StatusDetails));
return Task.FromResult(ExitSuccess);
}

Expand Down
Loading