Skip to content

Commit c73a5a7

Browse files
committed
Move installation of test modules to bootstrapping
So we can get this working on OneBranch.
1 parent 8d7e430 commit c73a5a7

File tree

3 files changed

+47
-55
lines changed

3 files changed

+47
-55
lines changed

test/PSDesiredStateConfiguration.Tests.ps1

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,6 @@
22
# Licensed under the MIT License.
33

44
BeforeDiscovery {
5-
Function global:Install-ModuleIfMissing {
6-
param(
7-
[parameter(Mandatory)]
8-
[String]
9-
$Name,
10-
[version]
11-
$MinimumVersion,
12-
[switch]
13-
$SkipPublisherCheck,
14-
[switch]
15-
$Force
16-
)
17-
18-
$module = Get-Module -Name $Name -ListAvailable -ErrorAction Ignore | Sort-Object -Property Version -Descending | Select-Object -First 1
19-
20-
if (!$module -or $module.Version -lt $MinimumVersion) {
21-
Write-Verbose "Installing module '$Name' ..." -Verbose
22-
Install-Module -Name $Name -Force -SkipPublisherCheck:$SkipPublisherCheck.IsPresent
23-
}
24-
}
25-
265
Function global:Test-IsInvokeDscResourceEnable {
276
return ($PSVersionTable.PSVersion.Major -ge 7)
287
}
@@ -87,7 +66,6 @@ Describe "Test PSDesiredStateConfiguration" {
8766
BeforeAll {
8867
$origProgress = $global:ProgressPreference
8968
$global:ProgressPreference = 'SilentlyContinue'
90-
Install-ModuleIfMissing -Name PSDscResources
9169
$testCases = @(
9270
@{
9371
TestCaseName = 'case mismatch in resource name'
@@ -162,10 +140,6 @@ Describe "Test PSDesiredStateConfiguration" {
162140
$origProgress = $global:ProgressPreference
163141
$global:ProgressPreference = 'SilentlyContinue'
164142

165-
Install-ModuleIfMissing -Name PSDscResources -Force
166-
167-
# Install PowerShellGet only if PowerShellGet 2.2.1 or newer does not exist
168-
Install-ModuleIfMissing -Name PowerShellGet -MinimumVersion '2.2.1'
169143
$module = Get-Module PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
170144

171145
$psGetModuleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() }
@@ -273,7 +247,6 @@ Describe "Test PSDesiredStateConfiguration" {
273247
BeforeAll {
274248
$origProgress = $global:ProgressPreference
275249
$global:ProgressPreference = 'SilentlyContinue'
276-
Install-ModuleIfMissing -Name XmlContentDsc -Force
277250
$classTestCases = @(
278251
@{
279252
TestCaseName = 'Good case'
@@ -340,11 +313,6 @@ Describe "Test PSDesiredStateConfiguration" {
340313
BeforeAll {
341314
$origProgress = $global:ProgressPreference
342315
$global:ProgressPreference = 'SilentlyContinue'
343-
$module = Get-InstalledModule -Name PsDscResources -ErrorAction Ignore
344-
if ($module) {
345-
Write-Verbose "removing PSDscResources, tests will re-install..." -Verbose
346-
Uninstall-Module -Name PsDscResources -AllVersions -Force
347-
}
348316
}
349317

350318
AfterAll {
@@ -372,26 +340,22 @@ Describe "Test PSDesiredStateConfiguration" {
372340
}
373341
)
374342

375-
Install-ModuleIfMissing -Name PowerShellGet -Force -SkipPublisherCheck -MinimumVersion '2.2.1'
376-
Install-ModuleIfMissing -Name xWebAdministration
377343
$module = Get-Module PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
378344

379345
$psGetModuleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() }
380346
}
381347
it "Set method should work" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
382-
if (!-not $IsWindows) {
383-
$result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method set -Property @{
384-
Name = 'PsDscResources'
385-
InstallationPolicy = 'Trusted'
386-
}
348+
if ($env:TF_BUILD -eq 'true') {
349+
Set-ItResult -Skipped -Because "cannot install PSDscResources module during tests in Azure DevOps"
387350
}
388-
else {
389-
# workraound because of https://github.com/PowerShell/PowerShellGet/pull/529
390-
Install-ModuleIfMissing -Name PsDscResources -Force
351+
352+
$result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method set -Property @{
353+
Name = 'PSDscResources'
354+
InstallationPolicy = 'Trusted'
391355
}
392356

393357
$result.RebootRequired | Should -BeFalse
394-
$module = Get-module PsDscResources -ListAvailable
358+
$module = Get-module PSDscResources -ListAvailable
395359
$module | Should -Not -BeNullOrEmpty -Because "Resource should have installed module"
396360
}
397361
it 'Set method should return RebootRequired=<expectedResult> when $global:DSCMachineStatus = <value>' -Skip:(!(Test-IsInvokeDscResourceEnable)) -TestCases $dscMachineStatusCases {
@@ -408,26 +372,38 @@ Describe "Test PSDesiredStateConfiguration" {
408372
}
409373

410374
it "Test method should return false" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
375+
if ($env:TF_BUILD -eq 'true') {
376+
Set-ItResult -Skipped -Because "cannot install PSDscResources module during tests in Azure DevOps"
377+
}
378+
411379
$result = Invoke-DscResource -Name Script -ModuleName PSDscResources -Method Test -Property @{TestScript = { Write-Output 'test'; return $false }; GetScript = { return @{ } }; SetScript = { return } }
412380
$result | Should -Not -BeNullOrEmpty
413381
$result.InDesiredState | Should -BeFalse -Because "Test method return false"
414382
}
415383

416384
it "Test method should return true" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
385+
if ($env:TF_BUILD -eq 'true') {
386+
Set-ItResult -Skipped -Because "cannot install PSDscResources module during tests in Azure DevOps"
387+
}
388+
417389
$result = Invoke-DscResource -Name Script -ModuleName PSDscResources -Method Test -Property @{TestScript = { Write-Verbose 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } }
418390
$result | Should -BeTrue -Because "Test method return true"
419391
}
420392

421393
it "Test method should return true with moduleSpecification" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
422-
$module = get-module PsDscResources -ListAvailable
394+
if ($env:TF_BUILD -eq 'true') {
395+
Set-ItResult -Skipped -Because "cannot install PSDscResources module during tests in Azure DevOps"
396+
}
397+
398+
$module = get-module PSDscResources -ListAvailable
423399
$moduleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() }
424400
$result = Invoke-DscResource -Name Script -ModuleName $moduleSpecification -Method Test -Property @{TestScript = { Write-Verbose 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } }
425401
$result | Should -BeTrue -Because "Test method return true"
426402
}
427403

428404
it "Invalid moduleSpecification" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
429405
Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/17"
430-
$moduleSpecification = @{ModuleName = 'PsDscResources'; ModuleVersion = '99.99.99.993' }
406+
$moduleSpecification = @{ModuleName = 'PSDscResources'; ModuleVersion = '99.99.99.993' }
431407
{
432408
Invoke-DscResource -Name Script -ModuleName $moduleSpecification -Method Test -Property @{TestScript = { Write-Host 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } -ErrorAction Stop
433409
} |
@@ -439,11 +415,15 @@ Describe "Test PSDesiredStateConfiguration" {
439415
Set-ItResult -Skipped -Because "Feature not enabled"
440416
}
441417

418+
if ($env:TF_BUILD -eq 'true') {
419+
Set-ItResult -Skipped -Because "cannot install PSDscResources module during tests in Azure DevOps"
420+
}
421+
442422
$resourceName="TestRes"
443423
$moduleName="TestEmbeddedDSCResource"
444424
$embObj = @(New-Object -TypeName psobject -Property @{embclassprop="property1"})
445425

446-
Install-ModuleIfMissing -Name $moduleName -Force
426+
Install-Module -Name $moduleName -Force
447427

448428
$resource = Get-DscResource -Name $resourceName -Module $moduleName -ErrorAction Stop
449429
$resource | Should -Not -BeNullOrEmpty
@@ -495,17 +475,21 @@ Describe "Test PSDesiredStateConfiguration" {
495475
}
496476

497477
it "Get method should work" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
478+
if ($env:TF_BUILD -eq 'true') {
479+
Set-ItResult -Skipped -Because "cannot install PSDscResources module during tests in Azure DevOps"
480+
}
481+
498482
if (-not $IsWindows) {
499483
Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/12 and https://github.com/PowerShell/PowerShellGet/pull/529"
500484
}
501485

502-
$result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method Get -Property @{ Name = 'PsDscResources' }
486+
$result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method Get -Property @{ Name = 'PSDscResources' }
503487
$result | Should -Not -BeNullOrEmpty
504488
$result.Author | Should -BeLike 'Microsoft*'
505489
$result.InstallationPolicy | Should -BeOfType [string]
506490
$result.Guid | Should -BeOfType [Guid]
507491
$result.Ensure | Should -Be 'Present'
508-
$result.Name | Should -be 'PsDscResources'
492+
$result.Name | Should -be 'PSDscResources'
509493
$result.Description | Should -BeLike 'This*DSC*'
510494
$result.InstalledVersion | should -BeOfType [Version]
511495
$result.ModuleBase | Should -BeLike '*PSDscResources*'
@@ -515,10 +499,6 @@ Describe "Test PSDesiredStateConfiguration" {
515499
}
516500

517501
Context "Class Based Resources" {
518-
BeforeAll {
519-
Install-ModuleIfMissing -Name XmlContentDsc -Force
520-
}
521-
522502
AfterAll {
523503
$Global:ProgressPreference = $origProgress
524504
}

test/configuration.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Describe "DSC MOF Compilation" {
88
}
99

1010
It "Should be able to compile a MOF using PSModule resource" {
11-
if ($env:GITHUB_ACTIONS -eq 'true') {
12-
Set-ItResult -Skipped -Because "Running in GitHub Actions"
11+
if ($env:GITHUB_ACTIONS -eq 'true' -or $env:TF_BUILD -eq 'true') {
12+
Set-ItResult -Skipped -Because "does not work in CI"
1313
}
1414

1515
if (-not $IsWindows) {
@@ -24,7 +24,7 @@ Describe "DSC MOF Compilation" {
2424
Node "localhost" {
2525
PSModule f1
2626
{
27-
Name = 'PsDscResources'
27+
Name = 'PSDscResources'
2828
InstallationPolicy = 'Trusted'
2929
}
3030
}

tools/installPSResources.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,16 @@ Install-PSResource -Verbose -TrustRepository -RequiredResource @{
2121
version = "5.7.1"
2222
repository = $PSRepository
2323
}
24+
PSDscResources = @{
25+
version = "2.12.0.0"
26+
repository = $PSRepository
27+
}
28+
XmlContentDsc = @{
29+
version = "0.0.1"
30+
repository = $PSRepository
31+
}
32+
xWebAdministration = @{
33+
version = "3.3.0"
34+
repository = $PSRepository
35+
}
2436
}

0 commit comments

Comments
 (0)