Skip to content

Commit c2366f7

Browse files
author
Lessley Dennington
committed
traceutils: add traceutils
The implementation of TRACE2 tracing will require truncation of long file names just as TRACE does. To prepare for this, move this logic out of the TRACE class and into its own method in a new static TraceUtils class. Additionally, add a unit test to validate this logic.
1 parent 7cee518 commit c2366f7

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using Xunit;
5+
6+
namespace GitCredentialManager.Tests;
7+
8+
public class TraceUtilsTests
9+
{
10+
[Theory]
11+
[InlineData("/foo/bar/baz/boo", 10, "...baz/boo")]
12+
[InlineData("thisfileshouldbetruncated", 12, "...truncated")]
13+
public void FormatSource_ReturnsExpectedSourceValues(string path, int sourceColumnMaxWidth, string expectedSource)
14+
{
15+
string actualSource = TraceUtils.FormatSource(path, sourceColumnMaxWidth);
16+
Assert.Equal(actualSource, expectedSource);
17+
}
18+
}

src/shared/Core/Trace.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,7 @@ private static string FormatText(string message, string filePath, int lineNumber
307307

308308
if (source.Length > sourceColumnMaxWidth)
309309
{
310-
int idx = 0;
311-
int maxlen = sourceColumnMaxWidth - 3;
312-
int srclen = source.Length;
313-
314-
while (idx >= 0 && (srclen - idx) > maxlen)
315-
{
316-
idx = source.IndexOf('\\', idx + 1);
317-
}
318-
319-
// If we cannot find a path separator which allows the path to be long enough, just truncate the file name
320-
if (idx < 0)
321-
{
322-
idx = srclen - maxlen;
323-
}
324-
325-
source = "..." + source.Substring(idx);
310+
source = TraceUtils.FormatSource(source, sourceColumnMaxWidth);
326311
}
327312

328313
// Git's trace format is "{timestamp,-15} {source,-23} trace: {details}"

src/shared/Core/TraceUtils.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace GitCredentialManager;
2+
3+
public static class TraceUtils
4+
{
5+
public static string FormatSource(string source, int sourceColumnMaxWidth)
6+
{
7+
int idx = 0;
8+
int maxlen = sourceColumnMaxWidth - 3;
9+
int srclen = source.Length;
10+
11+
while (idx >= 0 && (srclen - idx) > maxlen)
12+
{
13+
idx = source.IndexOf('\\', idx + 1);
14+
}
15+
16+
// If we cannot find a path separator which allows the path to be long enough, just truncate the file name
17+
if (idx < 0)
18+
{
19+
idx = srclen - maxlen;
20+
}
21+
22+
return "..." + source.Substring(idx);
23+
}
24+
}

0 commit comments

Comments
 (0)