functions/Get-EndjinGist.Tests.ps1
|
# <copyright file="Get-EndjinGist.Tests.ps1" company="Endjin Limited"> # Copyright (c) Endjin Limited. All rights reserved. # </copyright> Describe "Get-EndjinGist" { BeforeAll { # Define stubs for external module functions to avoid dependency and ensure mocking works function ConvertTo-Yaml { param($Data, $OutFile, [switch]$Force) } function ConvertFrom-Yaml { param([switch]$Ordered) } $sut = "$PSScriptRoot\Get-EndjinGist.ps1" . $sut # Mock Data $mockGistMap = @{ 'test-group' = @( @{ name = 'test-gist' source = 'https://github.com/org/repo.git' ref = 'main' includePaths = @('path/to/include') } ) } } Context "Prerequisites" { It "Should throw if 'vendir' is not in PATH" { Mock Get-Command { return $null } -ParameterFilter { $Name -eq 'vendir' } { Get-EndjinGist -Group 'any' -Name 'any' -GistMapPath 'dummy' } | Should -Throw "Please install 'vendir' to your PATH before using this tool." } } Context "Input Validation" { BeforeAll { Mock Get-Command { return $true } -ParameterFilter { $Name -eq 'vendir' } Mock Get-Content { return "yaml content" } Mock ConvertFrom-Yaml { return $mockGistMap } } It "Should throw if Group is unknown" { { Get-EndjinGist -Group 'unknown-group' -Name 'test-gist' -GistMapPath 'dummy' } | Should -Throw "Unknown gist group: unknown-group" } It "Should throw if Name is unknown in Group" { { Get-EndjinGist -Group 'test-group' -Name 'unknown-gist' -GistMapPath 'dummy' } | Should -Throw "Unknown gist 'unknown-gist' in group 'test-group'" } } Context "Execution" { BeforeAll { Mock Get-Command { return $true } -ParameterFilter { $Name -eq 'vendir' } Mock Get-Content { return "yaml content" } Mock ConvertFrom-Yaml { return $mockGistMap } Mock ConvertTo-Yaml { } Mock Remove-Item { } Mock Write-Host { } } It "Should invoke vendir with generated config" { Mock Invoke-Command { $global:LASTEXITCODE = 0 return "Success" } Get-EndjinGist -Group 'test-group' -Name 'test-gist' -GistMapPath 'dummy' Assert-MockCalled ConvertTo-Yaml -Times 1 Assert-MockCalled Invoke-Command -Times 1 } It "Should handle vendir failure" { Mock Invoke-Command { # Simulate failure $global:LASTEXITCODE = 1 return "Error message" } { Get-EndjinGist -Group 'test-group' -Name 'test-gist' -GistMapPath 'dummy' } | Should -Throw "Operation failed with exit code 1.`nError message" } } } |