Skip to content

Commit a3a4d4a

Browse files
authored
Merge pull request #3356 from cormacpayne/breaking-change-tool-refactor
Breaking change tool refactor
2 parents 4c03997 + 1a195c9 commit a3a4d4a

File tree

57 files changed

+46123
-324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+46123
-324
lines changed

tools/StaticAnalysis/BreakingChangeAnalyzer/BreakingChangeAnalyzer.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,37 @@ public void Analyze(
138138
}
139139
else
140140
{
141+
if (!File.Exists(filePath))
142+
{
143+
continue;
144+
}
145+
141146
var oldModuleMetadata = DeserializeCmdlets(filePath);
142147

143148
if (cmdletFilter != null)
144149
{
145-
string output = "Before filter\nOld module cmdlet count: " + oldModuleMetadata.Cmdlets.Count +
146-
"\nNew module cmdlet count: " + newModuleMetadata.Cmdlets.Count;
150+
string output = string.Format("Before filter\nOld module cmdlet count: {0}\nNew module cmdlet count: {1}",
151+
oldModuleMetadata.Cmdlets.Count, newModuleMetadata.Cmdlets.Count);
147152

148-
output += "\nCmdlet file: " + cmdletFileFullPath;
153+
output += string.Format("\nCmdlet file: {0}", cmdletFileFullPath);
149154

150155
oldModuleMetadata.FilterCmdlets(cmdletFilter);
151156
newModuleMetadata.FilterCmdlets(cmdletFilter);
152157

153-
output += "\nAfter filter\nOld module cmdlet count: " + oldModuleMetadata.Cmdlets.Count +
154-
"\nNew module cmdlet count: " + newModuleMetadata.Cmdlets.Count;
158+
output += string.Format("After filter\nOld module cmdlet count: {0}\nNew module cmdlet count: {1}",
159+
oldModuleMetadata.Cmdlets.Count, newModuleMetadata.Cmdlets.Count);
155160

156161
foreach (var cmdlet in oldModuleMetadata.Cmdlets)
157162
{
158-
output += "\n\tOld cmdlet - " + cmdlet.Name;
163+
output += string.Format("\n\tOld cmdlet - {0}", cmdlet.Name);
159164
}
160165

161166
foreach (var cmdlet in newModuleMetadata.Cmdlets)
162167
{
163-
output += "\n\tNew cmdlet - " + cmdlet.Name;
168+
output += string.Format("\n\tNew cmdlet - {0}", cmdlet.Name);
164169
}
165170

166-
issueLogger.WriteMessage(output + "\n");
171+
issueLogger.WriteMessage(output + Environment.NewLine);
167172
}
168173

169174
RunBreakingChangeChecks(oldModuleMetadata, newModuleMetadata, issueLogger);

tools/StaticAnalysis/BreakingChangeAnalyzer/CmdletBreakingChangeLoader.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,27 @@ public ModuleMetadata GetModuleMetadata(string assemblyPath)
101101
parameterData.ValidateSet.AddRange(validateSet.ValidValues);
102102
}
103103

104+
if (parameter.HasAttribute<ValidateRangeAttribute>())
105+
{
106+
var validateRange = parameter.GetAttribute<ValidateRangeAttribute>();
107+
parameterData.ValidateRangeMin = Convert.ToInt64(validateRange.MinRange);
108+
parameterData.ValidateRangeMax = Convert.ToInt64(validateRange.MaxRange);
109+
}
110+
104111
parameterData.ValidateNotNullOrEmpty = parameter.HasAttribute<ValidateNotNullOrEmptyAttribute>();
105112

106113
cmdletMetadata.Parameters.Add(parameterData);
107114

108115
foreach (var parameterSet in parameter.GetAttributes<ParameterAttribute>())
109116
{
110-
ParameterSetMetadata parameterSetMetadata = new ParameterSetMetadata()
111-
{
112-
Name = parameterSet.ParameterSetName ?? "__AllParameterSets"
113-
};
117+
var parameterSetMetadata = cmdletMetadata.ParameterSets.FirstOrDefault(s => s.Name.Equals(parameterSet.ParameterSetName));
114118

115-
foreach (var set in cmdletMetadata.ParameterSets)
119+
if (parameterSetMetadata == null)
116120
{
117-
if (set.Name.Equals(parameterSet.ParameterSetName))
121+
parameterSetMetadata = new ParameterSetMetadata()
118122
{
119-
parameterSetMetadata = set;
120-
break;
121-
}
123+
Name = parameterSet.ParameterSetName ?? "__AllParameterSets"
124+
};
122125
}
123126

124127
Parameter param = new Parameter

tools/StaticAnalysis/BreakingChangeAnalyzer/ParameterMetadata.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public class ParameterMetadata
4646
/// </summary>
4747
public List<string> ValidateSet { get { return _validateSet; } }
4848

49+
/// <summary>
50+
/// If the parameter implements ValidateRange, specifies the minimum value
51+
/// in the range; otherwise, the value is null
52+
/// </summary>
53+
public long? ValidateRangeMin { get; set; }
54+
55+
/// <summary>
56+
/// If the parameter implements ValidateRange, specifies the maximum value
57+
/// in the range; otherwise, the value is null
58+
/// </summary>
59+
public long? ValidateRangeMax { get; set; }
60+
4961
/// <summary>
5062
/// Specifies if the parameter has the ValidateNotNullOrEmpty attribute
5163
/// </summary>

