From 8c9962ea4f27d46ecb8d97727c9106d3101e0f6b Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Wed, 10 Apr 2024 22:29:25 +0200 Subject: [PATCH 1/8] add cmdlet & add pester test --- SteamPS/Private/API/Test-SteamAPIKey.ps1 | 59 +++++++++++++++++++ Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 23 ++++++++ 2 files changed, 82 insertions(+) create mode 100644 SteamPS/Private/API/Test-SteamAPIKey.ps1 create mode 100644 Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 diff --git a/SteamPS/Private/API/Test-SteamAPIKey.ps1 b/SteamPS/Private/API/Test-SteamAPIKey.ps1 new file mode 100644 index 0000000..6d1ab8a --- /dev/null +++ b/SteamPS/Private/API/Test-SteamAPIKey.ps1 @@ -0,0 +1,59 @@ +function Test-SteamAPIKey { + <# + .SYNOPSIS + Tests whether a Steam API key file exists. + + .DESCRIPTION + The `Test-SteamAPIKey` cmdlet checks if a Steam API key file exists in the specified path. + It returns a boolean value indicating whether the key file is present. + + .PARAMETER None + This cmdlet does not accept any parameters. + + .OUTPUTS + System.Boolean + Returns `$true` if the Steam API key file exists; otherwise, returns `$false`. + + .NOTES + File Path: The Steam API key file is expected to be located at "$env:AppData\SteamPS\SteamPSKey.json". + + .EXAMPLE + PS C:\> Test-SteamAPIKey + True + + Description: + This example checks if the Steam API key file exists and returns `True`. + + .EXAMPLE + PS C:\> Test-SteamAPIKey + False + + Description: + This example checks if the Steam API key file exists and returns `False`. + + .NOTES + Author: Frederik Hjorslev Nylander + #> + + [CmdletBinding()] + [OutputType('System.Boolean')] + param ( + ) + + begin { + Write-Verbose -Message "[BEGIN ] Starting: $($MyInvocation.MyCommand)" + $SteamPSKey = Test-Path -Path "$env:AppData\SteamPS\SteamPSKey.json" + } + + process { + if ($SteamPSKey -eq $true) { + return [bool]$true + } elseif ($SteamPSKey -eq $false) { + return [bool]$false + } + } + + end { + Write-Verbose -Message "[END ] Ending: $($MyInvocation.MyCommand)" + } +} \ No newline at end of file diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 new file mode 100644 index 0000000..43b07be --- /dev/null +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -0,0 +1,23 @@ +Describe 'Test-SteamAPIKey Tests' { + Context 'When the Steam API key file exists' { + BeforeAll { + Mock Test-Path { $true } + } + It 'Returns $true' { + $result = Test-SteamAPIKey + $result | Should -BeOfType [bool] + $result | Should -Be $true + } + } + + Context 'When the Steam API key file does not exist' { + BeforeAll { + Mock Test-Path { $false } + } + It 'Returns $false' { + $result = Test-SteamAPIKey + $result | Should -BeOfType [bool] + $result | Should -Be $false + } + } +} From e84ca2566b58be148b7554e87d9373cc8152d406 Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Wed, 10 Apr 2024 22:43:28 +0200 Subject: [PATCH 2/8] dot source cmdlet --- Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 index 43b07be..f7ccfed 100644 --- a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -1,4 +1,8 @@ -Describe 'Test-SteamAPIKey Tests' { +BeforeAll { + . $SteamPSModulePath\Private\API\Test-SteamAPIKey.ps1 +} + +Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file exists' { BeforeAll { Mock Test-Path { $true } From c62f623e6ae30277ec8da6c01d197355c7b9f1cc Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Thu, 11 Apr 2024 06:39:44 +0200 Subject: [PATCH 3/8] fix mock - hopefully --- Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 index f7ccfed..ec3e25e 100644 --- a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -5,7 +5,7 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file exists' { BeforeAll { - Mock Test-Path { $true } + Mock -CommandName Test-Path -ModuleName SteamPS -MockWith Test-Path { $true } } It 'Returns $true' { $result = Test-SteamAPIKey @@ -16,7 +16,7 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file does not exist' { BeforeAll { - Mock Test-Path { $false } + Mock -CommandName Test-Path -ModuleName SteamPS -MockWith { $false } } It 'Returns $false' { $result = Test-SteamAPIKey From 4ec16b7045c67e093d314d6301979133b42934d4 Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Thu, 11 Apr 2024 07:09:33 +0200 Subject: [PATCH 4/8] fix param in Mock --- Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 index ec3e25e..645a30f 100644 --- a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -5,7 +5,7 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file exists' { BeforeAll { - Mock -CommandName Test-Path -ModuleName SteamPS -MockWith Test-Path { $true } + Mock -CommandName Test-Path -ModuleName SteamPS -MockWith { $true } } It 'Returns $true' { $result = Test-SteamAPIKey From ceb0cba187bd67dc6898dfdd0f3694b534f3da3d Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Thu, 11 Apr 2024 09:04:59 +0200 Subject: [PATCH 5/8] remove mock and add temp file instead --- Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 index 645a30f..8730565 100644 --- a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -5,8 +5,15 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file exists' { BeforeAll { - Mock -CommandName Test-Path -ModuleName SteamPS -MockWith { $true } + # Create a dummy SteamPSKey.json file + $SteamPSKeyLocation = "$env:AppData\SteamPS\SteamPSKey.json" + New-Item -Path $SteamPSKeyLocation -ItemType File -Force } + AfterAll { + # Remove the dummy SteamPSKey.json file + Remove-Item -Path "$env:AppData\SteamPS\SteamPSKey.json" -Force + } + It 'Returns $true' { $result = Test-SteamAPIKey $result | Should -BeOfType [bool] @@ -15,9 +22,6 @@ Describe 'Test-SteamAPIKey Tests' { } Context 'When the Steam API key file does not exist' { - BeforeAll { - Mock -CommandName Test-Path -ModuleName SteamPS -MockWith { $false } - } It 'Returns $false' { $result = Test-SteamAPIKey $result | Should -BeOfType [bool] From b02f41897377224ae6ae90014851f4b0593b9027 Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Thu, 11 Apr 2024 09:37:12 +0200 Subject: [PATCH 6/8] fix module scope --- Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 index 8730565..2826171 100644 --- a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -5,27 +5,21 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file exists' { BeforeAll { - # Create a dummy SteamPSKey.json file - $SteamPSKeyLocation = "$env:AppData\SteamPS\SteamPSKey.json" - New-Item -Path $SteamPSKeyLocation -ItemType File -Force - } - AfterAll { - # Remove the dummy SteamPSKey.json file - Remove-Item -Path "$env:AppData\SteamPS\SteamPSKey.json" -Force + Mock -CommandName Test-Path -MockWith { return $true } } It 'Returns $true' { - $result = Test-SteamAPIKey - $result | Should -BeOfType [bool] - $result | Should -Be $true + Test-SteamAPIKey | Should -BeTrue } } Context 'When the Steam API key file does not exist' { + BeforeEach { + Mock -CommandName Test-Path -MockWith { return $false } + } + It 'Returns $false' { - $result = Test-SteamAPIKey - $result | Should -BeOfType [bool] - $result | Should -Be $false + Test-SteamAPIKey | Should -BeFalse } } } From eeecf52818962dd7a61b100935473b2b99a6348c Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Thu, 11 Apr 2024 10:38:41 +0200 Subject: [PATCH 7/8] fix comment based help --- SteamPS/Private/API/Test-SteamAPIKey.ps1 | 6 ------ 1 file changed, 6 deletions(-) diff --git a/SteamPS/Private/API/Test-SteamAPIKey.ps1 b/SteamPS/Private/API/Test-SteamAPIKey.ps1 index 6d1ab8a..04d7296 100644 --- a/SteamPS/Private/API/Test-SteamAPIKey.ps1 +++ b/SteamPS/Private/API/Test-SteamAPIKey.ps1 @@ -7,16 +7,10 @@ The `Test-SteamAPIKey` cmdlet checks if a Steam API key file exists in the specified path. It returns a boolean value indicating whether the key file is present. - .PARAMETER None - This cmdlet does not accept any parameters. - .OUTPUTS System.Boolean Returns `$true` if the Steam API key file exists; otherwise, returns `$false`. - .NOTES - File Path: The Steam API key file is expected to be located at "$env:AppData\SteamPS\SteamPSKey.json". - .EXAMPLE PS C:\> Test-SteamAPIKey True From df76ea1843eedd70de8f94fe6a1cd3506b10d066 Mon Sep 17 00:00:00 2001 From: Frederik Hjorslev Date: Thu, 18 Jul 2024 21:08:02 +0200 Subject: [PATCH 8/8] add module scope --- Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 index 2826171..65f69ef 100644 --- a/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 +++ b/Tests/Unit/Private/Test-SteamAPIKey.Tests.ps1 @@ -5,7 +5,7 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file exists' { BeforeAll { - Mock -CommandName Test-Path -MockWith { return $true } + Mock -CommandName Test-Path -ModuleName SteamPS -MockWith { return $true } } It 'Returns $true' { @@ -15,7 +15,7 @@ Describe 'Test-SteamAPIKey Tests' { Context 'When the Steam API key file does not exist' { BeforeEach { - Mock -CommandName Test-Path -MockWith { return $false } + Mock -CommandName Test-Path -ModuleName SteamPS -MockWith { return $false } } It 'Returns $false' {