refactor: make this a valid nuget package for Powershell gallery

feat: add build environment scripts
This commit is contained in:
Rodweil, Theodor 2023-08-07 20:56:50 +02:00
parent 69c2684ee7
commit 46b1b7c83f
No known key found for this signature in database
GPG key ID: F8BC1B0EB1F9CCF5
38 changed files with 200 additions and 42 deletions

83
tests/Connection.Tests.ps1 Executable file
View file

@ -0,0 +1,83 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' `
'PSConfluencePublisher.psd1')
}
Describe 'Test-Connection' `
{
Context 'default' {
BeforeAll `
{
Mock -ModuleName 'Connection' Get-PersonalAccessToken {
'01234567890123456789'
}
}
It 'throws no exception' {
Mock -ModuleName 'Connection' Invoke-WebRequest {
@{
'Content' = "{'type': 'known'}"
'StatusCode' = 200
}
}
Test-Connection -Host 'confluence.contoso.com'
Should -Invoke -CommandName 'Get-PersonalAccessToken' `
-ModuleName 'Connection' ` `
-Exact `
-Times 1
Should -Invoke -CommandName 'Invoke-WebRequest' `
-ModuleName 'Connection' ` `
-Exact `
-Times 1
}
It 'detects anonymous authentication' {
Mock -ModuleName 'Connection' Invoke-WebRequest {
@{
'Content' = "{'type': 'anonymous'}"
'StatusCode' = 200
}
}
{Test-Connection -Host 'confluence.contoso.com'} | Should -Throw
Should -Invoke -CommandName 'Get-PersonalAccessToken' `
-ModuleName 'Connection' ` `
-Exact `
-Times 1
Should -Invoke -CommandName 'Invoke-WebRequest' `
-ModuleName 'Connection' ` `
-Exact `
-Times 1
}
It 'detects non 200 status codes' {
Mock -ModuleName 'Connection' Invoke-WebRequest {
@{
'Content' = "{'type': 'anonymous'}"
'StatusCode' = 500
}
}
{Test-Connection -Host 'confluence.contoso.com'} | Should -Throw
Should -Invoke -CommandName 'Get-PersonalAccessToken' `
-ModuleName 'Connection' ` `
-Exact `
-Times 1
Should -Invoke -CommandName 'Invoke-WebRequest' `
-ModuleName 'Connection' ` `
-Exact `
-Times 1
}
}
}

417
tests/Manifest.Tests.ps1 Executable file
View file

@ -0,0 +1,417 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' `
'PSConfluencePublisher.psd1')
}
AfterAll {
}
Describe 'Get-Manifest' `
{
Context 'Parameterized' `
{
It 'can successfully validate against the schema' `
{
InModuleScope Manifest `
{
Mock Get-Content {
return '{"Pages":[], "Attachments": []}'
}
#mocking Get-Content, therefore file name can be bogus
Get-Manifest 'foobar.x'
}
}
It 'throws on schema mismatch' `
{
InModuleScope Manifest `
{
Mock Get-Content {
return '{"pagges":[], "attsdachments": []}'
}
#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
$Path | Should -Be 'foobar.x'
$Value | 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 `
$Path | Should -Be 'foobar.x'
$Destination | 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 `
$Path | Should -Be 'foo/bar/foobar.x'
$Destination | 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
}
}
}
}
Describe 'New-PagesManifestIndex' `
{
Context 'default' `
{
BeforeEach `
{
$mockManifest = @(
@{
'Title' = 'foobar0'
},
@{
'Title' = 'foobar1'
}
)
}
It 'from pipeline' `
{
$index = New-PagesManifestIndex -Manifest $mockManifest
$index.foobar0 | Should -Be 0
$index.foobar1 | Should -Be 1
}
It 'from pipeline' `
{
$index = ,$mockManifest | New-PagesManifestIndex
$index.foobar0 | Should -Be 0
$index.foobar1 | Should -Be 1
}
}
}
Describe 'New-AttachmentsManifestIndex' `
{
Context 'default' `
{
BeforeEach `
{
$mockManifest = @(
@{
'ContainerPageTitle' = 'foobar0'
'Name' = 'attachment0'
},
@{
'ContainerPageTitle' = 'foobar1'
'Name' = 'attachment1'
}
)
}
It 'from parameter' `
{
$index = New-AttachmentsManifestIndex -Manifest $mockManifest
$index."foobar0:attachment0" | Should -Be 0
$index."foobar1:attachment1" | Should -Be 1
}
It 'from pipeline' `
{
$index = ,$mockManifest | New-AttachmentsManifestIndex
$index."foobar0:attachment0" | Should -Be 0
$index."foobar1:attachment1" | Should -Be 1
}
}
}
Describe 'Get-AncestralPageGenerationCache' `
{
Context 'default' `
{
BeforeEach `
{
$mockManifest = @(
@{
'Title' = 'foobar0'
},
@{
'Title' = 'foobar1'
'AncestorTitle' = 'foobar0'
}
)
$mockIndex = @{
'foobar0' = 0
'foobar1' = 1
}
}
It 'selects single operation by title' `
{
$result = New-AncestralPageGenerationCache `
-Title 'foobar0' `
-Manifest $mockManifest `
-Index $mockIndex
$result.Count | Should -Be 1
$result.foobar0 | Should -Be 0
}
It 'automatically builds index' `
{
$result = New-AncestralPageGenerationCache `
-Title 'foobar0' `
-Manifest $mockManifest `
$result.Count | Should -Be 1
$result.foobar0 | Should -Be 0
}
It 'accepts a pipeline' `
{
$result = New-AncestralPageGenerationCache `
-Manifest $mockManifest `
-Index $mockIndex
$result.Count | Should -Be 2
$result.foobar0 | Should -Be 0
$result.foobar1 | Should -Be 1
}
}
Context 'more complex' `
{
BeforeEach `
{
$mockManifest = @(
@{
'Title' = 'foobar0'
},
@{
'Title' = 'foobar4'
'AncestorTitle' = 'foobar3'
},
@{
'Title' = 'foobar1'
'AncestorTitle' = 'foobar0'
},
@{
'Title' = 'foobar3'
'AncestorTitle' = 'foobar2'
},
@{
'Title' = 'foobar2'
'AncestorTitle' = 'foobar1'
}
)
$mockIndex = @{
'foobar0' = 0
'foobar1' = 2
'foobar2' = 4
'foobar3' = 3
'foobar4' = 1
}
}
It 'uses index' `
{
$result = New-AncestralPageGenerationCache `
-Manifest $mockManifest `
-Index $mockIndex
$result.foobar0 | Should -Be 0
$result.foobar1 | Should -Be 1
$result.foobar2 | Should -Be 2
$result.foobar3 | Should -Be 3
$result.foobar4 | Should -Be 4
}
}
}
Describe 'Optimize-PagesManifest' `
{
Context 'default' `
{
BeforeEach `
{
$mockManifest = @(
@{
'Title' = 'foobar0'
},
@{
'Title' = 'foobar4'
'AncestorTitle' = 'foobar3'
},
@{
'Title' = 'foobar1'
'AncestorTitle' = 'foobar0'
},
@{
'Title' = 'foobar3'
'AncestorTitle' = 'foobar2'
},
@{
'Title' = 'foobar5'
'AncestorTitle' = 'foobar2'
},
@{
'Title' = 'foobar2'
'AncestorTitle' = 'foobar1'
}
)
$mockGenerationCache = @{
'foobar0' = 0
'foobar1' = 1
'foobar2' = 2
'foobar3' = 3
'foobar4' = 4
'foobar5' = 3
}
}
It 'from parameter' `
{
Optimize-PagesManifest `
-Manifest $mockManifest `
-Lo 0 `
-Hi ($mockManifest.Count - 1) `
-GenerationCache $mockGenerationCache
$mockManifest[0].Title | Should -Be 'foobar0'
$mockManifest[1].Title | Should -Be 'foobar1'
$mockManifest[2].Title | Should -Be 'foobar2'
$mockManifest[3].Title | Should -Be 'foobar5'
$mockManifest[4].Title | Should -Be 'foobar3'
$mockManifest[5].Title | Should -Be 'foobar4'
}
}
}

260
tests/Page.Tests.ps1 Executable file
View file

@ -0,0 +1,260 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' `
'PSConfluencePublisher.psd1')
}
Describe 'New-Page' `
{
Context 'default' `
{
BeforeAll `
{
Mock -ModuleName 'Page' Get-Content {
'foobar content'
}
Mock -ModuleName 'Page' Get-PersonalAccessToken {
'01234567890123456789'
}
}
It 'succeeds' `
{
$mockPageMeta = @{
'Title' = 'foobar'
'Ref' = 'pages/320okffs.xml'
}
$mockManifest = @(
$mockPageMeta
)
Mock -ModuleName 'Page' Get-PageMeta {
$mockPageMeta
}
Mock -ModuleName 'Page' Update-PageMeta {
$Id | Should -Be '123'
$mockPageMeta.Id = '123'
$mockPageMeta.Version = 1
$mockPageMeta.Hash = 'NOTAREALHASH'
$mockPageMeta
}
Mock -ModuleName 'Page' Invoke-WebRequest {
$Uri | Should -Be 'https://confluence.contoso.com/rest/api/content'
$body_ = $Body | ConvertFrom-JSON
$body_.type | Should -Be 'page'
$body_.body.storage.representation | Should -Be 'storage'
$body_.body.storage.value | Should -Be 'foobar content'
$body_.space.key | Should -Be 'testitest'
$body_.title | Should -Be 'title'
@{
'Content' = '{"Id": "123", "version": {"number": 1}}'
}
}
New-Page `
-Host 'confluence.contoso.com' `
-Space 'testitest' `
-Title 'title' `
-Manifest $mockManifest
$mockPageMeta.Id | Should -Be "123"
$mockPageMeta.Version | Should -Be 1
$mockPageMeta.Hash | Should -Be (
'NOTAREALHASH'
)
Should -Invoke -CommandName 'Get-PageMeta' `
-ModuleName 'Page' `
-Exactly `
-Times 1
Should -Invoke -CommandName 'Update-PageMeta' `
-ModuleName 'Page' `
-Exactly `
-Times 1
}
}
}
Describe 'Update-Page' `
{
BeforeAll `
{
Mock -ModuleName 'Page' Get-Content {
'foobar content'
}
Mock -ModuleName 'Page' Get-PersonalAccessToken {
'01234567890123456789'
}
}
Context 'default' `
{
BeforeAll `
{
Mock -ModuleName 'Page' Get-StringHash {
@{
'Hash' = 'NOTAREALHASH'
}
}
}
It 'succeeds' `
{
$mockPageId = '0123456789'
$mockPageMeta = @{
'Title' = 'foobar'
'Ref' = 'pages/320okffs.xml'
'Id' = $mockPageId
'Version' = 3
}
$mockManifest = @(
$mockPageMeta
)
Mock -ModuleName 'Page' Get-PageMeta {
$mockPageMeta
}
Mock -ModuleName 'Page' Invoke-WebRequest {
$Uri | Should -Be (
'https://confluence.contoso.com/rest/api/content/' + `
$mockPageId
)
$body_ = $Body | ConvertFrom-JSON
$body_.type | Should -Be 'page'
$body_.body.storage.representation | Should -Be 'storage'
$body_.body.storage.value | Should -Be 'foobar content'
$body_.space.key | Should -Be 'testitest'
$body_.title | Should -Be 'foobar'
$body_.version.number | Should -Be 4
@{
'Content' = '{"Id": "123", "version": {"number": 4}}'
}
}
Update-Page `
-Host 'confluence.contoso.com' `
-Space 'testitest' `
-Title 'foobar' `
-Manifest $mockManifest
$mockPageMeta.Hash | Should -Be 'NOTAREALHASH'
$mockPageMeta.Version | Should -Be 4
}
It 'skips, if hash unchanged' `
{
$mockPageId = '0123456789'
$mockPageMeta = @{
'Title' = 'foobar'
'Ref' = 'pages/320okffs.xml'
'Id' = $mockPageId
'Version' = 3
'Hash' = 'NOTAREALHASH'
}
$mockManifest = @(
$mockPageMeta
)
Mock -ModuleName 'Page' Get-PageMeta {
$mockPageMeta
}
Update-Page `
-Host 'confluence.contoso.com' `
-Space 'testitest' `
-Title 'mockTitle' `
-Manifest $mockManifest
}
It 'fails, if page meta has no reference' `
{
$mockPageId = '0123456789'
$mockPageMeta = @{
'Title' = 'foobar'
'Id' = $mockPageId
'Version' = 3
}
$mockManifest = @(
$mockPageMeta
)
Mock -ModuleName 'Page' Get-PageMeta {
$mockPageMeta
}
{
Update-Page `
-Host 'confluence.contoso.com' `
-Space 'testitest' `
-Title 'mockTitle' `
-Manifest $mockManifest
} | Should -Throw "no reference to local content for page*"
}
It 'fails, if page meta has no id' `
{
$mockPageId = '0123456789'
$mockPageMeta = @{
'Title' = 'foobar'
'Ref' = 'foo/bar'
}
$mockManifest = @(
$mockPageMeta
)
Mock -ModuleName 'Page' Get-PageMeta {
$mockPageMeta
}
{
Update-Page `
-Host 'confluence.contoso.com' `
-Space 'testitest' `
-Title 'mockTitle' `
-Manifest $mockManifest
} | Should -Throw "no id for page*"
}
}
}

258
tests/PageMeta.Tests.ps1 Executable file
View file

@ -0,0 +1,258 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' `
'PSConfluencePublisher.psd1')
}
Describe 'Get-PageMetaCache' `
{
Context 'default' `
{
It 'uses index' `
{
$mockPageMeta = @{
'Title' = 'foobar'
}
$mockManifest = @(
$mockPageMeta
)
$mockIndex = @{
'foobar' = 0
}
$meta = Get-PageMetaCache `
-Title 'foobar' `
-Manifest $mockManifest `
-Index $mockIndex
$meta | Should -Be $mockPageMeta
}
It 'returns page meta when title exists' `
{
$mockPageMeta = @{
'Title' = 'foobar'
}
$mockManifest = @(
$mockPageMeta
)
$meta = Get-PageMetaCache `
-Title 'foobar' `
-Manifest $mockManifest
$meta | Should -Be $mockPageMeta
}
It 'returns null, if page with supplied title does not exist' `
{
$mockManifest = @(
@{}
)
$meta = Get-PageMetaCache `
-Title 'foobar' `
-Manifest $mockManifest
$meta | Should -Be $null
}
}
}
Describe 'Get-PageMeta' `
{
Context 'default' `
{
BeforeAll `
{
Mock -ModuleName 'PageMeta' Get-PersonalAccessToken {
'012345678901234567890'
}
}
It 'returns cache when page id present' `
{
$mockPageMeta = @{
'Title' = 'foobar'
'Id' = '0123456789'
}
$mockManifest = @(
$mockPageMeta
)
Mock -ModuleName 'PageMeta' Get-PageMetaCache {
$mockPageMeta
}
$meta = Get-PageMeta `
-Host 'foobar' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest $mockManifest
$meta | Should -Be $mockPageMeta
Should -Invoke -CommandName 'Get-PageMetaCache' `
-ModuleName 'PageMeta' `
-Exact `
-Times 1
}
It 'gets a page id remotely if there is exactly one result' `
{
$mockPageMeta = @{
'Version' = 'version'
'Hash' = 'hash'
'Ref' = 'ref'
}
Mock -ModuleName 'PageMeta' Get-PageMetaCache {
$mockPageMeta
}
Mock -ModuleName 'PageMeta' Update-PageMeta {
$Id | Should -Be '123'
$Version | Should -Be 9
$Title | Should -Be 'foobar'
$mockPageMeta
}
Mock -ModuleName 'PageMeta' Invoke-WebRequest {
@{
'Content' = '{"results": [{"id": "123","_expandable":{"version": 9}}]}'
}
}
$meta = Get-PageMeta `
-Host 'confluence.contoso.com' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest @{'Pages'= {}}
$meta | Should -Be $mockPageMeta
Should -Invoke 'Get-PageMetaCache' `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
Should -Invoke 'Invoke-WebRequest' `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
Should -Invoke 'Update-PageMeta' `
-ModuleName 'PageMeta' `
-Exactly `
-Times 1
}
It 'throws an exception, if there is more than one result' `
{
Mock -ModuleName 'PageMeta' Invoke-WebRequest {
@{
'Content' = '{"results": [{}, {}]}'
}
}
{
Get-PageMeta `
-Host 'confluence.contoso.com' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest @{'Pages'= {}}
} | Should -Throw 'more than one result for query*'
}
It 'throws an exception, if there is no result' `
{
Mock -ModuleName 'PageMeta' Invoke-WebRequest {
@{
'Content' = '{"results": []}'
}
}
$result = Get-PageMeta `
-Host 'confluence.contoso.com' `
-Title 'foobar' `
-Space 'foobar' `
-Manifest @{'Pages'= {}}
$result | Should -Be $null
}
}
}
Describe 'Update-PageMeta' `
{
Context 'default' `
{
It 'fails, if page meta index does not exist' `
{
{
Update-PageMeta `
-Id '0123456789' `
-Title 'foobar' `
-Manifest @{}
} | Should -Throw
}
It 'updates minimal' `
{
$mockPageMeta = @{
'Title' = 'foobar'
}
$mockManifest = @(
$mockPageMeta
)
$pageMeta = Update-PageMeta `
-Title 'foobar' `
-Id '0123456789' `
-Manifest $mockManifest
$mockPageMeta.Id | Should -Be '0123456789'
}
It 'updates extended' `
{
$mockPageMeta = @{
'Title' = 'foobar'
}
$mockManifest = @(
$mockPageMeta
)
Update-PageMeta `
-Title 'foobar' `
-Id 'pageId' `
-Version 9001 `
-AncestorTitle 'ancestorTitle' `
-Hash 'hash' `
-Manifest $mockManifest
$mockPageMeta.Id | Should -Be 'pageId'
$mockPageMeta.Version | Should -Be 9001
$mockPageMeta.AncestorTitle | Should -Be 'ancestorTitle'
$mockPageMeta.Hash | Should -Be 'hash'
}
}
}

View file

@ -0,0 +1,66 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' `
'PSConfluencePublisher.psd1')
$mockHost = 'confluence.contoso.com'
$mockPat = '01234567890123456789'
}
Describe 'Register-PersonalAccessToken' `
{
BeforeEach {
Initialize-PersonalAccessTokenStore
}
Context 'Parameterized' {
It 'throws no exception' {
Register-PersonalAccessToken -Host $mockHost -Token $mockPat
}
}
Context 'Shorthand' {
It 'throws no exception' {
Register-PersonalAccessToken $mockHost $mockPat
}
}
}
Describe 'Get-PersonalAccessToken' `
{
BeforeEach {
Initialize-PersonalAccessTokenStore
}
Context 'Parameterized' {
It 'gets an existing PAT' {
Register-PersonalAccessToken -Host $mockHost -Token $mockPat
Get-PersonalAccessToken -Host $mockHost | Should -Be $mockPat
}
It 'requires PAT to exist' {
{Get-PersonalAccessToken -Host $mockHost} | Should -Throw
}
}
Context 'Shorthand' {
It 'throws no exception' {
Register-PersonalAccessToken -Host $mockHost -Token $mockPat
Get-PersonalAccessToken $mockHost | Should -Be $mockPat
}
}
}

23
tests/StringHelper.Tests.ps1 Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
BeforeAll {
Import-Module (Join-Path $PSScriptRoot '..' 'src' `
'PSConfluencePublisher.psd1')
}
Describe 'Get-StringHash' `
{
Context 'default' `
{
It 'works' `
{
$result = Get-StringHash 'foobar'
$result.Hash | Should -Be (
'C3AB8FF13720E8AD9047DD3946' + `
'6B3C8974E592C2FA383D4A3960714CAEF0C4F2')
}
}
}