UnityGame/Library/PackageCache/com.unity.collections/Unity.Collections.Tests/EmbeddedPackageOnlyTestAttribute.cs
2024-10-27 10:53:47 +03:00

39 lines
1.4 KiB
C#

using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace Unity.Collections.Tests
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
internal class EmbeddedPackageOnlyTestAttribute : NUnitAttribute, IApplyToTest
{
public void ApplyToTest(Test test)
{
#if UNITY_EDITOR
var assembly = test.Method?.TypeInfo?.Assembly ?? test.TypeInfo?.Assembly;
if (assembly == null)
{
UnityEngine.Debug.LogError($"The {nameof(EmbeddedPackageOnlyTestAttribute)} attribute can only be applied to tests in an assembly.");
return;
}
var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(assembly);
if (package == null)
{
UnityEngine.Debug.LogError(
$"The {nameof(EmbeddedPackageOnlyTestAttribute)} attribute can only be applied to tests in a package.");
return;
}
if (package.source == UnityEditor.PackageManager.PackageSource.Embedded ||
package.source == UnityEditor.PackageManager.PackageSource.Local)
return;
#endif
test.RunState = RunState.Ignored;
test.Properties.Add(PropertyNames.SkipReason, "Only runs in the editor when this package is embedded or referenced locally.");
}
}
}