feat(manifest): initialize manifest

This commit is contained in:
Rodweil, Theodor 2023-07-30 20:04:25 +02:00
parent b8221f9abf
commit 8533997b68
No known key found for this signature in database
GPG key ID: F8BC1B0EB1F9CCF5
3 changed files with 139 additions and 49 deletions

View file

@ -3,7 +3,6 @@ $ErrorActionPreference = "Stop"
BeforeAll { BeforeAll {
Import-Module (Join-Path $PSScriptRoot 'PSConfluencePublisher.psd1') -Force Import-Module (Join-Path $PSScriptRoot 'PSConfluencePublisher.psd1') -Force
} }
AfterAll { AfterAll {
@ -13,57 +12,145 @@ AfterAll {
Describe 'Get-Manifest' ` Describe 'Get-Manifest' `
{ {
Context 'Parameterized' `
Context 'Parameterized' { {
It 'can successfully validate against the schema' `
It 'throws no exception' { {
InModuleScope Manifest `
InModuleScope Connection { {
Mock Get-Content {
Mock Get-PersonalAccessToken {'01234567890123456789'} return '{"pages":{}, "attachments": {}}'
Mock Invoke-WebRequest {
return @{
'Content' = "{'type': 'known'}"
'StatusCode' = 200
}
} }
Test-Connection -Host 'confluence.contoso.com' #mocking Get-Content, therefore file name can be bogus
Get-Manifest 'foobar.x'
} }
} }
It 'detects anonymous authentication' { It 'throws on schema mismatch' `
{
InModuleScope Connection { InModuleScope Manifest `
{
Mock Get-PersonalAccessToken {'01234567890123456789'} Mock Get-Content {
return '{"pagges":{}, "attsdachments": {}}'
Mock Invoke-WebRequest {
return @{
'Content' = "{'type': 'anonymous'}"
'StatusCode' = 200
}
} }
{Test-Connection -Host 'confluence.contoso.com'} | Should -Throw #mocking Get-Content, therefore file name can be bogus
} {Get-Manifest 'foobar.x'} | Should -Throw
} }
}
It 'detects non 200 status codes' { }
}
InModuleScope Connection {
Mock Get-PersonalAccessToken {'01234567890123456789'} Describe 'Set-Manifest' `
{
Mock Invoke-WebRequest { Context 'noBackup' `
return @{ {
'Content' = "{'type': 'anonymous'}" It 'can successfully validate against the schema' `
'StatusCode' = 500 {
} InModuleScope Manifest `
} {
$mockManifest = @{
{Test-Connection -Host 'confluence.contoso.com'} | Should -Throw 'pages' = @{}
'attachments' = @{}
}
Mock Set-Content {
Should -Invoke -CommandName 'Set-Content' -Exactly -Times 1
$args[1] | Should -Be 'foobar.x'
$args[3] | Should -Be ($mockManifest | ConvertTo-JSON)
}
#mocking Get-Content, therefore file name can be bogus
Set-Manifest `
-Manifest $mockManifest `
-File 'foobar.x'
}
}
It 'declines setting invalid schema' `
{
InModuleScope Manifest `
{
$mockManifest = @{
'pagges' = @{}
'attachments' = @{}
}
#mocking Get-Content, therefore file name can be bogus
{
Set-Manifest `
-Manifest $mockManifest `
-File 'foobar.x'
} | Should -Throw
}
}
}
Context 'Backup' `
{
It 'creates a backup when it should' `
{
InModuleScope Manifest `
{
$mockManifest = @{
'pages' = @{}
'attachments' = @{}
}
Mock Set-Content {
#FIXME: the scope is completely wrong
Should -Invoke -CommandName 'Set-Content' -Exactly -Times 1
}
Mock Copy-Item {
#FIXME: the scope is completely wrong
Should -Invoke -CommandName 'Copy-Item' -Exactly -Times 1
$args[1] | Should -Be 'foobar.x'
$args[3] | Should -Be 'foobar.x.bck'
}
#mocking Get-Content, therefore file name can be bogus
Set-Manifest `
-Manifest $mockManifest `
-File 'foobar.x' `
-Backup $true
}
}
It 'handles paths outside of the current working directory correctly' `
{
InModuleScope Manifest `
{
$mockManifest = @{
'pages' = @{}
'attachments' = @{}
}
Mock Set-Content {
#FIXME: the scope is completely wrong
Should -Invoke -CommandName 'Set-Content' -Exactly -Times 1
}
Mock Copy-Item {
#FIXME: the scope is completely wrong
Should -Invoke -CommandName 'Copy-Item' -Exactly -Times 1
$args[1] | Should -Be 'foo/bar/foobar.x'
$args[3] | Should -Be 'foo/bar/foobar.x.bck'
}
#mocking Get-Content, therefore file name can be bogus
Set-Manifest `
-Manifest $mockManifest `
-File 'foo/bar/foobar.x' `
-Backup $true
} }
} }
} }

View file

@ -25,11 +25,13 @@ function Get-Manifest
{ {
try try
{ {
$raw = Get-Content $File $raw = Get-Content $File | Out-String
} }
catch catch
{ {
Write-Debug $_
$raw = '{"pages":{}, "attachments": {}}' $raw = '{"pages":{}, "attachments": {}}'
} }
@ -66,6 +68,7 @@ function Set-Manifest
if ($Backup) if ($Backup)
{ {
#FIXME: this always assumes the current working directory
Copy-Item -Path $File -Destination "$(Split-Path -Leaf $File).bck" Copy-Item -Path $File -Destination "$(Split-Path -Leaf $File).bck"
} }

View file

@ -1,5 +1,5 @@
{ {
"$id": "https://spec.victory-k.it/psconfluencepublisher.json", "$id": "https://spec.victory-k.it/psconfluencepublisher.json",
"x-authors": [ "x-authors": [
"theodor.rodweil@victory-k.it" "theodor.rodweil@victory-k.it"
], ],