tests/unit/xRemoteDesktopSessionHost.tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<#
.Synopsis
   Tests for common module for xRemoteDesktopSessionHost
.DESCRIPTION
   Tests for common module for xRemoteDesktopSessionHost
 
.NOTES
   Code in HEADER and FOOTER regions are standard and may be moved into DSCResource.Tools in
   Future and therefore should not be altered if possible.
#>




#region HEADER
[String] $moduleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path))
if ( (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
     (-not (Test-Path -Path (Join-Path -Path $moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'))
}
else
{
    & git @('-C',(Join-Path -Path $moduleRoot -ChildPath '\DSCResource.Tests\'),'pull')
}

Import-module "$moduleRoot\xRemoteDesktopSessionHostCommon.psm1" -force
Write-Verbose -Message "$moduleRoot\DscResources\*.psm1" -Verbose
$global:resourceModules = Get-ChildItem -Path "$moduleRoot\DscResources\*.psm1" -Recurse
Write-Verbose -Message "$($resourceModules.Count)" -Verbose 
#endregion

# TODO: Other Optional Init Code Goes Here...

# Begin Testing
try
{

    #region Pester Tests

    # The InModuleScope command allows you to perform white-box unit testing on the internal
    # (non-exported) code of a Script Module.
    InModuleScope xRemoteDesktopSessionHostCommon {

        #region Pester Test Initialization
            
        #endregion


        #region Function Test-xRemoteDesktopSessionHostOsRequirement
        Describe "Test-xRemoteDesktopSessionHostOsRequirement" {
            Context 'Windows 10' {
                Mock Get-OsVersion -MockWith {return (new-object 'Version' 10,1,1,1)}
                it 'Should return true' {
                    Test-xRemoteDesktopSessionHostOsRequirement | should be $true
                }
            }
            Context 'Windows 8.1' {
                Mock Get-OsVersion -MockWith {return (new-object 'Version' 6,3,1,1)}
                it 'Should return true' {
                    Test-xRemoteDesktopSessionHostOsRequirement | should be $true
                }
            }
            Context 'Windows 8' {
                Mock Get-OsVersion -MockWith {return (new-object 'Version' 6,2,9200,0)}
                it 'Should return true' {
                    Test-xRemoteDesktopSessionHostOsRequirement | should be $true
                }
            }
            Context 'Windows 7' {
                Mock Get-OsVersion -MockWith {return (new-object 'Version' 6,1,1,0)}
                it 'Should return false' {
                    Test-xRemoteDesktopSessionHostOsRequirement | should be $false
                }
            }
        }
        #endregion


        # TODO: Pester Tests for any Helper Cmdlets

    }
    Describe "Test-xRemoteDesktopSessionHostOsRequirement use in modules" {
            Import-module "$moduleRoot\xRemoteDesktopSessionHost.psd1" -force
            Context 'Loading resource modules on Windows 10' {
                Mock Get-OsVersion -MockWith {return (new-object 'Version' 10,1,1,1)} -ModuleName xRemoteDesktopSessionHost
                foreach($resourceModule in $global:resourceModules)
                {
                    # The resource does not check if the remote desktop module existis before it loads it
                    # so this always fails. Pending this test for this issue
                    # https://github.com/PowerShell/xRemoteDesktopSessionHost/issues/6
                    it "$($resourceModule.Name) should not throw when imported" -Pending {
                        try {
                            $Error.Clear()
                            import-module $resourceModule.FullName -force -ErrorAction stop -ErrorVariable ImportVariable
                        }
                        catch {
                            Write-Verbose -Message 'in catch' -Verbose
                            $_ | should be $null
                        }
                        $Error.Count | should be 0
                    }
                }
            }            
    }
    #endregion
}
finally
{
    #region FOOTER
    #endregion

    # TODO: Other Optional Cleanup Code Goes Here...
}