|
| 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_FindsSlnFile() |
| 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", applicationBasePath: _tempDirectory); |
| 32 | + |
| 33 | + using var host = builder.Build(); |
| 34 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 35 | + |
| 36 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void UseSolutionRelativeContentRoot_FindsSlnxFile() |
| 41 | + { |
| 42 | + var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 43 | + File.WriteAllText(solutionFile, """ |
| 44 | + <Solution> |
| 45 | + <Configurations> |
| 46 | + <Configuration Name="Debug|Any CPU" /> |
| 47 | + <Configuration Name="Release|Any CPU" /> |
| 48 | + </Configurations> |
| 49 | + </Solution> |
| 50 | + """); |
| 51 | + |
| 52 | + var builder = new WebHostBuilder() |
| 53 | + .UseTestServer() |
| 54 | + .Configure(app => { }); |
| 55 | + |
| 56 | + builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory); |
| 57 | + |
| 58 | + using var host = builder.Build(); |
| 59 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 60 | + |
| 61 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void UseSolutionRelativeContentRoot_WithSolutionName_FindsSpecifiedFile() |
| 66 | + { |
| 67 | + var subDirectory = Path.Combine(_tempDirectory, "sub"); |
| 68 | + Directory.CreateDirectory(subDirectory); |
| 69 | + |
| 70 | + var slnFile = Path.Combine(subDirectory, "TestApp.sln"); |
| 71 | + var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 72 | + File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); |
| 73 | + File.WriteAllText(slnxFile, """ |
| 74 | + <Solution> |
| 75 | + <Configurations> |
| 76 | + <Configuration Name="Debug|Any CPU" /> |
| 77 | + </Configurations> |
| 78 | + </Solution> |
| 79 | + """); |
| 80 | + |
| 81 | + var builder = new WebHostBuilder() |
| 82 | + .UseTestServer() |
| 83 | + .Configure(app => { }); |
| 84 | + |
| 85 | + builder.UseSolutionRelativeContentRoot("src", _tempDirectory, "*.slnx"); |
| 86 | + |
| 87 | + using var host = builder.Build(); |
| 88 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 89 | + |
| 90 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_FindsInCurrentDirectoryFirst() |
| 95 | + { |
| 96 | + var expectedPath = Path.Combine(_contentDirectory, "sub"); |
| 97 | + Directory.CreateDirectory(expectedPath); |
| 98 | + |
| 99 | + var slnFile = Path.Combine(_tempDirectory, "TestApp.sln"); |
| 100 | + var slnxFile = Path.Combine(_contentDirectory, "TestApp.slnx"); |
| 101 | + File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); |
| 102 | + File.WriteAllText(slnxFile, """ |
| 103 | + <Solution> |
| 104 | + <Configurations> |
| 105 | + <Configuration Name="Debug|Any CPU" /> |
| 106 | + </Configurations> |
| 107 | + </Solution> |
| 108 | + """); |
| 109 | + |
| 110 | + var builder = new WebHostBuilder() |
| 111 | + .UseTestServer() |
| 112 | + .Configure(app => { }); |
| 113 | + |
| 114 | + builder.UseSolutionRelativeContentRoot("sub", _contentDirectory, ["*.sln", "*.slnx"]); |
| 115 | + |
| 116 | + using var host = builder.Build(); |
| 117 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 118 | + |
| 119 | + Assert.Equal(expectedPath, environment.ContentRootPath); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void UseSolutionRelativeContentRoot_WithMultipleSolutionNames_WorksWithMultipleFiles() |
| 124 | + { |
| 125 | + var slnFile = Path.Combine(_tempDirectory, "TestApp.sln"); |
| 126 | + var slnxFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 127 | + File.WriteAllText(slnFile, "Microsoft Visual Studio Solution File, Format Version 12.00"); |
| 128 | + File.WriteAllText(slnxFile, """ |
| 129 | + <Solution> |
| 130 | + <Configurations> |
| 131 | + <Configuration Name="Debug|Any CPU" /> |
| 132 | + </Configurations> |
| 133 | + </Solution> |
| 134 | + """); |
| 135 | + |
| 136 | + var builder = new WebHostBuilder() |
| 137 | + .UseTestServer() |
| 138 | + .Configure(app => { }); |
| 139 | + |
| 140 | + builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory, solutionNames: ["*.sln", "*.slnx"]); |
| 141 | + |
| 142 | + using var host = builder.Build(); |
| 143 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 144 | + |
| 145 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public void UseSolutionRelativeContentRoot_ThrowsWhenSolutionNotFound() |
| 150 | + { |
| 151 | + var builder = new WebHostBuilder() |
| 152 | + .UseTestServer() |
| 153 | + .Configure(app => { }); |
| 154 | + |
| 155 | + var exception = Assert.Throws<InvalidOperationException>(() => |
| 156 | + builder.UseSolutionRelativeContentRoot("src", applicationBasePath: _tempDirectory)); |
| 157 | + |
| 158 | + Assert.Contains("Solution root could not be located", exception.Message); |
| 159 | + Assert.Contains(_tempDirectory, exception.Message); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void UseSolutionRelativeContentRoot_WithSolutionName_SearchesParentDirectories() |
| 164 | + { |
| 165 | + var subDirectory = Path.Combine(_tempDirectory, "sub", "folder"); |
| 166 | + Directory.CreateDirectory(subDirectory); |
| 167 | + |
| 168 | + var solutionFile = Path.Combine(_tempDirectory, "TestApp.slnx"); |
| 169 | + File.WriteAllText(solutionFile, """ |
| 170 | + <Solution> |
| 171 | + <Configurations> |
| 172 | + <Configuration Name="Debug|Any CPU" /> |
| 173 | + </Configurations> |
| 174 | + </Solution> |
| 175 | + """); |
| 176 | + |
| 177 | + var builder = new WebHostBuilder() |
| 178 | + .UseTestServer() |
| 179 | + .Configure(app => { }); |
| 180 | + |
| 181 | + builder.UseSolutionRelativeContentRoot("src", subDirectory, "*.slnx"); |
| 182 | + |
| 183 | + using var host = builder.Build(); |
| 184 | + var environment = host.Services.GetRequiredService<IWebHostEnvironment>(); |
| 185 | + |
| 186 | + Assert.Equal(_contentDirectory, environment.ContentRootPath); |
| 187 | + } |
| 188 | + |
| 189 | + public void Dispose() |
| 190 | + { |
| 191 | + if (Directory.Exists(_tempDirectory)) |
| 192 | + { |
| 193 | + Directory.Delete(_tempDirectory, recursive: true); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments