|
| 1 | +using System.Reflection; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using Microsoft.Testing.Platform.CommandLine; |
| 4 | +using TUnit.Core; |
| 5 | +using TUnit.Engine.CommandLineProviders; |
| 6 | + |
| 7 | +namespace TUnit.Engine.Helpers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Helper class for determining test execution mode (source generation vs reflection). |
| 11 | +/// </summary> |
| 12 | +internal static class ExecutionModeHelper |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Determines whether to use source generation mode for test discovery and execution. |
| 16 | + /// </summary> |
| 17 | + /// <param name="commandLineOptions">Command line options from the test platform.</param> |
| 18 | + /// <returns> |
| 19 | + /// True if source generation mode should be used; false if reflection mode should be used. |
| 20 | + /// </returns> |
| 21 | + /// <remarks> |
| 22 | + /// Priority order: |
| 23 | + /// 1. AOT platform check (forces source generation) |
| 24 | + /// 2. Command line --reflection flag |
| 25 | + /// 3. Command line --tunit-execution-mode |
| 26 | + /// 4. Assembly-level [ReflectionMode] attribute |
| 27 | + /// 5. Environment variable TUNIT_EXECUTION_MODE |
| 28 | + /// 6. Default (SourceRegistrar.IsEnabled) |
| 29 | + /// </remarks> |
| 30 | + public static bool IsSourceGenerationMode(ICommandLineOptions commandLineOptions) |
| 31 | + { |
| 32 | +#if NET |
| 33 | + if (!RuntimeFeature.IsDynamicCodeSupported) |
| 34 | + { |
| 35 | + return true; // Force source generation on AOT platforms |
| 36 | + } |
| 37 | +#endif |
| 38 | + |
| 39 | + if (commandLineOptions.TryGetOptionArgumentList(ReflectionModeCommandProvider.ReflectionMode, out _)) |
| 40 | + { |
| 41 | + return false; // Reflection mode explicitly requested |
| 42 | + } |
| 43 | + |
| 44 | + // Check for command line option |
| 45 | + if (commandLineOptions.TryGetOptionArgumentList("tunit-execution-mode", out var modes) && modes.Length > 0) |
| 46 | + { |
| 47 | + var mode = modes[0].ToLowerInvariant(); |
| 48 | + if (mode == "sourcegeneration" || mode == "aot") |
| 49 | + { |
| 50 | + return true; |
| 51 | + } |
| 52 | + else if (mode == "reflection") |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Check for assembly-level ReflectionMode attribute |
| 59 | + var entryAssembly = Assembly.GetEntryAssembly(); |
| 60 | + if (entryAssembly != null) |
| 61 | + { |
| 62 | + var hasReflectionModeAttribute = entryAssembly.GetCustomAttributes(typeof(ReflectionModeAttribute), inherit: false).Length > 0; |
| 63 | + if (hasReflectionModeAttribute) |
| 64 | + { |
| 65 | + return false; // Assembly is marked for reflection mode |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Check environment variable |
| 70 | + var envMode = EnvironmentVariableCache.Get("TUNIT_EXECUTION_MODE"); |
| 71 | + if (!string.IsNullOrEmpty(envMode)) |
| 72 | + { |
| 73 | + var mode = envMode!.ToLowerInvariant(); |
| 74 | + if (mode == "sourcegeneration" || mode == "aot") |
| 75 | + { |
| 76 | + return true; |
| 77 | + } |
| 78 | + else if (mode == "reflection") |
| 79 | + { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return SourceRegistrar.IsEnabled; |
| 85 | + } |
| 86 | +} |
0 commit comments