Skip to content

Commit

Permalink
- Generate relative contentRoot paths in test manifests
Browse files Browse the repository at this point in the history
- Fixed the public API declarations
- React to changes in the WebApplicationFactory
  • Loading branch information
mkArtakMSFT committed Feb 12, 2025
1 parent 8683598 commit 9c0fb14
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected override IHost CreateHost(IHostBuilder builder)
return result;
}

protected override TestServer CreateServer(IWebHostBuilder builder)
protected override ITestServer CreateServer(IWebHostBuilder builder)
{
var result = base.CreateServer(builder);
EnsureDatabaseCreated(result.Host.Services);
Expand Down
48 changes: 47 additions & 1 deletion src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

//using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -33,11 +36,14 @@ public override bool Execute()
{
using var fileStream = File.Create(ManifestPath);
var output = new Dictionary<string, string>();
var manifestDirectory = Path.GetDirectoryName(ManifestPath);

foreach (var project in Projects)
{
var contentRoot = project.GetMetadata("ContentRoot");
var assemblyName = project.GetMetadata("Identity");
output[assemblyName] = contentRoot;
var relativeContentRoot = GetRelativePath(manifestDirectory, contentRoot);
output[assemblyName] = relativeContentRoot;
}

var serializer = new DataContractJsonSerializer(typeof(Dictionary<string, string>), new DataContractJsonSerializerSettings
Expand All @@ -49,4 +55,44 @@ public override bool Execute()

return !Log.HasLoggedErrors;
}

private static string GetRelativePath(string? relativeTo, string path)
{
if (string.IsNullOrEmpty(relativeTo))
{
return path;
}

// Ensure the paths are absolute
string absoluteRelativeTo = Path.GetFullPath(relativeTo);
string absolutePath = Path.GetFullPath(path);

// Split the paths into their components
string[] relativeToParts = absoluteRelativeTo.TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
string[] pathParts = absolutePath.TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);

// Find the common base path length
int commonLength = 0;
while (commonLength < relativeToParts.Length && commonLength < pathParts.Length &&
string.Equals(relativeToParts[commonLength], pathParts[commonLength], StringComparison.OrdinalIgnoreCase))
{
commonLength++;
}

// Calculate the number of directories to go up from the relativeTo path
int upDirectories = relativeToParts.Length - commonLength;

// Build the relative path
string relativePath = string.Join(Path.DirectorySeparatorChar.ToString(), new string[upDirectories].Select(_ => "..").ToArray());
if (commonLength < pathParts.Length)
{
if (relativePath.Length > 0)
{
relativePath += Path.DirectorySeparatorChar;
}
relativePath += string.Join(Path.DirectorySeparatorChar.ToString(), pathParts.Skip(commonLength));
}

return relativePath;
}
}
2 changes: 2 additions & 0 deletions src/Mvc/Mvc.Testing/src/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateDefaul
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateDefaultClient(System.Uri! baseAddress, params System.Net.Http.DelegatingHandler![]! handlers) -> System.Net.Http.HttpClient!
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Dispose() -> void
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Factories.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint!>!>!
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Server.get -> Microsoft.AspNetCore.TestHost.TestServer!
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.WebApplicationFactory() -> void
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.WithWebHostBuilder(System.Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder!>! configuration) -> Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint!>!
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions
Expand All @@ -40,6 +41,7 @@ virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Conf
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.ConfigureWebHost(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> void
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateHost(Microsoft.Extensions.Hosting.IHostBuilder! builder) -> Microsoft.Extensions.Hosting.IHost!
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateHostBuilder() -> Microsoft.Extensions.Hosting.IHostBuilder?
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateServer(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> Microsoft.AspNetCore.TestHost.TestServer!
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateWebHostBuilder() -> Microsoft.AspNetCore.Hosting.IWebHostBuilder?
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Dispose(bool disposing) -> void
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.DisposeAsync() -> System.Threading.Tasks.ValueTask
Expand Down
3 changes: 3 additions & 0 deletions src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#nullable enable
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Initialize() -> void
*REMOVED*Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Server.get -> Microsoft.AspNetCore.TestHost.TestServer!
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.Server.get -> Microsoft.AspNetCore.TestHost.TestServer?
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.TestServer.get -> Microsoft.AspNetCore.TestHost.ITestServer?
*REMOVED*virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateServer(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> Microsoft.AspNetCore.TestHost.TestServer!
virtual Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.CreateServer(Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder) -> Microsoft.AspNetCore.TestHost.ITestServer!

0 comments on commit 9c0fb14

Please sign in to comment.