Tests/ITGlueAPI.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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#Requires -Modules Pester # Obtain name of this module by parsing name of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1) $ThisModule = $PSCommandPath -replace '\.Tests\.ps1$' $ThisModuleName = $ThisModule | Split-Path -Leaf # Obtain path of the module based on location of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1) $ThisModulePath = Split-Path (Split-Path -Parent $PSCommandPath) -Parent # Make sure one or multiple versions of the module are not loaded Get-Module -Name $ThisModuleName | Remove-Module # Credit - borrowed with care from http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-manifest-file.html and modified as needed # Manifest file path $ManifestFile = "$ThisModulePath\$ThisModuleName.psd1" # Import the module and store the information about the module $ModuleInformation = Import-module -Name $ManifestFile -PassThru # Internal Files $InternalDirectoryFiles = ( 'APIKey.ps1', 'BaseURI.ps1', 'ModuleSettings.ps1' ) # Resource Files $ResourceDirectoryFiles = ( 'Attachments.ps1', 'ConfigurationInterfaces.ps1', 'Configurations.ps1', 'ConfigurationStatuses.ps1', 'ConfigurationTypes.ps1', 'Contacts.ps1', 'ContactTypes.ps1', 'Countries.ps1', 'Documents.ps1', 'Domains.ps1', 'Expirations.ps1', 'FlexibleAssetFields.ps1', 'FlexibleAssets.ps1', 'FlexibleAssetTypes.ps1', 'Groups.ps1', 'Locations.ps1', 'Logs.ps1', 'Manufacturers.ps1', 'Models.ps1', 'OperatingSystems.ps1', 'Organizations.ps1', 'OrganizationStatuses.ps1', 'OrganizationTypes.ps1', 'PasswordCategories.ps1', 'Passwords.ps1', 'Platforms.ps1', 'Regions.ps1', 'UserMetrics.ps1', 'Users.ps1' ) # Manifest Elements $ManifestFileElements = ( 'RootModule', 'Author', 'CompanyName', 'Description', 'Copyright', 'PowerShellVersion', 'NestedModules', 'HelpInfoURI' ) Describe "Module Tests" { Context "Test $ThisModuleName Module" { It "has the root module $ThisModuleName.psm1" { "$ThisModulePath\$ThisModuleName.psm1" | Should Exist } Context "Test Manifest File (.psd1)"{ It "Should pass Test-ModuleManifest" { $errors = $null $errors = Test-ModuleManifest -Path $ThisModulePath\$ThisModuleName.psd1 -ErrorAction Stop $errors.Count | Should Be 1 } # Credit - borrowed with care from http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-manifest-file.html and modified as needed ForEach ($ManifestFileElement in $ManifestFileElements) { It "Should contains $ManifestFileElement"{ $ModuleInformation.$ManifestFileElement | Should not BeNullOrEmpty } } } It "$ThisModuleName\Resources directory has functions" { "$ThisModulePath\Resources\*.ps1" | Should Exist } # TODO - Only checking one file currently It "$ThisModuleName is valid PowerShell code" { $psFile = Get-Content -Path "$ThisModulePath\$ThisModuleName.psm1" -ErrorAction Stop $errors = $null $null = [System.Management.Automation.PSParser]::Tokenize($psfile, [ref]$errors) $errors.Count | Should Be 0 } } # Check that Internal files exist ForEach ($InternalFile in $InternalDirectoryFiles) { Context "Test $InternalFile Internal File in .\Internal directory" { It "$InternalFile should exist" { "$ThisModulePath\Internal\$InternalFile" | Should Exist } It "$InternalFile is valid PowerShell code" { $psFile = Get-Content -Path "$ThisModulePath\Internal\$InternalFile" -ErrorAction Stop $errors = $null $null = [System.Management.Automation.PSParser]::Tokenize($psfile, [ref]$errors) $errors.Count | Should Be 0 } # Test for test files $InternalFileTest = $InternalFile -replace '\.ps1$' It "$InternalFileTest.Tests.ps1 should exist" { "$InternalFileTest.Tests.ps1" | Should Exist } } } # Check that Resource files exist ForEach ($ResourceFile in $ResourceDirectoryFiles) { Context "Test $ResourceFile Resource File in .\Resources directory" { It "$ResourceFile should exist" { "$ThisModulePath\Resources\$ResourceFile" | Should Exist } It "$ResourceFile is valid PowerShell code" { $psFile = Get-Content -Path "$ThisModulePath\Resources\$ResourceFile" -ErrorAction Stop $errors = $null $null = [System.Management.Automation.PSParser]::Tokenize($psfile, [ref]$errors) $errors.Count | Should Be 0 } } # TODO - add tests to check for tests files } Context "PowerShell $ThisModuleName Import Test" { # Credit - borrowed with care from https://github.com/TheMattCollins0/MattTools/blob/master/Tests/ModuleImport.Tests.ps1 and modified as needed It "Should import PowerShell $ThisModuleName successfully" { Import-Module -Name $ThisModulePath -ErrorVariable ImportError $ImportError | Should Be $null } } } |