Skip to content
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

Support .slnf file format in dotnet test #46392

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

mariam-abdulla
Copy link
Member

@mariam-abdulla mariam-abdulla commented Jan 29, 2025

This pull request focuses on refactoring and improving the handling of solution and project files in the CLI, as well as enhancing the build process. The most important changes include the introduction of new methods for parsing solution filter files, simplifying the MSBuildHandler class, and updating constants to support solution filter files.

Refactoring and Enhancements:

  • src/Cli/dotnet/SlnFileFactory.cs: Introduced new methods GetSolutionPathFromFilteredSolutionFile and ParseSolutionFilterFile to handle solution filter files, and refactored CreateFromFilteredSolutionFile to use these methods. [1] [2] [3] [4]

Simplification of MSBuildHandler:

Support for Solution Filter Files:

  • src/Cli/dotnet/commands/dotnet-test/CliConstants.cs: Added support for .slnf files by updating the SolutionExtensions array and introducing SolutionFilterExtensionPattern.
  • Solution filter files are now supported using --solution, e.g. dotnet test --solution "TestProjects.slnf".
  • If we run dotnet test and there exists only one .slnf file in the directory, we will run projects inside this .slnf file. But if we have multiple .slnf files, we will throw an error to specify the solution file. If we have 1 solution file (.sln/.slnx) and multiple .slnf files, we run only the solution file.

Relates to #45927

@mariam-abdulla mariam-abdulla requested a review from a team as a code owner January 29, 2025 14:25
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-DotNet Test untriaged Request triage from a team member labels Jan 29, 2025
@mariam-abdulla mariam-abdulla enabled auto-merge (squash) January 29, 2025 15:53
Copy link
Member

@edvilme edvilme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excited to see this getting support! :)

src/Cli/dotnet/SlnFileFactory.cs Show resolved Hide resolved
@@ -126,5 +127,23 @@ public static SolutionModel CreateFromFilteredSolutionFile(string filteredSoluti

return filteredSolution;
}

public static string GetSolutionPathFromFilteredSolutionFile(string filteredSolutionPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it is just a few lines, and just tossing some thoughts around, but maybe we could create a helper method that parses the .slnf into a valid JsonElement and have GetSolutionPathFromFilteredSolutionFile and CreateFromFilteredSolutionFile call it?
If possible I would advocate to avoiding reading and parsing the file more than once

src/Cli/dotnet/commands/dotnet-test/MSBuildUtility.cs Outdated Show resolved Hide resolved
Comment on lines 135 to 139
using JsonDocument jsonDocument = JsonDocument.Parse(File.ReadAllText(filteredSolutionPath));
JsonElement jsonElement = jsonDocument.RootElement;
JsonElement filteredSolutionJsonElement = jsonElement.GetProperty("solution");
string originalSolutionPath = filteredSolutionJsonElement.GetProperty("path").GetString();
return originalSolutionPath;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using JsonDocument jsonDocument = JsonDocument.Parse(File.ReadAllText(filteredSolutionPath));
JsonElement jsonElement = jsonDocument.RootElement;
JsonElement filteredSolutionJsonElement = jsonElement.GetProperty("solution");
string originalSolutionPath = filteredSolutionJsonElement.GetProperty("path").GetString();
return originalSolutionPath;
JsonNode? jsonNode = JsonNode.Parse(File.ReadAllText(filteredSolutionPath));
string? originalSolutionPath = jsonNode?["solution"]?["path"]?.GetValue<string>();
return originalSolutionPath!;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using System.Text.Json.Nodes;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's advised to use JsonNode for scenarios where we need to create or modify JSON data and this is not the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JsonNode is high performance api for both reading and writing. it's much better than reading a full document just to access selective field, which is this case. plus it has succinct syntax to access props.

}
}

private static JsonElement ParseSolutionFilterFile(string solutionFilterFilePath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

Suggested change
private static JsonElement ParseSolutionFilterFile(string solutionFilterFilePath)
private static JsonElement ParseSolutionFilterFileJson(string solutionFilterFilePath)

just to make it super clear that it returns a json?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-DotNet Test untriaged Request triage from a team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants