dotnet / dotnet/templating

Unable to test default names for templates using VerificationEngine

Open
#7,394 0 comments 2 reactions 0 assignees View on GitHub
triage:transfer-to-sdk
Dominant language
No language data
Stars
1.7k
Forks
399
Avg merge
3d 5h
Merged PRs (30d)
39

Description

### Product

dotnet CLI (dotnet new)

### Describe The Bug

I have a template (let's call it 'foo') that prefers the default name 'bar' when no `--name` is supplied, eg a `template.json` that contains:

```json
"preferDefaultName": true,
"defaultName": "bar"
```

when I run `dotnet new foo` I should get a template instantiation using the name "bar", however, because of https://github.com/dotnet/templating/blob/6fbfd58c03ea6060d2d81f8e2e96fd2464078104/tools/Microsoft.TemplateEngine.Authoring.TemplateVerifier/VerificationEngine.cs#L326C1-L332C14 if no name is provided via `TemplateSpecificArgs`, then an arg of `--name foo` is added to the dotnet new invocation.

This behavior breaks the test and is different from the behavior that users will see.

Note that I can work around the behavior by adding:

```csharp
TemplateVerifierOptions options = new (templateName: "foo")
{
TemplateSpecificArgs = new[] { "--name", string.Empty }, // BUG: Reset the name parameter
};
```

### To Reproduce

Steps:
1. Create a template with a layout like this:

```
|- content
|- .template.config
| |- template.json
|- bar.txt
```

2. Create a `template.json` that looks like this

```json
{
"$schema": "http://json.schemastore.org/template",
"author": "me",
"classifications": [
"MSBuild"
],
"identity": "sample.identity",
"name": "Sample project",
"shortName": "foo",
"tags": {
"type": "item"
},
"sourceName": "bar",
"preferDefaultName": true,
"defaultName": "bar"
}
```
3. Create a unit test that looks like this (I was using xunit)

```csharp
[Fact]
public async Task SampleTest()
{
TemplateVerifierOptions options = new (templateName: "foo")
{
};

VerificationEngine engine = new (_loggerFactory);
await engine.Execute(options);
}
```

4. Expected output is of the form `Snapshots\SampleTest.bar._.received\bar.txt`. However actual output is of the form `Snapshots\SampleTest.foo._.received\foo.txt`

### dotnet Info

output

.NET SDK:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.8d38d0cc

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22635
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed:
Workload version: 8.0.100-manifests.8d38d0cc
There are no installed workloads to display.

Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31a71

.NET SDKs installed:
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.417 [C:\Program Files\dotnet\sdk]
7.0.311 [C:\Program Files\dotnet\sdk]
8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
C:\Projects\MSBuildSdks\global.json

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

### Visual Studio Version

_No response_

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.