From 8533997b68d56da1e6453c503a7910c14d00f78f Mon Sep 17 00:00:00 2001 From: "Rodweil, Theodor" Date: Sun, 30 Jul 2023 20:04:25 +0200 Subject: [PATCH] feat(manifest): initialize manifest --- PSConfluencePublisher/Manifest.Tests.ps1 | 177 +++++++++++++++------ PSConfluencePublisher/Manifest.psm1 | 7 +- PSConfluencePublisher/manifest.schema.json | 4 +- 3 files changed, 139 insertions(+), 49 deletions(-) diff --git a/PSConfluencePublisher/Manifest.Tests.ps1 b/PSConfluencePublisher/Manifest.Tests.ps1 index 2674a9f..55f1760 100755 --- a/PSConfluencePublisher/Manifest.Tests.ps1 +++ b/PSConfluencePublisher/Manifest.Tests.ps1 @@ -3,7 +3,6 @@ $ErrorActionPreference = "Stop" BeforeAll { Import-Module (Join-Path $PSScriptRoot 'PSConfluencePublisher.psd1') -Force - } AfterAll { @@ -13,57 +12,145 @@ AfterAll { Describe 'Get-Manifest' ` { - - Context 'Parameterized' { - - It 'throws no exception' { - - InModuleScope Connection { - - Mock Get-PersonalAccessToken {'01234567890123456789'} - - Mock Invoke-WebRequest { - return @{ - 'Content' = "{'type': 'known'}" - 'StatusCode' = 200 - } + Context 'Parameterized' ` + { + It 'can successfully validate against the schema' ` + { + InModuleScope Manifest ` + { + Mock Get-Content { + return '{"pages":{}, "attachments": {}}' } - Test-Connection -Host 'confluence.contoso.com' + #mocking Get-Content, therefore file name can be bogus + Get-Manifest 'foobar.x' } } - It 'detects anonymous authentication' { - - InModuleScope Connection { - - Mock Get-PersonalAccessToken {'01234567890123456789'} - - Mock Invoke-WebRequest { - return @{ - 'Content' = "{'type': 'anonymous'}" - 'StatusCode' = 200 - } + It 'throws on schema mismatch' ` + { + InModuleScope Manifest ` + { + Mock Get-Content { + return '{"pagges":{}, "attsdachments": {}}' } - {Test-Connection -Host 'confluence.contoso.com'} | Should -Throw - } - } - - It 'detects non 200 status codes' { - - InModuleScope Connection { - - Mock Get-PersonalAccessToken {'01234567890123456789'} - - Mock Invoke-WebRequest { - return @{ - 'Content' = "{'type': 'anonymous'}" - 'StatusCode' = 500 - } - } - - {Test-Connection -Host 'confluence.contoso.com'} | Should -Throw + #mocking Get-Content, therefore file name can be bogus + {Get-Manifest 'foobar.x'} | Should -Throw + } + } + } +} + + +Describe 'Set-Manifest' ` +{ + Context 'noBackup' ` + { + It 'can successfully validate against the schema' ` + { + InModuleScope Manifest ` + { + $mockManifest = @{ + '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 } } } diff --git a/PSConfluencePublisher/Manifest.psm1 b/PSConfluencePublisher/Manifest.psm1 index dd52e27..e69339f 100755 --- a/PSConfluencePublisher/Manifest.psm1 +++ b/PSConfluencePublisher/Manifest.psm1 @@ -25,11 +25,13 @@ function Get-Manifest { try { - $raw = Get-Content $File + $raw = Get-Content $File | Out-String } catch - { + { + Write-Debug $_ + $raw = '{"pages":{}, "attachments": {}}' } @@ -66,6 +68,7 @@ function Set-Manifest if ($Backup) { + #FIXME: this always assumes the current working directory Copy-Item -Path $File -Destination "$(Split-Path -Leaf $File).bck" } diff --git a/PSConfluencePublisher/manifest.schema.json b/PSConfluencePublisher/manifest.schema.json index 62be76f..1910e70 100755 --- a/PSConfluencePublisher/manifest.schema.json +++ b/PSConfluencePublisher/manifest.schema.json @@ -1,5 +1,5 @@ { - "$id": "https://spec.victory-k.it/psconfluencepublisher.json", + "$id": "https://spec.victory-k.it/psconfluencepublisher.json", "x-authors": [ "theodor.rodweil@victory-k.it" ], @@ -90,4 +90,4 @@ ] } } -} \ No newline at end of file +}