Skip to content

Commit

Permalink
Fill in IRunnerUtilityContext, and add support for v3 runner utility
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Dec 5, 2023
1 parent 2b4f69b commit 7e82f3f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/xunit.analyzers/Utility/EmptyRunnerUtilityContext.cs
Original file line number Diff line number Diff line change
@@ -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();
}
12 changes: 12 additions & 0 deletions src/xunit.analyzers/Utility/IRunnerUtilityContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
using System;

namespace Xunit.Analyzers;

public interface IRunnerUtilityContext
{
/// <summary>
/// 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").
/// </summary>
string Platform { get; }

/// <summary>
/// Gets the version number of the runner utility assembly.
/// </summary>
Version Version { get; }
}
2 changes: 1 addition & 1 deletion src/xunit.analyzers/Utility/V2RunnerUtilityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 42 additions & 0 deletions src/xunit.analyzers/Utility/V3RunnerUtilityContext.cs
Original file line number Diff line number Diff line change
@@ -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;
}

/// <inheritdoc/>
public string Platform { get; }

/// <inheritdoc/>
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);
}
}
20 changes: 20 additions & 0 deletions src/xunit.analyzers/Utility/XunitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class XunitContext
{
IAssertContext? assert;
ICoreContext? core;
IRunnerUtilityContext? runnerUtility;
static readonly Version v2AbstractionsVersion = new(2, 0, 3);

XunitContext()
Expand Down Expand Up @@ -72,6 +73,19 @@ public ICoreContext Core
public bool HasV3References =>
V3Assert is not null || V3Core is not null;

/// <summary>
/// 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.*).
/// </summary>
public IRunnerUtilityContext RunnerUtility
{
get
{
runnerUtility ??= V3RunnerUtility ?? (IRunnerUtilityContext?)V2RunnerUtility ?? EmptyRunnerUtilityContext.Instance;
return runnerUtility;
}
}

/// <summary>
/// Gets information about the reference to xunit.abstractions (v2). If the project does
/// not reference v2 Abstractions, then returns <c>null</c>.
Expand Down Expand Up @@ -114,6 +128,12 @@ public ICoreContext Core
/// </summary>
public V3CoreContext? V3Core { get; private set; }

/// <summary>
/// Gets information about the reference to xunit.v3.runner.utility.* (v3). If the project does
/// not reference v3 Runner Utility, then returns <c>null</c>.
/// </summary>
public V3RunnerUtilityContext? V3RunnerUtility { get; private set; }

/// <summary>
/// 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).
Expand Down

0 comments on commit 7e82f3f

Please sign in to comment.