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

RazorProjectService cleanup #10989

Merged
merged 5 commits into from
Oct 9, 2024

Conversation

davidwengier
Copy link
Member

Noticed while working on my previous PR. There were a few methods in RazorProjectService that were only used by tests, and also resulted in some tests validating things that could never happen in the product (like passing null for a project file path in an update). This PR removes one unused method, moves one to a test accessor where it rightly should be, and updates the tests that were calling UpdateProjectAsync to instead call through IRazorProjectServiceListener.UpdatedAsync.

I don't love that these tests have to call through that interface, but I didn't change the method to be implicitly implemented because I don't think the name makes sense when written that way.

There are, or at least should be, no functionality changes in this PR.

@davidwengier davidwengier requested a review from a team as a code owner October 9, 2024 01:12
string? displayName,
CancellationToken cancellationToken)
{
var service = instance;
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ why?

Copy link
Member Author

@davidwengier davidwengier Oct 9, 2024

Choose a reason for hiding this comment

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

Short answer: it makes the compiler happy

Long answer: The test accessor is a struct, and a lambda in a struct can't access a member of the struct. Or at least that's what I took the error to mean when the compiler complained at me :)

Copy link
Contributor

Choose a reason for hiding this comment

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

ah interesting, so this is causing it to box? Will have to look more, but at first glance it was weird. Newfangled C#

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it would have to box the instance, and I guess they want that to be explicit and not hidden. I don't begin to understand this low level stuff :)

The error was CS1673 IIRC and you wanted to look up more about it.

Copy link
Member

@DustinCampbell DustinCampbell Oct 9, 2024

Choose a reason for hiding this comment

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

Yes, this is a problem because you're capturing a primary constructor parameter that is ultimately backed by a field on the struct. So, you're really capturing an instance field which means that the lambda must capture this, which requires boxing the struct. Otherwise, the closure would contain a copy of the struct.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, this definitely makes sense. In a PR it looks weird but wholistically in the language is 👍

@@ -956,9 +918,9 @@ public async Task UpdateDocument_ChangesDocumentInAllOwnerProjects()
const string RootNamespace = "TestRootNamespace";
const string DocumentFilePath = "C:/path/to/document.cshtml";

var ownerProjectKey1 = await _projectService.AddProjectAsync(
var ownerProjectKey1 = await _projectService.GetTestAccessor().AddProjectAsync(
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 should we just create a member for the test accessor?

Copy link
Member

@DustinCampbell DustinCampbell Oct 9, 2024

Choose a reason for hiding this comment

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

I'm not sure if I understood your suggestion correctly, but IMO we should actively get away from having members on production code "for testing".

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant in the RazorProjectServiceTest. It already is using a member variable for _projectService so I just assumed (without reading the full class) that allocating a new TestAccessor is just waisted typing becuase the underlying object being tested will always be _projectService

Copy link
Member Author

Choose a reason for hiding this comment

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

I kinda like how the methods being called to set up test data look distinctly different in the tests. I also don't care about saving allocations of test accessors, or anything else in tests really (which I know some people definitely don't agree with 😛)

Copy link
Contributor

Choose a reason for hiding this comment

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

allocations aren't my problem. I'm lazy and like typing fewer things. It's already here though let's stick with it :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Laziness wins (by me not changing the code 😛)

Copy link
Contributor

@alexgav alexgav left a comment

Choose a reason for hiding this comment

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

:shipit:

@davidwengier davidwengier merged commit 6f378ca into dotnet:main Oct 9, 2024
12 checks passed
@davidwengier davidwengier deleted the CleanUpRazorProjectService branch October 9, 2024 21:49
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 9, 2024
@phil-allen-msft phil-allen-msft modified the milestones: Next, 17.13 P1 Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants