Skip to content

Commit c230bcf

Browse files
authored
[CI Example Analyzer] Fix the random errors for import-module (Azure#19053)
* fix bug for import-module * delete retry import-module * remove debug
1 parent 7b20194 commit c230bcf

File tree

4 files changed

+40
-68
lines changed

4 files changed

+40
-68
lines changed

tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Custom rule for command name.
44
.NOTES
55
File: CommandName.psm1
6+
Import-Module should be at the beginning of the rule to avoid thread conflict.
67
#>
8+
Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global
9+
710
. $PSScriptRoot\..\utils.ps1
811

912
enum RuleNames {
@@ -25,10 +28,6 @@ function Measure-CommandName {
2528
[System.Management.Automation.Language.ScriptBlockAst]
2629
$ScriptBlockAst
2730
)
28-
begin{
29-
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
30-
Get-Item $modulePath | Import-Module -Global
31-
}
3231
process {
3332
$Results = @()
3433
$global:CommandParameterPair = @()
@@ -52,38 +51,34 @@ function Measure-CommandName {
5251
if ($CommandAst.InvocationOperator -eq "Unknown") {
5352
$CommandName = $CommandAst.CommandElements[0].Extent.Text
5453
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
55-
$ActualName = $GetCommand.Name
56-
if ($null -eq $GetCommand) {
54+
if($null -eq $GetCommand){
5755
# CommandName is not valid.
58-
# Redo import-module
59-
if(!(Redo-ImportModule $CommandName)){
60-
$global:CommandParameterPair += @{
61-
CommandName = $CommandName
62-
ParameterName = "<is not valid>"
63-
ModuleCmdletExNum = $ModuleCmdletExNum
64-
}
65-
return $true
56+
$global:CommandParameterPair += @{
57+
CommandName = $CommandName
58+
ParameterName = "<is not valid>"
59+
ModuleCmdletExNum = $ModuleCmdletExNum
6660
}
61+
return $true
6762
}
68-
else {
69-
if ($GetCommand.CommandType -eq "Alias") {
70-
# CommandName is an alias.
71-
$global:CommandParameterPair += @{
72-
CommandName = $CommandName
73-
ParameterName = "<is an alias>"
74-
ModuleCmdletExNum = $ModuleCmdletExNum
75-
}
76-
return $true
63+
64+
$ActualName = $GetCommand.Name
65+
if ($GetCommand.CommandType -eq "Alias") {
66+
# CommandName is an alias.
67+
$global:CommandParameterPair += @{
68+
CommandName = $CommandName
69+
ParameterName = "<is an alias>"
70+
ModuleCmdletExNum = $ModuleCmdletExNum
7771
}
78-
if ($CommandName -cne $ActualName) {
79-
# CommandName doesn't follow the Capitalization Conventions.
80-
$global:CommandParameterPair += @{
81-
CommandName = "$CommandName#@#$ActualName"
82-
ParameterName = "<doesn't follow the Capitalization Conventions>"
83-
ModuleCmdletExNum = $ModuleCmdletExNum
84-
}
85-
return $true
72+
return $true
73+
}
74+
if ($CommandName -cne $ActualName) {
75+
# CommandName doesn't follow the Capitalization Conventions.
76+
$global:CommandParameterPair += @{
77+
CommandName = "$CommandName#@#$ActualName"
78+
ParameterName = "<doesn't follow the Capitalization Conventions>"
79+
ModuleCmdletExNum = $ModuleCmdletExNum
8680
}
81+
return $true
8782
}
8883
}
8984
}

tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Custom rule for parameter name and value.
44
.NOTES
55
File: ParameterNameAndValue.psm1
6+
Import-Module should be at the beginning of the rule to avoid thread conflict.
67
#>
8+
Get-Item "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1" | Import-Module -Global
9+
710
. $PSScriptRoot\..\utils.ps1
811

912
enum RuleNames {
@@ -348,10 +351,6 @@ function Measure-ParameterNameAndValue {
348351
[System.Management.Automation.Language.ScriptBlockAst]
349352
$ScriptBlockAst
350353
)
351-
begin{
352-
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
353-
Get-Item $modulePath | Import-Module -Global
354-
}
355354
process {
356355
$Results = @()
357356
$global:CommandParameterPair = @()
@@ -367,7 +366,6 @@ function Measure-ParameterNameAndValue {
367366
[ScriptBlock]$Predicate = {
368367
param([System.Management.Automation.Language.Ast]$Ast)
369368
$global:Ast = $Ast
370-
371369
if ($Ast -is [System.Management.Automation.Language.AssignmentStatementAst]) {
372370
[System.Management.Automation.Language.AssignmentStatementAst]$AssignmentStatementAst = $Ast
373371
if($AssignmentStatementAst.Left -is [System.Management.Automation.Language.ConvertExpressionAst]){
@@ -402,13 +400,8 @@ function Measure-ParameterNameAndValue {
402400

403401
$CommandName = $CommandAst.CommandElements[0].Extent.Text
404402
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
405-
406-
# Skip parameters for invaild cmdlet
407-
if ($null -eq $GetCommand) {
408-
# Redo import-module
409-
if(!(Redo-ImportModule $CommandName)){
410-
return $false
411-
}
403+
if($null -eq $GetCommand){
404+
return $false
412405
}
413406
# Get command from alias
414407
if ($GetCommand.CommandType -eq "Alias") {
@@ -781,4 +774,4 @@ function Measure-ParameterNameAndValue {
781774
}
782775
}
783776

784-
Export-ModuleMember -Function Measure-*
777+
Export-ModuleMember -Function Measure-*

tools/StaticAnalysis/ExampleAnalyzer/Measure-MarkdownOrScript.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
5555
continue
5656
}
5757
Write-Output "Searching in file $($_.FullName) ..."
58-
$module = (Get-Item -Path $_.FullName).Directory.Parent.Name
58+
if((Get-Item -Path $_.FullName).Directory.Parent.Parent.Name -ne "src"){
59+
$module = (Get-Item -Path $_.FullName).Directory.Parent.Parent.Name
60+
}
61+
else{
62+
$module = (Get-Item -Path $_.FullName).Directory.Parent.Name
63+
}
5964
$cmdlet = $_.BaseName
6065
$result = Measure-SectionMissingAndOutputScript $module $cmdlet $_.FullName `
6166
-OutputScriptsInFile:$OutputScriptsInFile.IsPresent `
@@ -72,7 +77,7 @@ if ($PSCmdlet.ParameterSetName -eq "Markdown") {
7277
if ($PSCmdlet.ParameterSetName -eq "Script" -or $AnalyzeScriptsInFile.IsPresent) {
7378
# Read and analyze ".ps1" in \ScriptsByExample
7479
Write-Output "Analyzing file ..."
75-
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -ErrorAction SilentlyContinue
80+
$analysisResultsTable += Get-ScriptAnalyzerResult (Get-Item -Path $ScriptPaths) $RulePaths -IncludeDefaultRules:$IncludeDefaultRules.IsPresent -ErrorAction Continue
7681

7782
# Summarize analysis results, output in Result.csv
7883
if($analysisResultsTable){

tools/StaticAnalysis/ExampleAnalyzer/utils.ps1

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
Get-RecordsNotInAllowList
1313
Measure-SectionMissingAndOutputScript
1414
Get-ScriptAnalyzerResult
15-
Redo-ImportModule
1615
#>
1716

1817
$SYNOPSIS_HEADING = "## SYNOPSIS"
@@ -196,7 +195,7 @@ function Get-RecordsNotInAllowList{
196195
return $false
197196
}
198197
# Skip NeedDeleting in Storage
199-
if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage.Management"){
198+
if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage"){
200199
return $false
201200
}
202201
return $true
@@ -505,23 +504,3 @@ function Get-ScriptAnalyzerResult {
505504

506505
return $results
507506
}
508-
509-
<#
510-
.SYNOPSIS
511-
Retry import-module
512-
#>
513-
function Redo-ImportModule {
514-
param (
515-
[string]$CommandName
516-
)
517-
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
518-
Get-Item $modulePath | Import-Module -Global
519-
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
520-
if ($null -eq $GetCommand) {
521-
return $false
522-
}
523-
else{
524-
Write-Debug "Succeed by retrying import-module"
525-
return $true
526-
}
527-
}

0 commit comments

Comments
 (0)