Skip to content

Commit

Permalink
Emit diagnostic message when unable to load config
Browse files Browse the repository at this point in the history
* Adds proxy for ConfigReader to enable mock injection
* Modifies xunit to emit diagnostic message per assembly

Fixes xunit#1655
  • Loading branch information
skimmedsquare committed Dec 3, 2021
1 parent f71b2c5 commit 19c236c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
44 changes: 41 additions & 3 deletions src/xunit.v3.runner.msbuild.tests/xunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ public static void ReturnsFalseWhenExitCodeIsNonZero()

Assert.False(result);
}

[Fact]
public static void ReportsWhenUnableToReadConfig()
{
var mockedReporter = Substitute.For<IRunnerReporter>();
mockedReporter.IsEnvironmentallyEnabled.Returns(true);

var mockedMessageSink = Substitute.For<_IMessageSink>();
mockedReporter.CreateMessageHandler(Arg.Any<IRunnerLogger>(), Arg.Any<_IMessageSink>())
.Returns(new System.Threading.Tasks.ValueTask<_IMessageSink>(mockedMessageSink));

var mockedProxy = Substitute.For<_ConfigReaderProxy>();
mockedProxy.Load(Arg.Any<TestAssemblyConfiguration>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(false);

var stubAssembly = Substitute.For<ITaskItem>();
stubAssembly.GetMetadata("ConfigFile").Returns("DummyPath");

var assemblies = new ITaskItem[] { stubAssembly };

var xunit = new Testable_xunit(mockedProxy, mockedReporter)
{
Assemblies = assemblies,
NoLogo = true
};

xunit.Cancel();
xunit.Execute();

mockedMessageSink.Received().OnMessage(Arg.Any<_DiagnosticMessage>());
}
}

public class GetReporter
Expand Down Expand Up @@ -186,14 +217,22 @@ public void BadChosenReporter_WithAvailableReporters()

public class Testable_xunit : xunit
{
public readonly List<IRunnerReporter> AvailableReporters = new();
public readonly List<IRunnerReporter> AvailableReporters;
public IRunnerReporter? MockedReporter { get; init; }

public Testable_xunit()
: this(0)
{ }

public Testable_xunit(_ConfigReaderProxy proxy, IRunnerReporter reporter)
: base(proxy)
{
AvailableReporters = new() { reporter };
}

public Testable_xunit(int exitCode)
{
AvailableReporters = new();
BuildEngine = Substitute.For<IBuildEngine>();
Assemblies = new ITaskItem[0];
ExitCode = exitCode;
Expand All @@ -202,7 +241,6 @@ public Testable_xunit(int exitCode)
protected override List<IRunnerReporter> GetAvailableRunnerReporters() =>
AvailableReporters;

public new IRunnerReporter? GetReporter() =>
base.GetReporter();
public new IRunnerReporter? GetReporter() => base.GetReporter();
}
}
18 changes: 16 additions & 2 deletions src/xunit.v3.runner.msbuild/xunit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class xunit : MSBuildTask, ICancelableTask
{
volatile bool cancel;
readonly ConcurrentDictionary<string, ExecutionSummary> completionMessages = new();

readonly _ConfigReaderProxy configReader;
bool? diagnosticMessages;
bool? failSkips;
XunitFilters? filters;
Expand Down Expand Up @@ -106,6 +108,15 @@ protected XunitFilters Filters

public ITaskItem? XmlV1 { get; set; }

public xunit()
: this(new _ConfigReaderProxy())
{ }

public xunit(_ConfigReaderProxy proxy)
{
configReader = proxy;
}

public void Cancel()
{
cancel = true;
Expand Down Expand Up @@ -205,7 +216,10 @@ public override bool Execute()
TargetFramework = targetFramework
};

ConfigReader.Load(projectAssembly.Configuration, assemblyFileName, configFileName);
if (!configReader.Load(projectAssembly.Configuration, assemblyFileName, configFileName) && configFileName != null)
{
reporterMessageHandler.OnMessage(new _DiagnosticMessage() { Message = $"Unable to read '{configFileName}'; falling back to default values." });
}

if (Culture != null)
projectAssembly.Configuration.Culture = Culture switch
Expand Down Expand Up @@ -241,7 +255,7 @@ public override bool Execute()
foreach (var assembly in project.Assemblies)
{
var assemblyElement = ExecuteAssembly(assembly, appDomains);
if (assemblyElement != null)
if (assemblyElement != null && assemblyElement.Result != null)
assembliesElement!.Add(assemblyElement);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/xunit.v3.runner.utility/Configuration/ConfigReaderProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Xunit.Runner.Common
{
/// <summary>
/// This class is a simple proxy for ConfigReader allowing for dependency injection.
/// </summary>
public class _ConfigReaderProxy
{
/// <summary>
/// Proxies calls to ConfigReader.Load
/// </summary>
public virtual bool Load(
TestAssemblyConfiguration configuration,
string? assemblyFileName,
string? configFileName = null)
{
return ConfigReader.Load(configuration, assemblyFileName, configFileName);
}
}
}

0 comments on commit 19c236c

Please sign in to comment.