Skip to content

Commit

Permalink
tidy up the path in the report.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJump committed Mar 11, 2024
1 parent 2437d6e commit 199a407
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
35 changes: 35 additions & 0 deletions uSync.BackOffice/Extensions/PathExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.IO;
using System.Linq;

namespace uSync.BackOffice.Extensions;

/// <summary>
/// extensions to manipulate folder path strings.
/// </summary>
public static class PathExtensions
{
/// <summary>
/// truncate a folder path to only show the last count paths..
/// </summary>
public static string TruncatePath(this string path, int count = 3, bool includeFile = false)
{
if (string.IsNullOrWhiteSpace(path)) return path;

var result = "";

var fullPath = includeFile ? path : Path.GetDirectoryName(path);
if (string.IsNullOrWhiteSpace(fullPath)) return fullPath ?? string.Empty;

var bits = fullPath.Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]);

foreach (var item in bits.Reverse().Take(count))
{
if (Path.IsPathRooted(item)) continue;

result = $"{Path.DirectorySeparatorChar}{item}{result}";
}

return result;
}
}
3 changes: 2 additions & 1 deletion uSync.BackOffice/SyncHandlers/SyncHandlerRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Umbraco.Extensions;

using uSync.BackOffice.Configuration;
using uSync.BackOffice.Extensions;
using uSync.BackOffice.Models;
using uSync.BackOffice.Services;
using uSync.BackOffice.SyncHandlers.Models;
Expand Down Expand Up @@ -570,7 +571,7 @@ private string GetRootFolder(string path)
if (path.Contains(folder) is true) return Path.GetFileName(folder.TrimEnd('/'));
}

return path;
return path.TruncatePath(3, false);
}


Expand Down
33 changes: 33 additions & 0 deletions uSync.Tests/Extensions/PathNameTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using NUnit.Framework;

using Umbraco.Extensions;

using uSync.BackOffice.Extensions;
using uSync.Core;

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

Assert.AreEqual(expected, value);
}

[TestCase("C:\\Source\\OpenSource\\v13\\uSync\\README.md", "\\OpenSource\\v13\\uSync")]
[TestCase("C:\\Source\\OpenSource\\v13\\README.md", "\\Source\\OpenSource\\v13")]
[TestCase("C:\\Source\\OpenSource\\README.md", "\\Source\\OpenSource")]
[TestCase("C:\\Source\\README.md", "\\Source")]
[TestCase("C:\\Source/OpenSource\\v13\\uSync\\README.md", "\\OpenSource\\v13\\uSync")]
[TestCase("C:\\Source/OpenSource\\v13\\README.md", "\\Source\\OpenSource\\v13")]
[TestCase("C:\\Source/OpenSource/README.md", "\\Source\\OpenSource")]
[TestCase("C:/Source\\README.md", "\\Source")]
[TestCase("README.md", "")]
[TestCase("", "")]
public void TruncatedPathsWithoutFileName(string filename, string expected)
{
var result = filename.TruncatePath(3, false);

Assert.AreEqual(expected, result);
}

[TestCase("C:\\Source\\OpenSource\\v13\\uSync\\README.md", "\\v13\\uSync\\README.md")]
[TestCase("C:\\Source\\OpenSource\\v13\\README.md", "\\OpenSource\\v13\\README.md")]
[TestCase("C:\\Source\\OpenSource\\README.md", "\\Source\\OpenSource\\README.md")]
[TestCase("C:\\Source\\README.md", "\\Source\\README.md")]
[TestCase("README.md", "\\README.md")]
[TestCase("", "")]
public void TruncatedPathsWithFileName(string filename, string expected)
{
var result = filename.TruncatePath(3, true);

Assert.AreEqual(expected, result);
}
}
}

0 comments on commit 199a407

Please sign in to comment.