Skip to content

Commit f5ab511

Browse files
authored
Merge pull request #484 from microsoftgraph/bugfixes/InformationLogCleanUp
Information Stream Cleanup
2 parents 437e8bf + 94e63db commit f5ab511

File tree

9 files changed

+188
-23
lines changed

9 files changed

+188
-23
lines changed

.azure-pipelines/generate-beta-modules.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ jobs:
133133
displayName: 'Generate and Build Graph Resource Modules'
134134
inputs:
135135
filePath: '$(System.DefaultWorkingDirectory)/tools/GenerateModules.ps1'
136-
arguments: '-ArtifactsLocation $(Build.ArtifactStagingDirectory)\ -Build -EnableSigning'
136+
arguments: '-ArtifactsLocation $(Build.ArtifactStagingDirectory)\ -Build -Test -EnableSigning'
137137
pwsh: true
138138

139139
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@1

.azure-pipelines/generate-modules-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ jobs:
142142
pwsh: true
143143
script: |
144144
Write-Host $(BUILDNUMBER)
145-
pwsh $(System.DefaultWorkingDirectory)/tools/GenerateModules.ps1 -ArtifactsLocation $(Build.ArtifactStagingDirectory)\ -Build -EnableSigning -ModulePreviewNumber $(BUILDNUMBER) -UpdateAutoRest -RepositoryName "LocalNugetFeed"
145+
pwsh $(System.DefaultWorkingDirectory)/tools/GenerateModules.ps1 -ArtifactsLocation $(Build.ArtifactStagingDirectory)\ -Build -Test -EnableSigning -ModulePreviewNumber $(BUILDNUMBER) -UpdateAutoRest -RepositoryName "LocalNugetFeed"
146146
147147
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@1
148148
displayName: 'ESRP DLL Strong Name (Graph Resource Modules)'

.azure-pipelines/validate-pr-beta-modules.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
displayName: 'Generate and Build Graph Resource Modules'
4949
inputs:
5050
filePath: '$(System.DefaultWorkingDirectory)/tools/GenerateModules.ps1'
51-
arguments: '-RepositoryApiKey $(Api_Key) -Build'
51+
arguments: '-RepositoryApiKey $(Api_Key) -Build -Test'
5252
pwsh: true
5353

5454
- task: YodLabs.O365PostMessage.O365PostMessageBuild.O365PostMessageBuild@0

