-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Search for slnx files when setting solution-relative content root #61305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfcc236
Update UseSolutionRelativeContentRoot to support sln and slnx files b…
kimsey0 6bfe28f
Remove unnecessary suppressions
halter73 b7f3586
Move PublicAPI.Shipped.txt changes to PublicAPI.Unshipped.txt
halter73 ed40bda
Add RS0027 suppression
halter73 6d790f1
Use attribute for suppression instead like before
halter73 8e16754
Suppress WebHostBuilder obsoletion warnings in UseSolutionRelativeCon…
halter73 a9f95ad
Suppress WebHost obsoletion warnings in UseSolutionRelativeContentRoo…
halter73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
#nullable enable | ||
*REMOVED*static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! applicationBasePath, string! solutionName = "*.sln") -> Microsoft.AspNetCore.Hosting.IWebHostBuilder! | ||
*REMOVED*static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! solutionName = "*.sln") -> Microsoft.AspNetCore.Hosting.IWebHostBuilder! | ||
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder! | ||
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! applicationBasePath, string! solutionName) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder! | ||
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! applicationBasePath, System.ReadOnlySpan<string!> solutionNames = default(System.ReadOnlySpan<string!>)) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder! | ||
static Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(this Microsoft.AspNetCore.Hosting.IWebHostBuilder! builder, string! solutionRelativePath, string! solutionName) -> Microsoft.AspNetCore.Hosting.IWebHostBuilder! | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
src/Hosting/TestHost/test/UseSolutionRelativeContentRootTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.TestHost; | ||
|
||
#pragma warning disable ASPDEPR004 // WebHostBuilder is obsolete | ||
#pragma warning disable ASPDEPR008 // WebHost is obsolete | ||
public class UseSolutionRelativeContentRootTests : IDisposable | ||
{ | ||
private readonly string _tempDirectory; | ||
private readonly string _contentDirectory; | ||
|
||
public UseSolutionRelativeContentRootTests() | ||
{ | ||
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")[..8]); | ||
_contentDirectory = Path.Combine(_tempDirectory, "src"); | ||
Directory.CreateDirectory(_contentDirectory); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_FindsSlnFile() | ||
{ | ||
var solutionFile = Path.Combine(_tempDirectory, "TestApp.sln"); | ||
File.WriteAllText(solutionFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory); | ||
|
||
using var host = builder.Build(); | ||
var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
Assert.Equal(_contentDirectory, environment.ContentRootPath); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_FindsSlnxFile() | ||
{ | ||
var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx"); | ||
File.WriteAllText(solutionFile, """ | ||
<Solution> | ||
<Configurations> | ||
<Configuration Name="Debug|Any CPU" /> | ||
<Configuration Name="Release|Any CPU" /> | ||
</Configurations> | ||
</Solution> | ||
"""); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory); | ||
|
||
using var host = builder.Build(); | ||
var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
Assert.Equal(_contentDirectory, environment.ContentRootPath); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_WithSolutionName_FindsSpecifiedFile() | ||
{ | ||
var subDirectory = Path.Combine(_tempDirectory, "sub"); | ||
Directory.CreateDirectory(subDirectory); | ||
|
||
var slnFile = Path.Combine(subDirectory, "TestApp.sln"); | ||
var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx"); | ||
File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); | ||
File.WriteAllText(slnxFile, """ | ||
<Solution> | ||
<Configurations> | ||
<Configuration Name="Debug|Any CPU" /> | ||
</Configurations> | ||
</Solution> | ||
"""); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "*.slnx"); | ||
|
||
using var host = builder.Build(); | ||
var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
Assert.Equal(_contentDirectory, environment.ContentRootPath); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_FindsInCurrentDirectoryFirst() | ||
{ | ||
var expectedPath = Path.Combine(_contentDirectory, "sub"); | ||
Directory.CreateDirectory(expectedPath); | ||
|
||
var slnFile = Path.Combine(_tempDirectory, "TestApp.sln"); | ||
var slnxFile = Path.Combine(_contentDirectory, "TestApp.slnx"); | ||
File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); | ||
File.WriteAllText(slnxFile, """ | ||
<Solution> | ||
<Configurations> | ||
<Configuration Name="Debug|Any CPU" /> | ||
</Configurations> | ||
</Solution> | ||
"""); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
builder.UseSolutionRelativeContentRoot("sub", _contentDirectory, ["*.sln", "*.slnx"]); | ||
|
||
using var host = builder.Build(); | ||
var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
Assert.Equal(expectedPath, environment.ContentRootPath); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_WorksWithMultipleFiles() | ||
{ | ||
var slnFile = Path.Combine(_tempDirectory, "TestApp.sln"); | ||
var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx"); | ||
File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); | ||
File.WriteAllText(slnxFile, """ | ||
<Solution> | ||
<Configurations> | ||
<Configuration Name="Debug|Any CPU" /> | ||
</Configurations> | ||
</Solution> | ||
"""); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory, solutionNames: ["*.sln", "*.slnx"]); | ||
|
||
using var host = builder.Build(); | ||
var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
Assert.Equal(_contentDirectory, environment.ContentRootPath); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_ThrowsWhenSolutionNotFound() | ||
{ | ||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
var exception = Assert.Throws<InvalidOperationException>(() => | ||
builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory)); | ||
|
||
Assert.Contains("Solution root could not be located", exception.Message); | ||
Assert.Contains(_tempDirectory, exception.Message); | ||
} | ||
|
||
[Fact] | ||
public void UseSolutionRelativeContentRoot_WithSolutionName_SearchesParentDirectories() | ||
{ | ||
var subDirectory = Path.Combine(_tempDirectory, "sub", "folder"); | ||
Directory.CreateDirectory(subDirectory); | ||
|
||
var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx"); | ||
File.WriteAllText(solutionFile, """ | ||
<Solution> | ||
<Configurations> | ||
<Configuration Name="Debug|Any CPU" /> | ||
</Configurations> | ||
</Solution> | ||
"""); | ||
|
||
var builder = new WebHostBuilder() | ||
.UseTestServer() | ||
.Configure(app => { }); | ||
|
||
builder.UseSolutionRelativeContentRoot("src", subDirectory, "*.slnx"); | ||
|
||
using var host = builder.Build(); | ||
var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); | ||
|
||
Assert.Equal(_contentDirectory, environment.ContentRootPath); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (Directory.Exists(_tempDirectory)) | ||
{ | ||
Directory.Delete(_tempDirectory, recursive: true); | ||
} | ||
} | ||
} | ||
#pragma warning restore ASPDEPR008 // WebHost is obsolete | ||
#pragma warning disable ASPDEPR004 // WebHostBuilder is obsolete |
74 changes: 74 additions & 0 deletions
74
src/Mvc/test/Mvc.FunctionalTests/WebApplicationFactorySlnxTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using BasicWebSite; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests; | ||
|
||
public class WebApplicationFactorySlnxTests : IClassFixture<WebApplicationFactory<BasicWebSite.Startup>>, IDisposable | ||
{ | ||
private readonly string _tempDirectory; | ||
private readonly string _contentDirectory; | ||
|
||
public WebApplicationFactorySlnxTests(WebApplicationFactory<BasicWebSite.Startup> factory) | ||
{ | ||
Factory = factory; | ||
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")[..8]); | ||
_contentDirectory = Path.Combine(_tempDirectory, "BasicWebSite"); | ||
|
||
Directory.CreateDirectory(_tempDirectory); | ||
Directory.CreateDirectory(_contentDirectory); | ||
|
||
// Create a minimal wwwroot directory to satisfy content root expectations | ||
var wwwrootDir = Path.Combine(_contentDirectory, "wwwroot"); | ||
Directory.CreateDirectory(wwwrootDir); | ||
} | ||
|
||
public WebApplicationFactory<BasicWebSite.Startup> Factory { get; } | ||
|
||
[Fact] | ||
public async Task WebApplicationFactory_UsesSlnxForSolutionRelativeContentRoot() | ||
{ | ||
// Create .slnx file in temp directory | ||
var slnxFile = Path.Combine(_tempDirectory, "TestSolution.slnx"); | ||
File.WriteAllText(slnxFile, """ | ||
<Solution> | ||
<Configurations> | ||
<Configuration Name="Debug|Any CPU" /> | ||
<Configuration Name="Release|Any CPU" /> | ||
</Configurations> | ||
<Folder Name="/BasicWebSite/"> | ||
<Project Path="BasicWebSite/BasicWebSite.csproj" /> | ||
</Folder> | ||
</Solution> | ||
"""); | ||
|
||
var factory = Factory.WithWebHostBuilder(builder => | ||
{ | ||
builder.UseSolutionRelativeContentRoot("BasicWebSite", _tempDirectory, "TestSolution.slnx"); | ||
}); | ||
|
||
using var client = factory.CreateClient(); | ||
|
||
// Verify that the content root was set correctly by accessing the environment | ||
var environment = factory.Services.GetRequiredService<IWebHostEnvironment>(); | ||
Assert.Equal(_contentDirectory, environment.ContentRootPath); | ||
Assert.True(Directory.Exists(environment.ContentRootPath)); | ||
|
||
// Verify the factory is functional with the .slnx-resolved content root | ||
var response = await client.GetAsync("/"); | ||
Assert.True(response.IsSuccessStatusCode); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (Directory.Exists(_tempDirectory)) | ||
{ | ||
Directory.Delete(_tempDirectory, recursive: true); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.