ps/Modules/Alkami.PowerShell.Services/Public/Get-WindowsServiceApplicationPath.tests.ps1
2023-05-30 22:51:22 -07:00

92 lines
4.2 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Import-Module $functionPath -Force
$moduleForMock = ""
Describe "Get-WindowsServiceApplicationPath" {
$fakeServiceNormal = "SvcNormalPath"
$expectedPathNormal = "TestDrive:\TestPath"
$fakeServiceSpace = "SvcSpacePath"
$expectedPathWithSpace = "TestDrive:\TestPath WithSpace"
$fakeServiceName = -join ((65..90) + (97..122) | Get-Random -Count 15 | ForEach-Object { [char]$_ })
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Host -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Verbose -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Error -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Get-ServiceInfoByCIMFragment -ModuleName $moduleForMock -MockWith {
$exeName = "$($QueryFragment).exe"
$returnPath = Join-Path -Path $expectedPathNormal -ChildPath $exeName
$returnResults = @()
$returnResults += @{
#Name = $cimService.Name
#DisplayName = $cimService.DisplayName
#Path = $cimService.PathName
ExePath = $returnPath#($cimService.PathName.Remove($cimService.PathName.LastIndexOf(".exe")) + ".exe").Replace('"', '')
#Started = $cimService.Started
#State = $cimService.State
#Status = $cimService.Status
#StartMode = $cimService.StartMode
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
#StartType = $cimService.StartMode
#ProcessId = $cimService.ProcessId
#InstallDate = $cimService.InstallDate
#UserName = $cimService.StartName
}
return $returnResults
} -ParameterFilter { $QueryFragment -eq "SvcNormalPath" }
Mock -CommandName Get-ServiceInfoByCIMFragment -ModuleName $moduleForMock -MockWith {
$exeName = "$($QueryFragment).exe"
$returnPath = Join-Path -Path $expectedPathWithSpace -ChildPath $exeName
$returnResults = @()
$returnResults += @{
#Name = $cimService.Name
#DisplayName = $cimService.DisplayName
#Path = $cimService.PathName
ExePath = $returnPath#($cimService.PathName.Remove($cimService.PathName.LastIndexOf(".exe")) + ".exe").Replace('"', '')
#Started = $cimService.Started
#State = $cimService.State
#Status = $cimService.Status
#StartMode = $cimService.StartMode
# Provided to make comparison checks to other things easier.
# The name in several places is StartType
#StartType = $cimService.StartMode
#ProcessId = $cimService.ProcessId
#InstallDate = $cimService.InstallDate
#UserName = $cimService.StartName
}
return $returnResults
} -ParameterFilter { $QueryFragment -eq "SvcSpacePath" }
Context "Error Handling" {
It "Does not throw if the service is not found" {
{ Get-WindowsServiceApplicationPath "$fakeServiceName" } | Should -Not -Throw
Assert-MockCalled -CommandName Write-Warning -ParameterFilter { $Message -match "Unable to locate service info" } -Times 1 -Exactly -Scope It -ModuleName $moduleForMock
}
}
Context "Service Executable Formatting" {
It "Handles_Paths_With_Spaces" {
Get-WindowsServiceApplicationPath -serviceName $fakeServiceSpace | Should -Be $expectedPathWithSpace
Assert-MockCalled -CommandName Get-ServiceInfoByCIMFragment -Times 1 -Exactly -Scope It -ModuleName $moduleForMock
}
It "Handles_Paths_Without_Spaces" {
Get-WindowsServiceApplicationPath -serviceName $fakeServiceNormal | Should -Be $expectedPathNormal
Assert-MockCalled -CommandName Get-ServiceInfoByCIMFragment -Times 1 -Exactly -Scope It -ModuleName $moduleForMock
}
}
}