build.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<UpdateAutoRest Condition ="'$(UpdateAutoRest)' == ''">$True</UpdateAutoRest>
3232
<!-- PS command related -->
3333
<PowerShellCoreCommandPrefix>pwsh -NonInteractive -NoLogo -NoProfile -Command</PowerShellCoreCommandPrefix>
34-
<GenerateModules> $(PowerShellCoreCommandPrefix) &quot; $(RepoTools)/GenerateModules.ps1 -Build -SkipVersionCheck:$(SkipVersionCheck) -EnableSigning:$(EnableSigning) -UpdateAutoRest:$(UpdateAutoRest) &quot; </GenerateModules>
34+
<GenerateModules> $(PowerShellCoreCommandPrefix) &quot; $(RepoTools)/GenerateModules.ps1 -Build -Test -SkipVersionCheck:$(SkipVersionCheck) -EnableSigning:$(EnableSigning) -UpdateAutoRest:$(UpdateAutoRest) &quot; </GenerateModules>
3535
<GenerateRollup> $(PowerShellCoreCommandPrefix) &quot; $(RepoTools)/GenerateRollUpModule.ps1 &quot; </GenerateRollup>
3636
<GenerateAuth> $(PowerShellCoreCommandPrefix) &quot; $(RepoTools)/GenerateAuthenticationModule.ps1 -Build -BuildWhenEqual:$(BuildWhenEqual) -EnableSigning:$(EnableSigning) &quot; </GenerateAuth>
3737
<GenerateProfiles> $(PowerShellCoreCommandPrefix) &quot; $(RepoTools)/GenerateProfiles.ps1 &quot; </GenerateProfiles>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
BeforeAll {
2+
$ModuleName = "Microsoft.Graph.Applications"
3+
$ModulePath = Join-Path $PSScriptRoot "..\$ModuleName.psd1"
4+
}
5+
6+
Describe "Applications Module" {
7+
It "Module should be available when imported" {
8+
$LoadedModule = Get-Module -Name $ModuleName
9+
10+
$LoadedModule | Should -Not -Be $null
11+
$LoadedModule.ExportedCommands.Count | Should -Not -Be 0
12+
}
13+
14+
It "Module import should not write to streams when debug preference is not set" {
15+
$ps = [powershell]::Create()
16+
$ps.AddScript("Import-Module $ModulePath").Invoke()
17+
18+
$ps.Streams.Information.Count | Should -Be 0
19+
$ps.Streams.Debug.Count | Should -Be 0
20+
$ps.Streams.Error.Count | Should -Be 0
21+
$ps.Streams.Verbose.Count | Should -Be 0
22+
$ps.Streams.Warning.Count | Should -Be 0
23+
$ps.Streams.Progress.Count | Should -Be 0
24+
25+
$ps.Dispose()
26+
}
27+
28+
It "Module import should write to streams when debug preference is set" {
29+
$ps = [powershell]::Create()
30+
$ps.AddScript("`$DebugPreference = 'Inquire'; Import-Module $ModulePath").Invoke()
31+
32+
$ps.Streams.Information.Count | Should -Be 0
33+
$ps.Streams.Debug.Count | Should -Be 2
34+
$ps.Streams.Error.Count | Should -Be 0
35+
$ps.Streams.Verbose.Count | Should -Be 0
36+
$ps.Streams.Warning.Count | Should -Be 0
37+
$ps.Streams.Progress.Count | Should -Be 0
38+
39+
$ps.Dispose()
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
BeforeAll {
2+
$ModuleName = "Microsoft.Graph.CloudCommunications"
3+
$ModulePath = Join-Path $PSScriptRoot "..\$ModuleName.psd1"
4+
}
5+
6+
Describe "CloudCommunications Module" {
7+
It "Module should be available when imported" {
8+
$LoadedModule = Get-Module -Name $ModuleName
9+
10+
$LoadedModule | Should -Not -Be $null
11+
$LoadedModule.ExportedCommands.Count | Should -Not -Be 0
12+
}
13+
14+
It "Module import should not write to streams when debug preference is not set" {
15+
$ps = [powershell]::Create()
16+
$ps.AddScript("Import-Module $ModulePath").Invoke()
17+
18+
$ps.Streams.Information.Count | Should -Be 0
19+
$ps.Streams.Debug.Count | Should -Be 0
20+
$ps.Streams.Error.Count | Should -Be 0
21+
$ps.Streams.Verbose.Count | Should -Be 0
22+
$ps.Streams.Warning.Count | Should -Be 0
23+
$ps.Streams.Progress.Count | Should -Be 0
24+
25+
$ps.Dispose()
26+
}
27+
28+
It "Module import should write to streams when debug preference is set" {
29+
$ps = [powershell]::Create()
30+
$ps.AddScript("`$DebugPreference = 'Inquire'; Import-Module $ModulePath").Invoke()
31+
32+
$ps.Streams.Information.Count | Should -Be 0
33+
$ps.Streams.Debug.Count | Should -Be 2
34+
$ps.Streams.Error.Count | Should -Be 0
35+
$ps.Streams.Verbose.Count | Should -Be 0
36+
$ps.Streams.Warning.Count | Should -Be 0
37+
$ps.Streams.Progress.Count | Should -Be 0
38+
39+
$ps.Dispose()
40+
}
41+
}

src/Users/Users/test/Users.Tests.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
BeforeAll {
2+
$ModuleName = "Microsoft.Graph.Users"
3+
$ModulePath = Join-Path $PSScriptRoot "..\$ModuleName.psd1"
4+
}
5+
6+
Describe "Users Module" {
7+
It "Module should be available when imported" {
8+
$LoadedModule = Get-Module -Name $ModuleName
9+
10+
$LoadedModule | Should -Not -Be $null
11+
$LoadedModule.ExportedCommands.Count | Should -Not -Be 0
12+
}
13+
14+
It "Module import should not write to streams when debug preference is not set" {
15+
$ps = [powershell]::Create()
16+
$ps.AddScript("Import-Module $ModulePath").Invoke()
17+
18+
$ps.Streams.Information.Count | Should -Be 0
19+
$ps.Streams.Debug.Count | Should -Be 0
20+
$ps.Streams.Error.Count | Should -Be 0
21+
$ps.Streams.Verbose.Count | Should -Be 0
22+
$ps.Streams.Warning.Count | Should -Be 0
23+
$ps.Streams.Progress.Count | Should -Be 0
24+
25+
$ps.Dispose()
26+
}
27+
28+
It "Module import should write to streams when debug preference is set" {
29+
$ps = [powershell]::Create()
30+
$ps.AddScript("`$DebugPreference = 'Inquire'; Import-Module $ModulePath").Invoke()
31+
32+
$ps.Streams.Information.Count | Should -Be 0
33+
$ps.Streams.Debug.Count | Should -Be 2
34+
$ps.Streams.Error.Count | Should -Be 0
35+
$ps.Streams.Verbose.Count | Should -Be 0
36+
$ps.Streams.Warning.Count | Should -Be 0
37+
$ps.Streams.Progress.Count | Should -Be 0
38+
39+
$ps.Dispose()
40+
}
41+
}

tools/GenerateModules.ps1

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Param(
77
[string] $ModuleMappingConfigPath = (Join-Path $PSScriptRoot "..\config\ModulesMapping.jsonc"),
88
[switch] $UpdateAutoRest,
99
[switch] $Build,
10+
[switch] $Test,
1011
[switch] $Pack,
1112
[switch] $Publish,
1213
[switch] $EnableSigning,
@@ -34,6 +35,7 @@ $RequiredGraphModules = @()
3435
# PS Scripts
3536
$ManageGeneratedModulePS1 = Join-Path $PSScriptRoot ".\ManageGeneratedModule.ps1" -Resolve
3637
$BuildModulePS1 = Join-Path $PSScriptRoot ".\BuildModule.ps1" -Resolve
38+
$TestModulePS1 = Join-Path $PSScriptRoot ".\TestModule.ps1" -Resolve
3739
$PackModulePS1 = Join-Path $PSScriptRoot ".\PackModule.ps1" -Resolve
3840
$PublishModulePS1 = Join-Path $PSScriptRoot ".\PublishModule.ps1" -Resolve
3941
$ReadModuleReadMePS1 = Join-Path $PSScriptRoot ".\ReadModuleReadMe.ps1" -Resolve
@@ -54,8 +56,7 @@ if($ModulePreviewNumber -eq -1) {
5456
# Install module locally in order to specify it as a dependency for other modules down the generation pipeline.
5557
# https://stackoverflow.com/questions/46216038/how-do-i-define-requiredmodules-in-a-powershell-module-manifest-psd1.
5658
$ExistingAuthModule = Find-Module "Microsoft.Graph.Authentication" -Repository $RepositoryName -AllowPrerelease:$AllowPreRelease
57-
Write-Warning "Auth Module: $ExistingAuthModule.Name"
58-
Write-Warning "Auth Module: $ExistingAuthModule.Version"
59+
Write-Host -ForegroundColor Green "Auth Module: $($ExistingAuthModule.Name), $($ExistingAuthModule.Version)"
5960
if (!(Get-Module -Name $ExistingAuthModule.Name -ListAvailable)) {
6061
Install-Module $ExistingAuthModule.Name -Repository $RepositoryName -Force -AllowClobber -AllowPrerelease:$AllowPreRelease
6162
}
@@ -83,7 +84,8 @@ $ModuleMapping.Keys | ForEach-Object -ThrottleLimit $ModuleMapping.Keys.Count -P
8384
}
8485

8586
$ModuleName = $_
86-
Write-Warning "Generating $ModuleName"
87+
$FullyQualifiedModuleName = "$using:ModulePrefix.$ModuleName"
88+
Write-Host -ForegroundColor Green "Generating '$FullyQualifiedModuleName' module..."
8789
$ModuleProjectDir = Join-Path $Using:ModulesOutputDir "$ModuleName\$ModuleName"
8890

8991
# Copy AutoRest readme.md config is none exists.
@@ -98,37 +100,36 @@ $ModuleMapping.Keys | ForEach-Object -ThrottleLimit $ModuleMapping.Keys.Count -P
98100
$ModuleVersion = & $Using:ReadModuleReadMePS1 -ReadMePath $ModuleLevelReadMePath -FieldToRead "module-version"
99101
if ($ModuleVersion -eq $null) {
100102
# Module version not set in readme.md.
101-
Write-Error "Version number is not set on $Using:ModulePrefix.$ModuleName module. Please set 'module-version' in $ModuleLevelReadMePath."
103+
Write-Error "Version number is not set on $FullyQualifiedModuleName module. Please set 'module-version' in $ModuleLevelReadMePath."
102104
}
103105

104106
# Validate module version with the one on PSGallery.
105-
[VersionState] $VersionState = & $Using:ValidateUpdatedModuleVersionPS1 -ModuleName "$Using:ModulePrefix.$ModuleName" -NextVersion $ModuleVersion -PSRepository RepositoryName -ModulePreviewNumber $ModulePreviewNumber
107+
[VersionState] $VersionState = & $Using:ValidateUpdatedModuleVersionPS1 -ModuleName "$FullyQualifiedModuleName" -NextVersion $ModuleVersion -PSRepository RepositoryName -ModulePreviewNumber $ModulePreviewNumber
106108

107109
if ($VersionState.Equals([VersionState]::Invalid) -and !$Using:SkipVersionCheck) {
108-
Write-Warning "The specified version in $Using:ModulePrefix.$ModuleName module is either higher or lower than what's on $Using:RepositoryName. Update the 'module-version' in $ModuleLevelReadMePath"
110+
Write-Warning "The specified version in $FullyQualifiedModuleName module is either higher or lower than what's on $Using:RepositoryName. Update the 'module-version' in $ModuleLevelReadMePath"
109111
}
110112
elseif ($VersionState.Equals([VersionState]::EqualToFeed) -and !$SkipVersionCheck) {
111-
Write-Warning "$Using:ModulePrefix.$ModuleName module skipped. Version has not changed and is equal to what's on $Using:RepositoryName."
113+
Write-Warning "$FullyQualifiedModuleName module skipped. Version has not changed and is equal to what's on $Using:RepositoryName."
112114
}
113115
elseif ($VersionState.Equals([VersionState]::Valid) -or $VersionState.Equals([VersionState]::NotOnFeed) -or $Using:SkipVersionCheck) {
114116
# Read release notes from readme.
115117
$ModuleReleaseNotes = & $Using:ReadModuleReadMePS1 -ReadMePath $ModuleLevelReadMePath -FieldToRead "release-notes"
116118
if ($ModuleReleaseNotes -eq $null) {
117119
# Release notes not set in readme.md.
118-
Write-Error "Release notes not set on $Using:ModulePrefix.$ModuleName module. Please set 'release-notes' in $ModuleLevelReadMePath."
120+
Write-Error "Release notes not set on $FullyQualifiedModuleName module. Please set 'release-notes' in $ModuleLevelReadMePath."
119121
}
120122

121123
try {
122124
# Generate PowerShell modules.
123-
Write-Host -ForegroundColor Green "Generating '$Using:ModulePrefix.$ModuleName' module..."
124125
& autorest --module-version:$ModuleVersion --service-name:$ModuleName $ModuleLevelReadMePath --verbose
125126
if ($LASTEXITCODE) {
126127
Write-Error "Failed to generate '$ModuleName' module."
127128
}
128-
Write-Host -ForegroundColor Green "AutoRest generated '$Using:ModulePrefix.$ModuleName' successfully."
129+
Write-Host -ForegroundColor Green "AutoRest generated '$FullyQualifiedModuleName' successfully."
129130

130131
# Manage generated module.
131-
Write-Host -ForegroundColor Green "Managing '$Using:ModulePrefix.$ModuleName' module..."
132+
Write-Host -ForegroundColor Green "Managing '$FullyQualifiedModuleName' module..."
132133
& $Using:ManageGeneratedModulePS1 -Module $ModuleName -ModulePrefix $Using:ModulePrefix
133134

134135
if ($Using:Build) {
@@ -146,12 +147,12 @@ $ModuleMapping.Keys | ForEach-Object -ThrottleLimit $ModuleMapping.Keys.Count -P
146147
$Profiles = Get-ChildItem -Path $ModuleExportsPath -Directory | %{ $_.Name}
147148

148149
# Update module manifest wiht profiles.
149-
$ModuleManifestPath = Join-Path $ModuleProjectDir "$Using:ModulePrefix.$ModuleName.psd1"
150+
$ModuleManifestPath = Join-Path $ModuleProjectDir "$FullyQualifiedModuleName.psd1"
150151
[HashTable]$PrivateData = @{ Profiles = $Profiles }
151152
Update-ModuleManifest -Path $ModuleManifestPath -PrivateData $PrivateData
152153

153154
# Update module psm1 with Graph session profile name.
154-
$ModulePsm1 = Join-Path $ModuleProjectDir "/$Using:ModulePrefix.$ModuleName.psm1"
155+
$ModulePsm1 = Join-Path $ModuleProjectDir "/$FullyQualifiedModuleName.psm1"
155156
(Get-Content -Path $ModulePsm1) | ForEach-Object{
156157
if ($_ -match '\$instance = \[Microsoft.Graph.PowerShell.Module\]::Instance') {
157158
# Update main psm1 with Graph session profile name and module name.
@@ -161,13 +162,15 @@ $ModuleMapping.Keys | ForEach-Object -ThrottleLimit $ModuleMapping.Keys.Count -P
161162
# Rename all Azure instances in psm1 to `Microsoft Graph`.
162163
$updatedLine = $_ -replace 'Azure', 'Microsoft Graph'
163164
# Replace all 'instance.Name' declarations with fully qualified module name.
164-
$updatedLine = $updatedLine -replace '\$\(\$instance.Name\)', "$ModulePrefix.$ModuleName"
165+
$updatedLine = $updatedLine -replace '\$\(\$instance.Name\)', "$FullyQualifiedModuleName"
166+
# Replace Write-Information with Write-Debug
167+
$updatedLine = $updatedLine -replace 'Write\-Information', 'Write-Debug'
165168
$updatedLine
166169
}
167170
} | Set-Content $ModulePsm1
168171

169172
# Address AutoREST bug where it looks for exports in the wrong directory.
170-
$InternalModulePsm1 = Join-Path $ModuleProjectDir "/internal/$Using:ModulePrefix.$ModuleName.internal.psm1"
173+
$InternalModulePsm1 = Join-Path $ModuleProjectDir "/internal/$FullyQualifiedModuleName.internal.psm1"
171174
(Get-Content -Path $InternalModulePsm1) | ForEach-Object{
172175
$updatedLine = $_
173176
# Address AutoREST bug where it looks for exports in the wrong directory.
@@ -188,19 +191,22 @@ $ModuleMapping.Keys | ForEach-Object -ThrottleLimit $ModuleMapping.Keys.Count -P
188191
}
189192
}
190193

194+
if ($Using:Test) {
195+
& $Using:TestModulePS1 -ModulePath $ModuleProjectDir -ModuleName $FullyQualifiedModuleName
196+
}
197+
191198
if ($Using:Pack) {
192199
# Pack generated module.
193-
& $Using:PackModulePS1 -Module $ModuleName -ArtifactsLocation $Using:ArtifactsLocation
200+
. $Using:PackModulePS1 -Module $ModuleName -ArtifactsLocation $Using:ArtifactsLocation
194201
}
195202
}
196203
catch {
197-
Write-Error $_.Exception
204+
throw $_
198205
}
199-
Write-Warning "Generating $ModuleName Completed"
206+
Write-Host -ForeGroundColor Green "Generating $ModuleName Completed"
200207
}
201208
}
202209

203-
Write-Host -ForeGroundColor Green "Requests: $RequestCount"
204210
if ($Publish) {
205211
# Publish generated modules.
206212
& $PublishModulePS1 -Modules $ModuleMapping.Keys -ModulePrefix $ModulePrefix -ArtifactsLocation $ArtifactsLocation -RepositoryName $RepositoryName -RepositoryApiKey $RepositoryApiKey

tools/TestModule.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
param([string] $ModulePath, [string] $ModuleName, [switch]$Isolated)
4+
$ErrorActionPreference = 'Stop'
5+
6+
# Install Pester
7+
if (!(Get-Module -Name Pester -ListAvailable)) {
8+
Install-Module -Name Pester -Force -SkipPublisherCheck
9+
}
10+
11+
if(-not $Isolated) {
12+
Write-Host -ForegroundColor Green 'Creating isolated process...'
13+
$pwsh = [System.Diagnostics.Process]::GetCurrentProcess().Path
14+
& "$pwsh" -NonInteractive -NoLogo -NoProfile -File $MyInvocation.MyCommand.Path @PSBoundParameters -Isolated
15+
return
16+
}
17+
18+
$modulePsd1 = Get-Item -Path (Join-Path $ModulePath "./$ModuleName.psd1")
19+
20+
Import-Module -Name Pester
21+
Import-Module -Name $modulePsd1.FullName
22+
23+
$testFolder = Join-Path $ModulePath 'test'
24+
$PesterConfiguration = [PesterConfiguration]::Default
25+
$PesterConfiguration.Run.Path = $testFolder
26+
$PesterConfiguration.Run.Exit = $true
27+
$PesterConfiguration.TestResult.OutputPath = (Join-Path $testFolder "$moduleName-TestResults.xml")
28+
29+
try {
30+
Invoke-Pester -Configuration $PesterConfiguration
31+
}
32+
catch {
33+
throw $_
34+
}
35+
36+
Write-Host -ForegroundColor Green '-------------Done-------------'

0 commit comments

Comments
 (0)