From f21ff25ddc4bb9a326427710b5f3bc418526d00b Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 20 Nov 2020 14:31:37 -0800 Subject: [PATCH 01/12] Add initial support for reading content paths from file --- .../Mvc.Testing/src/WebApplicationFactory.cs | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index d9d9072cb9f7..c5dbb1616cb0 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Net.Http; using System.Reflection; +using System.Text.Json; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.TestHost; @@ -93,7 +94,7 @@ public virtual IServiceProvider Services /// /// Gets the of factories created from this factory - /// by further customizing the when calling + /// by further customizing the when calling /// . /// public IReadOnlyList> Factories => _derivedFactories.AsReadOnly(); @@ -164,13 +165,34 @@ private void EnsureServer() _server = CreateServer(builder); } - private void SetContentRoot(IWebHostBuilder builder) - { + private void SetContentRoot(IWebHostBuilder builder) { if (SetContentRootFromSetting(builder)) { return; } + // Where do we store the file that we read the JSON config from + var fromFile = File.Exists("AppManifest.json"); + var contentRoot = fromFile ? GetContentRootFromFile("AppManifest.json") : GetContentRootFromAssembly(); + + if (contentRoot != null) + { + builder.UseContentRoot(contentRoot); + } + else + { + builder.UseSolutionRelativeContentRoot(typeof(TEntryPoint).Assembly.GetName().Name); + } + } + + private string GetContentRootFromFile(string file) { + var data = JsonSerializer.Deserialize>(file); + var key = typeof(TEntryPoint).Assembly.GetName().Name; + return data[key]; + } + + private string GetContentRootFromAssembly() + { var metadataAttributes = GetContentRootMetadataAttributes( typeof(TEntryPoint).Assembly.FullName, typeof(TEntryPoint).Assembly.GetName().Name); @@ -194,14 +216,7 @@ private void SetContentRoot(IWebHostBuilder builder) } } - if (contentRoot != null) - { - builder.UseContentRoot(contentRoot); - } - else - { - builder.UseSolutionRelativeContentRoot(typeof(TEntryPoint).Assembly.GetName().Name); - } + return contentRoot; } private static bool SetContentRootFromSetting(IWebHostBuilder builder) From 00061221418acb24e8301640c93fa160f4c5dfe1 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Fri, 20 Nov 2020 15:20:18 -0800 Subject: [PATCH 02/12] Add task for generating app manifest file --- .../src/GenerateMvcTestManifestTask.cs | 44 +++++++++++++++++++ ...rosoft.AspNetCore.Mvc.Testing.Tasks.csproj | 14 ++++++ .../Microsoft.AspNetCore.Mvc.Testing.csproj | 10 +++++ .../Microsoft.AspNetCore.Mvc.Testing.targets | 2 +- 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs create mode 100644 src/Mvc/Mvc.Testing.Tasks/src/Microsoft.AspNetCore.Mvc.Testing.Tasks.csproj diff --git a/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs b/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs new file mode 100644 index 000000000000..53b55d424615 --- /dev/null +++ b/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs @@ -0,0 +1,44 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Runtime.Serialization.Json; +using System.Text; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Microsoft.AspNetCore.Mvc.Testing.Tasks +{ + public class GenerateMvcTestManifest : Task + { + [Required] + public string ManifestPath { get; set; } + + [Required] + public ITaskItem[] Projects { get; set; } + + public override bool Execute() + { + using var fileStream = File.Create(ManifestPath); + var output = new Dictionary(); + + foreach (var project in Projects) { + var path = project.GetMetadata("Path"); + var assemblyName = project.GetMetadata("Name"); + output[assemblyName] = path; + } + + var serializer = new DataContractJsonSerializer(typeof(Dictionary), new DataContractJsonSerializerSettings + { + UseSimpleDictionaryFormat = true + }); + using var writer = JsonReaderWriterFactory.CreateJsonWriter(fileStream, Encoding.UTF8, ownsStream: false, indent: true); + serializer.WriteObject(writer, output); + + return !Log.HasLoggedErrors; + } + } +} diff --git a/src/Mvc/Mvc.Testing.Tasks/src/Microsoft.AspNetCore.Mvc.Testing.Tasks.csproj b/src/Mvc/Mvc.Testing.Tasks/src/Microsoft.AspNetCore.Mvc.Testing.Tasks.csproj new file mode 100644 index 000000000000..31cd2b274868 --- /dev/null +++ b/src/Mvc/Mvc.Testing.Tasks/src/Microsoft.AspNetCore.Mvc.Testing.Tasks.csproj @@ -0,0 +1,14 @@ + + + Build tasks for functional tests. + netstandard2.0 + + true + false + + + + + + + diff --git a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.csproj b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.csproj index b39ab0349b5e..030b903554db 100644 --- a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.csproj +++ b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.csproj @@ -16,7 +16,17 @@ + + + + + diff --git a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets index 8e9f7093e19c..5344edaf38ad 100644 --- a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets +++ b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets @@ -56,4 +56,4 @@ - \ No newline at end of file + From 70d33804eaa4d4d7c14bed9498fadd71cd57aec8 Mon Sep 17 00:00:00 2001 From: Safia Abdalla Date: Thu, 10 Dec 2020 10:57:10 -0800 Subject: [PATCH 03/12] Completed support for generating AppManifest.json --- Directory.Build.props | 1 + .../src/GenerateMvcTestManifestTask.cs | 8 ++--- .../Microsoft.AspNetCore.Mvc.Testing.targets | 32 +++++++++---------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 2cbdaff55666..a8d5b34d80bc 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -202,6 +202,7 @@ false $(MSBuildThisFileDirectory)src\Mvc\Mvc.Testing\src\Microsoft.AspNetCore.Mvc.Testing.targets + $(ArtifactsBinDir)\Microsoft.AspNetCore.Mvc.Testing.Tasks\$(Configuration)\netstandard2.0\Microsoft.AspNetCore.Mvc.Testing.Tasks.dll true diff --git a/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs b/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs index 53b55d424615..a875ba67140d 100644 --- a/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs +++ b/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing.Tasks { - public class GenerateMvcTestManifest : Task + public class GenerateMvcTestManifestTask : Task { [Required] public string ManifestPath { get; set; } @@ -26,9 +26,9 @@ public override bool Execute() var output = new Dictionary(); foreach (var project in Projects) { - var path = project.GetMetadata("Path"); - var assemblyName = project.GetMetadata("Name"); - output[assemblyName] = path; + var contentRoot = project.GetMetadata("ContentRoot"); + var assemblyName = project.GetMetadata("Identity"); + output[assemblyName] = contentRoot; } var serializer = new DataContractJsonSerializer(typeof(Dictionary), new DataContractJsonSerializerSettings diff --git a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets index 5344edaf38ad..a38ac293ad05 100644 --- a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets +++ b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets @@ -1,5 +1,10 @@  + + $(MSBuildThisFileDirectory)../tasks/Microsoft.AspNetCore.Mvc.Testing.dll + + + $(MSBuildThisFileDirectory)src\Mvc\Mvc.Testing\src\Microsoft.AspNetCore.Mvc.Testing.targets - $(ArtifactsBinDir)\Microsoft.AspNetCore.Mvc.Testing.Tasks\$(Configuration)\netstandard2.0\Microsoft.AspNetCore.Mvc.Testing.Tasks.dll + <_MvcTestingTasksAssembly>$(ArtifactsBinDir)\Microsoft.AspNetCore.Mvc.Testing.Tasks\$(Configuration)\netstandard2.0\Microsoft.AspNetCore.Mvc.Testing.Tasks.dll true diff --git a/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs b/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs index a875ba67140d..080aa42ea302 100644 --- a/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs +++ b/src/Mvc/Mvc.Testing.Tasks/src/GenerateMvcTestManifestTask.cs @@ -12,20 +12,34 @@ namespace Microsoft.AspNetCore.Mvc.Testing.Tasks { + /// + /// Generate a JSON file mapping assemblies to content root paths. + /// public class GenerateMvcTestManifestTask : Task { + /// + /// The path to output the manifest file to. + /// [Required] public string ManifestPath { get; set; } + /// + /// A list of content root paths and assembly names to generate the + /// manifest from. + /// [Required] public ITaskItem[] Projects { get; set; } + /// + /// + /// public override bool Execute() { using var fileStream = File.Create(ManifestPath); var output = new Dictionary(); - foreach (var project in Projects) { + foreach (var project in Projects) + { var contentRoot = project.GetMetadata("ContentRoot"); var assemblyName = project.GetMetadata("Identity"); output[assemblyName] = contentRoot; diff --git a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets index 5598c77cfa96..febc2c4b0375 100644 --- a/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets +++ b/src/Mvc/Mvc.Testing/src/Microsoft.AspNetCore.Mvc.Testing.targets @@ -1,9 +1,9 @@  - $(MSBuildThisFileDirectory)../tasks/Microsoft.AspNetCore.Mvc.Testing.dll + <_MvcTestingTasksAssembly Condition="$(_MvcTestingTasksAssembly) == ''">$(MSBuildThisFileDirectory)../tasks/Microsoft.AspNetCore.Mvc.Testing.dll - +