Skip to content

Tests: Upgrade to xUnit v3 + Misc fixes #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion docs/Releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ layout: "default"
This page tracks major changes included in any update starting with version 4.0.0.3

#### Unreleased
No pending unreleased changes.
- **Fixes/Changes**:
- Fixes MiniProfiler.Current.RenderIncludes generating invalid HTML ([#681](https://github.com/MiniProfiler/dotnet/pull/681) - thanks [rikbosch](https://github.com/dazbradbury))
- Fixed [#671](https://github.com/MiniProfiler/dotnet/issues/671): SQL profiling network Time for some configurations ([#686](https://github.com/MiniProfiler/dotnet/pull/686) - thanks [dazbradbury](https://github.com/dazbradbury))

#### Version 4.5.4
- **New**:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;

namespace StackExchange.Profiling.Data
Expand Down Expand Up @@ -63,7 +64,7 @@ public void OnNext(KeyValuePair<string, object?> kv)
if (val is CommandExecutedEventData data && _commands.TryRemove(data.CommandId, out var current))
{
// A completion for a DataReader only means we *started* getting data back, not finished.
if (data.Result is RelationalDataReader or SqlDataReader)
if (data.Result is RelationalDataReader or DbDataReader)
{
_readers[data.CommandId] = current;
current.FirstFetchCompleted();
Expand Down
45 changes: 30 additions & 15 deletions src/MiniProfiler.Shared/SqlFormatters/InlineFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;

Expand All @@ -9,9 +11,24 @@ namespace StackExchange.Profiling.SqlFormatters
/// </summary>
public class InlineFormatter : ISqlFormatter
{
private static readonly Regex CommandSpacing = new Regex(@",([^\s])", RegexOptions.Compiled);
private static readonly Regex CommandSpacing = new(@",([^\s])", RegexOptions.Compiled);
private static bool includeTypeInfo;

private static readonly HashSet<string> QuotesDbTypes = new(StringComparer.OrdinalIgnoreCase)
{
nameof(DbType.AnsiString),
nameof(DbType.AnsiStringFixedLength),
nameof(DbType.Date),
nameof(DbType.DateTime),
nameof(DbType.DateTime2),
nameof(DbType.DateTimeOffset),
nameof(DbType.Guid),
nameof(DbType.String),
nameof(DbType.StringFixedLength),
nameof(DbType.Time),
nameof(DbType.Xml),
};

/// <summary>
/// Whether to modify the output query by adding spaces after commas.
/// </summary>
Expand Down Expand Up @@ -74,20 +91,18 @@ public string GetParameterValue(SqlTimingParameter param)

if (result != null)
{
switch (type.ToLower())
if (QuotesDbTypes.Contains(type))
{
result = string.Format("'{0}'", result);
}
else if (string.Equals(type, nameof(DbType.Boolean), StringComparison.OrdinalIgnoreCase))
{
case "string":
case "datetime":
result = string.Format("'{0}'", result);
break;
case "boolean":
result = result switch
{
"True" => "1",
"False" => "0",
_ => null,
};
break;
result = result switch
{
"True" => "1",
"False" => "0",
_ => null,
};
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<Import Project="$(MSBuildThisFileDirectory)..\Directory.Build.props" />
<PropertyGroup>
<OutputType>Library</OutputType>
<OutputType>Exe</OutputType>
<OutputPath>bin\$(Configuration)\</OutputPath>
<SignAssembly>false</SignAssembly>

Expand All @@ -12,9 +12,9 @@
<ItemGroup>
<ProjectReference Include="..\..\src\MiniProfiler.Shared\MiniProfiler.Shared.csproj" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit.v3" Version="3.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
Expand Down
1 change: 0 additions & 1 deletion tests/MiniProfiler.Tests.AspNet/AspNetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Profiling.Tests
{
Expand Down
29 changes: 17 additions & 12 deletions tests/MiniProfiler.Tests.AspNet/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Profiling.Tests
{
[Collection(NonParallel)]
public class BasicTests : AspNetTest
public class BasicTests(ITestOutputHelper output) : AspNetTest(output)
{
public BasicTests(ITestOutputHelper output) : base(output) { }

[Fact(WindowsOnly = true)]
[Fact]
public void Simple()
{
Skip.IfNotWindows();
using (GetRequest("http://localhost/Test.aspx", startAndStopProfiler: false))
{
var mp = Options.StartProfiler();
Expand All @@ -28,9 +26,10 @@ public void Simple()
}
}

[Fact(WindowsOnly = true)]
[Fact]
public void StepIf_Basic()
{
Skip.IfNotWindows();
using (GetRequest())
{
var mp = Options.StartProfiler();
Expand All @@ -56,9 +55,10 @@ public void StepIf_Basic()
}
}

[Fact(WindowsOnly = true)]
[Fact]
public void StepIf_IncludeChildren()
{
Skip.IfNotWindows();
using (GetRequest())
{
var mp = Options.StartProfiler();
Expand Down Expand Up @@ -100,9 +100,10 @@ public void StepIf_IncludeChildren()
}
}

[Fact(WindowsOnly = true)]
[Fact]
public void CustomTimingIf_Basic()
{
Skip.IfNotWindows();
using (GetRequest())
{
var mp = Options.StartProfiler();
Expand All @@ -128,9 +129,10 @@ public void CustomTimingIf_Basic()
}
}

[Fact(WindowsOnly = true)]
[Fact]
public void DiscardResults()
{
Skip.IfNotWindows();
using (GetRequest(startAndStopProfiler: false))
{
var mp = Options.StartProfiler();
Expand All @@ -143,9 +145,10 @@ public void DiscardResults()
}
}

[Fact(WindowsOnly = true)]
[Fact]
public void GetProfiler_NoChildren()
{
Skip.IfNotWindows();
// this won't create any child steps
var mp = GetProfiler();

Expand All @@ -154,9 +157,10 @@ public void GetProfiler_NoChildren()
Assert.False(mp.Root.HasChildren);
}

[Fact(WindowsOnly = true)]
[Fact]
public void GetProfiler_Children()
{
Skip.IfNotWindows();
const int depth = 5;

var mp = GetProfiler(childDepth: depth);
Expand All @@ -174,9 +178,10 @@ public void GetProfiler_Children()
Assert.Equal(depth, children);
}

[Fact(WindowsOnly = true)]
[Fact]
public void GetRequest_StartAndStopProfiler()
{
Skip.IfNotWindows();
MiniProfiler? mp;
using (GetRequest())
{
Expand Down
4 changes: 2 additions & 2 deletions tests/MiniProfiler.Tests.AspNet/EF6Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using StackExchange.Profiling.Data;
using StackExchange.Profiling.EntityFramework6;
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Profiling.Tests
{
Expand All @@ -16,9 +15,10 @@ public EF6Tests(ITestOutputHelper output) : base(output)
MiniProfilerEF6.Initialize();
}

[Fact(WindowsOnly = true)]
[Fact]
public void ServicesCheck()
{
Skip.IfNotWindows();
const string providerKey = "System.Data.SQLite";

Assert.IsType<EFProfiledDbProviderServices>(DbConfiguration.DependencyResolver.GetService(typeof(DbProviderServices), providerKey));
Expand Down
18 changes: 11 additions & 7 deletions tests/MiniProfiler.Tests.AspNet/MiniProfileHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace StackExchange.Profiling.Tests
{
public class MiniProfilerHandlerTests
{
[Theory(WindowsOnly = true)]
[Theory]
[InlineData("BRILLANT", 404)]
[InlineData("underscore.js", 404)]
[InlineData("results-list", 200)]
[InlineData("includes.min.js", 200)]
[InlineData("includes.min.css", 200)]
public void GivenContext_WhenAResourceIsRequested_ThenTheCorrectHttpStatusCodeIsReturned(string resourceName, int expectedHttpStatus)
{
Skip.IfNotWindows();
var sut = new MiniProfilerHandler(new MiniProfilerOptions()
{
ResultsListAuthorize = null
Expand All @@ -31,14 +32,15 @@ public void GivenContext_WhenAResourceIsRequested_ThenTheCorrectHttpStatusCodeIs
private static readonly FieldInfo _cacheability = typeof(HttpCachePolicy).GetField("_cacheability", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo _maxAge = typeof(HttpCachePolicy).GetField("_maxAge", BindingFlags.Instance | BindingFlags.NonPublic);

[Theory(WindowsOnly = true)]
[Theory]
[InlineData("BRILLANT", (HttpCacheability)6, null)]
[InlineData("underscore.js", (HttpCacheability)6, null)]
[InlineData("results-list", (HttpCacheability)6, null)]
[InlineData("includes.min.js", HttpCacheability.Public, 2592000)]
[InlineData("includes.min.css", HttpCacheability.Public, 2592000)]
public void GivenContext_WhenAResourceIsRequested_ThenTheCorrectHttpCacheControlIsReturned(string resourceName, HttpCacheability expectedCacheability, int? expectedMaxAgeSeconds)
{
Skip.IfNotWindows();
var sut = new MiniProfilerHandler(new MiniProfilerOptions()
{
ResultsListAuthorize = null
Expand All @@ -52,11 +54,12 @@ public void GivenContext_WhenAResourceIsRequested_ThenTheCorrectHttpCacheControl
}
}

[Theory(WindowsOnly = true)]
[Theory]
[InlineData(true, 200)]
[InlineData(false, 401)]
public void GivenContext_WhenIndexIsRequested_ThenTheCorrectHttpStatusCodeIsReturned(bool isRequestAuthorized, int expectedHttpStatus)
{
Skip.IfNotWindows();
var sut = new MiniProfilerHandler(new MiniProfilerOptions()
{
ResultsListAuthorize = _ => isRequestAuthorized
Expand All @@ -66,15 +69,16 @@ public void GivenContext_WhenIndexIsRequested_ThenTheCorrectHttpStatusCodeIsRetu
Assert.Equal(expectedHttpStatus, res);
}

[Theory(WindowsOnly = true)]
[Theory]
[InlineData("gzip", typeof(GZipStream))]
[InlineData("deflate", typeof(DeflateStream))]
[InlineData("unknown", null)]
[InlineData("", null)]
public void GivenContext_WhenIndexIsRequested_ThenTheCorrectHttpStatusCodeIsReturnedType(string acceptEncoding, Type? expectedEncodingFilterType)
{
// Arrange
var sut = new MiniProfilerHandler(new MiniProfilerOptions());
{
Skip.IfNotWindows();
// Arrange
var sut = new MiniProfilerHandler(new MiniProfilerOptions());

// Act
var res = GetRequestResponseEncoding(sut, "includes.min.js", acceptEncoding);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using StackExchange.Profiling.Storage;
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Profiling.Tests.Storage
{
public class MemoryCacheStorageTests : StorageBaseTest, IClassFixture<MemoryCacheStorageFixture>
public class MemoryCacheStorageTests(MemoryCacheStorageFixture fixture, ITestOutputHelper output) : StorageBaseTest(fixture, output), IClassFixture<MemoryCacheStorageFixture>
{
public MemoryCacheStorageTests(MemoryCacheStorageFixture fixture, ITestOutputHelper output) : base(fixture, output)
{
}
}

public class MemoryCacheStorageFixture : StorageFixtureBase<MemoryCacheStorage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public TestMemoryCacheStorage()
};
}

[Fact(WindowsOnly = true)]
[Fact]
public void TestWeCanSaveTheSameProfilerTwice()
{
Skip.IfNotWindows();
var profiler = new MiniProfiler("/", Options) { Started = DateTime.UtcNow, Id = Guid.NewGuid() };
Options.Storage.Save(profiler);
Options.Storage.Save(profiler);
Expand All @@ -29,9 +30,10 @@ public void TestWeCanSaveTheSameProfilerTwice()
Assert.Single(guids);
}

[Fact(WindowsOnly = true)]
[Fact]
public void TestRangeQueries()
{
Skip.IfNotWindows();
var now = DateTime.UtcNow;
var inASec = now.AddSeconds(1);
var in2Secs = now.AddSeconds(2);
Expand Down
16 changes: 13 additions & 3 deletions tests/MiniProfiler.Tests.AspNet/WebRequestProfilerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Web;
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Profiling.Tests
{
Expand All @@ -16,13 +16,23 @@ public void Dispose()
Options = null!;
}

[Fact(WindowsOnly = true)]
[Fact]
public void WebRequestEnsureName()
{
using (var rq = GetRequest("http://localhost/Test.aspx", startAndStopProfiler: false))
Skip.IfNotWindows();
using (var rq = GetRequest("http://localhost/Test.aspx"))
{
try
{
_ = HttpContext.Current.Request.Url;
}
catch (Exception ex)
{
Output.WriteLine("eating initial .config load exception: " + ex);
}
var mp = new MiniProfiler(null, Options);
mp.Increment(); // 1 ms
Output.WriteLine("Url: " + HttpContext.Current.Request.Url);
mp.Stop(false);

Assert.NotNull(mp);
Expand Down
Loading