/
MsTestV2GeneratorProvider.cs
165 lines (145 loc) · 8.32 KB
/
MsTestV2GeneratorProvider.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using TechTalk.SpecFlow.Generator.CodeDom;
namespace TechTalk.SpecFlow.Generator.UnitTestProvider
{
public class MsTestV2GeneratorProvider : MsTestGeneratorProvider
{
protected internal const string DONOTPARALLELIZE_ATTR = "Microsoft.VisualStudio.TestTools.UnitTesting.DoNotParallelize";
protected internal const string DONOTPARALLELIZE_TAG = "MsTest:donotparallelize";
protected internal const string CATEGORY_ATTR = "Microsoft.VisualStudio.TestTools.UnitTesting.TestCategoryAttribute";
protected internal const string OWNER_ATTR = "Microsoft.VisualStudio.TestTools.UnitTesting.OwnerAttribute";
protected internal const string WORKITEM_ATTR = "Microsoft.VisualStudio.TestTools.UnitTesting.WorkItemAttribute";
protected internal const string DEPLOYMENTITEM_ATTR = "Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute";
protected internal const string OWNER_TAG = "owner:";
protected internal const string WORKITEM_TAG = "workitem:";
protected internal const string DEPLOYMENTITEM_TAG = "MsTest:deploymentitem:";
public MsTestV2GeneratorProvider(CodeDomHelper codeDomHelper) : base(codeDomHelper)
{
}
public override UnitTestGeneratorTraits GetTraits()
{
return UnitTestGeneratorTraits.ParallelExecution;
}
public override void SetTestClassCategories(TestClassGenerationContext generationContext, IEnumerable<string> featureCategories)
{
var doNotParallelizeTags = featureCategories.Where(f => f.StartsWith(DONOTPARALLELIZE_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (doNotParallelizeTags.Any())
{
generationContext.CustomData[DONOTPARALLELIZE_TAG] = string.Empty;
}
generationContext.CustomData["featureCategories"] = GetNonMSTestSpecificTags(featureCategories).ToArray();
var ownerTags = featureCategories.Where(t => t.StartsWith(OWNER_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (ownerTags.Any())
{
generationContext.CustomData[OWNER_TAG] = ownerTags.Select(t => t.Substring(OWNER_TAG.Length).Trim('\"')).FirstOrDefault();
}
var workItemTags = featureCategories.Where(t => t.StartsWith(WORKITEM_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (workItemTags.Any())
{
int temp;
var workItemsAsStrings = workItemTags.Select(t => t.Substring(WORKITEM_TAG.Length).Trim('\"'));
if (workItemsAsStrings.Any())
{
generationContext.CustomData[WORKITEM_TAG] = workItemsAsStrings.Where(t => int.TryParse(t, out temp)).Select(t => int.Parse(t));
}
}
var deploymentItemTags = featureCategories.Where(t => t.StartsWith(DEPLOYMENTITEM_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (deploymentItemTags.Any())
{
var deploymentItemsAsStrings = deploymentItemTags.Select(t => t.Substring(DEPLOYMENTITEM_TAG.Length));
if (deploymentItemsAsStrings.Any())
{
generationContext.CustomData[DEPLOYMENTITEM_TAG] = deploymentItemsAsStrings;
}
}
}
public override void SetTestMethod(TestClassGenerationContext generationContext, CodeMemberMethod testMethod, string friendlyTestName)
{
if (generationContext.CustomData.ContainsKey(DONOTPARALLELIZE_TAG))
{
CodeDomHelper.AddAttribute(testMethod, DONOTPARALLELIZE_ATTR);
}
base.SetTestMethod(generationContext, testMethod, friendlyTestName);
if (generationContext.CustomData.ContainsKey("featureCategories"))
{
var featureCategories = (string[])generationContext.CustomData["featureCategories"];
CodeDomHelper.AddAttributeForEachValue(testMethod, CATEGORY_ATTR, featureCategories);
}
if (generationContext.CustomData.ContainsKey(OWNER_TAG))
{
string ownerName = generationContext.CustomData[OWNER_TAG] as string;
if (!string.IsNullOrEmpty(ownerName))
{
CodeDomHelper.AddAttribute(testMethod, OWNER_ATTR, ownerName);
}
}
if (generationContext.CustomData.ContainsKey(WORKITEM_TAG))
{
var workItems = generationContext.CustomData[WORKITEM_TAG] as IEnumerable<int>;
foreach (int workItem in workItems)
{
CodeDomHelper.AddAttribute(testMethod, WORKITEM_ATTR, workItem);
}
}
if (generationContext.CustomData.ContainsKey(DEPLOYMENTITEM_TAG))
{
var deploymentItems = generationContext.CustomData[DEPLOYMENTITEM_TAG] as IEnumerable<string>;
foreach (string deploymentItem in deploymentItems)
{
var outputDirProvided = deploymentItem.Split(':').Any();
if (outputDirProvided)
{
CodeDomHelper.AddAttribute(testMethod, DEPLOYMENTITEM_ATTR, deploymentItem.Split(':'));
}
else
{
CodeDomHelper.AddAttribute(testMethod, DEPLOYMENTITEM_ATTR, deploymentItem);
}
}
}
}
public override void SetTestMethodCategories(TestClassGenerationContext generationContext, CodeMemberMethod testMethod, IEnumerable<string> scenarioCategories)
{
var scenarioCategoriesArray = scenarioCategories.ToArray();
var doNotParallelizeTags = scenarioCategoriesArray.Where(s => s.StartsWith(DONOTPARALLELIZE_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (doNotParallelizeTags.Any() && !generationContext.CustomData.ContainsKey(DONOTPARALLELIZE_TAG))
{
CodeDomHelper.AddAttribute(testMethod, DONOTPARALLELIZE_ATTR);
}
if (doNotParallelizeTags.Any() && generationContext.CustomData.ContainsKey(DONOTPARALLELIZE_TAG))
{
scenarioCategoriesArray = scenarioCategoriesArray.Where(s => !s.StartsWith(DONOTPARALLELIZE_TAG)).ToArray();
}
var ownerTags = scenarioCategoriesArray.Where(t => t.StartsWith(OWNER_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (ownerTags.Any())
{
string ownerName = ownerTags.Select(t => t.Substring(OWNER_TAG.Length).Trim('\"')).FirstOrDefault();
if (!string.IsNullOrEmpty(ownerName))
{
CodeDomHelper.AddAttribute(testMethod, OWNER_ATTR, ownerName);
}
}
var workItemTags = scenarioCategoriesArray.Where(t => t.StartsWith(WORKITEM_TAG, StringComparison.InvariantCultureIgnoreCase)).Select(t => t);
if (workItemTags.Any())
{
var workItemsAsStrings = workItemTags.Select(t => t.Substring(WORKITEM_TAG.Length).Trim('\"'));
var workItems = workItemsAsStrings.Where(t => int.TryParse(t, out _)).Select(int.Parse);
foreach (int workItem in workItems)
{
CodeDomHelper.AddAttribute(testMethod, WORKITEM_ATTR, workItem);
}
}
CodeDomHelper.AddAttributeForEachValue(testMethod, CATEGORY_ATTR, GetNonMSTestSpecificTags(scenarioCategoriesArray));
}
private IEnumerable<string> GetNonMSTestSpecificTags(IEnumerable<string> tags)
{
return tags == null ? new string[0] : tags.Where(t => !t.StartsWith(OWNER_TAG, StringComparison.InvariantCultureIgnoreCase))
.Where(t => !t.StartsWith(WORKITEM_TAG, StringComparison.InvariantCultureIgnoreCase))
.Where(t => !t.StartsWith(DEPLOYMENTITEM_TAG, StringComparison.InvariantCultureIgnoreCase))
.Select(t => t);
}
}
}