| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Reflection; |
| | 8 | | using NUnit.Framework; |
| | 9 | | using NUnit.Framework.Interfaces; |
| | 10 | | using NUnit.Framework.Internal; |
| | 11 | |
|
| | 12 | | namespace Azure.Core.TestFramework |
| | 13 | | { |
| | 14 | | public class ClientTestFixtureAttribute : NUnitAttribute, IFixtureBuilder2, IPreFilter |
| | 15 | | { |
| 66 | 16 | | public static readonly string SyncOnlyKey = "SyncOnly"; |
| 66 | 17 | | public static readonly string RecordingDirectorySuffixKey = "RecordingDirectory"; |
| | 18 | |
|
| | 19 | | private readonly object[] _additionalParameters; |
| | 20 | | private readonly object[] _serviceVersions; |
| | 21 | | private readonly int? _maxServiceVersion; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes an instance of the <see cref="ClientTestFixtureAttribute"/> accepting additional fixture paramet |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="serviceVersions">The set of service versions that will be passed to the test suite.</param> |
| 472 | 27 | | public ClientTestFixtureAttribute(params object[] serviceVersions) : this(serviceVersions: serviceVersions, defa |
| 472 | 28 | | { } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Initializes an instance of the <see cref="ClientTestFixtureAttribute"/> accepting additional fixture paramet |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="serviceVersions">The set of service versions that will be passed to the test suite.</param> |
| | 34 | | /// <param name="additionalParameters">An array of additional parameters that will be passed to the test suite.< |
| 484 | 35 | | public ClientTestFixtureAttribute(object[] serviceVersions, object[] additionalParameters) |
| | 36 | | { |
| 484 | 37 | | _additionalParameters = additionalParameters ?? new object[] { }; |
| 484 | 38 | | _serviceVersions = serviceVersions ?? new object[] { }; |
| | 39 | |
|
| 282 | 40 | | _maxServiceVersion = _serviceVersions.Any() ? _serviceVersions.Max(s => Convert.ToInt32(s)) : (int?)null; |
| 484 | 41 | | } |
| | 42 | |
|
| | 43 | | public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo) |
| | 44 | | { |
| 0 | 45 | | return BuildFrom(typeInfo, this); |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo, IPreFilter filter) |
| | 49 | | { |
| | 50 | |
|
| 484 | 51 | | var suitePermutations = GeneratePermutations(); |
| | 52 | |
|
| 3424 | 53 | | foreach (var (fixture, isAsync, serviceVersion, parameter) in suitePermutations) |
| | 54 | | { |
| 4912 | 55 | | foreach (TestSuite testSuite in fixture.BuildFrom(typeInfo, filter)) |
| | 56 | | { |
| 1228 | 57 | | Process(testSuite, serviceVersion, isAsync, parameter); |
| 1228 | 58 | | yield return testSuite; |
| | 59 | | } |
| 1228 | 60 | | } |
| 484 | 61 | | } |
| | 62 | |
|
| | 63 | | private List<(TestFixtureAttribute suite, bool isAsync, object serviceVersion, object parameter)> GeneratePermut |
| | 64 | | { |
| 484 | 65 | | var result = new List<(TestFixtureAttribute suite, bool isAsync, object serviceVersion, object parameter)>() |
| | 66 | |
|
| 484 | 67 | | if (_serviceVersions.Any()) |
| | 68 | | { |
| 524 | 69 | | foreach (object serviceVersion in _serviceVersions) |
| | 70 | | { |
| 188 | 71 | | if (_additionalParameters.Any()) |
| | 72 | | { |
| 36 | 73 | | foreach (var parameter in _additionalParameters) |
| | 74 | | { |
| 12 | 75 | | result.Add((new TestFixtureAttribute(false, serviceVersion, parameter), false, serviceVersio |
| 12 | 76 | | result.Add((new TestFixtureAttribute(true, serviceVersion, parameter), true, serviceVersion, |
| | 77 | | } |
| | 78 | | } |
| | 79 | | else |
| | 80 | | { |
| | 81 | | // No additional parameters defined |
| 182 | 82 | | result.Add((new TestFixtureAttribute(false, serviceVersion), false, serviceVersion, null)); |
| 182 | 83 | | result.Add((new TestFixtureAttribute(true, serviceVersion), true, serviceVersion, null)); |
| | 84 | | } |
| | 85 | | } |
| | 86 | | } |
| | 87 | | else |
| | 88 | | { |
| 410 | 89 | | if (_additionalParameters.Any()) |
| | 90 | | { |
| 60 | 91 | | foreach (var parameter in _additionalParameters) |
| | 92 | | { |
| 20 | 93 | | result.Add((new TestFixtureAttribute(false, parameter), false, null, parameter)); |
| 20 | 94 | | result.Add((new TestFixtureAttribute(true, parameter), true, null, parameter)); |
| | 95 | | } |
| | 96 | | } |
| | 97 | | else |
| | 98 | | { |
| | 99 | | // No additional parameters defined |
| 400 | 100 | | result.Add((new TestFixtureAttribute(false), false, null, null)); |
| 400 | 101 | | result.Add((new TestFixtureAttribute(true), true, null, null)); |
| | 102 | | } |
| | 103 | | } |
| | 104 | |
|
| 484 | 105 | | return result; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | private void Process(TestSuite testSuite, object serviceVersion, bool isAsync, object parameter) |
| | 109 | | { |
| 1228 | 110 | | var serviceVersionNumber = Convert.ToInt32(serviceVersion); |
| 43216 | 111 | | foreach (Test test in testSuite.Tests) |
| | 112 | | { |
| 20380 | 113 | | if (test is ParameterizedMethodSuite parameterizedMethodSuite) |
| | 114 | | { |
| 10420 | 115 | | foreach (Test parameterizedTest in parameterizedMethodSuite.Tests) |
| | 116 | | { |
| 4452 | 117 | | ProcessTest(serviceVersion, isAsync, serviceVersionNumber, parameter, parameterizedTest); |
| | 118 | | } |
| | 119 | | } |
| | 120 | | else |
| | 121 | | { |
| 19622 | 122 | | ProcessTest(serviceVersion, isAsync, serviceVersionNumber, parameter, test); |
| | 123 | | } |
| | 124 | | } |
| 1228 | 125 | | } |
| | 126 | |
|
| | 127 | | private void ProcessTest(object serviceVersion, bool isAsync, int serviceVersionNumber, object parameter, Test t |
| | 128 | | { |
| 24074 | 129 | | if (parameter != null) |
| | 130 | | { |
| 544 | 131 | | test.Properties.Set(RecordingDirectorySuffixKey, parameter.ToString()); |
| | 132 | | } |
| 24074 | 133 | | if (test.GetCustomAttributes<SyncOnlyAttribute>(true).Any()) |
| | 134 | | { |
| 144 | 135 | | test.Properties.Set(SyncOnlyKey, true); |
| 144 | 136 | | if (isAsync) |
| | 137 | | { |
| 72 | 138 | | test.RunState = RunState.Ignored; |
| 72 | 139 | | test.Properties.Set("_SKIPREASON", $"Test ignored in async run because it's marked with {nameof(Sync |
| | 140 | | } |
| | 141 | | } |
| | 142 | |
|
| 24074 | 143 | | if (!isAsync && test.GetCustomAttributes<AsyncOnlyAttribute>(true).Any()) |
| | 144 | | { |
| 79 | 145 | | test.RunState = RunState.Ignored; |
| 79 | 146 | | test.Properties.Set("_SKIPREASON", $"Test ignored in sync run because it's marked with {nameof(AsyncOnly |
| | 147 | | } |
| | 148 | |
|
| 24074 | 149 | | if (serviceVersion == null) |
| | 150 | | { |
| 6044 | 151 | | return; |
| | 152 | | } |
| | 153 | |
|
| 18030 | 154 | | if (serviceVersionNumber != _maxServiceVersion) |
| | 155 | | { |
| 11628 | 156 | | test.Properties.Add("SkipRecordings", $"Test is ignored when not running live because the service versio |
| | 157 | | } |
| | 158 | |
|
| 18030 | 159 | | var minServiceVersion = test.GetCustomAttributes<ServiceVersionAttribute>(true); |
| 38784 | 160 | | foreach (ServiceVersionAttribute serviceVersionAttribute in minServiceVersion) |
| | 161 | | { |
| 2820 | 162 | | if (serviceVersionAttribute.Min != null && |
| 2820 | 163 | | Convert.ToInt32(serviceVersionAttribute.Min) > serviceVersionNumber) |
| | 164 | | { |
| 1662 | 165 | | test.RunState = RunState.Ignored; |
| 1662 | 166 | | test.Properties.Set("_SKIPREASON", $"Test ignored because it's minimum service version is set to {se |
| | 167 | | } |
| | 168 | |
|
| 2820 | 169 | | if (serviceVersionAttribute.Max != null && |
| 2820 | 170 | | Convert.ToInt32(serviceVersionAttribute.Max) < serviceVersionNumber) |
| | 171 | | { |
| 12 | 172 | | test.RunState = RunState.Ignored; |
| 12 | 173 | | test.Properties.Set("_SKIPREASON", $"Test ignored because it's maximum service version is set to {se |
| | 174 | | } |
| | 175 | | } |
| 18030 | 176 | | } |
| | 177 | |
|
| 0 | 178 | | bool IPreFilter.IsMatch(Type type) => true; |
| | 179 | |
|
| 0 | 180 | | bool IPreFilter.IsMatch(Type type, MethodInfo method) => true; |
| | 181 | | } |
| | 182 | | } |