inital move to a module

This commit is contained in:
James Smith
2020-12-22 22:56:52 +11:00
parent a82d4b5198
commit 563c377abc
56 changed files with 3097 additions and 432 deletions

108
tests/Help.tests.ps1 Normal file
View File

@@ -0,0 +1,108 @@
# Taken with love from @juneb_get_help (https://raw.githubusercontent.com/juneb/PesterTDD/master/Module.Help.Tests.ps1)
$outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'Output'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
# Get module commands
# Remove all versions of the module from the session. Pester can't handle multiple versions.
#Get-Module $env:BHProjectName | Remove-Module -Force
Import-Module -Name (Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1") -Verbose:$false -ErrorAction Stop
$commands = Get-Command -Module (Get-Module $env:BHProjectName) -CommandType Cmdlet, Function, Workflow # Not alias
## When testing help, remember that help is cached at the beginning of each session.
## To test, restart session.
foreach ($command in $commands) {
$commandName = $command.Name
# The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets
$help = Get-Help $commandName -ErrorAction SilentlyContinue
Describe "Test help for $commandName" {
# If help is not found, synopsis in auto-generated help is the syntax diagram
It 'should not be auto-generated' {
$help.Synopsis | Should Not BeLike '*`[`<CommonParameters`>`]*'
}
# Should be a description for every function
It "gets description for $commandName" {
$help.Description | Should Not BeNullOrEmpty
}
# Should be at least one example
It "gets example code from $commandName" {
($help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty
}
# Should be at least one example description
It "gets example help from $commandName" {
($help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty
}
Context "Test parameter help for $commandName" {
$common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer',
'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable', 'Confirm', 'Whatif'
$parameters = $command.ParameterSets.Parameters |
Sort-Object -Property Name -Unique |
Where-Object { $_.Name -notin $common }
$parameterNames = $parameters.Name
## Without the filter, WhatIf and Confirm parameters are still flagged in "finds help parameter in code" test
$helpParameters = $help.Parameters.Parameter |
Where-Object { $_.Name -notin $common } |
Sort-Object -Property Name -Unique
$helpParameterNames = $helpParameters.Name
foreach ($parameter in $parameters) {
$parameterName = $parameter.Name
$parameterHelp = $help.parameters.parameter | Where-Object Name -EQ $parameterName
# Should be a description for every parameter
It "gets help for parameter: $parameterName : in $commandName" {
$parameterHelp.Description.Text | Should Not BeNullOrEmpty
}
# Required value in Help should match IsMandatory property of parameter
It "help for $parameterName parameter in $commandName has correct Mandatory value" {
$codeMandatory = $parameter.IsMandatory.toString()
$parameterHelp.Required | Should Be $codeMandatory
}
# Parameter type in Help should match code
# It "help for $commandName has correct parameter type for $parameterName" {
# $codeType = $parameter.ParameterType.Name
# # To avoid calling Trim method on a null object.
# $helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() }
# $helpType | Should be $codeType
# }
}
foreach ($helpParm in $HelpParameterNames) {
# Shouldn't find extra parameters in help.
It "finds help parameter in code: $helpParm" {
$helpParm -in $parameterNames | Should Be $true
}
}
}
Context "Help Links should be Valid for $commandName" {
$link = $help.relatedLinks.navigationLink.uri
foreach ($link in $links) {
if ($link) {
# Should have a valid uri if one is provided.
it "[$link] should have 200 Status Code for $commandName" {
$Results = Invoke-WebRequest -Uri $link -UseBasicParsing
$Results.StatusCode | Should Be '200'
}
}
}
}
}
}

86
tests/Manifest.tests.ps1 Normal file
View File

@@ -0,0 +1,86 @@
$moduleName = $env:BHProjectName
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
$outputDir = Join-Path -Path $ENV:BHProjectPath -ChildPath 'Output'
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
$outputManifestPath = Join-Path -Path $outputModVerDir -Child "$($moduleName).psd1"
$changelogPath = Join-Path -Path $env:BHProjectPath -Child 'CHANGELOG.md'
Describe 'Module manifest' {
Context 'Validation' {
$script:manifest = $null
It 'has a valid manifest' {
{
$script:manifest = Test-ModuleManifest -Path $outputManifestPath -Verbose:$false -ErrorAction Stop -WarningAction SilentlyContinue
} | Should Not Throw
}
It 'has a valid name in the manifest' {
$script:manifest.Name | Should Be $env:BHProjectName
}
It 'has a valid root module' {
$script:manifest.RootModule | Should Be "$($moduleName).psm1"
}
It 'has a valid version in the manifest' {
$script:manifest.Version -as [Version] | Should Not BeNullOrEmpty
}
It 'has a valid description' {
$script:manifest.Description | Should Not BeNullOrEmpty
}
It 'has a valid author' {
$script:manifest.Author | Should Not BeNullOrEmpty
}
It 'has a valid guid' {
{
[guid]::Parse($script:manifest.Guid)
} | Should Not throw
}
It 'has a valid copyright' {
$script:manifest.CopyRight | Should Not BeNullOrEmpty
}
$script:changelogVersion = $null
It 'has a valid version in the changelog' {
foreach ($line in (Get-Content $changelogPath)) {
if ($line -match "^##\s\[(?<Version>(\d+\.){1,3}\d+)\]") {
$script:changelogVersion = $matches.Version
break
}
}
$script:changelogVersion | Should Not BeNullOrEmpty
$script:changelogVersion -as [Version] | Should Not BeNullOrEmpty
}
It 'changelog and manifest versions are the same' {
$script:changelogVersion -as [Version] | Should be ( $script:manifest.Version -as [Version] )
}
if (Get-Command git.exe -ErrorAction SilentlyContinue) {
$script:tagVersion = $null
It 'is tagged with a valid version' -skip {
$thisCommit = git.exe log --decorate --oneline HEAD~1..HEAD
if ($thisCommit -match 'tag:\s*(\d+(?:\.\d+)*)') {
$script:tagVersion = $matches[1]
}
$script:tagVersion | Should Not BeNullOrEmpty
$script:tagVersion -as [Version] | Should Not BeNullOrEmpty
}
It 'all versions are the same' {
$script:changelogVersion -as [Version] | Should be ( $script:manifest.Version -as [Version] )
#$script:manifest.Version -as [Version] | Should be ( $script:tagVersion -as [Version] )
}
}
}
}

41
tests/Meta.tests.ps1 Normal file
View File

@@ -0,0 +1,41 @@
Set-StrictMode -Version latest
# Make sure MetaFixers.psm1 is loaded - it contains Get-TextFilesList
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'MetaFixers.psm1') -Verbose:$false -Force
$projectRoot = $ENV:BHProjectPath
if(-not $projectRoot) {
$projectRoot = $PSScriptRoot
}
Describe 'Text files formatting' {
$allTextFiles = Get-TextFilesList $projectRoot
Context 'Files encoding' {
It "Doesn't use Unicode encoding" {
$unicodeFilesCount = 0
$allTextFiles | Foreach-Object {
if (Test-FileUnicode $_) {
$unicodeFilesCount += 1
Write-Warning "File $($_.FullName) contains 0x00 bytes. It's probably uses Unicode and need to be converted to UTF-8. Use Fixer 'Get-UnicodeFilesList `$pwd | ConvertTo-UTF8'."
}
}
$unicodeFilesCount | Should -Be 0
}
}
Context 'Indentations' {
It 'Uses spaces for indentation, not tabs' {
$totalTabsCount = 0
$allTextFiles | Foreach-Object {
$fileName = $_.FullName
(Get-Content $_.FullName -Raw) | Select-String "`t" | Foreach-Object {
Write-Warning "There are tab in $fileName. Use Fixer 'Get-TextFilesList `$pwd | ConvertTo-SpaceIndentation'."
$totalTabsCount++
}
}
$totalTabsCount | Should -Be 0
}
}
}

75
tests/MetaFixers.psm1 Normal file
View File

@@ -0,0 +1,75 @@
# Taken with love from https://github.com/PowerShell/DscResource.Tests/blob/master/MetaFixers.psm1
<#
This module helps fix problems, found by Meta.Tests.ps1
#>
$ErrorActionPreference = 'stop'
Set-StrictMode -Version latest
function ConvertTo-UTF8() {
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileInfo]$FileInfo
)
process {
$content = Get-Content -Raw -Encoding Unicode -Path $FileInfo.FullName
[System.IO.File]::WriteAllText($FileInfo.FullName, $content, [System.Text.Encoding]::UTF8)
}
}
function ConvertTo-SpaceIndentation() {
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileInfo]$FileInfo
)
process {
$content = (Get-Content -Raw -Path $FileInfo.FullName) -replace "`t", ' '
[System.IO.File]::WriteAllText($FileInfo.FullName, $content)
}
}
function Get-TextFilesList {
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory)]
[string]$Root
)
Get-ChildItem -Path $Root -File -Recurse |
Where-Object { @('.gitignore', '.gitattributes', '.ps1', '.psm1', '.psd1', '.json', '.xml', '.cmd', '.mof') -contains $_.Extension }
}
function Test-FileUnicode {
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileInfo]$FileInfo
)
process {
$path = $FileInfo.FullName
$bytes = [System.IO.File]::ReadAllBytes($path)
$zeroBytes = @($bytes -eq 0)
return [bool]$zeroBytes.Length
}
}
function Get-UnicodeFilesList() {
[CmdletBinding()]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(Mandatory)]
[string]$Root
)
Get-TextFilesList $Root | Where-Object { Test-FileUnicode $_ }
}

View File

@@ -0,0 +1,3 @@
@{
}