Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI Example Analyzer] Fix the random errors for import-module #19053

Merged
merged 3 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
.NOTES
File: CommandName.psm1
#>
Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global

. $PSScriptRoot\..\utils.ps1

enum RuleNames {
Expand All @@ -25,10 +27,6 @@ function Measure-CommandName {
[System.Management.Automation.Language.ScriptBlockAst]
$ScriptBlockAst
)
begin{
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
Get-Item $modulePath | Import-Module -Global
}
process {
$Results = @()
$global:CommandParameterPair = @()
Expand All @@ -52,38 +50,34 @@ function Measure-CommandName {
if ($CommandAst.InvocationOperator -eq "Unknown") {
$CommandName = $CommandAst.CommandElements[0].Extent.Text
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
$ActualName = $GetCommand.Name
if ($null -eq $GetCommand) {
if($null -eq $GetCommand){
# CommandName is not valid.
# Redo import-module
if(!(Redo-ImportModule $CommandName)){
$global:CommandParameterPair += @{
CommandName = $CommandName
ParameterName = "<is not valid>"
ModuleCmdletExNum = $ModuleCmdletExNum
}
return $true
$global:CommandParameterPair += @{
CommandName = $CommandName
ParameterName = "<is not valid>"
ModuleCmdletExNum = $ModuleCmdletExNum
}
return $true
}
else {
if ($GetCommand.CommandType -eq "Alias") {
# CommandName is an alias.
$global:CommandParameterPair += @{
CommandName = $CommandName
ParameterName = "<is an alias>"
ModuleCmdletExNum = $ModuleCmdletExNum
}
return $true

$ActualName = $GetCommand.Name
if ($GetCommand.CommandType -eq "Alias") {
# CommandName is an alias.
$global:CommandParameterPair += @{
CommandName = $CommandName
ParameterName = "<is an alias>"
ModuleCmdletExNum = $ModuleCmdletExNum
}
if ($CommandName -cne $ActualName) {
# CommandName doesn't follow the Capitalization Conventions.
$global:CommandParameterPair += @{
CommandName = "$CommandName#@#$ActualName"
ParameterName = "<doesn't follow the Capitalization Conventions>"
ModuleCmdletExNum = $ModuleCmdletExNum
}
return $true
return $true
}
if ($CommandName -cne $ActualName) {
# CommandName doesn't follow the Capitalization Conventions.
$global:CommandParameterPair += @{
CommandName = "$CommandName#@#$ActualName"
ParameterName = "<doesn't follow the Capitalization Conventions>"
ModuleCmdletExNum = $ModuleCmdletExNum
}
return $true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
.NOTES
File: ParameterNameAndValue.psm1
#>
Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global

. $PSScriptRoot\..\utils.ps1

enum RuleNames {
Expand Down Expand Up @@ -348,10 +350,6 @@ function Measure-ParameterNameAndValue {
[System.Management.Automation.Language.ScriptBlockAst]
$ScriptBlockAst
)
begin{
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
Get-Item $modulePath | Import-Module -Global
}
process {
$Results = @()
$global:CommandParameterPair = @()
Expand All @@ -367,7 +365,6 @@ function Measure-ParameterNameAndValue {
[ScriptBlock]$Predicate = {
param([System.Management.Automation.Language.Ast]$Ast)
$global:Ast = $Ast

if ($Ast -is [System.Management.Automation.Language.AssignmentStatementAst]) {
[System.Management.Automation.Language.AssignmentStatementAst]$AssignmentStatementAst = $Ast
if($AssignmentStatementAst.Left -is [System.Management.Automation.Language.ConvertExpressionAst]){
Expand Down Expand Up @@ -402,13 +399,8 @@ function Measure-ParameterNameAndValue {

$CommandName = $CommandAst.CommandElements[0].Extent.Text
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue

# Skip parameters for invaild cmdlet
if ($null -eq $GetCommand) {
# Redo import-module
if(!(Redo-ImportModule $CommandName)){
return $false
}
if($null -eq $GetCommand){
return $false
}
# Get command from alias
if ($GetCommand.CommandType -eq "Alias") {
Expand Down Expand Up @@ -781,4 +773,4 @@ function Measure-ParameterNameAndValue {
}
}

Export-ModuleMember -Function Measure-*
Export-ModuleMember -Function Measure-*
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
continue
}
Write-Output "Searching in file $($_.FullName) ..."
$module = (Get-Item -Path $_.FullName).Directory.Parent.Name
if((Get-Item -Path $_.FullName).Directory.Parent.Parent.Name -ne "src"){
$module = (Get-Item -Path $_.FullName).Directory.Parent.Parent.Name
}
else{
$module = (Get-Item -Path $_.FullName).Directory.Parent.Name
}
$cmdlet = $_.BaseName
$result = Measure-SectionMissingAndOutputScript $module $cmdlet $_.FullName `
-OutputScriptsInFile:$OutputScriptsInFile.IsPresent `
Expand All @@ -72,7 +77,7 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
if ($PSCmdlet.ParameterSetName -eq "Script" -or $AnalyzeScriptsInFile.IsPresent) {
# Read and analyze ".ps1" in \ScriptsByExample
Write-Output "Analyzing file ..."
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -ErrorAction SilentlyContinue
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -ErrorAction Continue

# Summarize analysis results, output in Result.csv
if($analysisResultsTable){
Expand Down
24 changes: 2 additions & 22 deletions tools/StaticAnalysis/ExampleAnalyzer/utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
Get-RecordsNotInAllowList
Measure-SectionMissingAndOutputScript
Get-ScriptAnalyzerResult
Redo-ImportModule
#>
$DebugPreference = 'Continue'
isra-fel marked this conversation as resolved.
Show resolved Hide resolved

$SYNOPSIS_HEADING = "## SYNOPSIS"
$SYNTAX_HEADING = "## SYNTAX"
Expand Down Expand Up @@ -196,7 +196,7 @@ function Get-RecordsNotInAllowList{
return $false
}
# Skip NeedDeleting in Storage
if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage.Management"){
if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage"){
return $false
}
return $true
Expand Down Expand Up @@ -505,23 +505,3 @@ function Get-ScriptAnalyzerResult {

return $results
}

<#
.SYNOPSIS
Retry import-module
#>
function Redo-ImportModule {
param (
[string]$CommandName
)
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
Get-Item $modulePath | Import-Module -Global
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
if ($null -eq $GetCommand) {
return $false
}
else{
Write-Debug "Succeed by retrying import-module"
return $true
}
}