Add unit tests for Set-GiteaConfiguration, Get-GiteaConfiguration, and Get-GiteaFileContent functions
This commit is contained in:
64
PS-GiteaUtilities/Tests/Set-GiteaConfiguration.Tests.ps1
Normal file
64
PS-GiteaUtilities/Tests/Set-GiteaConfiguration.Tests.ps1
Normal file
@@ -0,0 +1,64 @@
|
||||
# Set-GiteaConfiguration.Tests.ps1
|
||||
|
||||
Describe 'Set-GiteaConfiguration' {
|
||||
|
||||
BeforeAll {
|
||||
# Import the module dynamically
|
||||
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\PS-GiteaUtilities.psm1'
|
||||
Import-Module -Name $modulePath -Force
|
||||
|
||||
}
|
||||
|
||||
Context 'Parameter Validation' {
|
||||
It 'Should throw a MissingArgument exception if required parameters are missing' {
|
||||
{ Set-GiteaConfiguration -giteaURL -token} | Should -Throw
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Context 'Configuration Setup' {
|
||||
BeforeEach {
|
||||
Mock -CommandName Test-Path -ModuleName PS-GiteaUtilities -MockWith { $false }
|
||||
Mock -CommandName New-Item -ModuleName PS-GiteaUtilities -MockWith { return $null }
|
||||
Mock -CommandName Export-Clixml -ModuleName PS-GiteaUtilities -MockWith { return $null }
|
||||
}
|
||||
|
||||
It 'Should create the configuration directory if it does not exist' {
|
||||
Set-GiteaConfiguration -giteaURL 'https://gitea.example.com' -token 'ABC123' -defaultOwner 'testuser' -defaultRepo 'testrepo'
|
||||
|
||||
Assert-MockCalled -CommandName New-Item -ModuleName PS-GiteaUtilities -Times 1
|
||||
}
|
||||
|
||||
It 'Should export the configuration to an XML file' {
|
||||
Set-GiteaConfiguration -giteaURL 'https://gitea.example.com' -token 'ABC123' -defaultOwner 'testuser' -defaultRepo 'testrepo'
|
||||
|
||||
Assert-MockCalled -CommandName Export-Clixml -ModuleName PS-GiteaUtilities -Times 1
|
||||
}
|
||||
|
||||
It 'Should set defaultBranch to "main" if not specified' {
|
||||
# Important: override the Export-Clixml mock to capture the config
|
||||
Mock -CommandName Export-Clixml -ModuleName PS-GiteaUtilities -MockWith {
|
||||
param($Path, $InputObject)
|
||||
$script:CapturedConfig = $InputObject
|
||||
}
|
||||
|
||||
Set-GiteaConfiguration -giteaURL 'https://gitea.example.com' -token 'ABC123' -defaultOwner 'testuser' -defaultRepo 'testrepo'
|
||||
|
||||
$script:CapturedConfig.defaultBranch | Should -Be 'main'
|
||||
}
|
||||
}
|
||||
|
||||
Context 'When directory already exists' {
|
||||
BeforeEach {
|
||||
Mock -CommandName Test-Path -MockWith { $true }
|
||||
Mock -CommandName New-Item -MockWith { throw "Should not be called" }
|
||||
Mock -CommandName Export-Clixml -MockWith { return $null }
|
||||
}
|
||||
|
||||
It 'Should not attempt to create the directory' {
|
||||
Set-GiteaConfiguration -giteaURL 'https://gitea.example.com' -token 'ABC123' -defaultOwner 'testuser' -defaultRepo 'testrepo'
|
||||
|
||||
Assert-MockCalled -CommandName New-Item -Times 0
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user