Skip to content

Commit

Permalink
running unit tests - wip #1
Browse files Browse the repository at this point in the history
  • Loading branch information
razvangoga committed Feb 13, 2024
1 parent bd3e4fc commit 3ca36c8
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 34 deletions.
15 changes: 8 additions & 7 deletions test/E2ETest/ApplicationFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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");
Expand Down Expand Up @@ -205,7 +206,7 @@ public async Task ThrowIfSpecifyTargetFrameworkNotDefinedIsCsproj()
var outputContext = new OutputContext(_sink, Verbosity.Debug);
var projectFile = new FileInfo(yamlFile);

await Assert.ThrowsAsync<CommandException>(async () => await ApplicationFactory.CreateAsync(outputContext, projectFile, "net5.0"));
await Assert.ThrowsAsync<CommandException>(async () => await ApplicationFactory.CreateAsync(outputContext, projectFile, "netcoreapp1.1"));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Startup>();
});
#else
public static IWebHostBuilder CreateHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TargetFrameworkAttribute>()?.FrameworkName;
await httpContext.Response.WriteAsync(framework);
#endif
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0</TargetFrameworks>
<RootNamespace>MultiTargetFrameworks</RootNamespace>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
project: multi-targetframeworks/multi-targetframeworks.csproj
buildProperties:
- name: TargetFramework
value: netcoreapp2.1
value: net7.0
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
project: multi-targetframeworks/multi-targetframeworks.csproj
buildProperties:
- name: TargetFramework
value: netcoreapp3.1
value: net8.0
10 changes: 6 additions & 4 deletions test/Test.Infrastructure/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"));
Expand All @@ -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",
Expand All @@ -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.");
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tye2.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=targetframeworks/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 comments on commit 3ca36c8

Please sign in to comment.