|  | 
|  | 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. | 
|  | 2 | +# Licensed under the MIT License. | 
|  | 3 | + | 
|  | 4 | +# Keep in sync with the rule's reserved words list in | 
|  | 5 | +# Rules/AvoidReservedWordsAsFunctionNames.cs | 
|  | 6 | +$reservedWords = @( | 
|  | 7 | +    'begin','break','catch','class','configuration', | 
|  | 8 | +    'continue','data','define','do', | 
|  | 9 | +    'dynamicparam','else','elseif','end', | 
|  | 10 | +    'enum','exit','filter','finally', | 
|  | 11 | +    'for','foreach','from','function', | 
|  | 12 | +    'if','parallel','param','process', | 
|  | 13 | +    'return','sequence','switch', | 
|  | 14 | +    'throw','trap','try','type', | 
|  | 15 | +    'until','using','var','while','workflow' | 
|  | 16 | +) | 
|  | 17 | + | 
|  | 18 | +$randomCasedReservedWords = @( | 
|  | 19 | +    'bEgIN','bReAk','cAtCh','CLasS','cONfiGuRaTioN', | 
|  | 20 | +    'cONtiNuE','dAtA','dEFInE','Do', | 
|  | 21 | +    'DyNaMiCpArAm','eLsE','eLsEiF','EnD', | 
|  | 22 | +    'EnUm','eXiT','fIlTeR','fINaLLy', | 
|  | 23 | +    'FoR','fOrEaCh','fROm','fUnCtIoN', | 
|  | 24 | +    'iF','pArAlLeL','PaRaM','pRoCeSs', | 
|  | 25 | +    'ReTuRn','sEqUeNcE','SwItCh', | 
|  | 26 | +    'tHrOw','TrAp','tRy','TyPe', | 
|  | 27 | +    'uNtIl','UsInG','VaR','wHiLe','wOrKfLoW' | 
|  | 28 | +) | 
|  | 29 | + | 
|  | 30 | +$functionScopes = @( | 
|  | 31 | +	"global", "local", "script", "private" | 
|  | 32 | +) | 
|  | 33 | + | 
|  | 34 | +# Generate all combinations of reserved words and function scopes | 
|  | 35 | +$scopedReservedWordCases = foreach ($scope in $functionScopes) { | 
|  | 36 | +    foreach ($word in $reservedWords) { | 
|  | 37 | +        @{ | 
|  | 38 | +            Scope = $scope | 
|  | 39 | +            Name  = $word | 
|  | 40 | +        } | 
|  | 41 | +    } | 
|  | 42 | +} | 
|  | 43 | + | 
|  | 44 | +# Build variants of reserved words where the reserverd word: | 
|  | 45 | +# appearing at the start and end of a function | 
|  | 46 | +# name. | 
|  | 47 | +$substringReservedWords = $reservedWords | ForEach-Object { | 
|  | 48 | +    "$($_)A", "A$($_)", $_.Substring(0, $_.Length - 1) | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +$safeFunctionNames = @( | 
|  | 52 | +    'Get-Something','Do-Work','Classify-Data','Begin-Process' | 
|  | 53 | +) | 
|  | 54 | + | 
|  | 55 | +BeforeAll { | 
|  | 56 | +    $ruleName = 'PSAvoidReservedWordsAsFunctionNames' | 
|  | 57 | +} | 
|  | 58 | + | 
|  | 59 | +Describe 'AvoidReservedWordsAsFunctionNames' { | 
|  | 60 | +	Context 'When function names are reserved words' { | 
|  | 61 | +		It 'flags reserved word "<_>" as a violation' -TestCases $reservedWords { | 
|  | 62 | + | 
|  | 63 | +			$scriptDefinition = "function $_ { 'test' }" | 
|  | 64 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 65 | + | 
|  | 66 | +			$violations.Count | Should -Be 1 | 
|  | 67 | +			$violations[0].Severity | Should -Be 'Warning' | 
|  | 68 | +			$violations[0].RuleName | Should -Be $ruleName | 
|  | 69 | +			# Message text should include the function name as used | 
|  | 70 | +			$violations[0].Message | Should -Be "The reserved word '$_' was used as a function name. This should be avoided." | 
|  | 71 | +			# Extent should ideally capture only the function name | 
|  | 72 | +			$violations[0].Extent.Text | Should -Be $_ | 
|  | 73 | +		} | 
|  | 74 | + | 
|  | 75 | +		It 'flags the correct extent for a function named Function' { | 
|  | 76 | + | 
|  | 77 | +			$scriptDefinition = "Function Function { 'test' }" | 
|  | 78 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 79 | + | 
|  | 80 | +			$violations.Count | Should -Be 1 | 
|  | 81 | +			$violations[0].Severity | Should -Be 'Warning' | 
|  | 82 | +			$violations[0].RuleName | Should -Be $ruleName | 
|  | 83 | +			# Message text should include the function name as used | 
|  | 84 | +			$violations[0].Message | Should -Be "The reserved word 'Function' was used as a function name. This should be avoided." | 
|  | 85 | +			# Extent should ideally capture only the function name | 
|  | 86 | +			$violations[0].Extent.Text | Should -Be 'Function' | 
|  | 87 | + | 
|  | 88 | +			# Make sure the extent is the correct `Function` (not the one at the | 
|  | 89 | +			# very start) | 
|  | 90 | +			$violations[0].Extent.StartOffset | Should -not -Be 0 | 
|  | 91 | +		} | 
|  | 92 | + | 
|  | 93 | +		# Functions can have scopes. So function global:function {} should still | 
|  | 94 | +		# alert. | 
|  | 95 | +		It 'flags reserved word "<Name>" with scope "<Scope>" as a violation' -TestCases $scopedReservedWordCases { | 
|  | 96 | +			param($Scope, $Name) | 
|  | 97 | + | 
|  | 98 | +			$scriptDefinition = "function $($Scope):$($Name) { 'test' }" | 
|  | 99 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 100 | + | 
|  | 101 | +			$violations.Count | Should -Be 1 | 
|  | 102 | +			$violations[0].Severity | Should -Be 'Warning' | 
|  | 103 | +			$violations[0].RuleName | Should -Be $ruleName | 
|  | 104 | +			$violations[0].Message | Should -Be "The reserved word '$Name' was used as a function name. This should be avoided." | 
|  | 105 | +			$violations[0].Extent.Text | Should -Be "$($Scope):$($Name)" | 
|  | 106 | +		} | 
|  | 107 | + | 
|  | 108 | + | 
|  | 109 | +		It 'detects case-insensitively for "<_>"' -TestCases $randomCasedReservedWords { | 
|  | 110 | +			$scriptDefinition = "function $_ { }" | 
|  | 111 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 112 | +			$violations.Count | Should -Be 1 | 
|  | 113 | +			$violations[0].Message | Should -Be "The reserved word '$_' was used as a function name. This should be avoided." | 
|  | 114 | +		} | 
|  | 115 | + | 
|  | 116 | +		It 'reports one finding per offending function' { | 
|  | 117 | +			$scriptDefinition = 'function class { };function For { };function Safe-Name { };function TRy { }' | 
|  | 118 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 119 | + | 
|  | 120 | +			$violations.Count | Should -Be 3 | 
|  | 121 | +			$violations | ForEach-Object { $_.Severity | Should -Be 'Warning' } | 
|  | 122 | +			($violations | Select-Object -ExpandProperty Extent | Select-Object -ExpandProperty Text) | | 
|  | 123 | +				Sort-Object | | 
|  | 124 | +				Should -Be @('class','For','TRy') | 
|  | 125 | +		} | 
|  | 126 | +	} | 
|  | 127 | + | 
|  | 128 | +	Context 'When there are no violations' { | 
|  | 129 | +		It 'does not flag safe function name "<_>"' -TestCases $safeFunctionNames { | 
|  | 130 | +			$scriptDefinition = "function $_ { }" | 
|  | 131 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 132 | +			$violations.Count | Should -Be 0 | 
|  | 133 | +		} | 
|  | 134 | + | 
|  | 135 | +		It 'does not flag when script has no functions' { | 
|  | 136 | +			$scriptDefinition = '"hello";$x = 1..3 | ForEach-Object { $_ }' | 
|  | 137 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 138 | +			$violations.Count | Should -Be 0 | 
|  | 139 | +		} | 
|  | 140 | + | 
|  | 141 | +		It 'does not flag substring-like name "<_>"' -TestCases $substringReservedWords { | 
|  | 142 | +			$scriptDefinition = "function $_ { }" | 
|  | 143 | +			$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) | 
|  | 144 | +			$violations.Count | Should -Be 0 | 
|  | 145 | +		} | 
|  | 146 | +	} | 
|  | 147 | +} | 
0 commit comments