|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.AspNetCore.Hosting; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.TestHost; |
| 8 | + |
| 9 | +public class UseSolutionRelativeContentRootTests : IDisposable |
| 10 | +{ |
| 11 | + private readonly string _tempDirectory; |
| 12 | + private readonly string _contentDirectory; |
| 13 | + |
| 14 | + public UseSolutionRelativeContentRootTests() |
| 15 | + { |
| 16 | + _tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")[..8]); |
| 17 | + _contentDirectory = Path.Combine(_tempDirectory, "src"); |
| 18 | + Directory.CreateDirectory(_contentDirectory); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void UseSolutionRelativeContentRoot_FindsSolutionFile() |
| 23 | + { |
| 24 | + var solutionFile = Path.Combine(_tempDirectory, "TestApp.sln"); |
| 25 | + File.WriteAllText(solutionFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); |
| 26 | + |
| 27 | + var builder = new WebHostBuilder() |
| 28 | + .UseTestServer() |
| 29 | + .Configure(app => { }); |
| 30 | + |
| 31 | + builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "TestApp.sln"); |
| 32 | + |
| 33 | + using var host = builder.Build(); |
| 34 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 35 | + |
| 36 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 37 | + Assert.True(Directory.Exists(environment.ContentRootPath)); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void UseSolutionRelativeContentRoot_FindsSlnxFile() |
| 42 | + { |
| 43 | + var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 44 | + File.WriteAllText(solutionFile, """ |
| 45 | + <Solution> |
| 46 | + <Configurations> |
| 47 | + <Configuration Name="Debug|Any CPU" /> |
| 48 | + <Configuration Name="Release|Any CPU" /> |
| 49 | + </Configurations> |
| 50 | + </Solution> |
| 51 | + """); |
| 52 | + |
| 53 | + var builder = new WebHostBuilder() |
| 54 | + .UseTestServer() |
| 55 | + .Configure(app => { }); |
| 56 | + |
| 57 | + builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "TestApp.slnx"); |
| 58 | + |
| 59 | + using var host = builder.Build(); |
| 60 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 61 | + |
| 62 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 63 | + Assert.True(Directory.Exists(environment.ContentRootPath)); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void UseSolutionRelativeContentRoot_DefaultOverload_FindsBothSolutionTypes() |
| 68 | + { |
| 69 | + var slnFile = Path.Combine(_tempDirectory, "TestApp.sln"); |
| 70 | + var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 71 | + File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); |
| 72 | + File.WriteAllText(slnxFile, """ |
| 73 | + <Solution> |
| 74 | + <Configurations> |
| 75 | + <Configuration Name="Debug|Any CPU" /> |
| 76 | + </Configurations> |
| 77 | + </Solution> |
| 78 | + """); |
| 79 | + |
| 80 | + var builder = new WebHostBuilder() |
| 81 | + .UseTestServer() |
| 82 | + .Configure(app => { }); |
| 83 | + |
| 84 | + builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "TestApp.sln"); |
| 85 | + |
| 86 | + using var host = builder.Build(); |
| 87 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 88 | + |
| 89 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 90 | + Assert.True(Directory.Exists(environment.ContentRootPath)); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_FindsFirst() |
| 95 | + { |
| 96 | + var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 97 | + File.WriteAllText(slnxFile, """ |
| 98 | + <Solution> |
| 99 | + <Configurations> |
| 100 | + <Configuration Name="Debug|Any CPU" /> |
| 101 | + </Configurations> |
| 102 | + </Solution> |
| 103 | + """); |
| 104 | + |
| 105 | + var builder = new WebHostBuilder() |
| 106 | + .UseTestServer() |
| 107 | + .Configure(app => { }); |
| 108 | + |
| 109 | + builder.UseSolutionRelativeContentRoot("src", _tempDirectory, ["TestApp.sln", "TestApp.slnx"]); |
| 110 | + |
| 111 | + using var host = builder.Build(); |
| 112 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 113 | + |
| 114 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 115 | + Assert.True(Directory.Exists(environment.ContentRootPath)); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void UseSolutionRelativeContentRoot_ThrowsWhenSolutionNotFound() |
| 120 | + { |
| 121 | + var builder = new WebHostBuilder() |
| 122 | + .UseTestServer() |
| 123 | + .Configure(app => { }); |
| 124 | + |
| 125 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 126 | + builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "NonExistent.sln")); |
| 127 | + |
| 128 | + Assert.Contains("Solution root could not be located", exception.Message); |
| 129 | + Assert.Contains(_tempDirectory, exception.Message); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void UseSolutionRelativeContentRoot_SearchesParentDirectories() |
| 134 | + { |
| 135 | + var subDirectory = Path.Combine(_tempDirectory, "sub", "folder"); |
| 136 | + Directory.CreateDirectory(subDirectory); |
| 137 | + |
| 138 | + var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 139 | + File.WriteAllText(solutionFile, """ |
| 140 | + <Solution> |
| 141 | + <Configurations> |
| 142 | + <Configuration Name="Debug|Any CPU" /> |
| 143 | + </Configurations> |
| 144 | + </Solution> |
| 145 | + """); |
| 146 | + |
| 147 | + var builder = new WebHostBuilder() |
| 148 | + .UseTestServer() |
| 149 | + .Configure(app => { }); |
| 150 | + |
| 151 | + builder.UseSolutionRelativeContentRoot("src", subDirectory, "TestApp.slnx"); |
| 152 | + |
| 153 | + using var host = builder.Build(); |
| 154 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 155 | + |
| 156 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 157 | + Assert.True(Directory.Exists(environment.ContentRootPath)); |
| 158 | + } |
| 159 | + |
| 160 | + public void Dispose() |
| 161 | + { |
| 162 | + if (Directory.Exists(_tempDirectory)) |
| 163 | + { |
| 164 | + Directory.Delete(_tempDirectory, recursive: true); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments