Skip to content

Commit 884e440

Browse files
committed
Enable unit tests with pester
1 parent cf7a159 commit 884e440

File tree

9 files changed

+173
-8
lines changed

9 files changed

+173
-8
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: 9 additions & 4 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
@@ -189,19 +191,22 @@ $ModuleMapping.Keys | ForEach-Object -ThrottleLimit $ModuleMapping.Keys.Count -P
189191
}
190192
}
191193

194+
if ($Using:Test) {
195+
& $Using:TestModulePS1 -ModulePath $ModuleProjectDir -ModuleName $FullyQualifiedModuleName
196+
}
197+
192198
if ($Using:Pack) {
193199
# Pack generated module.
194-
& $Using:PackModulePS1 -Module $ModuleName -ArtifactsLocation $Using:ArtifactsLocation
200+
. $Using:PackModulePS1 -Module $ModuleName -ArtifactsLocation $Using:ArtifactsLocation
195201
}
196202
}
197203
catch {
198-
Write-Error $_.Exception
204+
throw $_
199205
}
200-
Write-Warning "Generating $ModuleName Completed"
206+
Write-Host -ForeGroundColor Green "Generating $ModuleName Completed"
201207
}
202208
}
203209

204-
Write-Host -ForeGroundColor Green "Requests: $RequestCount"
205210
if ($Publish) {
206211
# Publish generated modules.
207212
& $PublishModulePS1 -Modules $ModuleMapping.Keys -ModulePrefix $ModulePrefix -ArtifactsLocation $ArtifactsLocation -RepositoryName $RepositoryName -RepositoryApiKey $RepositoryApiKey

tools/TestModule.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
Write-Host ("$testFolder")
25+
$PesterConfiguration = [PesterConfiguration]::Default
26+
$PesterConfiguration.Run.Path = $testFolder
27+
$PesterConfiguration.Run.Exit = $true
28+
$PesterConfiguration.TestResult.OutputPath = (Join-Path $testFolder "$moduleName-TestResults.xml")
29+
30+
try {
31+
Invoke-Pester -Configuration $PesterConfiguration
32+
}
33+
catch {
34+
throw $_
35+
}
36+
37+
Write-Host -ForegroundColor Green '-------------Done-------------'

0 commit comments

Comments
 (0)