Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
881f6ee
delete wf from root. not sure why are here
rulasg Jun 23, 2023
573b47d
Update Import-template
rulasg Jun 23, 2023
a32c0b1
Add-ModuleLicense
rulasg Jun 23, 2023
f2f3c84
Add-ModuleReleaseScript , Add-ModulePSScriptAnalyzerWorkflow
rulasg Jun 23, 2023
7316406
test typo
rulasg Jun 23, 2023
20c381b
Add-Module* and 1 test
rulasg Jun 23, 2023
34d82bf
Implementation New Add-Module pattern - Pass[87]
rulasg Jun 24, 2023
317f408
Merge branch 'main' into 82-fea-get-module-|-add-modulesection
rulasg Jun 25, 2023
873739f
Rename Add-ModuleFull to Add-ModuleAll
rulasg Jun 25, 2023
71a4172
bug avoid leaving module loaded after test
rulasg Jun 25, 2023
7df94ad
Rename Ad-Module* to Add-ToModule*
rulasg Jun 25, 2023
c8512d4
Pass[85]
rulasg Jun 25, 2023
04389d2
Pass all but TestingHelperTest_Manual*
rulasg Jun 26, 2023
ebd2b16
Fix two small test bugs from Asserts.
rulasg Jun 26, 2023
a493cf1
Ref: Rename files and order some functions
rulasg Jun 26, 2023
a1deca2
file rename
rulasg Jun 26, 2023
d730f08
Make New-TestingVsCodeLaunchJsonV1 private
rulasg Jun 26, 2023
ef7bda3
docs - structure
rulasg Jun 26, 2023
5786b50
docs - set index.md as entry point
rulasg Jun 26, 2023
cc725dd
Mark New-Module V2 and V1 as Obsolete
rulasg Jun 26, 2023
b8565d1
Docs and Rename functions
rulasg Jun 27, 2023
9d436bb
function cleaning
rulasg Jun 27, 2023
a8c9c11
Pass[86]
rulasg Jun 27, 2023
a80bf82
Pass[86]
rulasg Jun 28, 2023
f142e79
Remove Passthru for Assert-Add* functions
rulasg Jun 28, 2023
7fea33f
Pass[2]
rulasg Jun 28, 2023
4fcb85c
remove return value from add-ToModule
rulasg Jun 28, 2023
921f2a9
Supress Obsolete Warning
rulasg Jun 28, 2023
31ca19a
Fix type WarningAction
rulasg Jun 28, 2023
862f2af
New-ModuleV3 -AddAll
rulasg Jun 28, 2023
fe1d2fb
New-ModuleV3 paramset
rulasg Jun 28, 2023
896ba75
TestingHelperTest_Manual_Work_Testing
rulasg Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
381 changes: 381 additions & 0 deletions TestingHelperTest/private/Add-ModuleSections.Asserts.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,381 @@

function TestingHelperTest_AssertAddSection_throwOnNull{
# All asserts here has a pattern
# This test will confirm tht the pattern will not miss a false negative

$hasthrown = $false
try{
Assert-AddLicense -Path $null
}
catch{
$hasthrown = $true
}
Assert-IsTrue -Condition $hasthrown
}

# Devcontainer.json
function Assert-AddDevContainerJson{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
Assert-ItemExist -Path ($Path | Join-Path -ChildPath ".devcontainer" | Join-Path -ChildPath "devcontainer.json") -Comment "devcontainer.json"
}
}


# License
function Assert-AddLicense{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "LICENSE") -Comment "LICENSE"
}
}

# ReadMe
function Assert-AddReadMe{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$name = $Path | Split-Path -LeafBase
$readMePath = $Path | Join-Path -ChildPath "README.md"
Assert-ItemExist -Path $readMePath -Comment "README.md"
$content = Get-Content -Path $readMePath

Assert-Contains -Expected "# $name" -Presented $content -Comment "README.md contains module name"

$manifest = Import-ModuleManifest -Path $Path
if($manifest){
$expectedDescription = $manifest.Description ?? "A powershell module that will hold Powershell functionality."
Assert-Contains -Expected $expectedDescription -Presented $content -Comment "README.md contains module description"
}
}
}

# AddAbout
function Assert-AddToModuleAbout{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{

$name = $Path | Split-Path -LeafBase
$aboutFilePath = $Path | Join-Path -ChildPath "en-US" -AdditionalChildPath "about_$name.help.txt"
Assert-ItemExist -Path $aboutFilePath -Comment "Missing about file"

$aboutContent = Get-Content -Path $aboutFilePath | Out-String
Assert-IsTrue -Condition ($aboutContent.Contains("TOPIC`n about_$moduleName")) -Comment "TOPIC"
Assert-IsTrue -Condition ($aboutContent.Contains("KEYWORDS`n Powershell Testing UnitTest Module TestingHelper")) -Comment "KEYWORDS"

# we will let to fail if manifest not present on assert
$moduleMonifest = Import-ModuleManifest -Path $Path
if($moduleMonifest){
Assert-IsTrue -Condition ($aboutContent.Contains("AUTHOR`n {0}" -f $moduleMonifest.Author)) -Comment "Author"
Assert-IsTrue -Condition ($aboutContent.Contains("SHORT DESCRIPTION`n {0}" -f $moduleMonifest.Description)) -Comment "Description"
Assert-IsTrue -Condition ($aboutContent.Contains("COPYRIGHT`n {0}" -f $moduleMonifest.CopyRight)) -Comment "CopyRight"
}
}
}

# Deploy
function Assert-AddDeployScript{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy.ps1") -Comment "deploy.ps1"
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy-helper.ps1") -Comment "deploy-helper.ps1"
}
}

# release script
function Assert-AddReleaseScript{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "release.ps1") -Comment "release.ps1"
}
}

# sync script
function Assert-AddSyncScript{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "sync.ps1") -Comment "sync.ps1"
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "sync-helper.ps1") -Comment "sync-helper.ps1"
}
}


# PSScriptAnalyzer
function Assert-AddPSScriptAnalyzerWorkflow{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows"
Assert-ItemExist -Path ($destination | Join-Path -ChildPath "powershell.yml") -Comment "powershell.yml"
}
}


# TestingWorkflow
function Assert-AddTestWorkflow{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows"
Assert-ItemExist -Path ($destination | Join-Path -ChildPath "test_with_TestingHelper.yml") -Comment "test_with_TestingHelper.yml"
}
}


# DeployWorkflow
function Assert-AddDeployWorkflow{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows"
Assert-ItemExist -Path ($destination | Join-Path -ChildPath "deploy_module_on_release.yml") -Comment "deploy_module_on_release.yml"
}
}

# SampleCodes
function Assert-AddSampleCodes{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "public" | Join-Path -ChildPath "samplePublicFunction.ps1") -Comment "public function"
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "private" | Join-Path -ChildPath "samplePrivateFunction.ps1") -Comment "private function"
}
}

#Testing SampleCode
function Assert-AddTestSampleCodes{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$name = $Path | Split-Path -LeafBase
$testingModuleName = $Name + "Test"
$testingModulePath = $path | Join-Path -ChildPath $testingModuleName

$samplePublicPath = $testingModulePath | Join-Path -ChildPath "public" -AdditionalChildPath SampleFunctionTests.ps1
Assert-ItemExist -Path $samplePublicPath
}
}

# Testing launch.json
function Assert-AddTestLaunchJson{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{
$launchFile = $Path | Join-Path -ChildPath ".vscode" -AdditionalChildPath "launch.json"

Assert-ItemExist -Path $launchFile -Comment "launch.json exists"
$json = Get-Content -Path $launchFile | ConvertFrom-Json

Assert-IsTrue -Condition ($json.configurations[0].name -eq 'PowerShell: Run Test')
Assert-IsTrue -Condition ($json.configurations[0].type -eq 'PowerShell')
Assert-IsTrue -Condition ($json.configurations[0].Request -eq "launch")
Assert-IsTrue -Condition ($json.configurations[0].Script -eq '${workspaceFolder}/test.ps1')
Assert-IsTrue -Condition ($json.configurations[0].cwd -eq '${workspaceFolder}')

Assert-IsTrue -Condition ($json.configurations[1].name -eq 'PowerShell Interactive Session')
Assert-IsTrue -Condition ($json.configurations[1].type -eq 'PowerShell')
Assert-IsTrue -Condition ($json.configurations[1].Request -eq "launch")
Assert-IsTrue -Condition ($json.configurations[1].cwd -eq '')
}
}

# Testing TestScript
function Assert-AddTestTestScript{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{

$testps1Path = $Path | Join-Path -ChildPath "test.ps1"

Assert-ItemExist -Path $testps1Path -Comment "test.ps1 exists"
}
}

# Asser Full module V3
function Assert-AddModuleV3{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path,
# Manifest data to check
[Parameter()][hashtable]$Expected
)
process{
$name = $Path | Split-Path -LeafBase

$psdname = $name + ".psd1"
$psmName = $name + ".psm1"

$fullExpected = Get-DefaultsManifest

# Update fullExpected with expected
ForEach($key in $Expected.Keys) { $fullExpected[$key] = $Expected[$key]}
$fullExpected.RootModule = $psmName

#PSM1
$psmPath = $Path | Join-Path -ChildPath $psmName
Assert-ItemExist -Path $psmPath

# public private
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "public") -Comment "public folder"
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "private") -Comment "private folder"

#PSD1
$psdPath = $Path | Join-Path -ChildPath $psdname
Assert-ItemExist -Path $psdPath

#manifest
$presented = Import-PowerShellDataFile -Path $psdPath

# GUID
# PrivateData
# @("RootModule", "AliasesToExport" , "Author" , "CmdletsToExport" , "VariablesToExport" , "ModuleVersion" , "Copyright" , "CompanyName") | ForEach-Object {
# $fullExpected | ForEach-Object {
foreach($key in $Expected.Keys){

# Check if value is NULL
if (!($fullExpected.$Key)) {
Assert-IsNull -Object $presented.$key -Comment "Manifest $key"
}

# skip if $Key is GUID
if ($key -eq "GUID") { continue }

# Check value based on type
switch ($fullExpected.$key.GetType().Name) {
"String" {
Assert-AreEqual -Expected $fullExpected.$key -Presented $presented.$key -Comment "Manifest $key"
}
"Object[]" {
Assert-AreEqual -Expected ($fullExpected.$key | ConvertTo-Json) -Presented ($presented.$key | ConvertTo-Json) -Comment "Manifest $key"
}
"Hashtable" {
Assert-AreEqual -Expected ($fullExpected.$key | ConvertTo-Json) -Presented ($presented.$key | ConvertTo-Json) -Comment "Manifest $key"
}
Default {
throw "Unknown type for $key"
}
}
}

Write-AssertionSectionEnd
}
}

function Assert-AddTestModuleV3{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)

process{
$name = $Path | Split-Path -LeafBase

# $modulePath = $Path | Join-Path -ChildPath $Name
$testingModuleName = $name + "Test"
$testingModulePath = $path | Join-Path -ChildPath $testingModuleName

Assert-AddModuleV3 -Path $testingModulePath
}
}

function Assert-AddTestAll {
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)

process{
$name = $Path | Split-Path -LeafBase

# $modulePath = $Path | Join-Path -ChildPath $Name
$testingModuleName = $name + "Test"
$testingModulePath = $path | Join-Path -ChildPath $testingModuleName

Assert-AddModuleV3 -Path $testingModulePath
Assert-AddTestSampleCodes -Path $Path

Assert-AddTestTestScript -Path $Path
Assert-AddTestLaunchJson -Path $Path
}
}

# Full
function Assert-AddAll{
param(
[Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Alias("PSPath")][ValidateNotNullOrEmpty()]
[string] $Path
)
process{

$Path | Assert-AddDevContainerJson
$Path | Assert-AddLicense
$Path | Assert-AddReadMe
$Path | Assert-AddToModuleAbout
$Path | Assert-AddDeployScript
$Path | Assert-AddReleaseScript
$Path | Assert-AddSyncScript
$Path | Assert-AddPSScriptAnalyzerWorkflow
$Path | Assert-AddTestWorkflow
$Path | Assert-AddDeployWorkflow
$Path | Assert-AddSampleCodes

$Path | Assert-AddTestAll
$Path | Assert-AddTestSampleCodes
}
}




7 changes: 7 additions & 0 deletions TestingHelperTest/private/ImportModule.Tests.Helper.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

function Remove-ImportedModule($Module){
# if ModuleName module is loaded, remove it
if(Get-Module -Name $Module -ListAvailable){
Remove-Module -Name $Module -Force
}
}
Loading