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

Fixes unit test templates for spaces in names #3959

Merged
merged 3 commits into from
Apr 26, 2024
Merged

Conversation

timheuer
Copy link
Member

@timheuer timheuer commented Apr 25, 2024

Fix #3957 and #3974

  • Renamed placeholder project name
  • Manual validation
  • Validated dotnet new aspire-starter -t xunit.net -o asp-starter-with-redis-with-xunit0
  • Validated dotnet new with aspire-xunit,aspire-nunit,aspire-mstest using various hyphenated names
  • Follows same pattern as project templates
  • Changed to look for new symbol name in SLN file

image

Microsoft Reviewers: Open in CodeFlow

@@ -1,6 +1,6 @@
using System.Net;

namespace Aspire.Tests1;
namespace Aspire.Tests._1;
Copy link
Member

Choose a reason for hiding this comment

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

Why _1?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I was going to ask this too.

Copy link
Member

Choose a reason for hiding this comment

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

Oh this is required to instruct the template engine which naming convention to use.

Copy link
Member Author

Choose a reason for hiding this comment

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

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
Member

@radical radical Apr 25, 2024

Choose a reason for hiding this comment

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

ah

namespace Template._1; //uses `namespace` form

Isn't that link suggesting that AspireStarterApplication.1 would be the correct one, and would allow picking which "form" to use for namespace/classname etc, and AspireStarterApplication1 would break it?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I made it AspireStarterApplication.1 when we first hit this issue but it still doesn't make the auto-value forms work. But, Aspire.StarerApplication1 would work, so we should just change to that to match what ASP.NET Core and others do.

Copy link
Member Author

@timheuer timheuer Apr 25, 2024

Choose a reason for hiding this comment

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

I think I'd recommend sticking with this change only. The change to the others will affect path names as well and you'll change the csproj reference paths. Using this convention allows you to use the value forms and keep the 'identity' form for what the user put in.

Example, if we were to make the change to all. and you dotnet new foo-bar-baz then the file-paths would not be translated (foo-bar-baz.csproj) but then you'd have sln/project references as foo_bar_baz.csproj.

Honestly it feels safer to look at just this change and keep the others as-is as they are functioning within the template engine value forms (identity + namespace + class).

visualized:
image

Copy link
Member

Choose a reason for hiding this comment

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

I admit I'm a bit lost now lol.
Are you saying that if you change the form to Aspire.StarterApplication1 it breaks? Why is it that the ASP.NET Core and Blazor templates work? Also how can you run Explorer with file extensions hidden!!? 😱

Copy link
Member Author

Choose a reason for hiding this comment

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

RE: File extensions LOL -- just a sandbox so no customizations -- view hidden always FTW!

But yes, As Ankit also points out the value form scheme allows us to actually achieve what we want. I can't explain why the Blazor ones work (as they have proj ref paths as well) other than the source name uses a dash rather than dot. But the Blazor one is actually broken as well with dashes :-) (root namespace doesn't get corrected)

@radical
Copy link
Member

radical commented Apr 25, 2024

/private/tmp/test/asp-starter-with-redis-with-xunit0/asp-starter-with-redis-with-xunit0.Tests/WebTests.cs(11,131): error CS1525: Invalid expression term ')' [/private/tmp/test/asp-starter-with-redis-with-xunit0/asp-starter-with-redis-with-xunit0.Tests/asp-starter-with-redis-with-xunit0.Tests.csproj]

line 11: var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.asp-starter-with-redis-with-xunit0_AppHost>();

@radical
Copy link
Member

radical commented Apr 25, 2024

This works;

diff --git a/src/Aspire.ProjectTemplates/templates/aspire-starter/AspireStarterApplication.1.Tests/WebTests.cs b/src/Aspire.ProjectTemplates/templates/aspire-starter/AspireStarterApplication.1.Tests/WebTests.cs
index 26eb9fb66..a0a114dde 100644
--- a/src/Aspire.ProjectTemplates/templates/aspire-starter/AspireStarterApplication.1.Tests/WebTests.cs
+++ b/src/Aspire.ProjectTemplates/templates/aspire-starter/AspireStarterApplication.1.Tests/WebTests.cs
@@ -17,7 +17,7 @@ public class WebTests
     public async Task GetWebResourceRootReturnsOkStatusCode()
     {
         // Arrange
-        var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AspireStarterApplication.1_AppHost>();
+        var appHost = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AspireStarterApplication._1_AppHost>();
         await using var app = await appHost.BuildAsync();
         await app.StartAsync();

@timheuer
Copy link
Member Author

@radical I implemented the last one you found...did your local tests find anything else in the template tests?

@@ -8,7 +8,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireStarterApplication.1.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspireStarterApplication.1.ApiService", "AspireStarterApplication.1.ApiService\AspireStarterApplication.1.ApiService.csproj", "{9FEB877E-015D-4E20-AE63-06C596E242E4}"
EndProject
#if (CreateTestsProject)
#if (TestFramework != 'None')
Copy link
Member

Choose a reason for hiding this comment

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

Good catch! I assume you verified this fixes it.

Copy link
Member

Choose a reason for hiding this comment

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

@radical do we have test coverage the starter template creation? Can we add this as a case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. I assume the conditionals may be case-insensitive, but matched the literal default value in template config and verified 'None' and 'none' both work

Copy link
Member

Choose a reason for hiding this comment

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

What's the case that was caught here? Using none type as the framework generated a test project when it shouldn't have? I check for non-existence of a test project in case of none but I didn't see it failing.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the current shipping templates the option is CreateTestsProject since there was only one type: xunit. Now that we added unit test framework choice, the option changed to TestFramework and is either none (no arg provided so no tests projects), xunit.net, nunit, or mstest

@timheuer
Copy link
Member Author

/backport to release/8.0

Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/aspire/actions/runs/8853529459

Copy link
Contributor

@timheuer an error occurred while backporting to release/8.0, please check the run log for details!

Error: @timheuer is not a repo collaborator, backporting is not allowed. If you're a collaborator please make sure your dotnet team membership visibility is set to Public on https://github.com/orgs/dotnet/people?query=timheuer

@DamianEdwards
Copy link
Member

/backport to release/8.0

Copy link
Contributor

Started backporting to release/8.0: https://github.com/dotnet/aspire/actions/runs/8854015349

Copy link
Contributor

@DamianEdwards backporting to release/8.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Fixes unit test templates for spaces in names Fix #3957
error: mode change for src/Aspire.ProjectTemplates/templates/aspire-mstest/Aspire.Tests1.csproj, which is not in current HEAD
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Fixes unit test templates for spaces in names Fix #3957
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

Copy link
Contributor

@DamianEdwards an error occurred while backporting to release/8.0, please check the run log for details!

Error: git am failed, most likely due to a merge conflict.

@DamianEdwards
Copy link
Member

@timheuer looks like you'll need to manually backport

@DamianEdwards DamianEdwards merged commit bfbc49e into main Apr 26, 2024
8 checks passed
@DamianEdwards DamianEdwards deleted the dev/th/3957 branch April 26, 2024 21:13
timheuer added a commit that referenced this pull request Apr 26, 2024
* Fixes unit test templates for spaces in names
Fix #3957

* PR Feedback: missed form change in starter test

* Fixes #3974
@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generated test template does not sanitize project name for use as namespace
3 participants