Skip to content

Slnx should now be found when using the DotnetSlnPostAction #48879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: release/9.0.3xx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ public DotnetSlnPostActionProcessor(Func<string, IReadOnlyList<string>, string?,

internal static IReadOnlyList<string> FindSolutionFilesAtOrAbovePath(IPhysicalFileSystem fileSystem, string outputBasePath)
{
return FileFindHelpers.FindFilesAtOrAbovePath(fileSystem, outputBasePath, "*.sln")
?? FileFindHelpers.FindFilesAtOrAbovePath(fileSystem, outputBasePath, "*.slnx");
return
[
..FileFindHelpers.FindFilesAtOrAbovePath(fileSystem, outputBasePath, "*.sln"),
..FileFindHelpers.FindFilesAtOrAbovePath(fileSystem, outputBasePath, "*.slnx")
];
}

// The project files to add are a subset of the primary outputs, specifically the primary outputs indicated by the primaryOutputIndexes post action argument (semicolon separated)
Expand Down
77 changes: 52 additions & 25 deletions test/dotnet.Tests/dotnet-new/DotnetSlnPostActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ public DotnetSlnPostActionTests(EnvironmentSettingsHelper environmentSettingsHel
_engineEnvironmentSettings = environmentSettingsHelper.CreateEnvironment(hostIdentifier: GetType().Name, virtualize: true);
}

