|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#nullable disable |
| 5 | + |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | + |
| 8 | +namespace dotnet.Tests; |
| 9 | + |
| 10 | +public class FrameworkOptionTests : SdkTest |
| 11 | +{ |
| 12 | + public FrameworkOptionTests(ITestOutputHelper log) : base(log) |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + [Theory] |
| 17 | + [InlineData("build", true)] |
| 18 | + [InlineData("clean", true)] |
| 19 | + [InlineData("publish", true)] |
| 20 | + [InlineData("test", true)] |
| 21 | + public void FrameworkOptionGeneratesWarningsWithSolutionFiles(string command, bool shouldWarn) |
| 22 | + { |
| 23 | + TestFrameworkWithSolution(command, useOption: true, shouldWarn: shouldWarn); |
| 24 | + } |
| 25 | + |
| 26 | + [Theory] |
| 27 | + [InlineData("build")] |
| 28 | + [InlineData("clean")] |
| 29 | + [InlineData("publish")] |
| 30 | + [InlineData("test")] |
| 31 | + public void TargetFrameworkPropertyDoesNotGenerateWarningsWithSolutionFiles(string command) |
| 32 | + { |
| 33 | + TestFrameworkWithSolution(command, useOption: false, shouldWarn: false); |
| 34 | + } |
| 35 | + |
| 36 | + void TestFrameworkWithSolution(string command, bool useOption, bool shouldWarn, [CallerMemberName] string callingMethod = "") |
| 37 | + { |
| 38 | + var testProject = new TestProject() |
| 39 | + { |
| 40 | + IsExe = true, |
| 41 | + TargetFrameworks = $"{ToolsetInfo.CurrentTargetFramework};net8.0" |
| 42 | + }; |
| 43 | + |
| 44 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, callingMethod, identifier: command); |
| 45 | + |
| 46 | + var slnDirectory = testAsset.TestRoot; |
| 47 | + |
| 48 | + Log.WriteLine($"Test root: {slnDirectory}"); |
| 49 | + |
| 50 | + new DotnetNewCommand(Log) |
| 51 | + .WithVirtualHive() |
| 52 | + .WithWorkingDirectory(slnDirectory) |
| 53 | + .Execute("sln") |
| 54 | + .Should().Pass(); |
| 55 | + |
| 56 | + new DotnetCommand(Log) |
| 57 | + .WithWorkingDirectory(slnDirectory) |
| 58 | + .Execute("sln", "add", testProject.Name) |
| 59 | + .Should().Pass(); |
| 60 | + |
| 61 | + Microsoft.DotNet.Cli.Utils.CommandResult commandResult; |
| 62 | + if (useOption) |
| 63 | + { |
| 64 | + commandResult = new DotnetCommand(Log) |
| 65 | + .WithWorkingDirectory(slnDirectory) |
| 66 | + .Execute(command, "--framework", "net8.0"); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + commandResult = new DotnetCommand(Log) |
| 71 | + .WithWorkingDirectory(slnDirectory) |
| 72 | + .Execute(command, "--property:TargetFramework=net8.0"); |
| 73 | + } |
| 74 | + commandResult.Should().Pass(); |
| 75 | + if (shouldWarn) |
| 76 | + { |
| 77 | + commandResult.Should().HaveStdOutContaining("NETSDK1234"); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + commandResult.Should().NotHaveStdOutContaining("NETSDK1234"); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments