Skip to content

Fix dotnet new nunit-test -n <name> creating an unwanted <name>/ folder - #54602

Merged
Evangelink merged 4 commits into
dotnet:mainfrom
Evangelink:dev/amauryleve/fix-49760-nunit-test-unwanted-folder
Jun 7, 2026
Merged

Evangelink merged 4 commits into
dotnet:mainfrom
Evangelink:dev/amauryleve/fix-49760-nunit-test-unwanted-folder

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #49760

Problem

cd ./Foo                   # an existing project directory
dotnet new nunit-test -n Bar
Test-Path ./Bar/Bar.cs     # True — but it should be ./Bar.cs

Root cause

The three NUnit item templates (C#, F#, VB) use preferNameDirectory: true, which is the directive used by project templates (to create a folder named after the project). For item templates this is wrong: it nests the generated file inside a folder named after the item.

Every other item template in Microsoft.DotNet.Common.ItemTemplates (Class, Interface, Struct, Enum, Record, Module, Structure, and the MSTest test class templates) correctly uses preferDefaultName: true instead.

Fix

  • Replace preferNameDirectory: true with preferDefaultName: true in the three NUnit item templates.
  • Align defaultName with sourceName (UnitTest1) so the no--n default matches the existing source file name, mirroring the MSTest item template pattern.
  • Add a regression test (ItemTemplate_WithNameAndNoOutput_DoesNotCreateUnwantedFolder) that creates a test project, then runs the item template with -n MyItem and the working directory set to the project directory (no -o), and asserts that:
    • MyItem.<ext> exists in the project directory
    • MyItem/ does not exist

The NUnit item templates (C#, F#, VB) used 'preferNameDirectory: true',
which causes a folder to be created when -n is specified — this is the
correct behavior for project templates but wrong for item templates.

Replace with 'preferDefaultName: true' to match every other item
template in Microsoft.DotNet.Common.ItemTemplates (Class, Interface,
Struct, Enum, Record, Module, Structure, MSTest test classes). Also
align defaultName with sourceName ('UnitTest1') for consistency with
MSTest item templates.

Adds a regression test that creates a project then runs the item
template with -n (and no -o), asserting the file is created in the
working directory and no nested folder is created.

Fixes dotnet#49760

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 4, 2026 19:59
@Evangelink
Evangelink requested a review from a team as a code owner June 4, 2026 19:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a dotnet new item-template behavior regression where dotnet new nunit-test -n <name> run from inside an existing project directory incorrectly creates a nested <name>/ folder. It corrects the NUnit item template configuration to behave like other item templates in Microsoft.DotNet.Common.ItemTemplates and adds an integration test to prevent regressions.

Changes:

  • Update the NUnit C#/F#/VB item templates to use preferDefaultName: true (instead of preferNameDirectory: true) to avoid creating <name>/ directories for item templates.
  • Align defaultName with sourceName (UnitTest1) so default generation matches the shipped file name when -n isn’t provided.
  • Add a regression integration test ensuring -n without -o creates <name>.<ext> in the working directory and does not create a <name>/ folder.
Show a summary per file
File Description
test/dotnet-new.IntegrationTests/DotnetNewTestTemplatesTests.cs Adds a regression test covering item-template -n without -o from within a project directory.
template_feed/Microsoft.DotNet.Common.ItemTemplates/content/NUnit-CSharp-Item/.template.config/template.json Switches NUnit C# item template to preferDefaultName and sets defaultName to UnitTest1.
template_feed/Microsoft.DotNet.Common.ItemTemplates/content/NUnit-FSharp-Item/.template.config/template.json Switches NUnit F# item template to preferDefaultName and sets defaultName to UnitTest1.
template_feed/Microsoft.DotNet.Common.ItemTemplates/content/NUnit-VisualBasic-Item/.template.config/template.json Switches NUnit VB item template to preferDefaultName and sets defaultName to UnitTest1.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 0

@Evangelink
Evangelink enabled auto-merge June 5, 2026 07:22
@Evangelink
Evangelink merged commit 7a2d894 into dotnet:main Jun 7, 2026
24 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/fix-49760-nunit-test-unwanted-folder branch June 7, 2026 10:54
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 10, 2026
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.

dotnet new nunit-test -n <name> creates unwanted folder

3 participants