-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathTestTheseTransportsAttribute.cs
74 lines (65 loc) · 2.71 KB
/
TestTheseTransportsAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace ServiceControl.Config.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ServiceControlInstaller.Engine.Instances;
public class TestTheseTransportsAttribute : TestCaseSourceAttribute
{
public TestTheseTransportsAttribute(params string[] transportNames)
: base(typeof(TransportTestCaseGenerator), nameof(TransportTestCaseGenerator.GetTestCases), new object[] { false, transportNames, null })
{
}
public TestTheseTransportsAttribute(string[] transportNames, string[] skipTheseExplicitTransportNames)
: base(typeof(TransportTestCaseGenerator), nameof(TransportTestCaseGenerator.GetTestCases), new object[] { false, transportNames, skipTheseExplicitTransportNames })
{
}
}
public class TestAllTransportsExceptAttribute : TestCaseSourceAttribute
{
public TestAllTransportsExceptAttribute(params string[] transportNames)
: base(typeof(TransportTestCaseGenerator), nameof(TransportTestCaseGenerator.GetTestCases), new object[] { true, transportNames, null })
{
}
}
public static class TransportTestCaseGenerator
{
public static object[] GetTestCases(bool invertList, string[] transportNames, string[] skipTransportNames = null)
{
var matching = Enumerate(transportNames)
.Where(name => skipTransportNames == null || !skipTransportNames.Contains(name))
.ToArray();
if (invertList)
{
var set = matching.ToHashSet();
matching = ServiceControlCoreTransports.GetAllTransports()
.Where(manifest => !set.Contains(manifest.Name))
.Select(manifest => manifest.Name)
.ToArray();
}
return matching
.Select(name => new object[] { name })
.ToArray();
}
static IEnumerable<string> Enumerate(string[] transportNames)
{
foreach (string transportName in transportNames)
{
var matching = ServiceControlCoreTransports.GetAllTransports()
.Where(t => t.Name == transportName || t.ZipName == transportName);
if (matching.Any())
{
foreach (var match in matching)
{
yield return match.Name;
}
}
else
{
throw new Exception($"Transport name '{transportName}' does not match to a transport in any detected manifest file.");
}
}
}
}
}