From f8eff73690a463afad6c2708a949968b25182cce Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Sun, 29 Jan 2023 23:08:47 +0100 Subject: [PATCH] Simplify attribute Former-commit-id: e51cf7cc6303a8a49170f4c2e1c43bf5caaa142b --- FFMpegCore.Test/AudioTest.cs | 3 +- .../Utilities/IgnoreIfAttribute.cs | 39 ---------- .../Utilities/OperatingSystemUtils.cs | 11 --- .../TestMethodWithIgnoreIfSupportAttribute.cs | 75 ------------------- .../Utilities/WindowsOnlyDataTestMethod.cs | 23 ++++++ .../Utilities/WindowsOnlyTestMethod.cs | 23 ++++++ FFMpegCore.Test/VideoTest.cs | 36 +++------ 7 files changed, 59 insertions(+), 151 deletions(-) delete mode 100644 FFMpegCore.Test/Utilities/IgnoreIfAttribute.cs delete mode 100644 FFMpegCore.Test/Utilities/OperatingSystemUtils.cs delete mode 100644 FFMpegCore.Test/Utilities/TestMethodWithIgnoreIfSupportAttribute.cs create mode 100644 FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs create mode 100644 FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs diff --git a/FFMpegCore.Test/AudioTest.cs b/FFMpegCore.Test/AudioTest.cs index ab432e9..d20d21b 100644 --- a/FFMpegCore.Test/AudioTest.cs +++ b/FFMpegCore.Test/AudioTest.cs @@ -66,8 +66,7 @@ public void Audio_Add() Assert.IsTrue(File.Exists(outputFile)); } - [TestMethodWithIgnoreIfSupport] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod] public void Image_AddAudio() { using var outputFile = new TemporaryFile("out.mp4"); diff --git a/FFMpegCore.Test/Utilities/IgnoreIfAttribute.cs b/FFMpegCore.Test/Utilities/IgnoreIfAttribute.cs deleted file mode 100644 index 526a76c..0000000 --- a/FFMpegCore.Test/Utilities/IgnoreIfAttribute.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Reflection; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace FFMpegCore.Test.Utilities; - -// https://matt.kotsenas.com/posts/ignoreif-mstest -/// -/// An extension to the [Ignore] attribute. Instead of using test lists / test categories to conditionally -/// skip tests, allow a [TestClass] or [TestMethod] to specify a method to run. If the method returns -/// `true` the test method will be skipped. The "ignore criteria" method must be `static`, return a single -/// `bool` value, and not accept any parameters. By default, it is named "IgnoreIf". -/// -[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] -public class IgnoreIfAttribute : Attribute -{ - public string IgnoreCriteriaMethodName { get; set; } - - public IgnoreIfAttribute(string ignoreCriteriaMethodName = "IgnoreIf") - { - IgnoreCriteriaMethodName = ignoreCriteriaMethodName; - } - - internal bool ShouldIgnore(ITestMethod testMethod) - { - try - { - // Search for the method specified by name in this class or any parent classes. - var searchFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Static; - var method = testMethod.MethodInfo.DeclaringType!.GetMethod(IgnoreCriteriaMethodName, searchFlags); - return (bool) method?.Invoke(null, null)!; - } - catch (Exception e) - { - var message = $"Conditional ignore method {IgnoreCriteriaMethodName} not found. Ensure the method is in the same class as the test method, marked as `static`, returns a `bool`, and doesn't accept any parameters."; - throw new ArgumentException(message, e); - } - } -} \ No newline at end of file diff --git a/FFMpegCore.Test/Utilities/OperatingSystemUtils.cs b/FFMpegCore.Test/Utilities/OperatingSystemUtils.cs deleted file mode 100644 index 5526a2a..0000000 --- a/FFMpegCore.Test/Utilities/OperatingSystemUtils.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Runtime.InteropServices; - -namespace FFMpegCore.Test.Utilities; - -public static class OperatingSystemUtils -{ - public static bool NotWindows() - { - return !RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - } -} \ No newline at end of file diff --git a/FFMpegCore.Test/Utilities/TestMethodWithIgnoreIfSupportAttribute.cs b/FFMpegCore.Test/Utilities/TestMethodWithIgnoreIfSupportAttribute.cs deleted file mode 100644 index 5ea2436..0000000 --- a/FFMpegCore.Test/Utilities/TestMethodWithIgnoreIfSupportAttribute.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace FFMpegCore.Test.Utilities; - -// https://matt.kotsenas.com/posts/ignoreif-mstest -/// -/// An extension to the [TestMethod] attribute. It walks the method and class hierarchy looking -/// for an [IgnoreIf] attribute. If one or more are found, they are each evaluated, if the attribute -/// returns `true`, evaluation is short-circuited, and the test method is skipped. -/// -public class TestMethodWithIgnoreIfSupportAttribute : TestMethodAttribute -{ - public override TestResult[] Execute(ITestMethod testMethod) - { - var ignoreResults = TestMethodUtils.GetIgnoreResults(testMethod); - return ignoreResults.Any() - ? ignoreResults - : base.Execute(testMethod); - } -} -public class DataTestMethodWithIgnoreIfSupportAttribute : DataTestMethodAttribute -{ - public override TestResult[] Execute(ITestMethod testMethod) - { - var ignoreResults = TestMethodUtils.GetIgnoreResults(testMethod); - return ignoreResults.Any() - ? ignoreResults - : base.Execute(testMethod); - } -} - -internal class TestMethodUtils -{ - internal static TestResult[] GetIgnoreResults(ITestMethod testMethod) - { - var ignoreAttributes = FindAttributes(testMethod); - - // Evaluate each attribute, and skip if one returns `true` - foreach (var ignoreAttribute in ignoreAttributes) - { - if (ignoreAttribute.ShouldIgnore(testMethod)) - { - var message = $"Test not executed. Conditional ignore method '{ignoreAttribute.IgnoreCriteriaMethodName}' evaluated to 'true'."; - { - return new[] - { - new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } - }; - } - } - } - - return Array.Empty(); - } - - private static IEnumerable FindAttributes(ITestMethod testMethod) - { - // Look for an [IgnoreIf] on the method, including any virtuals this method overrides - var ignoreAttributes = new List(); - ignoreAttributes.AddRange(testMethod.GetAttributes(inherit: true)); - - // Walk the class hierarchy looking for an [IgnoreIf] attribute - var type = testMethod.MethodInfo.DeclaringType; - while (type != null) - { - ignoreAttributes.AddRange(type.GetCustomAttributes(inherit: true)); - type = type.DeclaringType; - } - return ignoreAttributes; - } -} \ No newline at end of file diff --git a/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs b/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs new file mode 100644 index 0000000..01fce23 --- /dev/null +++ b/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs @@ -0,0 +1,23 @@ +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace FFMpegCore.Test.Utilities; + +public class WindowsOnlyDataTestMethod : DataTestMethodAttribute +{ + public override TestResult[] Execute(ITestMethod testMethod) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var message = $"Test not executed on other platforms than Windows"; + { + return new[] + { + new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } + }; + } + } + + return base.Execute(testMethod); + } +} \ No newline at end of file diff --git a/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs b/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs new file mode 100644 index 0000000..0dc4b9e --- /dev/null +++ b/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs @@ -0,0 +1,23 @@ +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace FFMpegCore.Test.Utilities; + +public class WindowsOnlyTestMethod : TestMethodAttribute +{ + public override TestResult[] Execute(ITestMethod testMethod) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var message = $"Test not executed on other platforms than Windows"; + { + return new[] + { + new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } + }; + } + } + + return base.Execute(testMethod); + } +} \ No newline at end of file diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 06e030c..46eac59 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -89,8 +89,7 @@ public void Video_ToH265_MKV_Args() } [SupportedOSPlatform("windows")] - [DataTestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyDataTestMethod, Timeout(10000)] [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] public void Video_ToMP4_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat) @@ -107,8 +106,7 @@ public void Video_ToMP4_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public void Video_ToMP4_Args_Pipe_DifferentImageSizes() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -128,8 +126,7 @@ public void Video_ToMP4_Args_Pipe_DifferentImageSizes() } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)][SupportedOSPlatform("windows")] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -149,8 +146,7 @@ public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -170,8 +166,7 @@ public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -326,8 +321,7 @@ public void Video_ToTS_Args() } [SupportedOSPlatform("windows")] - [DataTestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyDataTestMethod, Timeout(10000)] [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] public async Task Video_ToTS_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat) @@ -360,8 +354,7 @@ public async Task Video_ToOGV_Resize() } [SupportedOSPlatform("windows")] - [DataTestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyDataTestMethod, Timeout(10000)] [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] // [DataRow(PixelFormat.Format48bppRgb)] @@ -397,8 +390,7 @@ public void Scale_Mp4_Multithreaded() } [SupportedOSPlatform("windows")] - [DataTestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyDataTestMethod, Timeout(10000)] [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] // [DataRow(PixelFormat.Format48bppRgb)] @@ -416,8 +408,7 @@ public void Video_ToMP4_Resize_Args_Pipe(System.Drawing.Imaging.PixelFormat pixe } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public void Video_Snapshot_InMemory() { var input = FFProbe.Analyse(TestResources.Mp4Video); @@ -429,8 +420,7 @@ public void Video_Snapshot_InMemory() } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public void Video_Snapshot_PersistSnapshot() { var outputPath = new TemporaryFile("out.png"); @@ -466,8 +456,7 @@ public void Video_Join() Assert.AreEqual(input.PrimaryVideoStream.Width, result.PrimaryVideoStream.Width); } - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public void Video_Join_Image_Sequence() { var imageSet = new List(); @@ -577,8 +566,7 @@ public void Video_OutputsData() } [SupportedOSPlatform("windows")] - [TestMethodWithIgnoreIfSupport, Timeout(10000)] - [IgnoreIf(nameof(OperatingSystemUtils.NotWindows))] + [WindowsOnlyTestMethod, Timeout(10000)] public void Video_TranscodeInMemory() { using var resStream = new MemoryStream();