-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Use a module initializer to install ThrowingTraceListener #47500
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Roslyn.Test.Utilities | ||
{ | ||
internal static class ModuleInitializer | ||
{ | ||
[ModuleInitializer] | ||
internal static void Initialize() | ||
{ | ||
Trace.Listeners.Clear(); | ||
Trace.Listeners.Add(new ThrowingTraceListener()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#if !NET5_0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Referencing the .NET 5 TFM issue so we know to come back and change this if the design for what |
||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public sealed class ModuleInitializerAttribute : Attribute | ||
{ | ||
} | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
using System.Composition.Hosting; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis.Editor; | ||
|
@@ -59,6 +60,14 @@ public class UseExportProviderAttribute : BeforeAfterTestAttribute | |
|
||
private MefHostServices? _hostServices; | ||
|
||
static UseExportProviderAttribute() | ||
{ | ||
// Make sure we run the module initializer for Roslyn.Test.Utilities. C# projects do this via a | ||
// build-injected module initializer, but VB projects can ensure initialization occurs by applying the | ||
// UseExportProviderAttribute to test classes that rely on it. | ||
RuntimeHelpers.RunModuleConstructor(typeof(TestBase).Module.ModuleHandle); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the case of c# this will cause the constructor to run twice correct? I don't think this impacts correctness really but wanted to make sure I understood the implications. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea. I was expecting semantics similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the impl I think it only runs once |
||
} | ||
|
||
public override void Before(MethodInfo methodUnderTest) | ||
{ | ||
MefHostServices.TestAccessor.HookServiceCreation(CreateMefHostServices); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Module initializers work on .NET Framework too so why aren't we just universally doing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this pull request, the following are covered:
<UseExportProvider>
Keeping app.config for .NET Framework prevents a test gap for some Visual Basic projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think another way of stating is that there is a gap in VB for .NET Core projects with this change. Essentially paths that don't go through
<UseExportProvider>
. This gap does not exist on desktop because of theapp.config
work. Correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Xunit doesn't offer an AssemblyInitialize extension point, and Visual Basic does not offer module initializers, so we do not have a way to guarantee that the trace listener is installed by the time the first test executes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K. Think we should doc that so it's clearer what this still gives us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will submit a follow-up PR to document the condition here.