tools/StaticAnalysis/BreakingChangeAnalyzer/ParameterMetadataHelper.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void CompareParameterMetadata(
8585
CheckForRemovedParameterAlias(cmdlet, oldParameter, newParameter, issueLogger);
8686
CheckParameterValidationSets(cmdlet, oldParameter, newParameter, issueLogger);
8787
CheckForValidateNotNullOrEmpty(cmdlet, oldParameter, newParameter, issueLogger);
88+
CheckParameterValidateRange(cmdlet, oldParameter, newParameter, issueLogger);
8889
}
8990
// If the parameter cannot be found, log an issue
9091
else
@@ -177,6 +178,12 @@ private void CheckParameterValidationSets(
177178
var oldValidateSet = oldParameter.ValidateSet;
178179
var newValidateSet = newParameter.ValidateSet;
179180

181+
// If there is no validate set in the new assembly, return
182+
if (newValidateSet.Count == 0)
183+
{
184+
return;
185+
}
186+
180187
// If there was no validate set in the old assembly, but there is
181188
// one in the new assembly, log an issue
182189
if (oldValidateSet.Count == 0 && newValidateSet.Count > 0)
@@ -214,13 +221,72 @@ private void CheckParameterValidationSets(
214221
severity: 0,
215222
problemId: ProblemIds.BreakingChangeProblemId.RemovedValidateSetValue,
216223
description: string.Format(Properties.Resources.RemovedValidateSetValueDescription,
217-
oldParameter.Name, oldValue, cmdlet.Name),
224+
oldParameter.Name, cmdlet.Name, oldValue),
218225
remediation: string.Format(Properties.Resources.RemovedValidateSetValueRemediation,
219226
oldValue, oldParameter.Name));
220227
}
221228
}
222229
}
223230

231+
/// <summary>
232+
/// Check if the parameter gained a validation range for values, or if the
233+
/// existing validation range for values excludes values previously accepted.
234+
/// </summary>
235+
/// <param name="cmdlet">The cmdlet whose parameter metadata is currently being checked.</param>
236+
/// <param name="oldParameter">The parameter metadata from the old (serialized) assembly.</param>
237+
/// <param name="newParameter">The parameter metadata from new assembly.</param>
238+
/// <param name="issueLogger">ReportLogger that will keep track of the issues found.</param>
239+
private void CheckParameterValidateRange(
240+
CmdletBreakingChangeMetadata cmdlet,
241+
ParameterMetadata oldParameter,
242+
ParameterMetadata newParameter,
243+
ReportLogger<BreakingChangeIssue> issueLogger)
244+
{
245+
if (newParameter.ValidateRangeMin != null && newParameter.ValidateRangeMax != null)
246+
{
247+
// If the old parameter had no validation range, but the new parameter does, log an issue
248+
if (oldParameter.ValidateRangeMin == null && oldParameter.ValidateRangeMax == null)
249+
{
250+
issueLogger.LogBreakingChangeIssue(
251+
cmdlet: cmdlet,
252+
severity: 0,
253+
problemId: ProblemIds.BreakingChangeProblemId.AddedValidateRange ,
254+
description: string.Format(Properties.Resources.AddedValidateRangeDescription,
255+
oldParameter.Name, cmdlet.Name),
256+
remediation: string.Format(Properties.Resources.AddedValidateRangeRemediation,
257+
oldParameter.Name));
258+
}
259+
else
260+
{
261+
// If the minimum value of the range has increased, log an issue
262+
if (oldParameter.ValidateRangeMin < newParameter.ValidateRangeMin)
263+
{
264+
issueLogger.LogBreakingChangeIssue(
265+
cmdlet: cmdlet,
266+
severity: 0,
267+
problemId: ProblemIds.BreakingChangeProblemId.ChangedValidateRangeMinimum,
268+
description: string.Format(Properties.Resources.ChangedValidateRangeMinimumDescription,
269+
oldParameter.Name, oldParameter.ValidateRangeMin, newParameter.ValidateRangeMin),
270+
remediation: string.Format(Properties.Resources.ChangedValidateRangeMinimumRemediation,
271+
oldParameter.Name, oldParameter.ValidateRangeMin));
272+
}
273+
274+
// If the maximum value of the range has decreased, log an issue
275+
if (oldParameter.ValidateRangeMax > newParameter.ValidateRangeMax)
276+
{
277+
issueLogger.LogBreakingChangeIssue(
278+
cmdlet: cmdlet,
279+
severity: 0,
280+
problemId: ProblemIds.BreakingChangeProblemId.ChangedValidateRangeMaximum,
281+
description: string.Format(Properties.Resources.ChangedValidateRangeMaximumDescription,
282+
oldParameter.Name, oldParameter.ValidateRangeMax, newParameter.ValidateRangeMax),
283+
remediation: string.Format(Properties.Resources.ChangedValidateRangeMaximumRemediation,
284+
oldParameter.Name, oldParameter.ValidateRangeMax));
285+
}
286+
}
287+
}
288+
}
289+
224290
/// <summary>
225291
/// Check if the parameter now supports the ValidateNotNullOrEmpty attribute
226292
/// </summary>

tools/StaticAnalysis/BreakingChangeAnalyzer/TypeMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static implicit operator TypeMetadata(Type typeToProcess)
4040

4141
public TypeMetadata()
4242
{
43-
43+
4444
}
4545

4646
public TypeMetadata(Type inputType)

0 commit comments

Comments
 (0)