Skip to content

Commit 199a407

Browse files
committed
tidy up the path in the report.
1 parent 2437d6e commit 199a407

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace uSync.BackOffice.Extensions;
6+
7+
/// <summary>
8+
/// extensions to manipulate folder path strings.
9+
/// </summary>
10+
public static class PathExtensions
11+
{
12+
/// <summary>
13+
/// truncate a folder path to only show the last count paths..
14+
/// </summary>
15+
public static string TruncatePath(this string path, int count = 3, bool includeFile = false)
16+
{
17+
if (string.IsNullOrWhiteSpace(path)) return path;
18+
19+
var result = "";
20+
21+
var fullPath = includeFile ? path : Path.GetDirectoryName(path);
22+
if (string.IsNullOrWhiteSpace(fullPath)) return fullPath ?? string.Empty;
23+
24+
var bits = fullPath.Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]);
25+
26+
foreach (var item in bits.Reverse().Take(count))
27+
{
28+
if (Path.IsPathRooted(item)) continue;
29+
30+
result = $"{Path.DirectorySeparatorChar}{item}{result}";
31+
}
32+
33+
return result;
34+
}
35+
}

uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Umbraco.Extensions;
1818

1919
using uSync.BackOffice.Configuration;
20+
using uSync.BackOffice.Extensions;
2021
using uSync.BackOffice.Models;
2122
using uSync.BackOffice.Services;
2223
using uSync.BackOffice.SyncHandlers.Models;
@@ -570,7 +571,7 @@ private string GetRootFolder(string path)
570571
if (path.Contains(folder) is true) return Path.GetFileName(folder.TrimEnd('/'));
571572
}
572573

573-
return path;
574+
return path.TruncatePath(3, false);
574575
}
575576

576577

uSync.Tests/Extensions/PathNameTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using NUnit.Framework;
22

3+
using Umbraco.Extensions;
4+
5+
using uSync.BackOffice.Extensions;
36
using uSync.Core;
47

58
namespace uSync.Tests.Extensions
@@ -27,5 +30,35 @@ public void BadFileNamesAreAppended(string filename, string expected)
2730

2831
Assert.AreEqual(expected, value);
2932
}
33+
34+
[TestCase("C:\\Source\\OpenSource\\v13\\uSync\\README.md", "\\OpenSource\\v13\\uSync")]
35+
[TestCase("C:\\Source\\OpenSource\\v13\\README.md", "\\Source\\OpenSource\\v13")]
36+
[TestCase("C:\\Source\\OpenSource\\README.md", "\\Source\\OpenSource")]
37+
[TestCase("C:\\Source\\README.md", "\\Source")]
38+
[TestCase("C:\\Source/OpenSource\\v13\\uSync\\README.md", "\\OpenSource\\v13\\uSync")]
39+
[TestCase("C:\\Source/OpenSource\\v13\\README.md", "\\Source\\OpenSource\\v13")]
40+
[TestCase("C:\\Source/OpenSource/README.md", "\\Source\\OpenSource")]
41+
[TestCase("C:/Source\\README.md", "\\Source")]
42+
[TestCase("README.md", "")]
43+
[TestCase("", "")]
44+
public void TruncatedPathsWithoutFileName(string filename, string expected)
45+
{
46+
var result = filename.TruncatePath(3, false);
47+
48+
Assert.AreEqual(expected, result);
49+
}
50+
51+
[TestCase("C:\\Source\\OpenSource\\v13\\uSync\\README.md", "\\v13\\uSync\\README.md")]
52+
[TestCase("C:\\Source\\OpenSource\\v13\\README.md", "\\OpenSource\\v13\\README.md")]
53+
[TestCase("C:\\Source\\OpenSource\\README.md", "\\Source\\OpenSource\\README.md")]
54+
[TestCase("C:\\Source\\README.md", "\\Source\\README.md")]
55+
[TestCase("README.md", "\\README.md")]
56+
[TestCase("", "")]
57+
public void TruncatedPathsWithFileName(string filename, string expected)
58+
{
59+
var result = filename.TruncatePath(3, true);
60+
61+
Assert.AreEqual(expected, result);
62+
}
3063
}
3164
}

0 commit comments

Comments
 (0)