-
Notifications
You must be signed in to change notification settings - Fork 10.3k
WebApplicationFactory doesn't set solution relative content root if you switch to slnx solution #61304
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
Comments
This could be fixed by adding a wildcard to the |
Actually, using |
Is there a workaround for this? |
Doing something like this: builder.UseSolutionRelativeContentRoot(Path.Combine("src", "MyProjectName"), "*.slnx"); |
@martincostello I tried this, but unfortunately, I couldn't get it to work. The I ended up patching the Regardless, since This is the code I'm using now: public static class MvcTestingAppManifestHelper
{
private const string ManifestFileName = "MvcTestingAppManifest.json";
private static readonly JsonSerializerOptions JsonSerializerOptions = new() { WriteIndented = true };
public static void AddAssemblyToManifest(Assembly assembly)
{
if (!File.Exists(ManifestFileName))
{
return;
}
// The manifest file is a dictionary of Assembly.FullName to ContentRoot.
var manifest = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(ManifestFileName))!;
// Internally, the tilde is used to translate the content root to the AppContext.BaseDirectory.
if (manifest.TryAdd(assembly.FullName!, "~"))
{
File.WriteAllText(ManifestFileName, JsonSerializer.Serialize(manifest, JsonSerializerOptions));
}
}
} In each of the test projects, I then added a module initializer like this: public static class MvcTestingAppManifestInitializer
{
[ModuleInitializer]
public static void Initialize()
{
MvcTestingAppManifestHelper.AddAssemblyToManifest(Assembly.GetExecutingAssembly());
}
} |
Hmm - I'm not sure why it didn't work for you. I adopted
I don't think this is technically 100% true yet, as you need to use Visual Studio 2022 17.14 Preview to use |
Do you have any tests where the |
@martincostello I looked through your tests, and it seems like you are testing an actual MVC application with an entrypoint located in a different assembly, as @kimsey0 noted. You are right that |
We have both cases.
|
Are there any plans to fix it in dotnet 10? I see a PR was submitted but seems to be stale... |
I don't think my pull request is merge-ready (see the comment in the pull request about inadvertently matching solution filter files), but I'm happy to revise it if there's agreement on an acceptable fix. |
I can see the challenge. What about using regex? |
The solution file pattern is passed into Directory.EnumerateFiles which doesn't support regular expressions. But should we move the conversation about the specific implementation approach taken in my pull request into the pull request discussion? That way, we can leave this issue thread for larger questions like if the default is allowed to change or not. 🙂 |
Is there an existing issue for this?
Describe the bug
WebApplicationFactory
tries setting the content root based on a number of sources (a builder setting,MvcTestingAppManifest.json
, and the assembly metadata) before falling back to setting a solution-relative content root. This looks specifically for a.sln
file, which means any test that relies on it breaks when migrating to the new SLNX solution format.Expected Behavior
If ASP.NET Core supports falling back to searching for a solution file and setting the content root relative to it, it should also be able to locate slnx solution files.
Steps To Reproduce
The simplest way to trigger this is with a
WebApplicationFactory
that uses an entry point in the test assembly, instead of in a separate web API project. Create a solution with a solution file, a project file, and a test:SlnxReproduction.slnx
Tests/Tests.csproj
Tests/SlnxTest.cs
Then run the test with
dotnet test
.Exceptions (if any)
.NET Version
9.0.202
Anything else?
ASP.NET Core 9.0.3
The text was updated successfully, but these errors were encountered: