From 825f5bbf3ab6dfc47b410bb8e2d6fc607e5de088 Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Tue, 29 Oct 2024 01:50:32 -0700 Subject: [PATCH 1/2] Update tfm of dotnet/format test project to current --- .../multiple_projects/project_a.csproj | 2 +- .../multiple_projects/project_b.csproj | 2 +- .../project_and_solution.csproj | 2 +- .../single_project/single_project.csproj | 2 +- .../MSBuild/MSBuildWorkspaceFinderTests.cs | 63 ++++++++++++------- 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_a.csproj b/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_a.csproj index c73e0d1692ab..36dd7f3c7e78 100644 --- a/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_a.csproj +++ b/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_a.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + $(CurrentTargetFramework) diff --git a/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_b.csproj b/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_b.csproj index c73e0d1692ab..36dd7f3c7e78 100644 --- a/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_b.csproj +++ b/test/TestAssets/dotnet-format/for_workspace_finder/multiple_projects/project_b.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + $(CurrentTargetFramework) diff --git a/test/TestAssets/dotnet-format/for_workspace_finder/project_and_solution/project_and_solution.csproj b/test/TestAssets/dotnet-format/for_workspace_finder/project_and_solution/project_and_solution.csproj index c73e0d1692ab..36dd7f3c7e78 100644 --- a/test/TestAssets/dotnet-format/for_workspace_finder/project_and_solution/project_and_solution.csproj +++ b/test/TestAssets/dotnet-format/for_workspace_finder/project_and_solution/project_and_solution.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + $(CurrentTargetFramework) diff --git a/test/TestAssets/dotnet-format/for_workspace_finder/single_project/single_project.csproj b/test/TestAssets/dotnet-format/for_workspace_finder/single_project/single_project.csproj index c73e0d1692ab..36dd7f3c7e78 100644 --- a/test/TestAssets/dotnet-format/for_workspace_finder/single_project/single_project.csproj +++ b/test/TestAssets/dotnet-format/for_workspace_finder/single_project/single_project.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + $(CurrentTargetFramework) diff --git a/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs b/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs index 28526b8c95e4..8d79b9bdbef8 100644 --- a/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs +++ b/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs @@ -2,63 +2,78 @@ using Microsoft.CodeAnalysis.Tools.Tests.Utilities; using Microsoft.CodeAnalysis.Tools.Workspaces; +using Microsoft.NET.TestFramework; namespace Microsoft.CodeAnalysis.Tools.Tests.MSBuild { - public class MSBuildWorkspaceFinderTests + public class MSBuildWorkspaceFinderTests : SdkTest { + + public MSBuildWorkspaceFinderTests(ITestOutputHelper log) : base(log) + { + } private string ProjectsPath => TestProjectsPathHelper.GetProjectsDirectory(); [Fact] public void ThrowsException_CannotFindMSBuildProjectFile() { - var workspacePath = "for_workspace_finder/no_project_or_solution/"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/no_project_or_solution", testAssetSubdirectory: "dotnet-format") + .WithSource(); var exceptionMessageStart = string.Format( Resources.Could_not_find_a_MSBuild_project_or_solution_file_in_0_Specify_which_to_use_with_the_workspace_argument, - Path.Combine(ProjectsPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar); - var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, workspacePath)); + testInstance.Path).Replace('/', Path.DirectorySeparatorChar); + var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path)); Assert.StartsWith(exceptionMessageStart, exception.Message); } [Fact] public void ThrowsException_MultipleMSBuildProjectFiles() { - var workspacePath = "for_workspace_finder/multiple_projects/"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/multiple_projects", testAssetSubdirectory: "dotnet-format") + .WithSource(); var exceptionMessageStart = string.Format( Resources.Multiple_MSBuild_project_files_found_in_0_Specify_which_to_use_with_the_workspace_argument, - Path.Combine(ProjectsPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar); - var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, workspacePath)); + testInstance.Path).Replace('/', Path.DirectorySeparatorChar); + var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path)); Assert.Equal(exceptionMessageStart, exception.Message); } [Fact] public void ThrowsException_MultipleMSBuildSolutionFiles() { - var workspacePath = "for_workspace_finder/multiple_solutions/"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/multiple_solutions", testAssetSubdirectory: "dotnet-format") + .WithSource(); var exceptionMessageStart = string.Format( Resources.Multiple_MSBuild_solution_files_found_in_0_Specify_which_to_use_with_the_workspace_argument, - Path.Combine(ProjectsPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar); - var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, workspacePath)); + testInstance.Path).Replace('/', Path.DirectorySeparatorChar); + var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path)); Assert.Equal(exceptionMessageStart, exception.Message); } [Fact] public void ThrowsException_SolutionAndProjectAmbiguity() { - var workspacePath = "for_workspace_finder/project_and_solution/"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/project_and_solution", testAssetSubdirectory: "dotnet-format") + .WithSource(); var exceptionMessageStart = string.Format( Resources.Both_a_MSBuild_project_file_and_solution_file_found_in_0_Specify_which_to_use_with_the_workspace_argument, - Path.Combine(ProjectsPath, workspacePath)).Replace('/', Path.DirectorySeparatorChar); - var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, workspacePath)); + testInstance.Path).Replace('/', Path.DirectorySeparatorChar); + var exception = Assert.Throws(() => MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path)); Assert.Equal(exceptionMessageStart, exception.Message); } [Fact] public void FindsSolutionByFolder() { - const string Path = "for_workspace_finder/single_solution/"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/single_solution", testAssetSubdirectory: "dotnet-format") + .WithSource(); - var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, Path); + var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path); var solutionFileName = System.IO.Path.GetFileName(workspacePath); Assert.Equal("single_solution.sln", solutionFileName); @@ -68,9 +83,11 @@ public void FindsSolutionByFolder() [Fact] public void FindsSolutionByFilePath() { - const string Path = "for_workspace_finder/multiple_solutions/solution_b.sln"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/multiple_solutions", testAssetSubdirectory: "dotnet-format") + .WithSource(); - var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, Path); + var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path, "solution_b.sln"); var solutionFileName = System.IO.Path.GetFileName(workspacePath); Assert.Equal("solution_b.sln", solutionFileName); @@ -80,9 +97,11 @@ public void FindsSolutionByFilePath() [Fact] public void FindsProjectByFolder() { - const string Path = "for_workspace_finder/single_project/"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/single_project", testAssetSubdirectory: "dotnet-format") + .WithSource(); - var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, Path); + var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path); var solutionFileName = System.IO.Path.GetFileName(workspacePath); Assert.Equal("single_project.csproj", solutionFileName); @@ -92,9 +111,11 @@ public void FindsProjectByFolder() [Fact] public void FindsProjectByFilePath() { - const string Path = "for_workspace_finder/multiple_projects/project_b.csproj"; + var testInstance = _testAssetsManager + .CopyTestAsset(testProjectName: "for_workspace_finder/multiple_projects", testAssetSubdirectory: "dotnet-format") + .WithSource(); - var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(ProjectsPath, Path); + var (isSolution, workspacePath) = MSBuildWorkspaceFinder.FindWorkspace(testInstance.Path, "project_b.csproj"); var solutionFileName = System.IO.Path.GetFileName(workspacePath); Assert.Equal("project_b.csproj", solutionFileName); From 566a0cd5ac4e137326997648d46df5444c0fbc4a Mon Sep 17 00:00:00 2001 From: Jason Zhai Date: Tue, 5 Nov 2024 18:42:43 -0800 Subject: [PATCH 2/2] Add empty line after constructor based on review feedback --- .../tests/MSBuild/MSBuildWorkspaceFinderTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs b/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs index 8d79b9bdbef8..e7fdce2e6efc 100644 --- a/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs +++ b/test/dotnet-format.Tests/tests/MSBuild/MSBuildWorkspaceFinderTests.cs @@ -12,6 +12,7 @@ public class MSBuildWorkspaceFinderTests : SdkTest public MSBuildWorkspaceFinderTests(ITestOutputHelper log) : base(log) { } + private string ProjectsPath => TestProjectsPathHelper.GetProjectsDirectory(); [Fact]