EliziumNet / Loopz

PowerShell iteration utilities with additional tools like the Parameter Set Tools
https://eliziumnet.github.io/Loopz/
MIT License
3 stars 0 forks source link

Param Set Tool: find-InAllParameterSetsByAccident #131

Closed plastikfan closed 3 years ago

plastikfan commented 3 years ago
----> Parameter Set Violations Report [test-ParamInAllParameterSetsByAccident] ... 
 
      In All Parameter Sets By Accident 
      ================================= 
 
      Defining a parameter with multiple 'Parameter Blocks', some with  
      and some without a parameter set, is invalid.  
 
 
  *** Parameter 'ClaimB' (of 'Alpha'), accidentally in all Parameter Sets:  
   🟢 Other Parameter Sets: '' 
 
  *** Parameter 'ClaimE' (of 'Alpha'), accidentally in all Parameter Sets:  
   🟢 Other Parameter Sets: 'Beta' 
 
 
========================================================================================================================= 
>>>>> SUMMARY:  Found the following 2 violations: 
   🔶 'In All Parameter Sets By Accident', Count: 2 
      🟨 Reasons:  
         💠 {'ClaimB' of Alpha => } 
         💠 {'ClaimE' of Alpha => Beta} 
========================================================================================================================= 

The 'Other Parameter Sets' statement can sometime show an empty parameter set name. This is clearly not correct.

The display for this rules violation needs to be checked. Currently the stragtegy is to take each parameter set, and for each one of its parameters check to see if it in multiple parameter sets and if so, is one of them the '__AllParameterSets'. If it is then the rule is violated. However, the logic behind the so called 'other' parameter sets is not clear and should probably be replaced.

All we need to show is the violating parameter set and the parameter that is causing the violation. So in this example, we have 2 violations: 'ClaimB' and 'ClaimE', so all we need to show is:

  *** Parameter 'ClaimB', accidentally in all Parameter Sets:  
   🟢 Parameter Set: 'Alpha'   

  *** Parameter 'ClaimE', accidentally in all Parameter Sets:  
   🟢 Parameter Set:  'Beta' 

and

>>>>> SUMMARY:  Found the following 2 violations: 
   🔶 'In All Parameter Sets By Accident', Count: 2 
      🟨 Reasons:  
         💠 { parameter 'ClaimB' of parameter set 'Alpha'} 
         💠 { parameter 'ClaimE' of paraeter set 'Beta'} 

(Note: the example was copied from another test case, so the variable names dont make sense; make sure the rename the parameters so they make sense for this test)

plastikfan commented 3 years ago

add this test to rules.tests.ps1:

  Describe 'MustNotBeInAllParameterSetsByAccident' {
    BeforeAll {
      InModuleScope Elizium.Loopz {
        function script:test-ParamInAllParameterSetsByAccident {
          param(
            [Parameter(ValueFromPipeline = $true)]
            [object]$Chaff,

            [Parameter(ParameterSetName = 'Alpha', Mandatory, Position = 1)]
            [object]$paramA,

            [Parameter()]
            [Parameter(ParameterSetName = 'Alpha', Position = 2)]
            [object]$paramB,

            [Parameter(ParameterSetName = 'Alpha', Position = 3)]
            [object]$paramC,

            [Parameter(ParameterSetName = 'Beta', Position = 1)]
            [object]$paramD,

            [Parameter()]
            [Parameter(ParameterSetName = 'Beta', Position = 2)]
            [object]$paramE
          )
        }
      }
    }

    Context 'given: functions with violations' {
      It 'should: report violations' -Tag 'Current' {
        InModuleScope Elizium.Loopz {
          [string]$commandName = 'test-ParamInAllParameterSetsByAccident';
          [CommandInfo]$commandInfo = Get-Command $commandName;
          [RuleController]$controller = [RuleController]::new($commandInfo);
          [syntax]$syntax = New-Syntax -CommandName $commandName -Signals $_signals -Scribbler $_scribbler;
          $controller.Test($syntax).Result | Should -Be $false;

          $commandInfo | Show-ParameterSetReport;
        }
      }
    } # given: functions with violations
  }