diff --git a/test/dotnet.Tests/TelemetryTests/TelemetryFilterTest.cs b/test/dotnet.Tests/TelemetryTests/TelemetryFilterTest.cs index 300f1f90e0e1..8e5f306888a9 100644 --- a/test/dotnet.Tests/TelemetryTests/TelemetryFilterTest.cs +++ b/test/dotnet.Tests/TelemetryTests/TelemetryFilterTest.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands; using Microsoft.DotNet.Cli.Telemetry; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Utilities; @@ -52,7 +54,21 @@ public void TopLevelCommandNameShouldBeSentToTelemetryWithGlobalJsonState() [Fact] public void SubLevelCommandNameShouldBeSentToTelemetry() { - var parseResult = Parser.Parse(["new", "console"]); + // Use a fresh DotNetCommandDefinition to avoid test pollution from + // other tests that may have dynamically added template names (e.g. "console") + // as subcommands to the static Parser.RootCommand's "new" command. + // See InstantiateCommand.ExecuteAsync which mutates the static parser tree. + var rootCommand = new DotNetCommandDefinition(); + // Remove the built-in VersionOption that conflicts with the SDK's custom --version option. + // In production, Parser.NormalizeRootOptions handles this, but it's private. + for (int i = rootCommand.Options.Count - 1; i >= 0; i--) + { + if (rootCommand.Options[i] is VersionOption) + { + rootCommand.Options.RemoveAt(i); + } + } + var parseResult = rootCommand.Parse(["new", "console"], Parser.ParserConfiguration); TelemetryEventEntry.SendFiltered(parseResult); _fakeTelemetry .LogEntries.Should()