Skip to content

Commit

Permalink
Use proper string comparison for directory root to capture relative p…
Browse files Browse the repository at this point in the history
…aths (#579)

* Fix drive warning

* Get full path before obtaining path root
  • Loading branch information
mruxmohan4 authored Mar 8, 2024
1 parent a3f988f commit 2dca73e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
41 changes: 37 additions & 4 deletions src/Microsoft.VisualStudio.SlnGen.UnitTests/SlnFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ public void Save_WithSolutionItemsAddedToSpecificFolder_SolutionItemsExistInSpec
}

[Fact]
public void EmitWarningForProjectsOnMultipleDrives()
public void EmitWindowsWarningForProjectsOnMultipleDrives()
{
bool isWindowsPlatform = Utility.RunningOnWindows;
SlnProject projectA = new ()
Expand All @@ -1240,15 +1240,48 @@ public void EmitWarningForProjectsOnMultipleDrives()
TestLogger logger = new ();
SlnFile slnFile = new ();
SlnProject[] projects = new[] { projectA, projectB };
string solutionFilePath = @$"X:\{Path.GetRandomFileName()}";
string solutionFilePath = isWindowsPlatform ? @$"X:\{Path.GetRandomFileName()}" : $"/mnt/{Path.GetRandomFileName()}";
StringBuilderTextWriter writer = new (new StringBuilder(), new List<string>());

slnFile.AddProjects(projects);
slnFile.Save(solutionFilePath, writer, useFolders: true, logger);

logger.Errors.Count.ShouldBe(0);
logger.Warnings.Count.ShouldBe(1);
logger.Warnings.FirstOrDefault().Message.ShouldContain("Detected folder on a different drive from the root solution path");

if (isWindowsPlatform)
{
logger.Warnings.Count.ShouldBe(1);
logger.Warnings.FirstOrDefault().Message.ShouldContain("Detected folder on a different drive from the root solution path");
}
else
{
logger.Warnings.Count.ShouldBe(0);
}
}

[Fact]
public void DoNotEmitWarningForRootPath()
{
TestLogger logger = new ();
SlnFile slnFile = new ();
StringBuilderTextWriter writer = new (new StringBuilder(), new List<string>());

SlnProject project = new SlnProject
{
Configurations = new[] { "Debug", "Release" },
FullPath = Path.Combine(TestRootPath, "ProjectA.csproj"),
Name = "ProjectA",
Platforms = new[] { "AnyCPU" },
ProjectGuid = new Guid("{2ACFA184-2D17-4F80-A132-EC462B48A065}"),
ProjectTypeGuid = new Guid("{65815BD7-8B14-4E69-8328-D5C4ED3245BE}"),
};

string solutionFilePath = Path.Combine(TestRootPath, "sample.sln");
slnFile.AddProjects([project]);
slnFile.Save(solutionFilePath, writer, useFolders: true, logger, collapseFolders: true);

logger.Errors.Count.ShouldBe(0);
logger.Warnings.Count.ShouldBe(0);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.VisualStudio.SlnGen/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static string ToRelativePath(this string path, string relativeTo)

FileInfo relativeFullPath = new FileInfo(Path.GetFullPath(relativeTo));

if (fullPath.Directory == null || relativeFullPath.Directory == null || !string.Equals(fullPath.Directory.Root.FullName, relativeFullPath.Directory.Root.FullName))
if (fullPath.Directory == null || relativeFullPath.Directory == null || !string.Equals(fullPath.Directory.Root.FullName, relativeFullPath.Directory.Root.FullName, StringComparison.OrdinalIgnoreCase))
{
return fullPath.FullName;
}
Expand Down
9 changes: 6 additions & 3 deletions src/Microsoft.VisualStudio.SlnGen/SlnFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,18 @@ internal void Save(string rootPath, TextWriter writer, bool useFolders, ISlnGenL
if (hierarchy != null)
{
bool logDriveWarning = false;
string rootPathDrive = Path.GetPathRoot(rootPath);
string rootPathDrive = Path.GetPathRoot(Path.GetFullPath(rootPath));
foreach (SlnFolder folder in hierarchy.Folders)
{
bool useSeparateDrive = false;
bool hasFullPath = !string.IsNullOrEmpty(folder.FullPath);
if (hasFullPath)
{
string folderPathDrive = Path.GetPathRoot(folder.FullPath);
if (!string.Equals(rootPathDrive, folderPathDrive, StringComparison.OrdinalIgnoreCase))
string folderPathDrive = Path.GetPathRoot(Path.GetFullPath(folder.FullPath));
// Only compare path roots when root path has root directory information
if (!string.IsNullOrEmpty(rootPathDrive) &&
rootPathDrive.Length == folderPathDrive.Length &&
!string.Equals(rootPathDrive, folderPathDrive, StringComparison.OrdinalIgnoreCase))
{
useSeparateDrive = true;
if (!logDriveWarning)
Expand Down

0 comments on commit 2dca73e

Please sign in to comment.