Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,23 @@ public static Summary RunSource(string source, IConfig? config = null)

[MethodImpl(MethodImplOptions.NoInlining)]
private static Summary RunWithDirtyAssemblyResolveHelper(Type type, IConfig? config, string[]? args)
=> (args == null
{
var summaries = args == null
? BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.TypeToBenchmarks(type, config) })
: new BenchmarkSwitcher(new[] { type }).RunWithDirtyAssemblyResolveHelper(args, config, false))
.Single();
: new BenchmarkSwitcher(new[] { type }).RunWithDirtyAssemblyResolveHelper(args, config, false);

return summaries.SingleOrDefault()
?? Summary.ValidationFailed($"No benchmarks found in type '{type.Name}'", string.Empty, string.Empty);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static Summary RunWithDirtyAssemblyResolveHelper(Type type, MethodInfo[] methods, IConfig? config = null)
=> BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.MethodsToBenchmarks(type, methods, config) }).Single();
{
var summaries = BenchmarkRunnerClean.Run(new[] { BenchmarkConverter.MethodsToBenchmarks(type, methods, config) });

return summaries.SingleOrDefault()
?? Summary.ValidationFailed($"No benchmarks found in type '{type.Name}'", string.Empty, string.Empty);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static Summary[] RunWithDirtyAssemblyResolveHelper(Assembly assembly, IConfig? config, string[]? args)
Expand Down
35 changes: 26 additions & 9 deletions tests/BenchmarkDotNet.Tests/Running/RunningEmptyBenchmarkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,32 @@ public class RunningEmptyBenchmarkTests
/// </summary>
[Theory]
[InlineData(null)]
//[InlineData(new object[] { new string[] { " " } })]
[InlineData(new object[] { new string[] { " " } })]
public void GenericTypeWithoutBenchmarkAttribute_ThrowsValidationError_WhenNoBenchmarkAttribute(string[]? args)
{
GetConfigWithLogger(out var logger, out var config);

var summary = BenchmarkRunner.Run<EmptyBenchmark>(config, args);

if (args == null)
{
Assert.True(summary.HasCriticalValidationErrors);
Assert.Contains(summary.ValidationErrors, validationError => validationError.Message == GetValidationErrorForType(typeof(EmptyBenchmark)));
Assert.Contains(GetValidationErrorForType(typeof(EmptyBenchmark)), logger.GetLog());
}
else
{
// When args is provided and type is invalid, we get a ValidationFailed summary
// instead of an unhandled exception (fix for issue #2724)
Assert.NotNull(summary);
Assert.Contains("No benchmarks found", summary.Title);
}

Assert.Contains(GetValidationErrorForType(typeof(EmptyBenchmark)), logger.GetLog());
}
#pragma warning restore BDN1000

[Theory]
[InlineData(null)]
//[InlineData(new object[] { new string[] { " " } })]
[InlineData(new object[] { new string[] { " " } })]
public void GenericTypeWithBenchmarkAttribute_RunsSuccessfully(string[]? args)
{
GetConfigWithLogger(out var logger, out var config);
Expand All @@ -79,16 +86,26 @@ public void GenericTypeWithBenchmarkAttribute_RunsSuccessfully(string[]? args)
/// </summary>
[Theory]
[InlineData(null)]
//[InlineData(new object[] { new string[] { " " } })]
[InlineData(new object[] { new string[] { " " } })]
public void TypeWithoutBenchmarkAttribute_ThrowsValidationError_WhenNoBenchmarkAttribute(string[]? args)
{
GetConfigWithLogger(out var logger, out var config);


var summary = BenchmarkRunner.Run(typeof(EmptyBenchmark), config, args);
Assert.True(summary.HasCriticalValidationErrors);
Assert.Contains(summary.ValidationErrors, validationError => validationError.Message == GetValidationErrorForType(typeof(EmptyBenchmark)));
Assert.Contains(GetValidationErrorForType(typeof(EmptyBenchmark)), logger.GetLog());

if (args == null)
{
Assert.True(summary.HasCriticalValidationErrors);
Assert.Contains(summary.ValidationErrors, validationError => validationError.Message == GetValidationErrorForType(typeof(EmptyBenchmark)));
Assert.Contains(GetValidationErrorForType(typeof(EmptyBenchmark)), logger.GetLog());
}
else
{
// When args is provided and type is invalid, we get a ValidationFailed summary
// instead of an unhandled exception (fix for issue #2724)
Assert.NotNull(summary);
Assert.Contains("No benchmarks found", summary.Title);
}
}
#pragma warning restore BDN1000

Expand Down