From 600b6fe8be0cc11102a7279f3a85bc145f251349 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Thu, 13 Feb 2025 10:40:58 -0800 Subject: [PATCH 1/2] Add test case for adding projects outside solution dir descendants --- .../Dir/App.sln | 5 +++++ .../Dir/App.slnx | 1 + .../Lib/Lib.csproj | 7 +++++++ .../Lib/Library.cs | 15 +++++++++++++++ test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+) create mode 100644 test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.sln create mode 100644 test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.slnx create mode 100644 test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Lib.csproj create mode 100644 test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Library.cs diff --git a/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.sln b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.sln new file mode 100644 index 000000000000..8eca2536691f --- /dev/null +++ b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.sln @@ -0,0 +1,5 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26006.2 +MinimumVisualStudioVersion = 10.0.40219.1 diff --git a/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.slnx b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.slnx new file mode 100644 index 000000000000..4e2253ddceed --- /dev/null +++ b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Dir/App.slnx @@ -0,0 +1 @@ + diff --git a/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Lib.csproj b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Lib.csproj new file mode 100644 index 000000000000..c350d4032840 --- /dev/null +++ b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Lib.csproj @@ -0,0 +1,7 @@ + + + + netstandard1.4 + + + diff --git a/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Library.cs b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Library.cs new file mode 100644 index 000000000000..71a4d48322c7 --- /dev/null +++ b/test/TestAssets/TestProjects/TestAppWithSlnAndCsprojInParentDir/Lib/Library.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace Lib +{ + public class Library + { + public static string GetMessage() + { + return "Message from Lib"; + } + } +} diff --git a/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs b/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs index 6009f9c04b97..51bfa81242e5 100644 --- a/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs +++ b/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs @@ -1096,6 +1096,24 @@ public void WhenSolutionFolderIsPassedWithDirectorySeparatorFolderStructureIsCor .Should().BeVisuallyEquivalentTo(expectedSlnContents); } + [Theory] + [InlineData("sln", ".sln")] + [InlineData("sln", ".slnx")] + [InlineData("solution", ".sln")] + [InlineData("solution", ".slnx")] + public void WhenAddingProjectOutsideDirectoryItShouldNotAddSolutionFolders(string solutionCommand, string solutionExtension) + { + var projectDirectory = _testAssetsManager + .CopyTestAsset("TestAppWithSlnAndCsprojInParentDir", identifier: $"GivenDotnetSlnAdd-{solutionCommand}{solutionExtension}") + .WithSource() + .Path; + var projectToAdd = Path.Combine("..", "Lib", "Lib.csproj"); + var cmd = new DotnetCommand(Log) + .WithWorkingDirectory(Path.Join(projectDirectory, "Dir")) + .Execute(solutionCommand, $"App{solutionExtension}", "add", projectToAdd); + cmd.Should().Pass(); // TODO: Check actual contents + } + private string GetExpectedSlnContents( string slnPath, string slnTemplateName, From 3a7163ee42dabd7da417fbae2de3bf90ea6eacf2 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Fri, 14 Feb 2025 12:57:01 -0800 Subject: [PATCH 2/2] Check for solution folders and projects in sln(x) --- test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs b/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs index 51bfa81242e5..e22f2f979cda 100644 --- a/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs +++ b/test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs @@ -1101,7 +1101,7 @@ public void WhenSolutionFolderIsPassedWithDirectorySeparatorFolderStructureIsCor [InlineData("sln", ".slnx")] [InlineData("solution", ".sln")] [InlineData("solution", ".slnx")] - public void WhenAddingProjectOutsideDirectoryItShouldNotAddSolutionFolders(string solutionCommand, string solutionExtension) + public async Task WhenAddingProjectOutsideDirectoryItShouldNotAddSolutionFolders(string solutionCommand, string solutionExtension) { var projectDirectory = _testAssetsManager .CopyTestAsset("TestAppWithSlnAndCsprojInParentDir", identifier: $"GivenDotnetSlnAdd-{solutionCommand}{solutionExtension}") @@ -1111,7 +1111,12 @@ public void WhenAddingProjectOutsideDirectoryItShouldNotAddSolutionFolders(strin var cmd = new DotnetCommand(Log) .WithWorkingDirectory(Path.Join(projectDirectory, "Dir")) .Execute(solutionCommand, $"App{solutionExtension}", "add", projectToAdd); - cmd.Should().Pass(); // TODO: Check actual contents + cmd.Should().Pass(); + // Should have no solution folders + ISolutionSerializer serializer = SolutionSerializers.GetSerializerByMoniker(Path.Join(projectDirectory, "Dir", $"App{solutionExtension}")); + SolutionModel solution = await serializer.OpenAsync(Path.Join(projectDirectory, "Dir", $"App{solutionExtension}"), CancellationToken.None); + solution.SolutionProjects.Count.Should().Be(1); + solution.SolutionFolders.Count.Should().Be(0); } private string GetExpectedSlnContents(