endjin-gists.psm1.tests.ps1
|
# <copyright file="endjin-gists.psm1.tests.ps1" company="Endjin Limited"> # Copyright (c) Endjin Limited. All rights reserved. # </copyright> $moduleName = 'endjin-gists' Describe "'$moduleName' Module Initialization Tests" { BeforeDiscovery { $canImport = [bool](Get-Module -ListAvailable powershell-yaml -ErrorAction SilentlyContinue) } Context 'Script-Scoped Variable Defaults' { BeforeAll { $moduleName = 'endjin-gists' # Save current env var values $savedGistsMapUrl = $env:GISTS_MAP_URL $savedGistsMapRepoUrl = $env:GISTS_MAP_REPO $savedGistsMapRepoRef = $env:GISTS_MAP_REPO_REF $savedGistsMapRepoPath = $env:GISTS_MAP_REPO_PATH # Clear all env vars to test defaults Remove-Item Env:\GISTS_MAP_URL -ErrorAction SilentlyContinue Remove-Item Env:\GISTS_MAP_REPO -ErrorAction SilentlyContinue Remove-Item Env:\GISTS_MAP_REPO_REF -ErrorAction SilentlyContinue Remove-Item Env:\GISTS_MAP_REPO_PATH -ErrorAction SilentlyContinue Import-Module "$PSScriptRoot/$moduleName.psd1" -Force } AfterAll { # Restore env vars if ($null -ne $savedGistsMapUrl) { $env:GISTS_MAP_URL = $savedGistsMapUrl } else { Remove-Item Env:\GISTS_MAP_URL -ErrorAction SilentlyContinue } if ($null -ne $savedGistsMapRepoUrl) { $env:GISTS_MAP_REPO = $savedGistsMapRepoUrl } else { Remove-Item Env:\GISTS_MAP_REPO -ErrorAction SilentlyContinue } if ($null -ne $savedGistsMapRepoRef) { $env:GISTS_MAP_REPO_REF = $savedGistsMapRepoRef } else { Remove-Item Env:\GISTS_MAP_REPO_REF -ErrorAction SilentlyContinue } if ($null -ne $savedGistsMapRepoPath) { $env:GISTS_MAP_REPO_PATH = $savedGistsMapRepoPath } else { Remove-Item Env:\GISTS_MAP_REPO_PATH -ErrorAction SilentlyContinue } Import-Module "$PSScriptRoot/$moduleName.psd1" -Force } It 'DefaultGistMapUrl defaults to empty string when GISTS_MAP_URL is unset' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapUrl } $result | Should -Be '' } It 'DefaultGistMapGitSource.url defaults to endjin-gists repo' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapGitSource.url } $result | Should -Be 'endjin/endjin-gists' } It 'DefaultGistMapGitSource.ref defaults to main' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapGitSource.ref } $result | Should -Be 'main' } It 'DefaultGistMapGitSource.path defaults to module/gist-map.yml' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapGitSource.path } $result | Should -Be 'module/gist-map.yml' } } Context 'Script-Scoped Variable Env Var Overrides' { BeforeAll { $moduleName = 'endjin-gists' # Save current env var values $savedGistsMapUrl = $env:GISTS_MAP_URL $savedGistsMapRepoUrl = $env:GISTS_MAP_REPO $savedGistsMapRepoRef = $env:GISTS_MAP_REPO_REF $savedGistsMapRepoPath = $env:GISTS_MAP_REPO_PATH # Set test override values $env:GISTS_MAP_URL = 'https://example.com/custom-map.yml' $env:GISTS_MAP_REPO = 'test-org/test-repo' $env:GISTS_MAP_REPO_REF = 'develop' $env:GISTS_MAP_REPO_PATH = 'custom/path/gist-map.yml' Import-Module "$PSScriptRoot/$moduleName.psd1" -Force } AfterAll { # Restore env vars if ($null -ne $savedGistsMapUrl) { $env:GISTS_MAP_URL = $savedGistsMapUrl } else { Remove-Item Env:\GISTS_MAP_URL -ErrorAction SilentlyContinue } if ($null -ne $savedGistsMapRepoUrl) { $env:GISTS_MAP_REPO = $savedGistsMapRepoUrl } else { Remove-Item Env:\GISTS_MAP_REPO -ErrorAction SilentlyContinue } if ($null -ne $savedGistsMapRepoRef) { $env:GISTS_MAP_REPO_REF = $savedGistsMapRepoRef } else { Remove-Item Env:\GISTS_MAP_REPO_REF -ErrorAction SilentlyContinue } if ($null -ne $savedGistsMapRepoPath) { $env:GISTS_MAP_REPO_PATH = $savedGistsMapRepoPath } else { Remove-Item Env:\GISTS_MAP_REPO_PATH -ErrorAction SilentlyContinue } Import-Module "$PSScriptRoot/$moduleName.psd1" -Force } It 'DefaultGistMapUrl uses GISTS_MAP_URL when set' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapUrl } $result | Should -Be 'https://example.com/custom-map.yml' } It 'DefaultGistMapGitSource.url uses GISTS_MAP_REPO when set' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapGitSource.url } $result | Should -Be 'test-org/test-repo' } It 'DefaultGistMapGitSource.ref uses GISTS_MAP_REPO_REF when set' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapGitSource.ref } $result | Should -Be 'develop' } It 'DefaultGistMapGitSource.path uses GISTS_MAP_REPO_PATH when set' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $script:DefaultGistMapGitSource.path } $result | Should -Be 'custom/path/gist-map.yml' } } Context 'Argument Completer Registration' { BeforeAll { $moduleName = 'endjin-gists' Import-Module "$PSScriptRoot/$moduleName.psd1" -Force } It 'groupCompleter is defined as a scriptblock in module scope' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $groupCompleter } $result | Should -Not -BeNullOrEmpty $result | Should -BeOfType [scriptblock] } It 'nameCompleter is defined as a scriptblock in module scope' -Skip:(-not $canImport) { $result = InModuleScope $moduleName { $nameCompleter } $result | Should -Not -BeNullOrEmpty $result | Should -BeOfType [scriptblock] } } } |