From 7e82f3f9b2a502fcbe59ef3af177ecbfe302cea5 Mon Sep 17 00:00:00 2001 From: Brad Wilson Date: Mon, 4 Dec 2023 19:35:29 -0800 Subject: [PATCH] Fill in IRunnerUtilityContext, and add support for v3 runner utility --- .../Utility/EmptyRunnerUtilityContext.cs | 15 +++++++ .../Utility/IRunnerUtilityContext.cs | 12 ++++++ .../Utility/V2RunnerUtilityContext.cs | 2 +- .../Utility/V3RunnerUtilityContext.cs | 42 +++++++++++++++++++ src/xunit.analyzers/Utility/XunitContext.cs | 20 +++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/xunit.analyzers/Utility/EmptyRunnerUtilityContext.cs create mode 100644 src/xunit.analyzers/Utility/V3RunnerUtilityContext.cs diff --git a/src/xunit.analyzers/Utility/EmptyRunnerUtilityContext.cs b/src/xunit.analyzers/Utility/EmptyRunnerUtilityContext.cs new file mode 100644 index 00000000..f090d9fc --- /dev/null +++ b/src/xunit.analyzers/Utility/EmptyRunnerUtilityContext.cs @@ -0,0 +1,15 @@ +using System; + +namespace Xunit.Analyzers; + +public class EmptyRunnerUtilityContext : IRunnerUtilityContext +{ + EmptyRunnerUtilityContext() + { } + + public static EmptyRunnerUtilityContext Instance { get; } = new(); + + public string Platform => "N/A"; + + public Version Version { get; } = new(); +} diff --git a/src/xunit.analyzers/Utility/IRunnerUtilityContext.cs b/src/xunit.analyzers/Utility/IRunnerUtilityContext.cs index 0e14e74f..3a3f20ad 100644 --- a/src/xunit.analyzers/Utility/IRunnerUtilityContext.cs +++ b/src/xunit.analyzers/Utility/IRunnerUtilityContext.cs @@ -1,5 +1,17 @@ +using System; + namespace Xunit.Analyzers; public interface IRunnerUtilityContext { + /// + /// Gets a description of the target platform for the runner utility (i.e., "net452"). This is + /// typically extracted from the assembly name (i.e., "xunit.runner.utility.net452"). + /// + string Platform { get; } + + /// + /// Gets the version number of the runner utility assembly. + /// + Version Version { get; } } diff --git a/src/xunit.analyzers/Utility/V2RunnerUtilityContext.cs b/src/xunit.analyzers/Utility/V2RunnerUtilityContext.cs index 59f8204a..65464d7d 100644 --- a/src/xunit.analyzers/Utility/V2RunnerUtilityContext.cs +++ b/src/xunit.analyzers/Utility/V2RunnerUtilityContext.cs @@ -25,7 +25,7 @@ public class V2RunnerUtilityContext : IRunnerUtilityContext public string Platform { get; } - public Version Version { get; set; } + public Version Version { get; } public static V2RunnerUtilityContext? Get( Compilation compilation, diff --git a/src/xunit.analyzers/Utility/V3RunnerUtilityContext.cs b/src/xunit.analyzers/Utility/V3RunnerUtilityContext.cs new file mode 100644 index 00000000..152b22c1 --- /dev/null +++ b/src/xunit.analyzers/Utility/V3RunnerUtilityContext.cs @@ -0,0 +1,42 @@ +using System; +using System.Linq; +using Microsoft.CodeAnalysis; + +namespace Xunit.Analyzers; + +public class V3RunnerUtilityContext : IRunnerUtilityContext +{ + const string assemblyPrefix = "xunit.v3.runner.utility."; + + V3RunnerUtilityContext( + string platform, + Version version) + { + Platform = platform; + Version = version; + } + + /// + public string Platform { get; } + + /// + public Version Version { get; } + + public static V3RunnerUtilityContext? Get( + Compilation compilation, + Version? versionOverride = null) + { + var assembly = + compilation + .ReferencedAssemblyNames + .FirstOrDefault(a => a.Name.StartsWith(assemblyPrefix, StringComparison.OrdinalIgnoreCase)); + + if (assembly is null) + return null; + + var version = versionOverride ?? assembly.Version; + var platform = assembly.Name.Substring(assemblyPrefix.Length); + + return version is null ? null : new(platform, version); + } +} diff --git a/src/xunit.analyzers/Utility/XunitContext.cs b/src/xunit.analyzers/Utility/XunitContext.cs index 53eb1475..485aad64 100644 --- a/src/xunit.analyzers/Utility/XunitContext.cs +++ b/src/xunit.analyzers/Utility/XunitContext.cs @@ -10,6 +10,7 @@ public class XunitContext { IAssertContext? assert; ICoreContext? core; + IRunnerUtilityContext? runnerUtility; static readonly Version v2AbstractionsVersion = new(2, 0, 3); XunitContext() @@ -72,6 +73,19 @@ public ICoreContext Core public bool HasV3References => V3Assert is not null || V3Core is not null; + /// + /// Gets a combined view of features available to either v2 runners (linked against xunit.runner.utility.*) + /// or v3 runners (linked against xunit.v3.runner.utility.*). + /// + public IRunnerUtilityContext RunnerUtility + { + get + { + runnerUtility ??= V3RunnerUtility ?? (IRunnerUtilityContext?)V2RunnerUtility ?? EmptyRunnerUtilityContext.Instance; + return runnerUtility; + } + } + /// /// Gets information about the reference to xunit.abstractions (v2). If the project does /// not reference v2 Abstractions, then returns null. @@ -114,6 +128,12 @@ public ICoreContext Core /// public V3CoreContext? V3Core { get; private set; } + /// + /// Gets information about the reference to xunit.v3.runner.utility.* (v3). If the project does + /// not reference v3 Runner Utility, then returns null. + /// + public V3RunnerUtilityContext? V3RunnerUtility { get; private set; } + /// /// Used to create a context object for test purposes, which only contains a reference to /// xunit.abstraction (which is always set to version 2.0.3, since it did not float version).