174 lines
6.0 KiB
PowerShell
174 lines
6.0 KiB
PowerShell
# Get-GiteaChildItem.Tests.ps1
|
|
|
|
# Import the module under test
|
|
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\PS-GiteaUtilities.psm1'
|
|
Import-Module -Name $modulePath -Force
|
|
|
|
Describe 'Get-GiteaChildItem' {
|
|
|
|
BeforeEach {
|
|
# Mock config load
|
|
Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith {
|
|
return @{
|
|
giteaURL = 'https://mock.gitea.com'
|
|
defaultOwner = 'mockuser'
|
|
defaultRepo = 'mockrepo'
|
|
defaultBranch = 'main'
|
|
token = 'mocktoken'
|
|
}
|
|
}
|
|
|
|
# Mock LFS config
|
|
Mock -CommandName Get-GiteaLFSConfiguration -ModuleName PS-GiteaUtilities -MockWith {
|
|
return @(".lfs")
|
|
}
|
|
|
|
# Mock web call to list files
|
|
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith {
|
|
return @(
|
|
@{
|
|
name = "file1.txt"
|
|
path = "file1.txt"
|
|
type = "file"
|
|
size = 123
|
|
sha = "abc123"
|
|
download_url = "https://mock.gitea.com/file1.txt"
|
|
},
|
|
@{
|
|
name = "subdir"
|
|
path = "subdir"
|
|
type = "dir"
|
|
size = 0
|
|
sha = $null
|
|
download_url = $null
|
|
}
|
|
)
|
|
}
|
|
|
|
Mock -CommandName Write-Error -ModuleName PS-GiteaUtilities -MockWith { }
|
|
}
|
|
|
|
Context 'When listing basic items' {
|
|
It 'Should return files and directories correctly' {
|
|
$result = Get-GiteaChildItem -repoOwner 'mockuser' -repoName 'mockrepo' -Path '' -token 'mocktoken'
|
|
|
|
$result | Should -Not -BeNullOrEmpty
|
|
$result.Count | Should -BeGreaterThan 0
|
|
|
|
$file = $result | Where-Object { $_.type -eq 'file' }
|
|
$dir = $result | Where-Object { $_.type -eq 'dir' }
|
|
|
|
$file.name | Should -Be 'file1.txt'
|
|
$dir.name | Should -Be 'subdir'
|
|
}
|
|
}
|
|
|
|
Context 'When filtering only files' {
|
|
It 'Should return only files when -File is used' {
|
|
$result = Get-GiteaChildItem -repoOwner 'mockuser' -repoName 'mockrepo' -Path '' -token 'mocktoken' -File
|
|
|
|
$result | Should -Not -BeNullOrEmpty
|
|
$result.Count | Should -Be 1
|
|
$result[0].type | Should -Be 'file'
|
|
}
|
|
}
|
|
|
|
Context 'When recursing into directories' {
|
|
BeforeEach {
|
|
# Mock recursion call
|
|
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith {
|
|
param($Uri)
|
|
|
|
if ($Uri -like "*subdir*") {
|
|
return @(
|
|
@{
|
|
name = "nestedfile.txt"
|
|
path = "subdir/nestedfile.txt"
|
|
type = "file"
|
|
size = 456
|
|
sha = "def456"
|
|
download_url = "https://mock.gitea.com/subdir/nestedfile.txt"
|
|
}
|
|
)
|
|
} else {
|
|
return @(
|
|
@{
|
|
name = "file1.txt"
|
|
path = "file1.txt"
|
|
type = "file"
|
|
size = 123
|
|
sha = "abc123"
|
|
download_url = "https://mock.gitea.com/file1.txt"
|
|
},
|
|
@{
|
|
name = "subdir"
|
|
path = "subdir"
|
|
type = "dir"
|
|
size = 0
|
|
sha = $null
|
|
download_url = $null
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
It 'Should retrieve nested files when -Recurse is used' {
|
|
$result = Get-GiteaChildItem -repoOwner 'mockuser' -repoName 'mockrepo' -Path '' -token 'mocktoken' -Recurse
|
|
|
|
$nested = $result | Where-Object { $_.Path -eq 'subdir/nestedfile.txt' }
|
|
|
|
$nested | Should -Not -BeNullOrEmpty
|
|
$nested.size | Should -Be 456
|
|
}
|
|
}
|
|
|
|
Context 'When recursion depth limit is enforced' {
|
|
BeforeEach {
|
|
# Same recursion mock as above
|
|
Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith {
|
|
param($Uri)
|
|
|
|
if ($Uri -like "*subdir*") {
|
|
return @(
|
|
@{
|
|
name = "nestedfile.txt"
|
|
path = "subdir/nestedfile.txt"
|
|
type = "file"
|
|
size = 456
|
|
sha = "def456"
|
|
download_url = "https://mock.gitea.com/subdir/nestedfile.txt"
|
|
}
|
|
)
|
|
} else {
|
|
return @(
|
|
@{
|
|
name = "file1.txt"
|
|
path = "file1.txt"
|
|
type = "file"
|
|
size = 123
|
|
sha = "abc123"
|
|
download_url = "https://mock.gitea.com/file1.txt"
|
|
},
|
|
@{
|
|
name = "subdir"
|
|
path = "subdir"
|
|
type = "dir"
|
|
size = 0
|
|
sha = $null
|
|
download_url = $null
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
It 'Should not recurse deeper than depth limit' {
|
|
$result = Get-GiteaChildItem -repoOwner 'mockuser' -repoName 'mockrepo' -Path '' -token 'mocktoken' -Recurse -Depth 0
|
|
|
|
$nested = $result | Where-Object { $_.Path -eq 'subdir/nestedfile.txt' }
|
|
$nested | Should -BeNullOrEmpty
|
|
}
|
|
}
|
|
}
|