Skip to content

Commit 2d1c3e4

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

File tree

3 files changed

+35
-55
lines changed

3 files changed

+35
-55
lines changed

test/PSDesiredStateConfiguration.Tests.ps1

Lines changed: 20 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 modules in Azure DevOps tests"
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 {
@@ -419,15 +383,15 @@ Describe "Test PSDesiredStateConfiguration" {
419383
}
420384

421385
it "Test method should return true with moduleSpecification" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
422-
$module = get-module PsDscResources -ListAvailable
386+
$module = get-module PSDscResources -ListAvailable
423387
$moduleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() }
424388
$result = Invoke-DscResource -Name Script -ModuleName $moduleSpecification -Method Test -Property @{TestScript = { Write-Verbose 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } }
425389
$result | Should -BeTrue -Because "Test method return true"
426390
}
427391

428392
it "Invalid moduleSpecification" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
429393
Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/17"
430-
$moduleSpecification = @{ModuleName = 'PsDscResources'; ModuleVersion = '99.99.99.993' }
394+
$moduleSpecification = @{ModuleName = 'PSDscResources'; ModuleVersion = '99.99.99.993' }
431395
{
432396
Invoke-DscResource -Name Script -ModuleName $moduleSpecification -Method Test -Property @{TestScript = { Write-Host 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } -ErrorAction Stop
433397
} |
@@ -439,11 +403,15 @@ Describe "Test PSDesiredStateConfiguration" {
439403
Set-ItResult -Skipped -Because "Feature not enabled"
440404
}
441405

406+
if ($env:TF_BUILD -eq 'true') {
407+
Set-ItResult -Skipped -Because "Cannot install modules in Azure DevOps tests"
408+
}
409+
442410
$resourceName="TestRes"
443411
$moduleName="TestEmbeddedDSCResource"
444412
$embObj = @(New-Object -TypeName psobject -Property @{embclassprop="property1"})
445413

446-
Install-ModuleIfMissing -Name $moduleName -Force
414+
Install-Module -Name $moduleName -Force
447415

448416
$resource = Get-DscResource -Name $resourceName -Module $moduleName -ErrorAction Stop
449417
$resource | Should -Not -BeNullOrEmpty
@@ -495,17 +463,21 @@ Describe "Test PSDesiredStateConfiguration" {
495463
}
496464

497465
it "Get method should work" -Skip:(!(Test-IsInvokeDscResourceEnable)) {
466+
if ($env:TF_BUILD -eq 'true') {
467+
Set-ItResult -Skipped -Because "Cannot install modules in Azure DevOps tests"
468+
}
469+
498470
if (-not $IsWindows) {
499471
Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/12 and https://github.com/PowerShell/PowerShellGet/pull/529"
500472
}
501473

502-
$result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method Get -Property @{ Name = 'PsDscResources' }
474+
$result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method Get -Property @{ Name = 'PSDscResources' }
503475
$result | Should -Not -BeNullOrEmpty
504476
$result.Author | Should -BeLike 'Microsoft*'
505477
$result.InstallationPolicy | Should -BeOfType [string]
506478
$result.Guid | Should -BeOfType [Guid]
507479
$result.Ensure | Should -Be 'Present'
508-
$result.Name | Should -be 'PsDscResources'
480+
$result.Name | Should -be 'PSDscResources'
509481
$result.Description | Should -BeLike 'This*DSC*'
510482
$result.InstalledVersion | should -BeOfType [Version]
511483
$result.ModuleBase | Should -BeLike '*PSDscResources*'
@@ -515,10 +487,6 @@ Describe "Test PSDesiredStateConfiguration" {
515487
}
516488

517489
Context "Class Based Resources" {
518-
BeforeAll {
519-
Install-ModuleIfMissing -Name XmlContentDsc -Force
520-
}
521-
522490
AfterAll {
523491
$Global:ProgressPreference = $origProgress
524492
}

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)