Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Tests/Chocolatey.Type.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }

BeforeAll {
if (-not $env:BHProjectPath) {
Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force

Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force

$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/Chocolatey.ps1'
}

Describe 'Chocolatey script' -Tag 'WindowsOnly' {

BeforeAll {
InModuleScope PSDepend {
# Pretend choco.exe is present so we skip the bootstrap branch
Mock Get-Command { [pscustomobject]@{ Name = 'choco.exe' } } -ParameterFilter { $Name -eq 'choco.exe' }
# All choco invocations return empty CSV (no packages installed, none found upstream)
Mock Invoke-ExternalCommand { }
Mock Invoke-WebRequest { }
}
}

It 'Defaults Source to https://chocolatey.org/api/v2/ when not supplied' {
$dep = New-PSDependFixture -DependencyName 'git' -DependencyType 'Chocolatey'
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -WarningAction SilentlyContinue
}
# We can't verify the default by inspecting choco args (script bails out
# when latest lookup returns nothing) but the script should not throw.
$true | Should -BeTrue
Comment thread
HeyItsGilbert marked this conversation as resolved.
Outdated
}

It 'Invokes choco upgrade with -Force when -Force switch is set' {
$dep = New-PSDependFixture -DependencyName 'git' -DependencyType 'Chocolatey' -Version '2.0.2'
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -Force
}
Should -Invoke -CommandName Invoke-ExternalCommand -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter {
$Arguments -contains 'upgrade' -and $Arguments -contains '--force'
}
}

It 'Forwards Credential to choco as --username / --password args' {
$cred = New-TestCredential -UserName 'feeduser' -Password 'feedpass'
$dep = New-PSDependFixture -DependencyName 'git' -DependencyType 'Chocolatey' -Version '2.0.2' -Credential $cred
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -Force
}
Should -Invoke -CommandName Invoke-ExternalCommand -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter {
($Arguments -join ' ') -match "--username='feeduser'" -and ($Arguments -join ' ') -match "--password='feedpass'"
}
}
}
57 changes: 57 additions & 0 deletions Tests/Command.Type.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }

BeforeAll {
if (-not $env:BHProjectPath) {
Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force

Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force

$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/Command.ps1'
}

Describe 'Command script' {

It 'Executes the Source string as PowerShell in the current session' {
$flagPath = Join-Path 'TestDrive:' 'flag.txt'
$dep = New-PSDependFixture -DependencyName 'CmdOne' -DependencyType 'Command' -Source "Set-Content -Path '$flagPath' -Value 'ran'"
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
(Get-Content $flagPath) | Should -Be 'ran'
}

It 'Iterates multiple Source entries' {
$countPath = Join-Path 'TestDrive:' 'count.txt'
$dep = New-PSDependFixture -DependencyName 'CmdMulti' -DependencyType 'Command' -Source @(
"Add-Content '$countPath' 'a'"
"Add-Content '$countPath' 'b'"
)
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
(Get-Content $countPath) | Should -Be @('a', 'b')
}

It 'Throws by default when the Source errors' {
$dep = New-PSDependFixture -DependencyName 'CmdFail' -DependencyType 'Command' -Source "throw 'boom'"
{
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
} | Should -Throw -ExpectedMessage '*boom*'
}

It 'Continues past errors when -FailOnError is specified' {
$dep = New-PSDependFixture -DependencyName 'CmdSwallow' -DependencyType 'Command' -Source "throw 'boom'"
$err = $null
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -FailOnError -ErrorAction SilentlyContinue -ErrorVariable err
$script:capturedErr = $err
}
# Did not throw — Write-Error was used
$true | Should -BeTrue
Comment thread
HeyItsGilbert marked this conversation as resolved.
Outdated
}
}
58 changes: 58 additions & 0 deletions Tests/DotnetSdk.Type.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }

BeforeAll {
if (-not $env:BHProjectPath) {
Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force

Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force

$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/DotnetSdk.ps1'
$script:OrigPath = $env:PATH
}

AfterAll {
if ($script:OrigPath) { $env:PATH = $script:OrigPath }
}

Describe 'DotnetSdk script' {

BeforeAll {
InModuleScope PSDepend {
Mock Install-Dotnet { }
Mock Test-Dotnet { $false }
}
}

It 'PSDependAction Test delegates to Test-Dotnet' {
InModuleScope PSDepend { Mock Test-Dotnet { $true } }
$dep = New-PSDependFixture -DependencyName 'release' -DependencyType 'DotnetSdk' -Version '2.1.0'
$result = InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -PSDependAction Test
}
$result | Should -Be $true
Should -Invoke -CommandName Test-Dotnet -ModuleName PSDepend -Times 1
}

It 'Calls Install-Dotnet when Test-Dotnet reports SDK is missing' {
$installDir = (New-Item 'TestDrive:/dotnet' -ItemType Directory -Force).FullName
$dep = New-PSDependFixture -DependencyName 'release' -DependencyType 'DotnetSdk' -Version '2.1.0' -Target $installDir
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath; D = $installDir } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Install-Dotnet -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter {
$Channel -eq 'release' -and $Version -eq '2.1.0' -and $InstallDir -eq $installDir
}
}

It 'Skips Install-Dotnet when Test-Dotnet reports SDK is present' {
InModuleScope PSDepend { Mock Test-Dotnet { $true } }
$dep = New-PSDependFixture -DependencyName 'LTS' -DependencyType 'DotnetSdk' -Version '2.1.0'
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Install-Dotnet -ModuleName PSDepend -Times 0
}
}
69 changes: 69 additions & 0 deletions Tests/FileDownload.Type.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }

BeforeAll {
if (-not $env:BHProjectPath) {
Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force

Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force

$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/FileDownload.ps1'
}

Describe 'FileDownload script' {

BeforeAll {
InModuleScope PSDepend {
Mock Get-WebFile { }
Mock Add-ToItemCollection { }
}
}

It 'Downloads to Target with filename parsed from the URL when Target is an existing folder' {
$targetDir = (New-Item 'TestDrive:/dl' -ItemType Directory -Force).FullName
$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $targetDir
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath; T = $targetDir } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter {
$URL -eq 'https://example.com/sample.dll' -and ($Path -like "*sample.dll")
}
}

It 'Uses Source to override the URL when supplied' {
$targetDir = (New-Item 'TestDrive:/dl2' -ItemType Directory -Force).FullName
$dep = New-PSDependFixture -DependencyName 'ignored-key' -DependencyType 'FileDownload' -Target $targetDir -Source 'https://example.com/other.dll'
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 1 -Exactly -ParameterFilter {
$URL -eq 'https://example.com/other.dll'
}
}

It 'Skips download when the target file already exists' {
$targetDir = (New-Item 'TestDrive:/dl3' -ItemType Directory -Force).FullName
$existingFile = Join-Path $targetDir 'sample.dll'
Set-Content -Path $existingFile -Value 'existing'

$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $existingFile
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Get-WebFile -ModuleName PSDepend -Times 0
}

It 'PSDependAction Test returns $true when the file exists' {
$targetDir = (New-Item 'TestDrive:/dl4' -ItemType Directory -Force).FullName
$existingFile = Join-Path $targetDir 'sample.dll'
Set-Content -Path $existingFile -Value 'existing'

$dep = New-PSDependFixture -DependencyName 'https://example.com/sample.dll' -DependencyType 'FileDownload' -Target $existingFile
$result = InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -PSDependAction Test
}
$result | Should -Be $true
}
}
59 changes: 59 additions & 0 deletions Tests/FileSystem.Type.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }

BeforeAll {
if (-not $env:BHProjectPath) {
Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force

Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force

$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/FileSystem.ps1'
}

Describe 'FileSystem script' {

BeforeAll {
InModuleScope PSDepend {
Mock Copy-Item { }
}
}

It 'Copies a file from Source to Target when hashes differ' {
$src = Join-Path 'TestDrive:' 'src.txt'
$tgtDir = (New-Item 'TestDrive:/tgt' -ItemType Directory -Force).FullName
Set-Content -Path $src -Value 'hello'
$srcResolved = (Resolve-Path $src).ProviderPath

$dep = New-PSDependFixture -DependencyName 'fs-file' -DependencyType 'FileSystem' -Source $srcResolved -Target $tgtDir
InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Copy-Item -ModuleName PSDepend -Times 1
}

It 'PSDependAction Test returns $false when target is missing' {
$src = Join-Path 'TestDrive:' 'src2.txt'
$tgtDir = (New-Item 'TestDrive:/missing-tgt' -ItemType Directory -Force).FullName
Set-Content -Path $src -Value 'content'
$srcResolved = (Resolve-Path $src).ProviderPath

$dep = New-PSDependFixture -DependencyName 'fs-test' -DependencyType 'FileSystem' -Source $srcResolved -Target $tgtDir
$result = InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -PSDependAction Test
}
$result | Should -Be $false
}
Comment thread
HeyItsGilbert marked this conversation as resolved.
Outdated

It 'Errors and skips when Source does not exist' {
$tgtDir = (New-Item 'TestDrive:/tgt3' -ItemType Directory -Force).FullName
$missingSrc = (Join-Path (Resolve-Path 'TestDrive:').ProviderPath 'does-not-exist.txt')
$dep = New-PSDependFixture -DependencyName 'fs-missing' -DependencyType 'FileSystem' -Source $missingSrc -Target $tgtDir

InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -ErrorAction SilentlyContinue
}
Should -Invoke -CommandName Copy-Item -ModuleName PSDepend -Times 0
}
}
63 changes: 63 additions & 0 deletions Tests/Git.Type.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }

BeforeAll {
if (-not $env:BHProjectPath) {
Set-BuildEnvironment -Path "$PSScriptRoot/.." -Force
}
Remove-Module $env:BHProjectName -ErrorAction SilentlyContinue
Import-Module (Join-Path $env:BHProjectPath $env:BHProjectName) -Force

Import-Module (Join-Path $PSScriptRoot 'Shared/TestHelpers.psm1') -Force

$script:ScriptPath = Join-Path $env:BHProjectPath 'PSDepend/PSDependScripts/Git.ps1'
}

Describe 'Git script' {

BeforeAll {
InModuleScope PSDepend {
Mock Invoke-ExternalCommand { }
Mock Import-PSDependModule { }
Mock Add-ToItemCollection { }
}
}

It 'Clones the repo via git when the target does not exist' {
$targetDir = (New-Item 'TestDrive:/git-target' -ItemType Directory -Force).FullName
$dep = New-PSDependFixture -DependencyName 'https://example.com/user/repo.git' -DependencyType 'Git' -Target $targetDir

InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Comment thread
HeyItsGilbert marked this conversation as resolved.
Outdated
# git clone + git checkout
Should -Invoke -CommandName Invoke-ExternalCommand -ModuleName PSDepend -Times 1 -ParameterFilter {
$Arguments -contains 'clone'
}
Should -Invoke -CommandName Invoke-ExternalCommand -ModuleName PSDepend -Times 1 -ParameterFilter {
$Arguments -contains 'checkout'
}
}

It 'Converts account/repo shorthand to a GitHub URL' {
$targetDir = (New-Item 'TestDrive:/git-target2' -ItemType Directory -Force).FullName
$dep = New-PSDependFixture -DependencyName 'user/repo' -DependencyType 'Git' -Target $targetDir

InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep
}
Should -Invoke -CommandName Invoke-ExternalCommand -ModuleName PSDepend -Times 1 -ParameterFilter {
$Arguments -contains 'clone' -and ($Arguments -contains 'https://github.com/user/repo.git')
}
}

It 'PSDependAction Test returns $false when the repo path does not exist' {
$targetDir = (New-Item 'TestDrive:/git-test' -ItemType Directory -Force).FullName
$dep = New-PSDependFixture -DependencyName 'https://example.com/user/repo.git' -DependencyType 'Git' -Target $targetDir

$result = InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } {
& $ScriptPath -Dependency $Dep -PSDependAction Test
}
$result | Should -Be $false
Should -Invoke -CommandName Invoke-ExternalCommand -ModuleName PSDepend -Times 0
}
}
Loading
Loading