Skip to content

Commit 010beff

Browse files
phil-scott-78patriksvensson
authored andcommitted
Normalizes paths when writing exceptions to the console for tests.
The verified output for Should_Write_GenericException was outputting ProjectDirectory}Data\Exceptions.cs on my Windows machine, but the Verified version wasProjectDirectory}Data/Exceptions.cs Wasn't causing a build issue because we run those on Ubuntu, but locally it was giving me an error.
1 parent 5af8e09 commit 010beff

File tree

2 files changed

+41
-59
lines changed

2 files changed

+41
-59
lines changed

src/Spectre.Console.Testing/Extensions/TestConsoleExtensions.Exceptions.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Spectre.Console.Tests;
2+
3+
public static class TestConsoleExtensions
4+
{
5+
private static readonly Regex _lineNumberRegex = new Regex(":\\d+", RegexOptions.Singleline);
6+
private static readonly Regex _filenameRegex = new Regex("\\sin\\s.*cs:nn", RegexOptions.Multiline);
7+
private static readonly Regex _pathSeparatorRegex = new Regex(@"[/\\]+");
8+
9+
public static string WriteNormalizedException(this TestConsole console, Exception ex, ExceptionFormats formats = ExceptionFormats.Default)
10+
{
11+
if (!string.IsNullOrWhiteSpace(console.Output))
12+
{
13+
throw new InvalidOperationException("Output buffer is not empty.");
14+
}
15+
16+
console.WriteException(ex, formats);
17+
return string.Join("\n", NormalizeStackTrace(console.Output)
18+
.NormalizeLineEndings()
19+
.Split(new char[] { '\n' })
20+
.Select(line => line.TrimEnd()));
21+
}
22+
23+
public static string NormalizeStackTrace(string text)
24+
{
25+
// First normalize line numbers
26+
text = _lineNumberRegex.Replace(text, ":nn");
27+
28+
// Then normalize paths and filenames
29+
text = _filenameRegex.Replace(text, match =>
30+
{
31+
var value = match.Value;
32+
var index = value.LastIndexOfAny(new[] { '\\', '/' });
33+
var filename = value.Substring(index + 1, value.Length - index - 1);
34+
35+
return $" in /xyz/{filename}";
36+
});
37+
38+
// Finally normalize any remaining path separators
39+
return _pathSeparatorRegex.Replace(text, "/");
40+
}
41+
}

0 commit comments

Comments
 (0)