Skip to content

Commit ae3e91f

Browse files
committed
Enhance path handling in SnapshotBuilder: normalize Windows path separators to forward slashes for consistent cross-platform snapshots.
1 parent 05f7006 commit ae3e91f

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/Confix.Tool/test/Confix.Tool.Tests/TestHelpers/SnapshotBuilder.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.Runtime.InteropServices;
21
using System.Text;
32
using System.Text.RegularExpressions;
4-
using Snapshooter;
53
using Snapshooter.Xunit;
64

75
namespace Confix.Inputs;
@@ -25,6 +23,13 @@ public SnapshotBuilder Append(string name, string content)
2523
public SnapshotBuilder AddReplacement(string original, string replacement)
2624
{
2725
_processors.Add(x => x.Replace(original, replacement));
26+
27+
// On Windows, also replace the path with forward slashes (in case output uses normalized paths)
28+
if (Path.DirectorySeparatorChar == '\\' && original.Contains('\\'))
29+
{
30+
_processors.Add(x => x.Replace(original.Replace('\\', '/'), replacement));
31+
}
32+
2833
return this;
2934
}
3035

@@ -39,18 +44,22 @@ public void MatchSnapshot()
3944
content = _processors
4045
.Aggregate(content, (current, processor) => processor(current));
4146

42-
content.MatchSnapshot(SnapshotNameExtension.Create(GetOS()));
43-
}
47+
// Normalize Windows path separators to forward slashes for consistent cross-platform snapshots.
48+
// Only replace backslashes that look like path separators (followed by word chars, dots, or angle brackets)
49+
// but NOT JSON escape sequences like \u, \n, \r, \t, \b, \f, \\, \"
50+
content = PathSeparatorRegex().Replace(content, "/$1");
4451

45-
private static string GetOS()
46-
{
47-
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
48-
? "windows"
49-
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
50-
? "osx"
51-
: "linux";
52+
content.MatchSnapshot();
5253
}
5354

55+
/// <summary>
56+
/// Matches backslashes followed by characters that indicate a path segment,
57+
/// excluding JSON/string escape sequences (\u, \n, \r, \t, \b, \f, \\, \", \/).
58+
/// The negative lookahead (?![ubfnrt\\""/]) ensures we don't match escape sequences.
59+
/// </summary>
60+
[GeneratedRegex(@"\\(?![ubfnrt\\""/])([A-Za-z0-9_.<>])")]
61+
private static partial Regex PathSeparatorRegex();
62+
5463
public SnapshotBuilder RemoveLineThatStartsWith(string value)
5564
{
5665
_processors.Add(

0 commit comments

Comments
 (0)