コード例 #1
0
        /// <summary>
        /// Check whether there exist lenient mandatory equal in the cmdlet
        /// Lenient mandatory equal means for two parameter set, one parameter set's mandatory parameters can
        /// be found in another parameter set whether as mandatory or optional.
        /// If all these two parameter set are not defualt, it may cause confusion.
        /// </summary>
        public void ValidateParameterSetWithLenientMandatoryEqual(CmdletMetadata cmdlet, ReportLogger <SignatureIssue> issueLogger)
        {
            var defaultParameterSet = cmdlet.DefaultParameterSet;

            foreach (var parameterSet1 in cmdlet.ParameterSets)
            {
                foreach (var parameterSet2 in cmdlet.ParameterSets)
                {
                    if (!parameterSet1.Equals(parameterSet2) &&
                        cmdlet.DefaultParameterSetName != parameterSet1.Name &&
                        cmdlet.DefaultParameterSetName != parameterSet2.Name &&
                        parameterSet1.Name.CompareTo(parameterSet2.Name) > 0)
                    {
                        if (parameterSet1.AllMandatoryParemeterLenientEquals(parameterSet2) &&
                            !IsParameterSetIntersectionCoveredByDefault(parameterSet1, parameterSet2, defaultParameterSet))
                        {
                            issueLogger.LogSignatureIssue(
                                cmdlet: cmdlet,
                                severity: 1,
                                problemId: SignatureProblemId.ParameterSetWithLenientMandatoryEqual,
                                description:
                                string.Format(
                                    "Parameter set '{0}' and '{1}' of cmdlet '{2}', for all mandatory parameters in {0} " +
                                    "we can find mandatory and optional parameter in {1}, and all mandatory parameter in " +
                                    "{1} can find the corresponding mandatory parameter in {0}, " +
                                    "and both of them are not default parameter set which may cause confusion.",
                                    parameterSet1.Name, parameterSet2.Name, cmdlet.Name),
                                remediation: "Merge these parameter sets into one parameter set.");
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Check whether there exist mandatory equal in the cmdlet.
        /// Mandatory equal means two parameter set has exactly the same mandatory parameters.
        /// If all these two parameter set are not defualt, it may cause confusion.
        /// An example: https://github.com/Azure/azure-powershell/issues/10954
        /// </summary>
        public void ValidateParameterSetWithMandatoryEqual(CmdletMetadata cmdlet, ReportLogger <SignatureIssue> issueLogger)
        {
            var defaultParameterSet = cmdlet.DefaultParameterSet;
            List <HashSet <string> > mandatoryEqualSetList = new List <HashSet <string> >();

            foreach (var parameterSet1 in cmdlet.ParameterSets)
            {
                foreach (var parameterSet2 in cmdlet.ParameterSets)
                {
                    if (!parameterSet1.Equals(parameterSet2) &&
                        cmdlet.DefaultParameterSetName != parameterSet1.Name &&
                        cmdlet.DefaultParameterSetName != parameterSet2.Name)
                    {
                        if (parameterSet1.AllMandatoryParemeterEquals(parameterSet2) &&
                            !IsParameterSetIntersectionCoveredByDefault(parameterSet1, parameterSet2, defaultParameterSet))
                        {
                            var isExistInSet = false;
                            foreach (var mandatoryEqualSet in mandatoryEqualSetList)
                            {
                                if (mandatoryEqualSet.Contains(parameterSet1.Name) || mandatoryEqualSet.Contains(parameterSet2.Name))
                                {
                                    mandatoryEqualSet.Add(parameterSet1.Name);
                                    mandatoryEqualSet.Add(parameterSet2.Name);
                                    isExistInSet = true;
                                    break;
                                }
                            }
                            if (!isExistInSet)
                            {
                                HashSet <string> newSet = new HashSet <string>();
                                newSet.Add(parameterSet1.Name);
                                newSet.Add(parameterSet2.Name);
                                mandatoryEqualSetList.Add(newSet);
                            }
                        }
                    }
                }
            }

            if (mandatoryEqualSetList.Count > 0)
            {
                foreach (var mandatoryEqualSet in mandatoryEqualSetList)
                {
                    string mandatoryEqualSetNames = "";
                    foreach (var mandatoryEqualSetName in mandatoryEqualSet)
                    {
                        if (mandatoryEqualSetNames != "")
                        {
                            mandatoryEqualSetNames += ", ";
                        }
                        mandatoryEqualSetNames += "'" + mandatoryEqualSetName + "'";
                    }
                    issueLogger.LogSignatureIssue(
                        cmdlet: cmdlet,
                        severity: 1,
                        problemId: SignatureProblemId.ParameterSetWithStrictMandatoryEqual,
                        description:
                        string.Format(
                            "Parameter set {0} of cmdlet '{1}' have the same mandatory parameters, " +
                            "and both of them are not default parameter set which may cause confusion.",
                            mandatoryEqualSetNames, cmdlet.Name),
                        remediation: "Merge these parameter sets into one parameter set.");
                }
            }
        }