Skip to content

Commit

Permalink
Get full path before obtaining path root
Browse files Browse the repository at this point in the history
  • Loading branch information
mruxmohan4 committed Mar 8, 2024
1 parent eb10dd6 commit 949ea01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
21 changes: 15 additions & 6 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,19 +1240,27 @@ 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 DoNotEmitWarningForEmptyRootPathDrive()
public void DoNotEmitWarningForRootPath()
{
TestLogger logger = new ();
SlnFile slnFile = new ();
Expand All @@ -1268,8 +1276,9 @@ public void DoNotEmitWarningForEmptyRootPathDrive()
ProjectTypeGuid = new Guid("{65815BD7-8B14-4E69-8328-D5C4ED3245BE}"),
};

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

logger.Errors.Count.ShouldBe(0);
logger.Warnings.Count.ShouldBe(0);
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.IsNullOrEmpty(rootPathDrive) && !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 949ea01

Please sign in to comment.