Skip to content

Commit 2fd5d4f

Browse files
authored
Handle multiple trailing directory separators (#1475)
* Handle multiple trailing directory separators * Don't trim the root separator
1 parent ae34c34 commit 2fd5d4f

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/Microsoft.ComponentDetection.Detectors/dotnet/DotNetComponentDetector.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,22 @@ public DotNetComponentDetector(
5858

5959
public override IEnumerable<string> Categories => ["DotNet"];
6060

61+
private static string TrimAllEndingDirectorySeparators(string path)
62+
{
63+
string last;
64+
65+
do
66+
{
67+
last = path;
68+
path = Path.TrimEndingDirectorySeparator(last);
69+
}
70+
while (!ReferenceEquals(last, path));
71+
72+
return path;
73+
}
74+
6175
[return: NotNullIfNotNull(nameof(path))]
62-
private string? NormalizeDirectory(string? path) => string.IsNullOrEmpty(path) ? path : Path.TrimEndingDirectorySeparator(this.pathUtilityService.NormalizePath(path));
76+
private string? NormalizeDirectory(string? path) => string.IsNullOrEmpty(path) ? path : TrimAllEndingDirectorySeparators(this.pathUtilityService.NormalizePath(path));
6377

6478
/// <summary>
6579
/// Given a path under sourceDirectory, and the same path in another filesystem,

test/Microsoft.ComponentDetection.Detectors.Tests/DotNetComponentDetectorTests.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,19 @@ public async Task TestDotNetDetectorNoGlobalJsonSourceRoot()
654654
discoveredComponents.Where(component => component.Component.Id == "0.0.0 net8.0 unknown - DotNet").Should().ContainSingle();
655655
}
656656

657+
#pragma warning disable SA1201 // Elements should appear in the correct order
658+
private static IEnumerable<object[]> AdditionalPathSegments { get; } =
659+
#pragma warning restore SA1201 // Elements should appear in the correct order
660+
[
661+
[string.Empty],
662+
[$"{Path.DirectorySeparatorChar}{Path.DirectorySeparatorChar}"],
663+
[$"{Path.AltDirectorySeparatorChar}{Path.DirectorySeparatorChar}"],
664+
[$"{Path.AltDirectorySeparatorChar}{Path.AltDirectorySeparatorChar}"],
665+
];
666+
657667
[TestMethod]
658-
public async Task TestDotNetDetectorRebasePaths()
668+
[DynamicData(nameof(AdditionalPathSegments))]
669+
public async Task TestDotNetDetectorRebasePaths(string additionalPathSegment)
659670
{
660671
// DetectorTestUtility runs under Path.GetTempPath()
661672
var scanRoot = Path.TrimEndingDirectorySeparator(Path.GetTempPath());
@@ -676,7 +687,7 @@ public async Task TestDotNetDetectorRebasePaths()
676687
this.AddFile(libraryProjectPath, null);
677688

678689
var libraryOutputPath = Path.Combine(Path.GetDirectoryName(libraryProjectPath), "obj");
679-
var libraryBuildOutputPath = Path.Combine(Path.GetDirectoryName(libraryBuildProjectPath), "obj");
690+
var libraryBuildOutputPath = Path.Combine(Path.GetDirectoryName(libraryBuildProjectPath), "obj") + additionalPathSegment;
680691
var libraryAssetsPath = Path.Combine(libraryOutputPath, "project.assets.json");
681692

682693
// use "build" paths to simulate an Assets file that has a different root. Here the build assets have RootDir, but the scanned filesystem has scanRoot.

0 commit comments

Comments
 (0)