Tests/Orbit.Teams.tests.ps1

# Module: Orbit.Teams
# Function: Test
# Author: David Eberhardt
# Updated: 03-JUN 2023

# Script Analyzer Exceptions
#[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification = 'Context Boundaries')]

BeforeDiscovery {
  # Finding relevant Paths from $PSScriptRoot
  $ModuleRoot = ($PsScriptRoot -split '\\Tests')[0]
  $ModuleName = Split-Path -Leaf $ModuleRoot
  $OrbitSrc = Split-Path -Parent $ModuleRoot

  $OrbitDirs = Get-ChildItem -Path $OrbitSrc -Directory | Sort-Object Name -Descending
  $OrbitModules = $OrbitDirs.Basename
  $OrbitModules | ForEach-Object { if ( -not (Get-Module $_) ) { Import-Module "$OrbitSrc\$_\$_.psd1" -Force } }

  # Generate command list for generating Context / TestCases
  $Module = Get-Module $ModuleName
  $CommandList = @(
    $Module.CommandList.Keys
    $Module.ExportedCmdlets.Keys
  )
  Write-Output "Module '$ModuleName' - $($CommandList.count) exported Commands found"

  # Determining Functions
  $PublicFunctions = Get-ChildItem "$ModuleRoot\Private" -Include '*.ps1' -Exclude '*.Tests.ps1' -Recurse
  Write-Output "Module '$ModuleName' - $($PublicFunctions.Count) public functions discovered"
  $PrivateFunctions = Get-ChildItem "$ModuleRoot\Private" -Include '*.ps1' -Exclude '*.Tests.ps1' -Recurse
  Write-Output "Module '$ModuleName' - $($PrivateFunctions.Count) private functions discovered"
}

BeforeAll {
  # Need to do this here again, otherwise it will not disover ModuleRoot correctly
  $ModuleRoot = ($PsScriptRoot -split '\\Tests')[0]
  $ModuleName = Split-Path -Leaf $ModuleRoot
}
# Unit Tests
Describe -Tags ('Unit', 'Acceptance') "Module '$ModuleName'" {
  Context 'Validating Module Framework' {
    It "has the root module '$ModuleName`.psm1'" {
      "$ModuleRoot\$ModuleName.psm1" | Should -Exist
    }

    It "has the root module '$ModuleName`.psm1'" {
      "$ModuleRoot\$ModuleName.psm1" | Should -Exist
    }

    It "has the a manifest file of '$ModuleName`.psd1'" {
      "$ModuleRoot\$ModuleName.psd1" | Should -Exist
    }

    It "$ModuleName has folder for Functions" {
      "$ModuleRoot\Public" | Should -Exist
      "$ModuleRoot\Private" | Should -Exist
    }

    It "$ModuleName has folder for Tests" {
      "$ModuleRoot\Tests\Unit\Public" | Should -Exist
      "$ModuleRoot\Tests\Unit\Private" | Should -Exist
    }

    It "$ModuleName is valid PowerShell code" {
      $psFile = Get-Content -Path "$ModuleRoot\$ModuleName.psm1" -ErrorAction Stop
      $errors = $null
      $null = [System.Management.Automation.PSParser]::Tokenize($psFile, [ref]$errors)
      $errors.Count | Should -Be 0
    }
  }
}