|
| 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 | +namespace Microsoft.EntityFrameworkCore.Tools; |
| 5 | + |
| 6 | +public sealed class ProjectTest(ITestOutputHelper output) |
| 7 | +{ |
| 8 | + private const string TargetFramework = "net10.0"; |
| 9 | + |
| 10 | + [Fact] |
| 11 | + public void Csproj_metadata_can_be_extracted() |
| 12 | + { |
| 13 | + using var directory = new TempDirectory(); |
| 14 | + var csprojFile = Path.Combine(directory.Path, "MyApp.csproj"); |
| 15 | + File.WriteAllText(csprojFile, $""" |
| 16 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 17 | + <PropertyGroup> |
| 18 | + <TargetFramework>{TargetFramework}</TargetFramework> |
| 19 | + </PropertyGroup> |
| 20 | + </Project> |
| 21 | + """); |
| 22 | + |
| 23 | + Exe.Run("dotnet", ["restore", csprojFile], handleOutput: _ => { }); |
| 24 | + |
| 25 | + var project = Project.FromFile(csprojFile); |
| 26 | + |
| 27 | + Assert.Equal("C#", project.Language); |
| 28 | + Assert.Equal("MyApp", project.AssemblyName); |
| 29 | + Assert.Equal(TargetFramework, project.TargetFramework); |
| 30 | + Assert.NotNull(project.OutputPath); |
| 31 | + Assert.NotNull(project.ProjectDir); |
| 32 | + Assert.Equal("MyApp.dll", project.TargetFileName); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void File_based_app_can_be_built() |
| 37 | + { |
| 38 | + WithVerboseOutput(() => |
| 39 | + { |
| 40 | + using var directory = new TempDirectory(); |
| 41 | + var csFile = Path.Combine(directory.Path, "MyApp.cs"); |
| 42 | + File.WriteAllText(csFile, $""" |
| 43 | + #:property TargetFramework={TargetFramework} |
| 44 | + Console.WriteLine("Hello"); |
| 45 | + """); |
| 46 | + |
| 47 | + Exe.Run("dotnet", ["restore", csFile], handleOutput: Reporter.WriteVerbose); |
| 48 | + |
| 49 | + var project = Project.FromFile(csFile); |
| 50 | + |
| 51 | + Assert.Equal("C#", project.Language); |
| 52 | + Assert.Equal("MyApp", project.AssemblyName); |
| 53 | + Assert.Equal(TargetFramework, project.TargetFramework); |
| 54 | + Assert.NotNull(project.OutputPath); |
| 55 | + Assert.NotNull(project.ProjectDir); |
| 56 | + Assert.NotNull(project.TargetFileName); |
| 57 | + |
| 58 | + project.Build(additionalArgs: null); |
| 59 | + |
| 60 | + var targetDir = Path.GetFullPath(Path.Combine(project.ProjectDir!, project.OutputPath!)); |
| 61 | + var targetPath = Path.Combine(targetDir, project.TargetFileName!); |
| 62 | + Assert.True(File.Exists(targetPath), $"Expected build output at {targetPath}"); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private void WithVerboseOutput(Action action) |
| 67 | + { |
| 68 | + var previousIsVerbose = Reporter.IsVerbose; |
| 69 | + var previousPrefixOutput = Reporter.PrefixOutput; |
| 70 | + Reporter.IsVerbose = true; |
| 71 | + Reporter.PrefixOutput = true; |
| 72 | + Reporter.SetStdOut(new TestOutputWriter(output)); |
| 73 | + try |
| 74 | + { |
| 75 | + action(); |
| 76 | + } |
| 77 | + finally |
| 78 | + { |
| 79 | + Reporter.IsVerbose = previousIsVerbose; |
| 80 | + Reporter.PrefixOutput = previousPrefixOutput; |
| 81 | + Reporter.SetStdOut(Console.Out); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private sealed class TestOutputWriter(ITestOutputHelper output) : StringWriter |
| 86 | + { |
| 87 | + public override void WriteLine(string? value) |
| 88 | + { |
| 89 | + if (value != null) |
| 90 | + { |
| 91 | + output.WriteLine(value); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments