Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test utils #28

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 26 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,40 @@ on:
pull_request:
branches:
- main
- dev

jobs:
PSScriptAnalyzer:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4

- name: Install PSScriptAnalyzer
- name: PSScriptAnalyzer
shell: pwsh
run: |
Install-Module -Name PSScriptAnalyzer -Force
Invoke-ScriptAnalyzer -Path ./ -Recurse -Severity Error -EnableExit

- name: Run PSScriptAnalyzer
shell: pwsh
Pester-pwsh:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Run Pester tests (pwsh)
run: |
Write-host $PSVersionTable.PSVersion.Major $PSVersionTable.PSRemotingProtocolVersion.Minor
Set-PSRepository psgallery -InstallationPolicy trusted
Install-Module -Name Pester -RequiredVersion 5.6.1 -confirm:$false -Force
Invoke-Pester -Path ./
shell: pwsh

Pester-posh:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Run Pester tests (PowerShell)
run: |
Invoke-ScriptAnalyzer -Path ./ -Recurse -Severity Error -EnableExit
Write-host $PSVersionTable.PSVersion.Major $PSVersionTable.PSRemotingProtocolVersion.Minor
Set-PSRepository psgallery -InstallationPolicy trusted
Install-Module -Name Pester -RequiredVersion 5.6.1 -Confirm:$false -Force
Invoke-Pester -Path ./
if ($Error[0].Fullyqualifiederrorid -eq 'PesterAssertionFailed') {exit 1}
shell: powershell
8 changes: 1 addition & 7 deletions scripts/cli.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
. .\functions.ps1
Import-Module (Join-Path $PSScriptRoot functions.ps1)

Do {
switch ($mainMenuOption) {
Expand Down Expand Up @@ -157,12 +157,6 @@ Do {
}
}

# Check paths were set, or exit
if (!(getConfigProperty "gamePath") -or !(getConfigProperty "newGamePath")) {
Read-Host "Cannot find game paths, delete config.json and re-run script."
pause
exit
}

Clear-Host
Write-Host
Expand Down
16 changes: 12 additions & 4 deletions scripts/functions.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
. .\utils.ps1
Import-Module (Join-Path $PSScriptRoot utils.psm1)

# A PS script to patch/install Gamepass SFSE in one click

Expand All @@ -13,6 +13,7 @@ $powershellVersion = $host.Version.Major
$version = "1.5.20"

# Paths
$rootPath = $PSScriptRoot | Split-Path
$logFolderPath = Join-Path $rootPath 'logs'
$LogPath = Join-Path $logFolderPath $logfileName
$sfsePath = Join-Path $rootPath 'sfse'
Expand Down Expand Up @@ -568,7 +569,7 @@ function moveGameEXE() {
Start-Process -Wait -Verb RunAs $psExecPath "-s -i -nobanner -accepteula powershell
Copy-Item (Join-Path '$newGamePath' 'Starfield.exe') -Destination (Join-Path '$gamePath' 'Starfield.exe')"
Start-Sleep -Seconds 5

# check exe was copied back to starfield install folder
if (fileExists $gamePath 'Starfield.exe') {
writeToConsole "`n`tCopy of Starfield.exe created in original folder!" -logPath $LogPath
Expand Down Expand Up @@ -833,7 +834,14 @@ if (![System.Convert]::ToBoolean((getConfigProperty "debug"))) {
$ErrorActionPreference = "Stop"
}

# Display start message/ set paths if missing
if (!(testPath (getConfigProperty "gamePath")) -and !(testPath (getConfigProperty "newGamePath"))) {
# Display start message/ set paths if missing/invalid
$pathsExistAndValid = $True
if (getConfigProperty "gamePath" -and getConfigProperty "newGamePath") {
if (!(testPath (getConfigProperty "gamePath")) -or !(testPath (getConfigProperty "newGamePath"))) {
$pathsExistAndValid = $False
}
}

if (!$pathsExistAndValid) {
welcomeScreen
}
278 changes: 278 additions & 0 deletions scripts/utils.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
BeforeAll {
$modulePath = (Join-Path $PSScriptRoot utils.psm1)
Import-Module -Name $modulePath

# Mock default commands
Mock -ModuleName utils Test-Path { return $true }
Mock -ModuleName utils Join-Path { return '/fake/' }
Mock -ModuleName utils Write-Host {}
Mock -ModuleName utils Write-Information {}

Mock -ModuleName utils Get-Date {}
Mock -ModuleName utils Out-File {}
}

Describe "Utils" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
}

Describe "testPath" {

It "should have a testPath function" {
$moduleFunctions | Should -Contain 'testPath'
}

Context "parameters" {
It "should have a parameter named path" {
Get-Command testPath | Should -HaveParameter path -Type String
Get-Command testPath | Should -HaveParameter path -Mandatory:$false
}
}

Context "functionality" {
It "should call Test-Path" {
testPath 'path/fake' |

Should -Be $true
Should -Invoke Test-Path -ModuleName utils -Times 1
}
}
}
}

Describe "fileExists" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
}

It "should have a fileExists function" {
$moduleFunctions | Should -Contain 'fileExists'
}

Context "parameters" {
It "should have a mandatory parameter named path" {
Get-Command fileExists | Should -HaveParameter path -Type String
Get-Command fileExists | Should -HaveParameter path -Mandatory:$true
}
It "should have an optional parameter named fileName" {
Get-Command fileExists | Should -HaveParameter fileName -Type String
Get-Command fileExists | Should -HaveParameter fileName -Mandatory:$false
}
}

Context "functionality" {
It "should call Test-Path" {
fileExists -Path 'path/fake' |

Should -Be $true
Should -Invoke Test-Path -ModuleName utils -Times 1
}

It "should call Join-Path if fileName provided" {
fileExists -Path 'path/fake' -FileName 'test'
Should -Invoke Join-Path -ModuleName utils -Times 1
}
}
}

