functions/Resolve-ExtensionMetadata.Tests.ps1
|
# <copyright file="Resolve-ExtensionMetadata.Tests.ps1" company="Endjin Limited"> # Copyright (c) Endjin Limited. All rights reserved. # </copyright> BeforeAll { # sut . $PSCommandPath.Replace('.Tests.ps1','.ps1') # in-module dependencies . (Join-Path (Split-Path -Parent $PSCommandPath) '_resolveModuleNameFromPath.ps1') } Describe 'Resolve-ExtensionMetadata' { BeforeAll { # Setup mock extension on filesystem $extensionPath = Join-Path TestDrive: "MyExtension" "MyExtension.psd1" New-Item -Path $extensionPath -ItemType File -Force | Out-Null } Context 'Resolving from string-formatted configuration' { Context 'Valid specification by name' { BeforeAll { $config = "MyExtension" $result = Resolve-ExtensionMetadata $config } It 'Should return name metadata' { $result.Name | Should -Be $config } It 'Should not return path metadata' { $result.Keys | Should -Not -Contain 'Path' } } Context 'Valid specification by path' { BeforeAll { $config = "$extensionPath" $result = Resolve-ExtensionMetadata $config } It 'Should return path metadata' { $result.Path | Should -Be $config } It 'Should return name metadata' { $result.Name | Should -Be 'MyExtension' } } } Context 'Resolving from hashtable-formatted configuration' { Context 'Minimal valid specification by name' { BeforeAll { $config = @{ Name = "MyExtension" } $result = Resolve-ExtensionMetadata $config } It 'Should return name metadata' { $result.Name | Should -Be $config.Name } It 'Should not return path metadata' { $result.Keys | Should -Not -Contain 'Path' } } Context 'Minimal valid specification by path' { BeforeAll { $config = @{ Path = "$extensionPath" } $result = Resolve-ExtensionMetadata $config } It 'Should return path metadata' { $result.Path | Should -Be $config.Path } It 'Should return name metadata' { $result.Name | Should -Be 'MyExtension' } } Context 'Valid specification by name & version' { BeforeAll { $config = @{ Name = "MyExtension" Version = "1.0.0" } $result = Resolve-ExtensionMetadata $config } It 'Should return name metadata' { $result.Name | Should -Be $config.Name } It 'Should return version metadata' { $result.Version | Should -Be $config.Version } It 'Should not return path metadata' { $result.Keys | Should -Not -Contain 'Path' } } Context 'Valid specification by name & minimum version' { BeforeAll { $config = @{ Name = "MyExtension" MinimumVersion = "1.4.0" } $result = Resolve-ExtensionMetadata $config } It 'Should return minimum version metadata' { $result.MinimumVersion | Should -Be $config.MinimumVersion } It 'Should not include pre-release versions' { $result.ContainsKey('PreRelease') | Should -Be $false } } Context 'Valid specification by name & pre-release minimum version (<MinimumVersion>)' -ForEach @( @{ MinimumVersion = '1.4.0-beta1' } @{ MinimumVersion = '1.4-beta1' } ) { It 'Should include pre-release versions' { $result = Resolve-ExtensionMetadata @{ Name = "MyExtension"; MinimumVersion = $MinimumVersion } $result.PreRelease | Should -Be $true } } } Context 'Invalid configuration' { It 'Should throw an exception when Name and Path are missing' { $config = @{} { Resolve-ExtensionMetadata $config } | Should -Throw "Invalid extension configuration syntax." } It 'Should throw an exception when MinimumVersion is used with <Key>' -TestCases @( @{ Key = 'Version'; Value = '1.5.0' } @{ Key = 'GitRef'; Value = 'main' } @{ Key = 'Path'; Value = "$extensionPath" } ) { $config = @{ Name = "MyExtension" MinimumVersion = "1.4.0" $Key = $Value } { Resolve-ExtensionMetadata $config } | Should -Throw "Invalid extension configuration for 'MyExtension': 'MinimumVersion' cannot be used with '$Key'." } } } |