Skip to content

Commit

Permalink
[CI Example Analyzer] Fix the random errors for import-module (Azure#…
Browse files Browse the repository at this point in the history
…19053)

* fix bug for import-module

* delete retry import-module

* remove debug
  • Loading branch information
MoChilia authored Jul 27, 2022
1 parent 7b20194 commit c230bcf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 68 deletions.
57 changes: 26 additions & 31 deletions tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
Custom rule for command name.
.NOTES
File: CommandName.psm1
Import-Module should be at the beginning of the rule to avoid thread conflict.
#>
Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global

. $PSScriptRoot\..\utils.ps1

enum RuleNames {
Expand All @@ -25,10 +28,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 +51,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 @@ -3,7 +3,10 @@
Custom rule for parameter name and value.
.NOTES
File: ParameterNameAndValue.psm1
Import-Module should be at the beginning of the rule to avoid thread conflict.
#>
Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global

. $PSScriptRoot\..\utils.ps1

enum RuleNames {
Expand Down Expand Up @@ -348,10 +351,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 +366,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 +400,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 +774,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
23 changes: 1 addition & 22 deletions tools/StaticAnalysis/ExampleAnalyzer/utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Get-RecordsNotInAllowList
Measure-SectionMissingAndOutputScript
Get-ScriptAnalyzerResult
Redo-ImportModule
#>

$SYNOPSIS_HEADING = "## SYNOPSIS"
Expand Down Expand Up @@ -196,7 +195,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 +504,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
}
}

0 comments on commit c230bcf

Please sign in to comment.