From 3ca36c8ff34be106d54a0898bbf67821f8d043ce Mon Sep 17 00:00:00 2001 From: Razvan Goga Date: Tue, 13 Feb 2024 19:52:25 +0100 Subject: [PATCH] running unit tests - wip #1 --- test/E2ETest/ApplicationFactoryTests.cs | 15 ++++++++------- .../multi-targetframeworks/Program.cs | 11 ----------- .../multi-targetframeworks/Startup.cs | 5 ----- .../multi-targetframeworks.csproj | 6 +----- ...ith-netcoreapp21.yaml => tye-with-net7.0.yaml} | 2 +- ...ith-netcoreapp31.yaml => tye-with-net8.0.yaml} | 2 +- test/Test.Infrastructure/TestHelpers.cs | 10 ++++++---- tye2.sln.DotSettings | 2 ++ 8 files changed, 19 insertions(+), 34 deletions(-) rename test/E2ETest/testassets/projects/multi-targetframeworks/{tye-with-netcoreapp21.yaml => tye-with-net7.0.yaml} (91%) rename test/E2ETest/testassets/projects/multi-targetframeworks/{tye-with-netcoreapp31.yaml => tye-with-net8.0.yaml} (91%) create mode 100644 tye2.sln.DotSettings diff --git a/test/E2ETest/ApplicationFactoryTests.cs b/test/E2ETest/ApplicationFactoryTests.cs index 3646d25..db08685 100644 --- a/test/E2ETest/ApplicationFactoryTests.cs +++ b/test/E2ETest/ApplicationFactoryTests.cs @@ -135,33 +135,34 @@ public async Task TargetFrameworkFromCliArgs() // Debug targets can be null if not specified, so make sure calling host.Start does not throw. var outputContext = new OutputContext(_sink, Verbosity.Debug); var projectFile = new FileInfo(yamlFile); - var applicationBuilder = await ApplicationFactory.CreateAsync(outputContext, projectFile, "netcoreapp3.1"); + var framework = "net7.0"; + var applicationBuilder = await ApplicationFactory.CreateAsync(outputContext, projectFile, framework); Assert.Single(applicationBuilder.Services); var service = applicationBuilder.Services.Single(s => s.Name == "multi-targetframeworks"); var containsTargetFramework = ((DotnetProjectServiceBuilder)service).BuildProperties.TryGetValue("TargetFramework", out var targetFramework); Assert.True(containsTargetFramework); - Assert.Equal("netcoreapp3.1", targetFramework); + Assert.Equal(framework, targetFramework); } [Fact] public async Task TargetFrameworkFromCliArgsDoesNotOverwriteYaml() { using var projectDirectory = TestHelpers.CopyTestProjectDirectory(Path.Combine("multi-targetframeworks")); - var yamlFile = Path.Combine(projectDirectory.DirectoryPath, "tye-with-netcoreapp21.yaml"); + var yamlFile = Path.Combine(projectDirectory.DirectoryPath, "tye-with-net7.0.yaml"); // Debug targets can be null if not specified, so make sure calling host.Start does not throw. var outputContext = new OutputContext(_sink, Verbosity.Debug); var projectFile = new FileInfo(yamlFile); - var applicationBuilder = await ApplicationFactory.CreateAsync(outputContext, projectFile, "netcoreapp3.1"); + var applicationBuilder = await ApplicationFactory.CreateAsync(outputContext, projectFile, "net8.0"); Assert.Single(applicationBuilder.Services); var service = applicationBuilder.Services.Single(s => s.Name == "multi-targetframeworks"); var containsTargetFramework = ((DotnetProjectServiceBuilder)service).BuildProperties.TryGetValue("TargetFramework", out var targetFramework); Assert.True(containsTargetFramework); - Assert.Equal("netcoreapp2.1", targetFramework); + Assert.Equal("net7.0", targetFramework); } [Fact] @@ -173,7 +174,7 @@ public async Task TargetFrameworkFromCliArgsDoesNotOverrideSingleTFM() // Debug targets can be null if not specified, so make sure calling host.Start does not throw. var outputContext = new OutputContext(_sink, Verbosity.Debug); var projectFile = new FileInfo(yamlFile); - var applicationBuilder = await ApplicationFactory.CreateAsync(outputContext, projectFile, "net5.0"); + var applicationBuilder = await ApplicationFactory.CreateAsync(outputContext, projectFile, "net7.0"); Assert.Single(applicationBuilder.Services); var service = applicationBuilder.Services.Single(s => s.Name == "test-project"); @@ -205,7 +206,7 @@ public async Task ThrowIfSpecifyTargetFrameworkNotDefinedIsCsproj() var outputContext = new OutputContext(_sink, Verbosity.Debug); var projectFile = new FileInfo(yamlFile); - await Assert.ThrowsAsync(async () => await ApplicationFactory.CreateAsync(outputContext, projectFile, "net5.0")); + await Assert.ThrowsAsync(async () => await ApplicationFactory.CreateAsync(outputContext, projectFile, "netcoreapp1.1")); } [Fact] diff --git a/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Program.cs b/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Program.cs index 2798478..7c3c02b 100644 --- a/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Program.cs +++ b/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Program.cs @@ -5,11 +5,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; -#if NETCOREAPP3_1 -#else -using Microsoft.AspNetCore; -#endif - namespace MultiTargetFrameworks { public class Program @@ -19,17 +14,11 @@ public static void Main(string[] args) CreateHostBuilder(args).Build().Run(); } -#if NETCOREAPP3_1 public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(web => { web.UseStartup(); }); -#else - public static IWebHostBuilder CreateHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); -#endif } } diff --git a/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Startup.cs b/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Startup.cs index 60c8428..33c92b1 100644 --- a/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Startup.cs +++ b/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/Startup.cs @@ -28,12 +28,7 @@ public void Configure(IApplicationBuilder app) { app.Use(async (httpContext, next) => { -#if NETCOREAPP3_1 await httpContext.Response.WriteAsync(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription); -#else - var framework = Assembly.GetEntryAssembly()?.GetCustomAttribute()?.FrameworkName; - await httpContext.Response.WriteAsync(framework); -#endif }); } } diff --git a/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/multi-targetframeworks.csproj b/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/multi-targetframeworks.csproj index 55ff0ba..dd3154c 100644 --- a/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/multi-targetframeworks.csproj +++ b/test/E2ETest/testassets/projects/multi-targetframeworks/multi-targetframeworks/multi-targetframeworks.csproj @@ -1,12 +1,8 @@ - netcoreapp3.1;netcoreapp2.1 + net8.0;net7.0 MultiTargetFrameworks - - - - diff --git a/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-netcoreapp21.yaml b/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-net7.0.yaml similarity index 91% rename from test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-netcoreapp21.yaml rename to test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-net7.0.yaml index 246c41a..d24c449 100644 --- a/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-netcoreapp21.yaml +++ b/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-net7.0.yaml @@ -6,4 +6,4 @@ services: project: multi-targetframeworks/multi-targetframeworks.csproj buildProperties: - name: TargetFramework - value: netcoreapp2.1 + value: net7.0 diff --git a/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-netcoreapp31.yaml b/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-net8.0.yaml similarity index 91% rename from test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-netcoreapp31.yaml rename to test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-net8.0.yaml index 2aedef8..e323ec8 100644 --- a/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-netcoreapp31.yaml +++ b/test/E2ETest/testassets/projects/multi-targetframeworks/tye-with-net8.0.yaml @@ -6,4 +6,4 @@ services: project: multi-targetframeworks/multi-targetframeworks.csproj buildProperties: - name: TargetFramework - value: netcoreapp3.1 + value: net8.0 diff --git a/test/Test.Infrastructure/TestHelpers.cs b/test/Test.Infrastructure/TestHelpers.cs index fc5eabc..626fcb6 100644 --- a/test/Test.Infrastructure/TestHelpers.cs +++ b/test/Test.Infrastructure/TestHelpers.cs @@ -19,6 +19,8 @@ namespace Test.Infrastructure { public static class TestHelpers { + private const string solutionName = "tye2"; + private static readonly TimeSpan WaitForServicesTimeout = Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromMinutes(1); // https://github.com/dotnet/aspnetcore/blob/5a0526dfd991419d5bce0d8ea525b50df2e37b04/src/Testing/src/TestPathUtilities.cs @@ -47,7 +49,7 @@ public static string GetSolutionRootDirectory(string solution) public static DirectoryInfo GetTestAssetsDirectory() { return new DirectoryInfo(Path.Combine( - TestHelpers.GetSolutionRootDirectory("tye"), + TestHelpers.GetSolutionRootDirectory(solutionName), "test", "E2ETest", "testassets")); @@ -56,7 +58,7 @@ public static DirectoryInfo GetTestAssetsDirectory() public static DirectoryInfo GetTestProjectDirectory(string projectName) { var directory = new DirectoryInfo(Path.Combine( - TestHelpers.GetSolutionRootDirectory("tye"), + TestHelpers.GetSolutionRootDirectory(solutionName), "test", "E2ETest", "testassets", @@ -69,7 +71,7 @@ public static DirectoryInfo GetTestProjectDirectory(string projectName) public static DirectoryInfo GetSampleProjectDirectory(string projectName) { var directory = new DirectoryInfo(Path.Combine( - TestHelpers.GetSolutionRootDirectory("tye"), + TestHelpers.GetSolutionRootDirectory(solutionName), "samples", projectName)); Assert.True(directory.Exists, $"Project {projectName} not found."); @@ -83,7 +85,7 @@ public static TempDirectory CopyTestProjectDirectory(string projectName) // We need to hijack any P2P references to Tye libraries. // Test projects must use $(TyeLibrariesPath) to find their references. - var libraryPath = Path.Combine(TestHelpers.GetSolutionRootDirectory("tye"), "src"); + var libraryPath = Path.Combine(TestHelpers.GetSolutionRootDirectory(solutionName), "src"); if (!libraryPath.EndsWith(Path.DirectorySeparatorChar)) { libraryPath += Path.DirectorySeparatorChar; diff --git a/tye2.sln.DotSettings b/tye2.sln.DotSettings new file mode 100644 index 0000000..cb3b729 --- /dev/null +++ b/tye2.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file