diff --git a/Tests/PSDepend.Tests.ps1 b/Tests/PSDepend.Tests.ps1 index c8b82eb..19b61b2 100644 --- a/Tests/PSDepend.Tests.ps1 +++ b/Tests/PSDepend.Tests.ps1 @@ -5,96 +5,101 @@ if(-not $ENV:BHProjectPath) Remove-Module $ENV:BHProjectName -ErrorAction SilentlyContinue Import-Module (Join-Path $ENV:BHProjectPath $ENV:BHProjectName) -Force -$PSVersion = $PSVersionTable.PSVersion.Major +BeforeAll { + $PSVersion = $PSVersionTable.PSVersion.Major -# Verbose output for non-master builds on appveyor -# Handy for troubleshooting. -# Splat @Verbose against commands as needed (here or in pester tests) - $Verbose = @{} - if($ENV:BHBranchName -notlike "master" -or $env:BHCommitMessage -match "!verbose") - { - $Verbose.add("Verbose",$True) - } + # Verbose output for non-master builds on appveyor + # Handy for troubleshooting. + # Splat @Verbose against commands as needed (here or in pester tests) + $Verbose = @{} + if($ENV:BHBranchName -notlike "master" -or $env:BHCommitMessage -match "!verbose") + { + $Verbose.add("Verbose",$True) + } -$TestDepends = Join-Path $ENV:BHProjectPath Tests\DependFiles + $TestDepends = Join-Path $ENV:BHProjectPath Tests\DependFiles +} Describe "$ENV:BHProjectName PS$PSVersion" { Context 'Strict mode' { - - Set-StrictMode -Version latest + BeforeAll { + Set-StrictMode -Version latest + } It 'Should load' { $Module = Get-Module $ENV:BHProjectName - $Module.Name | Should be $ENV:BHProjectName - $Module.ExportedFunctions.Keys -contains 'Get-Dependency' | Should be $True + $Module.Name | Should -Be $ENV:BHProjectName + $Module.ExportedFunctions.Keys -contains 'Get-Dependency' | Should -Be $True } } } Describe "Get-Dependency PS$PSVersion" { Context 'Strict mode' { - Set-StrictMode -Version latest + BeforeAll { + Set-StrictMode -Version latest + } It 'Should read ModuleName=Version syntax' { $Dependencies = Get-Dependency -Path $TestDepends\simple.depend.psd1 - $Dependencies.Count | Should be 4 - @( $Dependencies.DependencyType -like 'PSGalleryModule' ).count | Should be 4 - @( $Dependencies | Where {$_.Name -like $_.DependencyName} ).count | Should be 4 + $Dependencies.Count | Should -Be 4 + @( $Dependencies.DependencyType -like 'PSGalleryModule' ).count | Should -Be 4 + @( $Dependencies | Where {$_.Name -like $_.DependencyName} ).count | Should -Be 4 } It 'Should read DependencyType::DependencyName=Version syntax' { $Dependencies = Get-Dependency -Path $TestDepends\simple.helpers.depend.psd1 - $Dependencies.Count | Should be 2 - @( $Dependencies.DependencyType -like 'PSGalleryModule' ).count | Should be 1 - @( $Dependencies.DependencyType -like 'GitHub' ).count | Should be 1 - @( $Dependencies | Where {$_.Name -like $_.DependencyName} ).count | Should be 2 + $Dependencies.Count | Should -Be 2 + @( $Dependencies.DependencyType -like 'PSGalleryModule' ).count | Should -Be 1 + @( $Dependencies.DependencyType -like 'GitHub' ).count | Should -Be 1 + @( $Dependencies | Where {$_.Name -like $_.DependencyName} ).count | Should -Be 2 } It 'Should read each property correctly' { $Dependencies = Get-Dependency -Path $TestDepends\allprops.depend.psd1 - @( $Dependencies ).count | Should Be 1 - $Dependencies.DependencyName | Should be 'DependencyName' - $Dependencies.Name | Should be 'Name' - $Dependencies.Version | Should be 'Version' - $Dependencies.DependencyType | Should be 'noop' - $Dependencies.Parameters.ContainsKey('Random') | Should Be $True - $Dependencies.Parameters['Random'] | Should be 'Value' - $Dependencies.Source | Should be 'Source' - $Dependencies.Target | Should be 'Target' - $Dependencies.AddToPath | Should be $True - $Dependencies.Tags.Count | SHould Be 2 - $Dependencies.Tags -contains 'tags' | Should be $True - $Dependencies.DependsOn | Should be 'DependsOn' - $Dependencies.PreScripts | Should be 'C:\PreScripts.ps1' - $Dependencies.PostScripts | Should be 'C:\PostScripts.ps1' - $Dependencies.Raw.ContainsKey('ExtendedSchema') | Should be $True - $Dependencies.Raw.ExtendedSchema['IsTotally'] | Should Be 'Captured' + @( $Dependencies ).count | Should -Be 1 + $Dependencies.DependencyName | Should -Be 'DependencyName' + $Dependencies.Name | Should -Be 'Name' + $Dependencies.Version | Should -Be 'Version' + $Dependencies.DependencyType | Should -Be 'noop' + $Dependencies.Parameters.ContainsKey('Random') | Should -Be $True + $Dependencies.Parameters['Random'] | Should -Be 'Value' + $Dependencies.Source | Should -Be 'Source' + $Dependencies.Target | Should -Be 'Target' + $Dependencies.AddToPath | Should -Be $True + $Dependencies.Tags.Count | Should -Be 2 + $Dependencies.Tags -contains 'tags' | Should -Be $True + $Dependencies.DependsOn | Should -Be 'DependsOn' + $Dependencies.PreScripts | Should -Be 'C:\PreScripts.ps1' + $Dependencies.PostScripts | Should -Be 'C:\PostScripts.ps1' + $Dependencies.Raw.ContainsKey('ExtendedSchema') | Should -Be $True + $Dependencies.Raw.ExtendedSchema['IsTotally'] | Should -Be 'Captured' } It 'Should handle DependsOn' { $Dependencies = Get-Dependency -Path $TestDepends\dependson.depend.psd1 - @( $Dependencies ).count | Should Be 3 - $Dependencies[0].DependencyName | Should be 'One' - $Dependencies[1].DependencyName | Should be 'Two' - $Dependencies[2].DependencyName | Should be 'THree' + @( $Dependencies ).count | Should -Be 3 + $Dependencies[0].DependencyName | Should -Be 'One' + $Dependencies[1].DependencyName | Should -Be 'Two' + $Dependencies[2].DependencyName | Should -Be 'THree' } It 'Should inject variables' { $Dependencies = Get-Dependency -Path $TestDepends\inject.variables.depend.psd1 $DependencyFolder = Split-Path $Dependencies.DependencyFile -Parent - $Dependencies.Source | Should Be "PWD=$($PWD.Path)" - $Dependencies.Target | Should Be "$($PWD.Path)\Dependencies;$DependencyFolder" + $Dependencies.Source | Should -Be "PWD=$($PWD.Path)" + $Dependencies.Target | Should -Be "$($PWD.Path)\Dependencies;$DependencyFolder" } It 'Should not mangle dependencies if multiple PSGallery modules specified' { $Dependencies = Get-Dependency -Path $TestDepends\multiplepsgallerymodule.depend.psd1 - $Dependencies.Count | Should be 3 - $Dependencies[0].Version | Should BeNullOrEmpty - $Dependencies[1].Version | Should BeNullOrEmpty - $Dependencies[2].Version | Should BeNullOrEmpty - @($Dependencies[0].Tags) -contains 'prd' | Should Be $True - @($Dependencies[1].Tags) -contains 'prd' | Should Be $True - @($Dependencies[2].Tags) -contains 'prd' | Should Be $True + $Dependencies.Count | Should -Be 3 + $Dependencies[0].Version | Should -BeNullOrEmpty + $Dependencies[1].Version | Should -BeNullOrEmpty + $Dependencies[2].Version | Should -BeNullOrEmpty + @($Dependencies[0].Tags) -contains 'prd' | Should -Be $True + @($Dependencies[1].Tags) -contains 'prd' | Should -Be $True + @($Dependencies[2].Tags) -contains 'prd' | Should -Be $True } } } \ No newline at end of file diff --git a/Tests/PSModuleGallery.Type.Tests.ps1 b/Tests/PSModuleGallery.Type.Tests.ps1 index a524aea..c349224 100644 --- a/Tests/PSModuleGallery.Type.Tests.ps1 +++ b/Tests/PSModuleGallery.Type.Tests.ps1 @@ -10,118 +10,147 @@ Import-Module (Join-Path $ENV:BHProjectPath $ENV:BHProjectName) -Force InModuleScope ('{0}' -f $ENV:BHProjectName) { - $TestDepends = Join-Path $ENV:BHProjectPath "Tests/DependFiles" - $PSVersion = $PSVersionTable.PSVersion.Major - $ProjectRoot = $ENV:BHProjectPath - $ExistingPSModulePath = $env:PSModulePath.PSObject.Copy() - $ExistingPath = $env:PATH.PSObject.Copy() - - $Password = 'testPassword' | ConvertTo-SecureString -AsPlainText -Force - $TestCredential = New-Object System.Management.Automation.PSCredential('testUser', $Password) - $OtherCredential = New-Object System.Management.Automation.PSCredential('otherUser', $Password) - $Credentials = @{ - 'imaginaryCreds' = $TestCredential - 'otherCreds' = $OtherCredential - } + BeforeAll { + + $TestDepends = Join-Path $ENV:BHProjectPath "Tests/DependFiles" + $PSVersion = $PSVersionTable.PSVersion.Major + $ProjectRoot = $ENV:BHProjectPath + $ExistingPSModulePath = $env:PSModulePath.PSObject.Copy() + $ExistingPath = $env:PATH.PSObject.Copy() + + $Password = 'testPassword' | ConvertTo-SecureString -AsPlainText -Force + $TestCredential = New-Object System.Management.Automation.PSCredential('testUser', $Password) + $OtherCredential = New-Object System.Management.Automation.PSCredential('otherUser', $Password) + $Credentials = @{ + 'imaginaryCreds' = $TestCredential + 'otherCreds' = $OtherCredential + } - $Verbose = @{} - if($ENV:BHBranchName -notlike "master" -or $env:BHCommitMessage -match "!verbose") - { - $Verbose.add("Verbose",$True) + $Verbose = @{} + if($ENV:BHBranchName -notlike "master" -or $env:BHCommitMessage -match "!verbose") + { + $Verbose.add("Verbose",$True) + } } Describe "PSGalleryModule Type PS$PSVersion" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } Context 'Installs Modules' { - Mock Install-Module { Return $true } + BeforeAll { + Mock Install-Module { Return $true } + } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.depend.psd1" -Force + BeforeEach { + $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.depend.psd1" -Force + } It 'Should execute Install-Module' { Assert-MockCalled Install-Module -Times 1 -Exactly } It 'Should Return Mocked output' { - $Results | Should be $True + $Results | Should -Be $True } } Context 'Installs Modules with credentials' { - Mock Install-Module { Return $true } + BeforeAll { + Mock Install-Module { Return $true } + } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.withcredentials.depend.psd1" -Force -Credentials $Credentials + BeforeEach { + $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.withcredentials.depend.psd1" -Force -Credentials $Credentials + } It 'Should execute Install-Module' { Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Credential -ne $null -and $Credential.Username -eq 'testUser' } } It 'Should Return Mocked output' { - $Results | Should be $True + $Results | Should -Be $True } } Context 'Installs Modules with multiple credentials' { - Mock Install-Module { Return $true } + BeforeAll{ + Mock Install-Module { Return $true } + } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.multiplecredentials.depend.psd1" -Force -Credentials $Credentials + BeforeEach { + $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.multiplecredentials.depend.psd1" -Force -Credentials $Credentials + } - It 'Should execute Install-Module with the correct credentials' { + It 'Should execute Install-Module with the correct credentials' { Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq 'imaginary' -and $Credential -ne $null -and $Credential.Username -eq 'testUser' } Assert-MockCalled Install-Module -Times 1 -Exactly -ParameterFilter { $Name -eq 'other' -and $Credential -ne $null -and $Credential.Username -eq 'otherUser' } } It 'Should Return Mocked output' { - $Results | Should be @($True, $True) + $Results | Should -Be @($True, $True) } } Context 'Saves Modules' { - Mock Save-Module { Return $true } + BeforeAll{ + Mock Save-Module { Return $true } + } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/savemodule.depend.psd1" -Force + BeforeEach { + $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/savemodule.depend.psd1" -Force + } It 'Should execute Save-Module' { Assert-MockCalled Save-Module -Times 1 -Exactly } It 'Should Return Mocked output' { - $Results | Should be $True + $Results | Should -Be $True } } Context 'Saves Modules with credentials' { - Mock Save-Module { Return $true } + BeforeAll{ + Mock Save-Module { Return $true } + } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/savemodule.withcredentials.depend.psd1" -Force -Credentials $Credentials + BeforeEach { + $Results = Invoke-PSDepend @Verbose -Path "$TestDepends/savemodule.withcredentials.depend.psd1" -Force -Credentials $Credentials + } It 'Should execute Save-Module' { Assert-MockCalled Save-Module -Times 1 -Exactly -ParameterFilter { $Credential -ne $null -and $Credential.Username -eq 'testUser' } } It 'Should Return Mocked output' { - $Results | Should be $True + $Results | Should -Be $True } } Context 'Repository does not Exist' { - Mock Install-Module { throw "Unable to find repository 'Blah'" } -ParameterFilter { $Repository -eq 'Blah'} + BeforeAll{ + Mock Install-Module { throw "Unable to find repository 'Blah'" } -ParameterFilter { $Repository -eq 'Blah'} + } It 'Throws because Repository could not be found' { $Results = { Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.missingrepo.depend.psd1" -Force -ErrorAction Stop } - $Results | Should Throw + $Results | Should -Throw } } Context 'Same module version exists (Version)' { - Mock Install-Module {} - Mock Get-Module { - [pscustomobject]@{ - Version = '1.2.5' + BeforeAll{ + Mock Install-Module {} + Mock Get-Module { + [pscustomobject]@{ + Version = '1.2.5' + } } + Mock Find-Module } - Mock Find-Module It 'Skips Install-Module' { Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.sameversion.depend.psd1" -Force -ErrorAction Stop @@ -133,13 +162,15 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } Context 'Same module version exists (SemVersion)' { - Mock Install-Module {} - Mock Get-Module { - [pscustomobject]@{ - Version = '1.2.5-preview0002' + BeforeAll{ + Mock Install-Module {} + Mock Get-Module { + [pscustomobject]@{ + Version = '1.2.5-preview0002' + } } + Mock Find-Module } - Mock Find-Module It 'Skips Install-Module' { Invoke-PSDepend @Verbose -Path "$TestDepends/psgallerymodule.SameSemanticVersion.depend.psd1" -Force -ErrorAction Stop @@ -151,15 +182,17 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } Context 'Latest module required, and already installed (version)' { - Mock Install-Module {} - Mock Get-Module { - [pscustomobject]@{ - Version = '1.2.5' + BeforeAll{ + Mock Install-Module {} + Mock Get-Module { + [pscustomobject]@{ + Version = '1.2.5' + } } - } - Mock Find-Module { - [pscustomobject]@{ - Version = '1.2.5' + Mock Find-Module { + [pscustomobject]@{ + Version = '1.2.5' + } } } @@ -173,15 +206,17 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } Context 'Latest module required, and already installed (SemVersion)' { - Mock Install-Module {} - Mock Get-Module { - [pscustomobject]@{ - Version = '1.2.5-preview0002' + BeforeAll{ + Mock Install-Module {} + Mock Get-Module { + [pscustomobject]@{ + Version = '1.2.5-preview0002' + } } - } - Mock Find-Module { - [pscustomobject]@{ - Version = '1.2.5-preview0002' + Mock Find-Module { + [pscustomobject]@{ + Version = '1.2.5-preview0002' + } } } @@ -196,7 +231,7 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Context 'Test-Dependency' { - BeforeEach { + BeforeAll { Mock Install-Module {} Mock Find-Module {} } @@ -209,8 +244,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.sameversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It 'Returns $true when it finds an existing module (SemVersion)' { @@ -221,8 +256,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.SameSemanticVersion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It 'Returns $true when it finds an existing latest module (Version)' { @@ -238,8 +273,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.latestversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It 'Returns $true when it finds an existing latest module (SemVersion)' { @@ -255,24 +290,24 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.latestversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It "Returns `$false when it doesn't find an existing module (Version)" { Mock Get-Module { $null } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.sameversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It "Returns `$false when it doesn't find an existing module (SemVersion)" { Mock Get-Module { $null } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.SameSemanticVersion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It "Returns `$false when it finds an existing module with a lower version (Version)" { @@ -283,8 +318,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.sameversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It 'Returns $false when it finds an existing module with a lower version (SemVersion)' { @@ -295,8 +330,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.SameSemanticVersion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It 'Returns $false when it finds an existing module with a lower version (SemVersion-Version)' { @@ -307,8 +342,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.SameSemanticVersion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It 'Returns $false when it finds an existing module with a lower version than latest (Version)' { @@ -324,8 +359,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.latestversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It 'Returns $false when it finds an existing module with a lower version than latest (SemVersion)' { @@ -341,8 +376,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends/psgallerymodule.latestversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } } @@ -360,35 +395,37 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { It 'Adds folder to path' { Mock Save-Module {$True} Invoke-PSDepend @Verbose -Path "$TestDepends\psgallerymodule.addtopath.depend.psd1" -Force -ErrorAction Stop - ($env:PSModulePath -split ([IO.Path]::PathSeparator)) -contains $SavePath | Should Be $True + ($env:PSModulePath -split ([IO.Path]::PathSeparator)) -contains $SavePath | Should -Be $True $ENV:PSModulePath = $ExistingPSModulePath } } Context 'AddToPath on import of module in target folder' { - $addToPathTestCases = @( - @{ - Version = 'specific version' - DependPsd1File = "psgallerymodule.addtopath.depend.psd1" - }, - @{ - Version = 'latest version' - DependPsd1File = "psgallerymodule.latestaddtopath.depend.psd1" - } - ) - - Mock Install-Module - Mock Import-Module - Mock Get-Module { - [pscustomobject]@{ - Version = '1.2.5' - Path = "C:\dummy\path" + BeforeAll { + $addToPathTestCases = @( + @{ + Version = 'specific version' + DependPsd1File = "psgallerymodule.addtopath.depend.psd1" + }, + @{ + Version = 'latest version' + DependPsd1File = "psgallerymodule.latestaddtopath.depend.psd1" + } + ) + + Mock Install-Module + Mock Import-Module + Mock Get-Module { + [pscustomobject]@{ + Version = '1.2.5' + Path = "C:\dummy\path" + } } - } - Mock Find-Module { - [pscustomobject]@{ - Version = '1.2.5' + Mock Find-Module { + [pscustomobject]@{ + Version = '1.2.5' + } } } @@ -407,7 +444,7 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Assert-MockCalled Import-Module -Times 1 -Exactly -Scope It # then - ($env:PSModulePath -split ([IO.Path]::PathSeparator)) -contains $SavePath | Should Be $True + ($env:PSModulePath -split ([IO.Path]::PathSeparator)) -contains $SavePath | Should -Be $True } } #> @@ -436,114 +473,128 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Describe "Git Type PS$PSVersion" -Tag "WindowsOnly" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } Context 'Installs Module' { - Mock Invoke-ExternalCommand { - [pscustomobject]@{ - PSB = $PSBoundParameters - Arg = $Args - } - } -ParameterFilter {$Arguments -contains 'checkout' -or $Arguments -contains 'clone'} - Mock New-Item { return $true } - Mock Push-Location {} - Mock Pop-Location {} - Mock Set-Location {} - Mock Test-Path { return $False } -ParameterFilter {$Path -match "Invoke-Build$|PSDeploy$"} + BeforeAll { + Mock Invoke-ExternalCommand { + [pscustomobject]@{ + PSB = $PSBoundParameters + Arg = $Args + } + } -ParameterFilter {$Arguments -contains 'checkout' -or $Arguments -contains 'clone'} + Mock New-Item { return $true } + Mock Push-Location {} + Mock Pop-Location {} + Mock Set-Location {} + Mock Test-Path { return $False } -ParameterFilter {$Path -match "Invoke-Build$|PSDeploy$"} - $Dependencies = Get-Dependency @Verbose -Path "$TestDepends\git.depend.psd1" + } It 'Parses the Git dependency type' { - $Dependencies.count | Should be 3 - ( $Dependencies | Where {$_.DependencyType -eq 'Git'} ).Count | Should Be 3 - ( $Dependencies | Where {$_.DependencyName -like '*nightroman/Invoke-Build'}).Version | Should be 'ac54571010d8ca5107fc8fa1a69278102c9aa077' - ( $Dependencies | Where {$_.DependencyName -like '*ramblingcookiemonster/PSDeploy'}).Version | Should be 'master' + $Dependencies = Get-Dependency @Verbose -Path "$TestDepends\git.depend.psd1" + $Dependencies.count | Should -Be 3 + ( $Dependencies | Where {$_.DependencyType -eq 'Git'} ).Count | Should -Be 3 + ( $Dependencies | Where {$_.DependencyName -like '*nightroman/Invoke-Build'}).Version | Should -Be 'ac54571010d8ca5107fc8fa1a69278102c9aa077' + ( $Dependencies | Where {$_.DependencyName -like '*ramblingcookiemonster/PSDeploy'}).Version | Should -Be 'master' } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\git.depend.psd1" -Force It 'Invokes the Git dependency type' { + Invoke-PSDepend @Verbose -Path "$TestDepends\git.depend.psd1" -Force Assert-MockCalled -CommandName Invoke-ExternalCommand -Times 6 -Exactly } } Context 'Tests dependency' { - Mock New-Item { return $true } - Mock Push-Location {} - Mock Pop-Location {} - Mock Set-Location {} - Mock Invoke-ExternalCommand -ParameterFilter {$Arguments -contains 'checkout' -or $Arguments -contains 'clone'} + BeforeAll{ + Mock New-Item { return $true } + Mock Push-Location {} + Mock Pop-Location {} + Mock Set-Location {} + Mock Invoke-ExternalCommand -ParameterFilter {$Arguments -contains 'checkout' -or $Arguments -contains 'clone'} + } It 'Returns $false if git repo does not exist' { Mock Test-Path { return $False } -ParameterFilter {$Path -match "PSDeploy$"} $Results = @( Get-Dependency @Verbose -Path "$TestDepends\git.test.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.count | Should be 1 - $Results[0] | Should be $False + $Results.count | Should -Be 1 + $Results[0] | Should -Be $False } It 'Returns $true if git repo does exist' { Mock Test-Path { return $true } -ParameterFilter {$Path -match "PSDeploy$"} Mock Invoke-ExternalCommand { return 'imaginary_branch' } -ParameterFilter {$Arguments -contains 'rev-parse'} $Results = @( Get-Dependency @Verbose -Path "$TestDepends\git.test.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.count | Should be 1 - $Results[0] | Should be $true + $Results.count | Should -Be 1 + $Results[0] | Should -Be $true } } } Describe "FileDownload Type PS$PSVersion" -Tag "WindowsOnly" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } + + AfterAll{ + Remove-Item $SavePath -Force -Recurse + $null = New-Item $SavePath -ItemType Directory -Force + } Context 'Installs dependency' { - Mock Get-WebFile { - [pscustomobject]@{ - PSB = $PSBoundParameters - Arg = $Args + BeforeAll{ + Mock Get-WebFile { + [pscustomobject]@{ + PSB = $PSBoundParameters + Arg = $Args + } } + } - $Dependencies = @(Get-Dependency @Verbose -Path "$TestDepends\filedownload.depend.psd1") + BeforeEach { + $Dependencies = @(Get-Dependency @Verbose -Path "$TestDepends\filedownload.depend.psd1") + } It 'Parses the FileDownload dependency type' { - $Dependencies.count | Should be 1 - $Dependencies[0].DependencyType | Should be 'FileDownload' + $Dependencies.count | Should -Be 1 + $Dependencies[0].DependencyType | Should -Be 'FileDownload' } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\filedownload.depend.psd1" -Force - It 'Invokes the FileDownload dependency type' { + Invoke-PSDepend @Verbose -Path "$TestDepends\filedownload.depend.psd1" -Force Assert-MockCalled Get-WebFile -Times 1 -Exactly } - New-Item -ItemType File -Path (Join-Path $SavePath 'System.Data.SQLite.dll') - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\filedownload.depend.psd1" -Force - It 'Parses URL file name and skips on existing' { - Assert-MockCalled Get-WebFile -Times 1 -Exactly # already called, so still 1, not 2... + New-Item -ItemType File -Path (Join-Path $SavePath 'System.Data.SQLite.dll') + Invoke-PSDepend @Verbose -Path "$TestDepends\filedownload.depend.psd1" -Force + Assert-MockCalled Get-WebFile -Times 0 -Exactly } } - Remove-Item $SavePath -Force -Recurse - $null = New-Item $SavePath -ItemType Directory -Force - Context 'Tests dependency' { It 'Returns $false if file does not exist' { Mock Get-WebFile {} $Results = @( Get-Dependency @Verbose -Path "$TestDepends\filedownload.depend.psd1" | Test-Dependency @Verbose -Quiet) - $Results.count | Should be 1 - $Results[0] | Should be $False + $Results.count | Should -Be 1 + $Results[0] | Should -Be $False Assert-MockCalled -CommandName Get-WebFile -Times 0 -Exactly } - New-Item -ItemType File -Path (Join-Path $SavePath 'System.Data.SQLite.dll') -Force + It 'Returns $true if file does exist' { + New-Item -ItemType File -Path (Join-Path $SavePath 'System.Data.SQLite.dll') -Force Mock Get-WebFile {} $Results = @( Get-Dependency @Verbose -Path "$TestDepends\filedownload.depend.psd1" | Test-Dependency @Verbose -Quiet) - $Results.count | Should be 1 - $Results[0] | Should be $true + $Results.count | Should -Be 1 + $Results[0] | Should -Be $true Assert-MockCalled -CommandName Get-WebFile -Times 0 -Exactly } } @@ -551,33 +602,40 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Describe "PSGalleryNuget Type PS$PSVersion" -Tag "WindowsOnly" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } Context 'Installs Modules' { - Mock Test-Path { Return $true } -ParameterFilter { $PathType -eq 'Container' } - Mock Invoke-ExternalCommand { Return $true } - Mock Find-NugetPackage { Return $true } + BeforeAll{ + Mock Test-Path { Return $true } -ParameterFilter { $PathType -eq 'Container' } + Mock Invoke-ExternalCommand { Return $true } + Mock Find-NugetPackage { Return $true } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\psgallerynuget.depend.psd1" -Force + } It 'Should execute Invoke-ExternalCommand' { + Invoke-PSDepend @Verbose -Path "$TestDepends\psgallerynuget.depend.psd1" -Force Assert-MockCalled Invoke-ExternalCommand -Times 1 -Exactly } It 'Should Return Mocked output' { - $Results | Should be $True + $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\psgallerynuget.depend.psd1" -Force + $Results | Should -Be $True } } Context 'Same module version exists' { - Mock Test-Path {return $True} -ParameterFilter {$Path -match 'jenkins'} - Mock Invoke-ExternalCommand {} - Mock Import-LocalizedData { - [pscustomobject]@{ - ModuleVersion = '1.2.5' - } - } -ParameterFilter {$FileName -eq 'jenkins.psd1'} - Mock Find-NugetPackage + BeforeAll{ + Mock Test-Path {return $True} -ParameterFilter {$Path -match 'jenkins'} + Mock Invoke-ExternalCommand {} + Mock Import-LocalizedData { + [pscustomobject]@{ + ModuleVersion = '1.2.5' + } + } -ParameterFilter {$FileName -eq 'jenkins.psd1'} + Mock Find-NugetPackage + } It 'Skips Invoke-ExternalCommand' { Invoke-PSDepend @Verbose -Path "$TestDepends\psgallerynuget.sameversion.depend.psd1" -Force -ErrorAction Stop @@ -589,16 +647,18 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } Context 'Latest module required, and already installed' { - Mock Test-Path {return $True} -ParameterFilter {$Path -match 'jenkins'} - Mock Invoke-ExternalCommand {} - Mock Import-LocalizedData { - [pscustomobject]@{ - ModuleVersion = '1.2.5' - } - } -ParameterFilter {$FileName -eq 'jenkins.psd1'} - Mock Find-NugetPackage { - [pscustomobject]@{ - Version = '1.2.5' + BeforeAll{ + Mock Test-Path {return $True} -ParameterFilter {$Path -match 'jenkins'} + Mock Invoke-ExternalCommand {} + Mock Import-LocalizedData { + [pscustomobject]@{ + ModuleVersion = '1.2.5' + } + } -ParameterFilter {$FileName -eq 'jenkins.psd1'} + Mock Find-NugetPackage { + [pscustomobject]@{ + Version = '1.2.5' + } } } @@ -613,7 +673,7 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Context 'Tests dependencies' { - BeforeEach { + BeforeAll { Mock Invoke-ExternalCommand {} Mock Test-Path {return $True} -ParameterFilter {$Path -match 'jenkins'} Mock Find-NugetPackage {} @@ -627,8 +687,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } -ParameterFilter {$FileName -eq 'jenkins.psd1'} $Results = @( Get-Dependency @Verbose -Path "$TestDepends\psgallerynuget.sameversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It 'Returns $true when it finds an existing latest module' { @@ -644,16 +704,16 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends\psgallerynuget.latestversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It "Returns `$false when it doesn't find an existing module" { Mock Import-LocalizedData -ParameterFilter {$FileName -eq 'jenkins.psd1'} $Results = @( Get-Dependency @Verbose -Path "$TestDepends\psgallerynuget.sameversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It "Returns `$false when it finds an existing module with a lower version" { @@ -665,8 +725,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { $Results = @( Get-Dependency @Verbose -Path "$TestDepends\psgallerynuget.sameversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It "Returns `$false when it finds an existing module with a lower version than latest" { @@ -683,8 +743,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { $Results = @( Get-Dependency @Verbose -Path "$TestDepends\psgallerynuget.latestversion.depend.psd1" | Test-Dependency -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } } @@ -703,7 +763,7 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Mock Invoke-ExternalCommand {$True} Mock Import-Module $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\psgallerynuget.addtopath.depend.psd1" -Force -ErrorAction Stop - $env:PSModulePath -split ([IO.Path]::PathSeparator) -contains $SavePath | Should Be $True + $env:PSModulePath -split ([IO.Path]::PathSeparator) -contains $SavePath | Should -Be $True $ENV:PSModulePath = $ExistingPSModulePath } } @@ -711,28 +771,30 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Context 'AddToPath on import of module in target folder' { - $addToPathTestCases = @( - @{ - Version = 'specific version' - DependPsd1File = "psgallerynuget.addtopath.depend.psd1" - }, - @{ - Version = 'latest version' - DependPsd1File = "psgallerynuget.latestaddtopath.depend.psd1" - } - ) - - Mock Test-Path {return $True} -ParameterFilter {$Path -match 'imaginary'} - Mock Invoke-ExternalCommand {} - Mock Import-Module - Mock Import-LocalizedData { - [pscustomobject]@{ - ModuleVersion = '1.2.5' - } - } -ParameterFilter {$FileName -eq 'imaginary.psd1'} - Mock Find-NugetPackage { - [pscustomobject]@{ - Version = '1.2.5' + BeforeAll{ + $addToPathTestCases = @( + @{ + Version = 'specific version' + DependPsd1File = "psgallerynuget.addtopath.depend.psd1" + }, + @{ + Version = 'latest version' + DependPsd1File = "psgallerynuget.latestaddtopath.depend.psd1" + } + ) + + Mock Test-Path {return $True} -ParameterFilter {$Path -match 'imaginary'} + Mock Invoke-ExternalCommand {} + Mock Import-Module + Mock Import-LocalizedData { + [pscustomobject]@{ + ModuleVersion = '1.2.5' + } + } -ParameterFilter {$FileName -eq 'imaginary.psd1'} + Mock Find-NugetPackage { + [pscustomobject]@{ + Version = '1.2.5' + } } } @@ -751,58 +813,65 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Assert-MockCalled Import-Module -Times 1 -Exactly -Scope It # then - (($env:PSModulePath -split ([IO.Path]::PathSeparator))) -contains $SavePath | Should Be $True + (($env:PSModulePath -split ([IO.Path]::PathSeparator))) -contains $SavePath | Should -Be $True } } } Describe "FileSystem Type PS$PSVersion" -Tag "WindowsOnly" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } Context 'Installs dependency' { - Mock Copy-Item + BeforeAll { + Mock Copy-Item - $Dependencies = @(Get-Dependency @Verbose -Path "$TestDepends\filesystem.depend.psd1") + $Dependencies = @(Get-Dependency @Verbose -Path "$TestDepends\filesystem.depend.psd1") + } + + AfterAll { + Remove-Item $SavePath -Force -Recurse + $null = New-Item $SavePath -ItemType Directory -Force + } It 'Parses the FileDownload dependency type' { - $Dependencies.count | Should be 1 - $Dependencies[0].DependencyType | Should be 'FileSystem' + $Dependencies.count | Should -Be 1 + $Dependencies[0].DependencyType | Should -Be 'FileSystem' } - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\filesystem.depend.psd1" -Force It 'Invokes the FileSystem dependency type' { + Invoke-PSDepend @Verbose -Path "$TestDepends\filesystem.depend.psd1" -Force Assert-MockCalled Copy-Item -Times 1 -Exactly } - New-Item -ItemType File -Path (Join-Path $SavePath 'notepad.exe') - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\filesystem.depend.psd1" -Force It 'Still copies if file hashes do not match' { - Assert-MockCalled Copy-Item -Times 2 -Exactly # already called, so 2... + New-Item -ItemType File -Path (Join-Path $SavePath 'notepad.exe') + Invoke-PSDepend @Verbose -Path "$TestDepends\filesystem.depend.psd1" -Force + Assert-MockCalled Copy-Item -Times 1 -Exactly } } - Remove-Item $SavePath -Force -Recurse - $null = New-Item $SavePath -ItemType Directory -Force Context 'Tests dependency' { It 'Returns $false if file does not exist' { Mock Copy-Item $Results = @( Get-Dependency @Verbose -Path "$TestDepends\filesystem.depend.psd1" | Test-Dependency @Verbose -Quiet) - $Results.count | Should be 1 - $Results[0] | Should be $False + $Results.count | Should -Be 1 + $Results[0] | Should -Be $False Assert-MockCalled -CommandName Copy-Item -Times 0 -Exactly } - xcopy C:\Windows\notepad.exe $(Join-Path $SavePath '*') /Y It 'Returns $true if file does exist' { + xcopy C:\Windows\notepad.exe $(Join-Path $SavePath '*') /Y Mock Copy-Item $Results = @( Get-Dependency @Verbose -Path "$TestDepends\filesystem.depend.psd1" | Test-Dependency @Verbose -Quiet) - $Results.count | Should be 1 - $Results[0] | Should be $true + $Results.count | Should -Be 1 + $Results[0] | Should -Be $true Assert-MockCalled -CommandName Copy-Item -Times 0 -Exactly } } @@ -810,11 +879,13 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Describe "Package Type PS$PSVersion" -tag pkg { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + # So... these didn't work with mocking. Create function, define alias to override any function call, mock that. + function Get-Package {[cmdletbinding()]param( $ProviderName, $Name, $RequiredVersion)} + function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion)} - # So... these didn't work with mocking. Create function, define alias to override any function call, mock that. - function Get-Package {[cmdletbinding()]param( $ProviderName, $Name, $RequiredVersion)} - function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion)} + } <# Works, but waiting on https://github.com/pester/Pester/issues/604... # Got past Get-Package, but Install-Package is still giving the parameter error @@ -830,24 +901,28 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } It 'Should Return Mocked output' { - $Results | Should be $True + $Results | Should -Be $True } } #> Context 'PackageSource does not Exist' { - Mock Install-Package - Mock Get-PackageSource + BeforeAll { + Mock Install-Package + Mock Get-PackageSource + } It 'Throws because Repository could not be found' { $Results = { Invoke-PSDepend @Verbose -Path "$TestDepends\package.depend.psd1" -Force -ErrorAction Stop } - $Results | Should Throw + $Results | Should -Throw } } Context 'Same package version exists' { - function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion, $Force)} - function Get-PackageSource { @([pscustomobject]@{Name = 'chocolatey'; ProviderName = 'chocolatey'}) } + BeforeAll{ + function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion, $Force)} + function Get-PackageSource { @([pscustomobject]@{Name = 'chocolatey'; ProviderName = 'chocolatey'}) } + } It 'Skips Install-Package' { @@ -884,8 +959,10 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { #> - function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion, $Force)} - function Get-PackageSource { @([pscustomobject]@{Name = 'chocolatey'; ProviderName = 'chocolatey'}) } + BeforeAll{ + function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion, $Force)} + function Get-PackageSource { @([pscustomobject]@{Name = 'chocolatey'; ProviderName = 'chocolatey'}) } + } It 'Runs Get-Package and Find-Package, skips Install-Package' -Skip { @@ -911,15 +988,15 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Context 'Test-Dependency' { - if (-not (Get-Command Get-Package -Module PackageManagement)) { - function Get-Package {[cmdletbinding()]param( $ProviderName, $Name, $RequiredVersion) write-verbose "WTF NOW"} - } - if (-not (Get-Command Install-Package -Module PackageManagement)) { - function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion, $Force)} - } - function Get-PackageSource { @([pscustomobject]@{Name = 'chocolatey'; ProviderName = 'chocolatey'}) } + BeforeAll { + if (-not (Get-Command Get-Package -Module PackageManagement)) { + function Get-Package {[cmdletbinding()]param( $ProviderName, $Name, $RequiredVersion) write-verbose "WTF NOW"} + } + if (-not (Get-Command Install-Package -Module PackageManagement)) { + function Install-Package {[cmdletbinding()]param( $Source, $Name, $RequiredVersion, $Force)} + } + function Get-PackageSource { @([pscustomobject]@{Name = 'chocolatey'; ProviderName = 'chocolatey'}) } - BeforeEach { Mock Install-Package {} Mock Find-Package {} } @@ -932,8 +1009,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends\package.sameversion.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It 'Returns $true when it finds an existing latest module' { @@ -949,16 +1026,16 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends\package.latestversion.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $True + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $True } It "Returns `$false when it doesn't find an existing module" { Mock Get-Package { $null } $Results = @( Get-Dependency @Verbose -Path "$TestDepends\package.sameversion.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It "Returns `$false when it finds an existing module with a lower version" { @@ -969,8 +1046,8 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends\package.sameversion.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } It "Returns `$false when it finds an existing module with a lower version than latest" { @@ -986,70 +1063,79 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } $Results = @( Get-Dependency @Verbose -Path "$TestDepends\package.latestversion.depend.psd1" | Test-Dependency @Verbose -Quiet ) - $Results.Count | Should be 1 - $Results[0] | Should be $False + $Results.Count | Should -Be 1 + $Results[0] | Should -Be $False } } } Describe "Command Type PS$PSVersion" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } Context 'Invokes a command' { - $Dependencies = @(Get-Dependency @Verbose -Path "$TestDepends\command.depend.psd1") - It 'Parses the command dependency type' { - $Dependencies.count | Should be 1 - $Dependencies[0].DependencyType | Should be 'Command' + $Dependencies = @(Get-Dependency @Verbose -Path "$TestDepends\command.depend.psd1") + $Dependencies.count | Should -Be 1 + $Dependencies[0].DependencyType | Should -Be 'Command' } It 'Invokes a command' { $Output = Invoke-PSDepend @Verbose -Path "$TestDepends\command.depend.psd1" -Force - $Output | Should be 'hello world' + $Output | Should -Be 'hello world' } } } Describe "Npm Type PS$PSVersion" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + } Context 'Installs Dependency' { - Mock Get-NodeModule {return $null} - Mock Install-NodeModule {} - Mock New-Item {return true} - Mock Push-Location - Mock Pop-Location - $Dependencies = Get-Dependency @Verbose -Path "$TestDepends\npm.depend.psd1" - $Results = Invoke-PSDepend @Verbose -Path "$TestDepends\npm.depend.psd1" -Force + BeforeAll { + Mock Get-NodeModule {return $null} + Mock Install-NodeModule {} + Mock New-Item {return true} + Mock Push-Location + Mock Pop-Location + + } It 'Parses the Npm dependency type' { - $Dependencies.count | Should be 2 - ( $Dependencies | Where-Object {$_.DependencyType -eq 'Npm'} ).Count | Should Be 2 - ( $Dependencies | Where-Object {$_.DependencyName -like 'gitbook-cli'}).Version | Should be '2.3.0' - ( $Dependencies | Where-Object {$_.DependencyName -like 'gitbook-cli'}).Target | Should be 'Global' - ( $Dependencies | Where-Object {$_.DependencyName -like 'gitbook-summary'}).Version | Should BeNullOrEmpty + $Dependencies = Get-Dependency @Verbose -Path "$TestDepends\npm.depend.psd1" + $Dependencies.count | Should -Be 2 + ( $Dependencies | Where-Object {$_.DependencyType -eq 'Npm'} ).Count | Should -Be 2 + ( $Dependencies | Where-Object {$_.DependencyName -like 'gitbook-cli'}).Version | Should -Be '2.3.0' + ( $Dependencies | Where-Object {$_.DependencyName -like 'gitbook-cli'}).Target | Should -Be 'Global' + ( $Dependencies | Where-Object {$_.DependencyName -like 'gitbook-summary'}).Version | Should -BeNullOrEmpty } It 'Invokes the Nppm dependency type' { + Invoke-PSDepend @Verbose -Path "$TestDepends\npm.depend.psd1" -Force Assert-MockCalled -CommandName Install-NodeModule -Times 2 -Exactly } } Context 'Tests Dependency' { - Mock Install-NodeModule {} - Mock New-Item {return true} - Mock Push-Location - Mock Pop-Location - $Dependencies = Get-Dependency @Verbose -Path "$TestDepends\npm.depend.psd1" + BeforeAll { + Mock Install-NodeModule {} + Mock New-Item {return true} + Mock Push-Location + Mock Pop-Location + + $Dependencies = Get-Dependency @Verbose -Path "$TestDepends\npm.depend.psd1" + } It 'Returns $false if the module is not installed' { Mock Get-NodeModule {return $null} - Invoke-PSDepend @Verbose -Path "$TestDepends\npm.depend.psd1" -Test -Quiet | Should Be $false + Invoke-PSDepend @Verbose -Path "$TestDepends\npm.depend.psd1" -Test -Quiet | Should -Be $false } It 'Returns $true if the module is installed' { @@ -1063,19 +1149,25 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { version = '1.2.3' } }} - Invoke-PSDepend @Verbose -Path "$TestDepends\npm.depend.psd1" -Test -Quiet | Should Be $true + Invoke-PSDepend @Verbose -Path "$TestDepends\npm.depend.psd1" -Test -Quiet | Should -Be $true } } } Describe "DotnetSdk Type PS$PSVersion" { - $IsWindowsEnv = !$PSVersionTable.Platform -or $PSVersionTable.Platform -eq "Win32NT" - $GlobalDotnetSdkLocation = if ($IsWindowsEnv) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" } - $DotnetFile = if ($IsWindowsEnv) { "dotnet.exe" } else { "dotnet" } - $SavePath = '.dotnet' + + BeforeAll { + $IsWindowsEnv = !$PSVersionTable.Platform -or $PSVersionTable.Platform -eq "Win32NT" + $GlobalDotnetSdkLocation = if ($IsWindowsEnv) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" } + $DotnetFile = if ($IsWindowsEnv) { "dotnet.exe" } else { "dotnet" } + $SavePath = '.dotnet' + } Context 'Installs Dependency' { - $Dependency = Get-Dependency @Verbose -Path "$TestDepends\dotnetsdk.complex.depend.psd1" + BeforeAll { + $Dependency = Get-Dependency @Verbose -Path "$TestDepends\dotnetsdk.complex.depend.psd1" + } + It 'Parses the DotnetSdk dependency type' { $Dependency | Should -Not -BeNullOrEmpty $Dependency.DependencyType | Should -Be 'DotnetSdk' @@ -1105,10 +1197,13 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } Context 'Tests Dependency' { - # used to see if 'dotnet' is already on the PATH - we need this to return false - Mock Get-Command { return $false } -ParameterFilter { $Name -eq 'dotnet' } - Mock Test-Path { return $true } -ParameterFilter { $Path -eq (Join-Path $GlobalDotnetSdkLocation $DotnetFile) } - Mock Get-DotnetVersion { return '2.1.330-rc1' } + + BeforeAll { + # used to see if 'dotnet' is already on the PATH - we need this to return false + Mock Get-Command { return $false } -ParameterFilter { $Name -eq 'dotnet' } + Mock Test-Path { return $true } -ParameterFilter { $Path -eq (Join-Path $GlobalDotnetSdkLocation $DotnetFile) } + Mock Get-DotnetVersion { return '2.1.330-rc1' } + } It 'Can propertly compare semantic versions' { # '2.1.330-rc1' >= '2.1.330-preview1' @@ -1119,10 +1214,10 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { } Context 'Imports Dependency' { - # used to see if 'dotnet' is already on the PATH - we need this to return false - Mock Get-Command { return $false } -ParameterFilter { $Name -eq 'dotnet' } - BeforeAll { + # used to see if 'dotnet' is already on the PATH - we need this to return false + Mock Get-Command { return $false } -ParameterFilter { $Name -eq 'dotnet' } + $originalPath = $env:PATH } @@ -1151,21 +1246,23 @@ InModuleScope ('{0}' -f $ENV:BHProjectName) { Describe "Chocolatey Type PS$PSVersion" -Tag 'Chocolatey', "WindowsOnly" { - $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName + BeforeAll { + $SavePath = (New-Item 'TestDrive:/PSDependPesterTest' -ItemType Directory -Force).FullName - # So... these didn't work with mocking. Create function, define alias to override any function call, mock that. - function Invoke-ChocoInstallPackage - { - [cmdletbinding()]param($Name, $Version, $Source, $Force, $Credential) - } - function Get-ChocoLatestPackage - { - [cmdletbinding()]param( $Source, $Name, $RequiredVersion) - } + # So... these didn't work with mocking. Create function, define alias to override any function call, mock that. + function Invoke-ChocoInstallPackage + { + [cmdletbinding()]param($Name, $Version, $Source, $Force, $Credential) + } + function Get-ChocoLatestPackage + { + [cmdletbinding()]param( $Source, $Name, $RequiredVersion) + } - function Get-ChocoInstalledPackage - { - [cmdletbinding()]param($Name) + function Get-ChocoInstalledPackage + { + [cmdletbinding()]param($Name) + } } Context 'Chocolatey is not installed' { diff --git a/build.ps1 b/build.ps1 index 81c53e7..4c698bb 100644 --- a/build.ps1 +++ b/build.ps1 @@ -9,7 +9,7 @@ param ( Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null Install-Module Psake, PSDeploy, BuildHelpers -force -AllowClobber -Scope CurrentUser -Install-Module Pester -RequiredVersion 4.10.1 -Force -AllowClobber -SkipPublisherCheck -Scope CurrentUser +Install-Module Pester -Force -AllowClobber -SkipPublisherCheck -Scope CurrentUser Import-Module Psake, BuildHelpers Set-BuildEnvironment -ErrorAction SilentlyContinue diff --git a/psake.ps1 b/psake.ps1 index c166dfa..f2a5b4a 100644 --- a/psake.ps1 +++ b/psake.ps1 @@ -44,14 +44,18 @@ Task Test -Depends Init { "`n`tSTATUS: Testing with PowerShell $PSVersion" # Gather test results. Store them in a variable and file - $pesterParameters = @{ - Path = "$ProjectRoot\Tests" - PassThru = $true - OutputFormat = "NUnitXml" - OutputFile = "$ProjectRoot\$TestFile" + $pesterConfig = New-PesterConfiguration + $pesterConfig.Run.Path = "$ProjectRoot\Tests" + $pesterConfig.Run.PassThru = $true + $pesterConfig.TestResult.Enabled = $true + $pesterConfig.TestResult.OutputFormat = "NUnitXml" + $pesterConfig.TestResult.OutputPath = "$ProjectRoot\$TestFile" + + if (-Not $IsWindows) { + $pesterConfig.Run.ExcludeTag = "WindowsOnly" } - if (-Not $IsWindows) { $pesterParameters["ExcludeTag"] = "WindowsOnly" } - $TestResults = Invoke-Pester @pesterParameters + + $TestResults = Invoke-Pester -Configuration $pesterConfig # In Appveyor? Upload our tests! #Abstract this into a function? If($ENV:BHBuildSystem -eq 'AppVeyor')