暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmbeddedPackageOnlyTestAttribute.cs 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using NUnit.Framework;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. namespace Unity.Collections.Tests
  6. {
  7. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
  8. internal class EmbeddedPackageOnlyTestAttribute : NUnitAttribute, IApplyToTest
  9. {
  10. public void ApplyToTest(Test test)
  11. {
  12. #if UNITY_EDITOR
  13. var assembly = test.Method?.TypeInfo?.Assembly ?? test.TypeInfo?.Assembly;
  14. if (assembly == null)
  15. {
  16. UnityEngine.Debug.LogError($"The {nameof(EmbeddedPackageOnlyTestAttribute)} attribute can only be applied to tests in an assembly.");
  17. return;
  18. }
  19. var package = UnityEditor.PackageManager.PackageInfo.FindForAssembly(assembly);
  20. if (package == null)
  21. {
  22. UnityEngine.Debug.LogError(
  23. $"The {nameof(EmbeddedPackageOnlyTestAttribute)} attribute can only be applied to tests in a package.");
  24. return;
  25. }
  26. if (package.source == UnityEditor.PackageManager.PackageSource.Embedded ||
  27. package.source == UnityEditor.PackageManager.PackageSource.Local)
  28. return;
  29. #endif
  30. test.RunState = RunState.Ignored;
  31. test.Properties.Add(PropertyNames.SkipReason, "Only runs in the editor when this package is embedded or referenced locally.");
  32. }
  33. }
  34. }