[Fact(DisplayName = nameof(AddProjectToSolutionPostActionFindSolutionFileAtOutputPath))]
public void AddProjectToSolutionPostActionFindSolutionFileAtOutputPath()
[Theory]
[InlineData("MySln.sln")]
[InlineData("MySln.slnx")]
public void AddProjectToSolutionPostActionFindSolutionFileAtOutputPath(string solutionFileName)
{
string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string solutionFileFullPath = Path.Combine(targetBasePath, "MySln.sln");
string targetBasePath = GetVirtualizedRootPath();
string solutionFileFullPath = Path.Combine(targetBasePath, solutionFileName);
_engineEnvironmentSettings.Host.FileSystem.WriteAllText(solutionFileFullPath, string.Empty);

IReadOnlyList<string> solutionFiles = DotnetSlnPostActionProcessor.FindSolutionFilesAtOrAbovePath(_engineEnvironmentSettings.Host.FileSystem, targetBasePath);
Expand All @@ -34,7 +36,7 @@ public void AddProjectToSolutionPostActionFindSolutionFileAtOutputPath()
[Fact(DisplayName = nameof(AddProjectToSolutionPostActionFindsOneProjectToAdd))]
public void AddProjectToSolutionPostActionFindsOneProjectToAdd()
{
string outputBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string outputBasePath = GetVirtualizedRootPath();
IPostAction postAction = new MockPostAction(default, default, default, default, default!)
{
ActionId = DotnetSlnPostActionProcessor.ActionProcessorId,
Expand Down Expand Up @@ -160,14 +162,16 @@ public void AddProjectToSolutionPostActionWithoutPrimaryOutputIndexesWithOutputB
Assert.Contains(outputFileFullPath1, foundProjectFiles.ToList());
}

[Fact(DisplayName = nameof(AddProjectToSolutionCanTargetASingleProjectWithAJsonArray))]
public void AddProjectToSolutionCanTargetASingleProjectWithAJsonArray()
[Theory]
[InlineData("MySln.sln")]
[InlineData("MySln.slnx")]
public void AddProjectToSolutionCanTargetASingleProjectWithAJsonArray(string solutionFileName)
{
var callback = new MockAddProjectToSolutionCallback();
var actionProcessor = new DotnetSlnPostActionProcessor(callback.AddProjectToSolution);

string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string slnFileFullPath = Path.Combine(targetBasePath, "MyApp.sln");
string targetBasePath = GetVirtualizedRootPath();
string slnFileFullPath = Path.Combine(targetBasePath, solutionFileName);
string projFileFullPath = Path.Combine(targetBasePath, "MyApp.csproj");

_engineEnvironmentSettings.Host.FileSystem.WriteAllText(slnFileFullPath, "");
Expand All @@ -189,14 +193,16 @@ public void AddProjectToSolutionCanTargetASingleProjectWithAJsonArray()
Assert.Equal(slnFileFullPath, callback.Solution);
}

[Fact(DisplayName = nameof(AddProjectToSolutionCanTargetASingleProjectWithTheProjectName))]
public void AddProjectToSolutionCanTargetASingleProjectWithTheProjectName()
[Theory]
[InlineData("MySln.sln")]
[InlineData("MySln.slnx")]
public void AddProjectToSolutionCanTargetASingleProjectWithTheProjectName(string solutionFileName)
{
var callback = new MockAddProjectToSolutionCallback();
var actionProcessor = new DotnetSlnPostActionProcessor(callback.AddProjectToSolution);

string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string slnFileFullPath = Path.Combine(targetBasePath, "MyApp.sln");
string targetBasePath = GetVirtualizedRootPath();
string slnFileFullPath = Path.Combine(targetBasePath, solutionFileName);
string projFileFullPath = Path.Combine(targetBasePath, "MyApp.csproj");

_engineEnvironmentSettings.Host.FileSystem.WriteAllText(slnFileFullPath, "");
Expand All @@ -218,14 +224,16 @@ public void AddProjectToSolutionCanTargetASingleProjectWithTheProjectName()
Assert.Equal(slnFileFullPath, callback.Solution);
}

[Fact(DisplayName = nameof(AddProjectToSolutionCanPlaceProjectInSolutionRoot))]
public void AddProjectToSolutionCanPlaceProjectInSolutionRoot()
[Theory]
[InlineData("MySln.sln")]
[InlineData("MySln.slnx")]
public void AddProjectToSolutionCanPlaceProjectInSolutionRoot(string solutionFileName)
{
var callback = new MockAddProjectToSolutionCallback();
var actionProcessor = new DotnetSlnPostActionProcessor(callback.AddProjectToSolution);

string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string slnFileFullPath = Path.Combine(targetBasePath, "MyApp.sln");
string targetBasePath = GetVirtualizedRootPath();
string slnFileFullPath = Path.Combine(targetBasePath, solutionFileName);
string projFileFullPath = Path.Combine(targetBasePath, "MyApp.csproj");

_engineEnvironmentSettings.Host.FileSystem.WriteAllText(slnFileFullPath, "");
Expand All @@ -250,14 +258,16 @@ public void AddProjectToSolutionCanPlaceProjectInSolutionRoot()
Assert.Null(callback.TargetFolder);
}

[Fact]
public void AddProjectToSolutionCanPlaceProjectInSolutionFolder()
[Theory]
[InlineData("MySln.sln")]
[InlineData("MySln.slnx")]
public void AddProjectToSolutionCanPlaceProjectInSolutionFolder(string solutionFileName)
{
var callback = new MockAddProjectToSolutionCallback();
var actionProcessor = new DotnetSlnPostActionProcessor(callback.AddProjectToSolution);

string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string slnFileFullPath = Path.Combine(targetBasePath, "MyApp.sln");
string targetBasePath = GetVirtualizedRootPath();
string slnFileFullPath = Path.Combine(targetBasePath, solutionFileName);
string projFileFullPath = Path.Combine(targetBasePath, "MyApp.csproj");

_engineEnvironmentSettings.Host.FileSystem.WriteAllText(slnFileFullPath, "");
Expand All @@ -282,14 +292,16 @@ public void AddProjectToSolutionCanPlaceProjectInSolutionFolder()
Assert.Equal("src", callback.TargetFolder);
}

[Fact]
public void AddProjectToSolutionFailsWhenSolutionFolderAndInRootSpecified()
[Theory]
[InlineData("MySln.sln")]
[InlineData("MySln.slnx")]
public void AddProjectToSolutionFailsWhenSolutionFolderAndInRootSpecified(string solutionFileName)
{
var callback = new MockAddProjectToSolutionCallback();
var actionProcessor = new DotnetSlnPostActionProcessor(callback.AddProjectToSolution);

string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
string slnFileFullPath = Path.Combine(targetBasePath, "MyApp.sln");
string targetBasePath = GetVirtualizedRootPath();
string slnFileFullPath = Path.Combine(targetBasePath, solutionFileName);
string projFileFullPath = Path.Combine(targetBasePath, "MyApp.csproj");

_engineEnvironmentSettings.Host.FileSystem.WriteAllText(slnFileFullPath, "");
Expand All @@ -314,6 +326,21 @@ public void AddProjectToSolutionFailsWhenSolutionFolderAndInRootSpecified()
Assert.False(result);
}

/// <summary>
/// Creates a virtualized path at the root.
/// This is used to test the behavior of finding *.slnx files after failing to find *.sln files.
///
/// If the root is not virtualized, tests will fail because the "FindFilesAtOrAbovePath" method
/// will run into a non-virtualized directory and fail due to it not existing within the "virtual cone".
/// </summary>
/// <returns>The root path virtualized</returns>
private string GetVirtualizedRootPath()
{
var root = Path.GetPathRoot(Directory.GetCurrentDirectory())!;
_engineEnvironmentSettings.Host.VirtualizeDirectory(root);
return Path.Combine(Directory.GetCurrentDirectory(), "sandbox", Guid.NewGuid().ToString()) + Path.DirectorySeparatorChar;
}

private class MockAddProjectToSolutionCallback
{
public string? Solution { get; private set; }
Expand Down
Loading