Skip to content

Commit

Permalink
Merge pull request #22 from startersclan/fix/tests-fix-unit-tests-to-…
Browse files Browse the repository at this point in the history
…work-correctly

Fix (tests): Fix unit tests to work correctly
  • Loading branch information
leojonathanoh authored Feb 7, 2023
2 parents fa1872a + 6d94585 commit 74c61d6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 47 deletions.
49 changes: 27 additions & 22 deletions src/PSSourceQuery/public/GoldSourceRcon.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

Describe "GoldSourceRcon" -Tag 'Unit' {

Context 'Runs' {

$gameservers = [ordered]@{
# Goldsource
cstrike = @{
Address = 'cs.startersclan.com'
Port = 27815
}
czero = @{
Address = 'cs.startersclan.com'
Port = 27615
}
valve = @{
Address = 'hl.startersclan.com'
Port = 27915
}
$gameservers = [ordered]@{
# Goldsource
cstrike = @{
Address = 'cs.startersclan.com'
Port = 27815
}
czero = @{
Address = 'cs.startersclan.com'
Port = 27615
}
valve = @{
Address = 'hl.startersclan.com'
Port = 27915
}
}

Context 'Error handling' {

It 'Handles errors (error stream)' {
$password = 'foo'
$command = 'status'
$ErrorActionPreference = 'Continue'
Mock Write-Verbose {
. "$here\..\private\Resolve-DNS.ps1"
Mock Resolve-DNS {
throw 'some error'
}

Expand All @@ -41,7 +42,8 @@ Describe "GoldSourceRcon" -Tag 'Unit' {
$password = 'foo'
$command = 'status'
$ErrorActionPreference = 'Stop'
Mock Write-Verbose {
. "$here\..\private\Resolve-DNS.ps1"
Mock Resolve-DNS {
throw 'some exception'
}

Expand All @@ -50,18 +52,21 @@ Describe "GoldSourceRcon" -Tag 'Unit' {
{ GoldSourceRcon @params -Password $password -Command $command } | Should -Throw 'some exception'
}
}
}

Context 'Behavior' {

It 'Fails when rcon password is wrong' {
$password = "$( Get-Random -Minimum 1 -Maximum 1000000 )"
$command = 'status'
$ErrorActionPreference = 'Stop'
Mock Write-Verbose {}

. "$here\..\private\Resolve-DNS.ps1"
foreach ($game in $gameservers.Keys) {
$params = $gameservers[$game]
{ GoldSourceRcon @params -Password $password -Command $command } | Should -Throw 'Bad rcon_password'
{
GoldSourceRcon @params -Password $password -Command $command
} | Should -Throw 'Bad rcon_password'
}

}

}
Expand Down
15 changes: 9 additions & 6 deletions src/PSSourceQuery/public/SourceQuery.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

Describe "SourceQuery" -Tag 'Unit' {

Context 'Runs' {
Context 'Behavior' {

$gameservers = [ordered]@{
# Source
left4dead2 = @{
Address = 'l4d.startersclan.com'
Port = 27015
Engine = 'Source'
}
# left4dead2 = @{
# Address = 'l4d.startersclan.com'
# Port = 27016
# Engine = 'Source'
# }
csgo = @{
Address = 'cs.startersclan.com'
Port = 27115
Expand Down Expand Up @@ -44,6 +44,7 @@ Describe "SourceQuery" -Tag 'Unit' {
It 'Gets info' {
$type = 'info'
$ErrorActionPreference = 'Stop'
. "$here\..\private\Resolve-DNS.ps1"

foreach ($game in $gameservers.Keys) {
$params = $gameservers[$game]
Expand All @@ -56,6 +57,7 @@ Describe "SourceQuery" -Tag 'Unit' {
It 'Gets players' {
$type = 'players'
$ErrorActionPreference = 'Stop'
. "$here\..\private\Resolve-DNS.ps1"

foreach ($game in $gameservers.Keys) {
$params = $gameservers[$game]
Expand All @@ -68,6 +70,7 @@ Describe "SourceQuery" -Tag 'Unit' {
It 'Gets rules' {
$type = 'rules'
$ErrorActionPreference = 'Stop'
. "$here\..\private\Resolve-DNS.ps1"

foreach ($game in $gameservers.Keys) {
$params = $gameservers[$game]
Expand Down
44 changes: 25 additions & 19 deletions src/PSSourceQuery/public/SourceRcon.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

Describe "SourceRcon" -Tag 'Unit' {

Context 'Runs' {

$gameservers = [ordered]@{
# Source
left4dead2 = @{
Address = 'cs.startersclan.com'
Port = 27015
}
csgo = @{
Address = 'cs.startersclan.com'
Port = 27115
}
hl2mp = @{
Address = 'hl.startersclan.com'
Port = 27215
}
$gameservers = [ordered]@{
# Source
# left4dead2 = @{
# Address = 'l4d.startersclan.com'
# Port = 27015
# }
csgo = @{
Address = 'cs.startersclan.com'
Port = 27115
}
hl2mp = @{
Address = 'hl.startersclan.com'
Port = 27215
}
}

Context 'Error handling' {

It 'Handles errors (error stream)' {
$password = 'foo'
$command = 'status'
$ErrorActionPreference = 'Continue'
Mock Write-Verbose {
function Resolve-DNS {
throw 'some error'
}

Expand All @@ -41,7 +41,7 @@ Describe "SourceRcon" -Tag 'Unit' {
$password = 'foo'
$command = 'status'
$ErrorActionPreference = 'Stop'
Mock Write-Verbose {
function Resolve-DNS {
throw 'some exception'
}

Expand All @@ -51,6 +51,10 @@ Describe "SourceRcon" -Tag 'Unit' {
}
}

}

Context 'Behavior' {

It 'Fails when rcon password is wrong' {
$password = "$( Get-Random -Minimum 1 -Maximum 1000000 )"
$command = 'status'
Expand All @@ -59,7 +63,9 @@ Describe "SourceRcon" -Tag 'Unit' {

foreach ($game in $gameservers.Keys) {
$params = $gameservers[$game]
{ SourceRcon @params -Password $password -Command $command } | Should -Throw 'Bad rcon password.'
{
SourceRcon @params -Password $password -Command $command
} | Should -Throw 'Bad rcon password.'
}

}
Expand Down

0 comments on commit 74c61d6

Please sign in to comment.