Skip to content
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

🚧Fix issue with failing tests #386

Merged
merged 3 commits into from
Feb 3, 2025
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
10 changes: 9 additions & 1 deletion CASL/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,15 @@ internal Audio(
/// Finalizes an instance of the <see cref="Audio"/> class.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Finalizers cannot be tested")]
~Audio() => Dispose(false);
~Audio()
{
if (UnitTestDetector.IsRunningFromUnitTest)
{
return;
}

Dispose(false);
}

/// <inheritdoc/>
public string Name => this.path.GetFileNameWithoutExtension(FilePath);
Expand Down
10 changes: 9 additions & 1 deletion CASL/Data/FullBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ public FullBuffer(
/// Finalizes an instance of the <see cref="FullBuffer"/> class.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Finalizers cannot be tested")]
~FullBuffer() => Dispose(false);
~FullBuffer()
{
if (UnitTestDetector.IsRunningFromUnitTest)
{
return;
}

Dispose(false);
}

/// <inheritdoc/>
public float TotalSeconds => this.audioDecoder.TotalSeconds;
Expand Down
10 changes: 9 additions & 1 deletion CASL/Data/StreamBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ public StreamBuffer(
/// Finalizes an instance of the <see cref="StreamBuffer"/> class.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Finalizers cannot be tested")]
~StreamBuffer() => Dispose(false);
~StreamBuffer()
{
if (UnitTestDetector.IsRunningFromUnitTest)
{
return;
}

Dispose(false);
}

/// <inheritdoc/>
public float TotalSeconds => this.audioDecoder.TotalSeconds;
Expand Down
10 changes: 9 additions & 1 deletion CASL/Devices/AudioDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ public AudioDeviceManager(IOpenALInvoker alInvoker)
/// Finalizes an instance of the <see cref="AudioDeviceManager"/> class.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Finalizers cannot be tested")]
~AudioDeviceManager() => Dispose(false);
~AudioDeviceManager()
{
if (UnitTestDetector.IsRunningFromUnitTest)
{
return;
}

Dispose(false);
}

/// <inheritdoc/>
public event EventHandler<EventArgs>? DeviceChanging;
Expand Down
52 changes: 52 additions & 0 deletions CASL/UnitTestDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <copyright file="UnitTestDetector.cs" company="KinsonDigital">
// Copyright (c) KinsonDigital. All rights reserved.
// </copyright>

namespace CASL;

using System;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Detects if the code ahs been executed from a unit test vs the rest of the application.
/// </summary>
[ExcludeFromCodeCoverage(Justification = "Do not need to see coverage for code used for testing.")]
internal static class UnitTestDetector
{
private static readonly bool IsRunningFromUnitTestValue;

/// <summary>
/// Initializes static members of the <see cref="UnitTestDetector"/> class.
/// </summary>
static UnitTestDetector()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (var assembly in assemblies)
{
if (string.IsNullOrEmpty(assembly.FullName) ||
!assembly.FullName.ToLowerInvariant().StartsWith("xunit."))
{
continue;
}

IsRunningFromUnitTestValue = true;
break;
}
}

/// <summary>
/// Gets a value indicating whether the code is being executed from a unit test.
/// </summary>
public static bool IsRunningFromUnitTest
{
get
{
#if DEBUG
return IsRunningFromUnitTestValue;
#else
return false;
#endif
}
}
}
Loading