Skip to content

Commit

Permalink
Improve path validation and reporting markup escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Feb 18, 2025
1 parent 44d62dc commit 483a7d0
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/dotnet-trx/TrxCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Devlooped;

public partial class TrxCommand : Command<TrxCommand.TrxSettings>
{
static readonly JsonSerializerOptions indentedJson = new() { WriteIndented = true };
const string Header = "<!-- header -->";
const string Footer = "<!-- footer -->";

Expand Down Expand Up @@ -72,22 +73,27 @@ public class TrxSettings : CommandSettings
[CommandOption("--gh-summary")]
[DefaultValue(true)]
public bool GitHubSummary { get; set; } = true;

public override ValidationResult Validate()
{
// Validate, normalize and default path.
var path = Path ?? Directory.GetCurrentDirectory();
if (!System.IO.Path.IsPathFullyQualified(path))
path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), path);

Path = File.Exists(path) ? new FileInfo(path).DirectoryName! : System.IO.Path.GetFullPath(path);

return base.Validate();
}
}

public override int Execute(CommandContext context, TrxSettings settings)
{
if (Environment.GetEnvironmentVariable("RUNNER_DEBUG") == "1")
WriteLine(JsonSerializer.Serialize(new { settings }, new JsonSerializerOptions { WriteIndented = true }));

var path = settings.Path ?? Directory.GetCurrentDirectory();
if (!Path.IsPathFullyQualified(path))
path = Path.Combine(Directory.GetCurrentDirectory(), path);

if (File.Exists(path))
path = new FileInfo(path).DirectoryName!;
else
path = Path.GetFullPath(path);
WriteLine(JsonSerializer.Serialize(new { settings }, indentedJson));

// We get this validated by the settings, so it's always non-null.
var path = settings.Path!;
var search = settings.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var testIds = new HashSet<string>();
var passed = 0;
Expand All @@ -112,7 +118,7 @@ public override int Execute(CommandContext context, TrxSettings settings)
// Process from newest files to oldest so that newest result we find (by test id) is the one we keep
foreach (var trx in Directory.EnumerateFiles(path, "*.trx", search).OrderByDescending(File.GetLastWriteTime))
{
ctx.Status($"Discovering test results in {Path.GetFileName(trx)}...");
ctx.Status($"Discovering test results in {Path.GetFileName(trx).EscapeMarkup()}...");
using var file = File.OpenRead(trx);
// Clears namespaces
var doc = HtmlDocument.Load(file, new HtmlReaderSettings { CaseFolding = Sgml.CaseFolding.None });
Expand Down

0 comments on commit 483a7d0

Please sign in to comment.