Describe "writeToConsole" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
Mock -ModuleName utils logToFile -MockWith {}
}

It "should have a writeToConsole function" {
$moduleFunctions | Should -Contain 'writeToConsole'
}

Context "parameters" {
It "should have an optional parameter named msg" {
Get-Command writeToConsole | Should -HaveParameter msg -Type String
Get-Command writeToConsole | Should -HaveParameter msg -Mandatory:$true
}
It "should have an optional parameter named type" {
Get-Command writeToConsole | Should -HaveParameter type -Type switch
Get-Command writeToConsole | Should -HaveParameter type -Mandatory:$false
}

It "should have an optional parameter named color" {
Get-Command writeToConsole | Should -HaveParameter color -Type String
Get-Command writeToConsole | Should -HaveParameter color -Mandatory:$false
}

It "should have an optional parameter named bgcolor" {
Get-Command writeToConsole | Should -HaveParameter bgcolor -Type String
Get-Command writeToConsole | Should -HaveParameter bgcolor -Mandatory:$false
}
It "should have an optional parameter named logPath" {
Get-Command writeToConsole | Should -HaveParameter logPath -Type String
Get-Command writeToConsole | Should -HaveParameter logPath -Mandatory:$false
}
}

Context "functionality" {
It "should call Write-Information when passing only msg" {
writeToConsole -msg "test"
Should -Invoke Write-Information -ModuleName utils -Times 1
}

It "should call Write-Information & logToFile when passing msg and logPath" {
#Add global variable for logpath
$global:LogPath = '/path/'

writeToConsole -msg "test" -logPath '/fake/path'
Should -Invoke Write-Information -ModuleName utils -Times 1
Should -Invoke logToFile -ModuleName utils -Times 1

# Remove global variable
Remove-Variable -Name LogPath -Scope Global
}
}
}

Describe "getLatestFileName" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys

$unsortedArrayOfHexTables =
'hex_table_1.7.29_d4e7645.json',
'hex_table_1.12.30_dd60417.json',
'hex_table_1.9.51_4cde620.json',
'hex_table_1.13.61_2f1f1ce.json'

Mock -ModuleName utils -CommandName 'Get-ChildItem' -MockWith {
return $unsortedArrayOfHexTables | ForEach-Object { @{ Name = $_ } }
}

Mock -ModuleName utils -CommandName 'getFullPath' -MockWith {}
}

It "should have a getLatestFileName function" {
$moduleFunctions | Should -Contain 'getLatestFileName'
}

Context "functionality" {
It "should return the latest file name" {
getLatestFileName |

Should -eq 'hex_table_1.13.61_2f1f1ce.json'
Should -Invoke Get-ChildItem -ModuleName utils -Times 1
}
}
}

Describe "getLatestCommitId" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
Mock -ModuleName utils -CommandName 'getLatestFileName' -MockWith { return 'hex_table_1.13.61_2f1f1ce.json' }
}

It "should have a getLatestCommitId function" {
$moduleFunctions | Should -Contain 'getLatestCommitId'
}

Context "functionality" {
It "should return the latest commit id" {
getLatestCommitId |

Should -eq '2f1f1ce'
Should -Invoke getLatestFileName -ModuleName utils -Times 1
}
}
}

Describe "getGameVersion" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
Mock -ModuleName utils -CommandName 'getLatestFileName' -MockWith { return 'hex_table_1.13.61_2f1f1ce.json' }
}

It "should have a getGameVersion function" {
$moduleFunctions | Should -Contain 'getGameVersion'
}

Context "functionality" {
It "should return the latest game version and replace the delimiter" {
getGameVersion |

Should -eq '1_13_61'
Should -Invoke getLatestFileName -ModuleName utils -Times 1
}
}
}

Describe "getConfigProperty" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
$configJson = '{ "gamePath": "C:\\XboxGames\\Starfield\\Content" }'

Mock -ModuleName utils -CommandName 'Get-Content' -MockWith { return $configJson }
}

It "should have a getConfigProperty function" {
$moduleFunctions | Should -Contain 'getConfigProperty'
}

Context "functionality" {
It "should return the property value if present in the config file" {
getConfigProperty "gamePath" |

Should -eq "C:\XboxGames\Starfield\Content"
Should -Invoke Get-Content -ModuleName utils -Times 1
}

It "should return $null if the property does not exist" {
getConfigProperty "debug" |

Should -BeNullOrEmpty
Should -Invoke Get-Content -ModuleName utils -Times 1
}

It "should write to log if the config file does not exist" {
#Add global variable for logpath
$global:LogPath = '/path/'

Mock -ModuleName utils -CommandName 'fileExists' -MockWith { return $False }
Mock -ModuleName utils -CommandName 'logToFile'

getConfigProperty "gamePath" |

Should -BeNullOrEmpty
Should -Invoke Get-Content -ModuleName utils -Times 0
Should -Invoke fileExists -ModuleName utils -Times 1
Should -Invoke logToFile -ModuleName utils -Times 1

# Remove global variable
Remove-Variable -Name LogPath -Scope Global
}
}
}


Describe "setConfigProperty" {
BeforeAll {
$moduleFunctions = (Get-Module -ListAvailable $modulePath).ExportedFunctions.Keys
$configJson = '{ "gamePath": "C:\\XboxGames\\Starfield\\Content" }'

Mock -ModuleName utils -CommandName 'Get-Content' -MockWith { return $configJson }
}

It "should have a setConfigProperty function" {
$moduleFunctions | Should -Contain 'setConfigProperty'
}

Context "functionality" {
It "should create a new config file, if it doesn't already exist and store the given prop/val" {
}

It "should replace the value of the propert if it already exists in the config" {
}

It "should add the prop/value to the end of the file if it does not exist in the config" {
}

It "should log any errors encountered" {
}
}
}
Loading