diff --git a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 new file mode 100644 index 0000000..9cc0667 --- /dev/null +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -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 + } +} + + + + diff --git a/TestingHelperTest/private/tests-helper-DUMMY_Constants.ps1 b/TestingHelperTest/private/DUMMY.Tests.Constants.ps1 similarity index 100% rename from TestingHelperTest/private/tests-helper-DUMMY_Constants.ps1 rename to TestingHelperTest/private/DUMMY.Tests.Constants.ps1 diff --git a/TestingHelperTest/private/ImportModule.Tests.Helper.ps1 b/TestingHelperTest/private/ImportModule.Tests.Helper.ps1 new file mode 100644 index 0000000..848375b --- /dev/null +++ b/TestingHelperTest/private/ImportModule.Tests.Helper.ps1 @@ -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 + } +} \ No newline at end of file diff --git a/TestingHelperTest/private/Manifest.Tests.Helper.ps1 b/TestingHelperTest/private/Manifest.Tests.Helper.ps1 new file mode 100644 index 0000000..6d37048 --- /dev/null +++ b/TestingHelperTest/private/Manifest.Tests.Helper.ps1 @@ -0,0 +1,36 @@ +function Get-ModuleManifestPath ($Path){ + $localPath = $Path | Convert-Path + + $Name = $localPath | Split-Path -leafbase + + $manifestPath = $Path | Join-Path -ChildPath "$Name.psd1" + + $ret = ($manifestPath | Test-Path) ? $manifestPath : $null + + return $ret +} + + +function Import-ModuleManifest ($Path){ + + $manifestPath = Get-ModuleManifestPath -Path $Path + + $ret = $manifestPath ? (Import-PowerShellDataFile -Path $manifestPath) : $null + + return $ret +} + +# function Import-ModuleManifest ($Path){ + +# $localPath = $Path | Convert-Path + +# $psdpath = Get-ChildItem -Path $localPath -Filter "*.psd1" -ErrorAction SilentlyContinue + +# if($psdpath.count -ne 1){ +# return $null +# } + +# $manifest = Import-PowerShellDataFile -Path $psdpath.FullName + +# return $manifest +# } \ No newline at end of file diff --git a/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 new file mode 100644 index 0000000..a601346 --- /dev/null +++ b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 @@ -0,0 +1,8 @@ +function Get-DefaultsManifest { + New-ModuleManifest -Path defaults.psd1 -RootModule defaults.psm1 + $defaultsManifest = Import-PowerShellDataFile -Path defaults.psd1 + Remove-Item defaults.psd1 + return $defaultsManifest +} + + diff --git a/TestingHelperTest/public/Tested-Manifest.ps1 b/TestingHelperTest/private/TestingHelper_TestedManifest.Helper.ps1 similarity index 62% rename from TestingHelperTest/public/Tested-Manifest.ps1 rename to TestingHelperTest/private/TestingHelper_TestedManifest.Helper.ps1 index d8e2208..6c35f52 100644 --- a/TestingHelperTest/public/Tested-Manifest.ps1 +++ b/TestingHelperTest/private/TestingHelper_TestedManifest.Helper.ps1 @@ -1,6 +1,6 @@ # return the manifest of the tested module -function Get-TestedModuleManifestPath{ +function Get-TestingHelperTestedModuleManifestPath{ $localPath = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent | Convert-Path @@ -13,8 +13,8 @@ function Get-TestedModuleManifestPath{ return $psdpath.FullName } -function Get-TestedModuleManifest{ - $manifestPath = Get-TestedModuleManifestPath +function Get-TestingHelperTestedModuleManifest{ + $manifestPath = Get-TestingHelperTestedModuleManifestPath $manifest = Import-PowerShellDataFile -Path $manifestPath $manifest.PsdPath = $manifestPath @@ -24,6 +24,7 @@ function Get-TestedModuleManifest{ } # return handle of the tested module -function Get-TestedModuleHandle{ - Get-TestedModuleManifestPath | Import-Module -PassThru -} Export-ModuleMember -Function Get-TestedModuleHandle \ No newline at end of file +function Get-TestingHelperTestedModuleHandle{ + Get-TestingHelperTestedModuleManifestPath | Import-Module -PassThru +} +# Export-ModuleMember -Function Get-TestingHelperTestedModuleHandle \ No newline at end of file diff --git a/TestingHelperTest/private/tests-helper-WarInfoParams.ps1 b/TestingHelperTest/private/WarInfoParams.Tests.Helper.ps1 similarity index 100% rename from TestingHelperTest/private/tests-helper-WarInfoParams.ps1 rename to TestingHelperTest/private/WarInfoParams.Tests.Helper.ps1 diff --git a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 deleted file mode 100644 index c0f2f58..0000000 --- a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 +++ /dev/null @@ -1,193 +0,0 @@ -function Get-DefaultsManifest { - New-ModuleManifest -Path defaults.psd1 -RootModule defaults.psm1 - $defaultsManifest = Import-PowerShellDataFile -Path defaults.psd1 - return $defaultsManifest -} - -function Assert-AddModuleV3 { - param( - # Name of the folder to Assert - [Parameter()][string]$Name, - # ModulePath where to Assert the Module Content - [Parameter()][string]$Path, - # Metadata for the manifest to assert - [Parameter()][hashtable]$Expected, - # Switch to check SampleCode - [Parameter()][switch]$AddSampleCode, - #Switch to assert devcontainerjson file - [Parameter()][switch]$AddDevContainerJson, - # Switch to asser licens file - [Parameter()][switch]$AddLicense, - # Swithc to assert ReadME file - [Parameter()][switch]$AddReadMe, - # Switch to assert Deploy script - [Parameter()][switch]$AddDeployScript, - # Switch to assert release script - [Parameter()][switch]$AddReleaseScript, - # Switch to assert sync script - [Parameter()][switch]$AddSyncScript, - # Switch to assert PSScriptAnalyzer workflow - [Parameter()][switch]$AddPSScriptAnalyzerWorkflow, - # Switch to assert testing workflow - [Parameter()][switch]$AddTestingWorkflow, - # Switch to assert deploy workflow - [Parameter()][switch]$AddDeployWorkflow - - ) - - $psdname = $Name + ".psd1" - $psmName = $Name + ".psm1" - - $fullExpected = Get-DefaultsManifest - - # Update fullExpected with expected - ForEach($key in $Expected.Keys) { $fullExpected[$key] = $Expected[$key]} - - #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" - - # Sample code - if ($AddSampleCode) { - 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" - } - - # Devcontainer.json - if ($AddDevContainerJson) { - Assert-ItemExist -Path ($Path | Join-Path -ChildPath ".devcontainer" | Join-Path -ChildPath "devcontainer.json") -Comment "devcontainer.json" - } - - # License - if ($AddLicense) { - Assert-ItemExist -Path ($Path | Join-Path -ChildPath "LICENSE") -Comment "LICENSE" - } - - # ReadMe - if ($AddReadMe) { - $readMePath = $Path | Join-Path -ChildPath "README.md" - Assert-ItemExist -Path $readMePath -Comment "README.md" - Assert-IsTrue -Condition ((Get-Content -Path $readMePath) -contains "# $modulename") - } - - # Deploy - if ($AddDeployScript) { - 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 - if ($AddReleaseScript) { - Assert-ItemExist -Path ($Path | Join-Path -ChildPath "release.ps1") -Comment "release.ps1" - } - - # sync script - if ($AddSyncScript) { - 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 - if ($AddPSScriptAnalyzerWorkflow) { - $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Assert-ItemExist -Path ($destination | Join-Path -ChildPath "powershell.yml") -Comment "powershell.yml" - } - - # TestingWorkflow - if ($AddTestingWorkflow) { - $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 - if ($AddDeployWorkflow) { - $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" - } - - #PSD1 - $psdPath = $Path | Join-Path -ChildPath $psdname - Assert-ItemExist -Path $psdPath - - #manifest - $presented = Import-PowerShellDataFile -Path $psdPath - - # GUID - # PrivateData - Assert-AreEqual -Expected $fullExpected.AliasesToExport -Presented $presented.AliasesToExport -Comment "Manifest AliasesToExport" - Assert-AreEqual -Expected $fullExpected.Author -Presented $presented.Author -Comment "Manifest Author" - Assert-AreEqual -Expected $fullExpected.CmdletsToExport -Presented $presented.CmdletsToExport -Comment "Manifest CmdletsToExport" - Assert-AreEqual -Expected $fullExpected.VariablesToExport -Presented $presented.VariablesToExport -Comment "Manifest VariablesToExport" - Assert-AreEqual -Expected $fullExpected.ModuleVersion -Presented $presented.ModuleVersion -Comment "Manifest ModuleVersion" - Assert-AreEqual -Expected $fullExpected.Copyright -Presented $presented.Copyright -Comment "Manifest Copyright" - Assert-AreEqual -Expected $fullExpected.CompanyName -Presented $presented.CompanyName -Comment "Manifest CompanyName" - - # Not Strings - Assert-AreEqual -Expected ($fullExpected.FunctionsToExport | ConvertTo-Json) -Presented ($presented.FunctionsToExport | ConvertTo-Json) -Comment "Manifest FunctionsToExport" - - #Exceptions - Assert-AreEqual -Expected "$Name.psm1" -Presented $presented.RootModule -Comment "Manifest RootModule" - Assert-AreEqual -Expected ($fullExpected.Description ?? "") -Presented ($presented.Description ?? "") -Comment "Manifest Description" - - Write-AssertionSectionEnd -} - -function Assert-TestingV3 { - param( - [Parameter()][string]$Name, - [Parameter()][string]$Path, - [Parameter()][hashtable]$Expected, - [Parameter()][switch]$AddSampleCode - ) - - # $modulePath = $Path | Join-Path -ChildPath $Name - $testingModuleName = $Name + "Test" - $testingModulePath = $path | Join-Path -ChildPath $testingModuleName - - Assert-AddModuleV3 -Name $testingModuleName -Path $testingModulePath -Expected $Expected - Assert-LaunchJson -Path $modulePath - Assert-TestScript -Path $modulePath - - if ($AddSampleCode) { - $samplePublicPath = $testingModulePath | Join-Path -ChildPath "public" -AdditionalChildPath SampleFunctionTests.ps1 - Assert-ItemExist -Path $samplePublicPath - } -} - -function Assert-LaunchJson{ - [CmdletBinding()] - param( - [Parameter()][string]$Path - ) - - $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 '') -} - -function Assert-TestScript{ - [CmdletBinding()] - param( - [Parameter()][string]$Path - ) - - $testps1Path = $Path | Join-Path -ChildPath "test.ps1" - - Assert-ItemExist -Path $testps1Path -Comment "test.ps1 exists" -} diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 new file mode 100644 index 0000000..7b121db --- /dev/null +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -0,0 +1,72 @@ + +function TestingHelperTest_AddModuleSection_PipeCalls_NewModuleV3{ + + $result = New-TT_ModuleV3 -Name "MyModule" | Add-TT_ToModuleLicense -PassThru + + $result | Assert-Addlicense +} + +function TestingHelperTest_AddModuleSection_PipeCalls_Folder{ + + New-TestingFolder -Path "folderName" + + $result = Get-Item -path "folderName" | Add-TT_ToModuleLicense -PassThru + $result | Assert-Addlicense +} + +function TestingHelperTest_AddModuleSection_PipeCalls_Module{ + + $moduleName = "AddModuleSection_PipeCalls_Module_" + (New-Guid).ToString().Substring(0,8) + + $modulePath = New-TT_ModuleV3 -Name $moduleName + Import-Module -Name $modulePath + $module = Get-Module -Name $moduleName + + $result = $module | Add-TT_ToModuleLicense -PassThru + + $result | Assert-Addlicense + + Remove-Module -Name $moduleName +} + +function TestingHelperTest_AddModuleSection_PipeCalls_Chain{ + + $modulePath = New-TT_ModuleV3 -Name "MyModule" + + $result = $modulePath | Add-TT_ToModuleLicense -PassThru | Add-TT_ToModuleAbout + + $result | Assert-Addlicense + $result | Assert-AddToModuleAbout +} + +function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ + + New-TestingFolder -Path "folderName" + + + $result = Add-TT_ToModuleAll -PassThru -Path "./folderName" + $result | Assert-AddAll +} + +function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ + + New-TestingFolder -Path "folderName" + + $result = Get-Item -path "folderName" | Add-TT_ToModuleAll -PassThru + $result | Assert-AddAll +} + +function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Module{ + + $modulePath = New-TT_ModuleV3 -Name "MyModule" -Description "Module Description" -Author "myName" -ModuleVersion "5.5.5" + + Import-Module -Name $modulePath + + $module = Get-Module -Name "MyModule" + $result = $module | Add-TT_ToModuleAll -PassThru + $result | Assert-AddAll + + Remove-Module -Name "MyModule" +} + + diff --git a/TestingHelperTest/public/Tests-Assert-guid.ps1 b/TestingHelperTest/public/Tests-Assert-guid.ps1 index 64f2ab9..1d02e06 100644 --- a/TestingHelperTest/public/Tests-Assert-guid.ps1 +++ b/TestingHelperTest/public/Tests-Assert-guid.ps1 @@ -8,7 +8,7 @@ function TestingHelperTest_IsGuid_Fail { $hasThrow = $false try { - Asset-TT_IsGuid -Presented "NotAValidGuid" + Assert-TT_IsGuid -Presented "NotAValidGuid" } catch { $hasThrow = $true diff --git a/TestingHelperTest/public/Tests-Assert-path.ps1 b/TestingHelperTest/public/Tests-Assert-path.ps1 index 389e2ff..140aad4 100644 --- a/TestingHelperTest/public/Tests-Assert-path.ps1 +++ b/TestingHelperTest/public/Tests-Assert-path.ps1 @@ -154,7 +154,7 @@ function TestingHelperTest_ItemExists_Fail{ $hasThrow = $false try { - Asset-TT_ItemExist -Path "Thispathdoesnotexist" + Assert-TT_ItemExist -Path "Thispathdoesnotexist" } catch { $hasThrow = $true diff --git a/TestingHelperTest/public/Tests-Assert.ps1 b/TestingHelperTest/public/Tests-Assert.ps1 index c73d04f..7c66af1 100644 --- a/TestingHelperTest/public/Tests-Assert.ps1 +++ b/TestingHelperTest/public/Tests-Assert.ps1 @@ -2,7 +2,7 @@ function TestingHelperTest_Assert{ [CmdletBinding()] param () - $tested = Get-TestedModuleHandle + $tested = Get-TestingHelperTestedModuleHandle & $tested { diff --git a/TestingHelperTest/public/Tests-ModuleHelperTest.ps1 b/TestingHelperTest/public/Tests-ModuleHelperTest.ps1 index d27089e..cd6a335 100644 --- a/TestingHelperTest/public/Tests-ModuleHelperTest.ps1 +++ b/TestingHelperTest/public/Tests-ModuleHelperTest.ps1 @@ -1,13 +1,13 @@ function GetExpectedHeader{ - $tedmanifest = Get-TestedModuleManifest + $tedmanifest = Get-TestingHelperTestedModuleManifest $expected = "{0} v{1} {2}" -f $tedmanifest.Name, $tedmanifest.ModuleVersion, $tedmanifest.PrivateData.PSData.Prerelease return $expected } function TestingHelperTest_GetModuleHeader { - $module = Get-TestedModuleHandle + $module = Get-TestingHelperTestedModuleHandle $result = & $module { Get-ModuleHeader @@ -16,7 +16,7 @@ function TestingHelperTest_GetModuleHeader { Assert-AreEqual -Expected (GetExpectedHeader) -Presented $result } -# Testing TestingHelperTest private function Get-TestedModuleHandle +# Testing TestingHelperTest private function Get-TestingHelperTestedModuleHandle function TestingHelperTest_GetModuleHandle { $localPath = $PSScriptRoot | Split-Path -Parent | Split-Path -Parent @@ -24,7 +24,7 @@ function TestingHelperTest_GetModuleHandle { $manifest = Import-PowerShellDataFile -Path $psdpath # Internal TestingHelperTest function - $result = Get-TestedModuleHandle + $result = Get-TestingHelperTestedModuleHandle Assert-IsNotNull -Object $result Assert-AreEqual -Expected ($manifest.RootModule | Split-Path -LeafBase) -Presented $result.Name diff --git a/TestingHelperTest/public/Tests-New-ModuleV1.ps1 b/TestingHelperTest/public/Tests-New-ModuleV1.ps1 index 01aeaf4..397154f 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV1.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV1.ps1 @@ -1,6 +1,5 @@ function TestingHelperTest_NewModuleV1{ - New-TT_ModuleV1 -Name "ModuleName" -Description "description of the Module" - + New-TT_ModuleV1 -Name "ModuleName" -Description "description of the Module" -warningAction SilentlyContinue $psdPath = Join-Path -Path . -ChildPath ModuleName -AdditionalChildPath ModuleName.psd1 $psmPath = Join-Path -Path . -ChildPath ModuleName -AdditionalChildPath ModuleName.psm1 @@ -56,19 +55,4 @@ function TestingHelperTest_NewTestingModule{ Assert-FileContains -Path $psmPathTest -Pattern "function ModuleNameTest_Sample()" -Comment "Function header" Assert-FileContains -Path $psmPathTest -Pattern "Export-ModuleMember -Function ModuleNameTest_*" -Comment "Export" -} - -function TestingHelperTest_NewTestingVsCodeLaunchJson{ - New-TT_TestingVsCodeLaunchJson -Path . -ModuleName "ModuleName" - - $launchFile = Join-Path -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.Request -eq "launch") - Assert-IsTrue -Condition ($json.configurations.Script -eq '${workspaceFolder}/ModuleNameTest.ps1') - Assert-IsTrue -Condition ($json.configurations.cwd -eq '${workspaceFolder}') - Assert-IsTrue -Condition ($json.configurations.type -eq 'PowerShell') - Assert-IsTrue -Condition ($json.configurations.name -like '*ModuleName.ps1') } \ No newline at end of file diff --git a/TestingHelperTest/public/Tests-New-ModuleV2.ps1 b/TestingHelperTest/public/Tests-New-ModuleV2.ps1 index 832261f..6690605 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV2.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV2.ps1 @@ -1,6 +1,6 @@ function TestingHelperTest_NewModuleV2{ - - New-TT_Modulev2 -Name "ModuleName" -Description "description of the Module" -Version "9.9.9" + # + New-TT_Modulev2 -Name "ModuleName" -Description "description of the Module" -Version "9.9.9" -WarningAction SilentlyContinue # Supress Obsolete Warning #PSD1 $psdPath = Join-Path -Path . -ChildPath ModuleName -AdditionalChildPath ModuleName.psd1 @@ -56,7 +56,7 @@ function TestingHelperTest_NewModuleV2_RunModuleTest_RunFromAnyLocation_AnyName{ $ModuleName = "ModuleName_{0}" -f (New-Guid).ToString().Substring(0,8) - New-TT_Modulev2 -Name $ModuleName -Description "description of the Module" -Version "9.9.9" + New-TT_Modulev2 -Name $ModuleName -Description "description of the Module" -Version "9.9.9" -WarningAction SilentlyContinue # Supress Obsolete Warning $test = $ModuleName | Join-Path -ChildPath "test.ps1" | Resolve-Path diff --git a/TestingHelperTest/public/Tests-New-ModuleV3-Add-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-Add-ModuleV3.ps1 index 3198582..032e630 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3-Add-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3-Add-ModuleV3.ps1 @@ -41,7 +41,7 @@ function TestingHelperTest_NewModuleV3_AddModule_DefaultManifest { Assert-AreEqualPath -Expected $moduleName -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $moduleName -Expected $defaultsManifest + Assert-AddModuleV3 -Path $moduleName -Expected $defaultsManifest } function TestingHelperTest_NewModuleV3_AddModule_MyManifest { @@ -60,7 +60,7 @@ function TestingHelperTest_NewModuleV3_AddModule_MyManifest { $result = Add-TT_ModuleV3 -Name $moduleName -Metadata $param - Assert-AddModuleV3 -Name $moduleName -Path $result -Expected $param + Assert-AddModuleV3 -Path $result -Expected $param } diff --git a/TestingHelperTest/public/Tests-New-ModuleV3-Add-TestingModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-Add-TestingModuleV3.ps1 deleted file mode 100644 index 8399c6b..0000000 --- a/TestingHelperTest/public/Tests-New-ModuleV3-Add-TestingModuleV3.ps1 +++ /dev/null @@ -1,57 +0,0 @@ - -function TestingHelperTest_NewModuleV3_AddTestingToModule_AddTestingModuleV3_Simple { - - $moduleName = "MyModule" - $folderName = "MyFolder" - New-TestingFolder -Name $folderName - $modulePath = $folderName - - $param = @{ - RootModule = "MyModuleTest.psm1" - Author = "Me" - CompanyName = "MyCompany" - ModuleVersion = "6.6.6" - Description = "MyDescription of the module" - FunctionsToExport = @("MyFunction") - CopyRight = "(c) 2020 MyCompany. All rights reserved." - } - - $result = Add-TT_TestingToModuleV3 -Name $moduleName -Path $modulePath -Metadata $param - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestingV3 -Name $moduleName -Path $modulePath -Expected $param -} - -function TestingHelperTest_NewModuleV3_AddTestingToModule_AddTestingModuleV3_NoParam{ - - $moduleName = "MyModule" - $modulePath = '.' | Join-Path -ChildPath $moduleName - - $result = Add-TT_TestingToModuleV3 -Name $moduleName -Path $modulePath - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestingV3 -Name $moduleName -Path $modulePath -Expected $param -} - - -function TestingHelperTest_NewModuleV3_AddTestingToModule_Simple{ - - $moduleName = "MyModule" - $path = "." - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = Add-TT_TestingToModuleV3 -Name $moduleName -Path $modulePath - - Assert-AreEqualPath -Expected $modulePath -Presented $result - - Assert-TestingV3 -Name $moduleName -Path $modulePath -Expected $param - -} - -function TestingHelperTest_NewModuleV3_AddTestingToModule_WhatIf{ - Assert-NotImplemented -} - -function TestingHelperTest_NewModuleV3_AddTestingToModule_AddTestModuleV3_WhatIf{ - Assert-NotImplemented -} \ No newline at end of file diff --git a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 new file mode 100644 index 0000000..fc7ef54 --- /dev/null +++ b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 @@ -0,0 +1,35 @@ + +function TestingHelperTest_AddTestToModuleAll_NoParam{ + + $moduleName = "MyModule" + $modulePath = New-TT_ModuleV3 -Name $moduleName + + $modulePath | Set-Location + + # Should find module on current location and add test to it + $result = Add-TT_ToModuleTestAll -Passthru + + Assert-AreEqualPath -Expected $modulePath -Presented $result + Assert-AddTestModuleV3 -Path $modulePath +} + + +function TestingHelperTest_AddTestToModuleAll_Simple{ + + $moduleName = "MyModule" + $modulePath = New-ModuleV3 -Name $moduleName + + $result = Add-TT_ToModuleTestAll -Path $modulePath -Passthru + + Assert-AreEqualPath -Expected $modulePath -Presented $result + Assert-AddTestModuleV3 -Path $modulePath + +} + +# function TestingHelperTest_AddTestToModuleAll_WhatIf{ +# Assert-NotImplemented +# } + +# function TestingHelperTest_AddTestToModuleAll_AddTestModuleV3_WhatIf{ +# Assert-NotImplemented +# } \ No newline at end of file diff --git a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 index 27e6579..b7d97fe 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -15,10 +15,10 @@ function TestingHelperTest_NewModuleV3_WithName { $result = New-TT_ModuleV3 -Name $moduleName Assert-AreEqualPath -Expected $moduleName -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $moduleName + $result | Assert-AddModuleV3 } -function TestingHelperTest_NewModuleV3_WithNameRemotePath { +function TestingHelperTest_NewModuleV3_WithName_RemotePath { $moduleName = "MyModule" $folderName = "FolderName" @@ -27,14 +27,15 @@ function TestingHelperTest_NewModuleV3_WithNameRemotePath { $result = New-TT_ModuleV3 -Name $moduleName -Path $folderName Assert-AreEqualPath -Expected $expectedPath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $expectedPath + Assert-AddModuleV3 -Path $expectedPath } function TestingHelperTest_NewModuleV3_WithOutName { - # Figure out the Name from folder Name and path + # Error as the name is mandatory - New-TestingFolder -Name "folderName" -PassThru | Set-Location + $folder = New-TestingFolder -Name "ModulefolderName" -PassThru + Set-Location -Path $folder $result = New-TT_ModuleV3 @ErrorParameters @@ -42,7 +43,7 @@ function TestingHelperTest_NewModuleV3_WithOutName { Assert-Contains -Expected "Path and Name cannot be null or empty at the same time." -Presented $errorVar.Exception.Message } -function TestingHelperTest_NewModuleV3_AddTestingToModuleV3_Simple{ +function TestingHelperTest_NewModuleV3_AddTesting{ $moduleName = "MyModule" $path = '.' @@ -52,167 +53,24 @@ function TestingHelperTest_NewModuleV3_AddTestingToModuleV3_Simple{ Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestingV3 -Name $moduleName -Path $modulePath -} - -function TestingHelperTest_NewModuleV3_AddModuleV3_AddTestingToModuleV3_AddSampleCode{ - - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddTesting -AddSampleCode - - Assert-AreEqualPath -Expected $modulePath -Presented $result - - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddSampleCode - Assert-TestingV3 -Name $moduleName -Path $modulePath -AddSampleCode -} - -function TestingHelperTest_NewModuleV3_AddDevcontainerjson{ - - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddDevContainerJson - - Assert-AreEqualPath -Expected $modulePath -Presented $result - - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddDevContainerJson -} - -function TestingHelperTest_NewModuleV3_AddLICENSE{ - - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddLicense - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddLicense -} - -function TestingHelperTest_NewModuleV3_AddReadme{ - - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddReadme - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddReadme -} - -function TestingHelperTest_NewModuleV3_AddReadme_WithDescription{ - - $myDescription = "This is my Description" - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddReadme -Description $myDescription - - Assert-AreEqualPath -Expected $modulePath -Presented $result - - $readMePath = $modulePath | Join-Path -ChildPath "README.md" - Assert-IsTrue -Condition ((Get-Content -Path $readMePath) -contains $myDescription) -} - -function TestingHelperTest_NewModuleV3_AddAbout{ + Assert-AddModuleV3 -Path $modulePath + Assert-AddSampleCodes -Path $modulePath - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $param = @{ - Description = "This is my Description" - Author = "Me" - } - - $result = New-TT_ModuleV3 -Name $moduleName -AddAbout @param - - Assert-AreEqualPath -Expected $modulePath -Presented $result - - $moduleMonifest = Import-PowerShellDataFile -path ($modulePath | Join-Path -ChildPath "$moduleName.psd1" ) - Assert-AreEqual -Expected $param.Description -Presented $moduleMonifest.Description - Assert-AreEqual -Expected $param.Author -Presented $moduleMonifest.Author - Assert-AreEqual -Expected "$moduleName.psm1" -Presented $moduleMonifest.RootModule - - $aboutContent = Get-Content -Path ($modulePath | Join-Path -ChildPath "en-US" -AdditionalChildPath "about_MyModule.help.txt") | Out-String - Assert-IsTrue -Condition ($aboutContent.Contains("about_$moduleName")) - Assert-IsTrue -Condition ($aboutContent.Contains($moduleMonifest.Author)) - Assert-IsTrue -Condition ($aboutContent.Contains($moduleMonifest.Description)) - Assert-IsTrue -Condition ($aboutContent.Contains($moduleMonifest.CopyRight)) - Assert-IsTrue -Condition ($aboutContent.Contains("Powershell Testing UnitTest Module TestingHelper")) + Assert-AddTestModuleV3 -Path $modulePath + Assert-AddTestSampleCodes -Path $modulePath } -function TestingHelperTest_NewModuleV33_AddDeployScript{ - - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddDeployScript +function TestingHelperTest_NewModuleV3_AddAll{ - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddDeployScript -} - -function TestingHelperTest_NewModuleV3_AddReleaseScript{ - $moduleName = "MyModule" $path = '.' $modulePath = $path | Join-Path -ChildPath $moduleName - $result = New-TT_ModuleV3 -Name $moduleName -AddReleaseScript - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddReleaseScript -} - -function TestingHelperTest_NewModuleV3_AddSyncScript{ - - $moduleName = "MyModule" - $modulePath = '.' | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddSyncScript + $result = New-TT_ModuleV3 -Name $moduleName -AddAll Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddSyncScript -} - -function TestingHelperTest_NewModuleV3_AddPSScriptAnalyzer { - - $moduleName = "MyModule" - $modulePath = '.' | Join-Path -ChildPath $moduleName - $result = New-TT_ModuleV3 -Name $moduleName -AddPSScriptAnalyzerWorkflow + Assert-AddAll -Path $modulePath - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddPSScriptAnalyzerWorkflow } -function TestingHelperTest_NewModuleV3_AddTestingWorkflow { - - $moduleName = "MyModule" - $modulePath = '.' | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddTestingWorkflow - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddTestingWorkflow -} - -function TestingHelperTest_NewModuleV3_AddDeployWorkflow { - - $moduleName = "MyModule" - $modulePath = '.' | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddDeployWorkflow - - Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-AddModuleV3 -Name $moduleName -Path $modulePath -AddDeployWorkflow -} \ No newline at end of file diff --git a/TestingHelperTest/public/Tests-NewModule_operations.ps1 b/TestingHelperTest/public/Tests-NewModule_operations.ps1 new file mode 100644 index 0000000..2fdd3c4 --- /dev/null +++ b/TestingHelperTest/public/Tests-NewModule_operations.ps1 @@ -0,0 +1,69 @@ + +function TestingHelperTest_Manual_Work_StepsToFull{ + + $path = '.' | Join-Path -ChildPath "ModuleName" + + # Create module + $null = Add-TT_ModuleV3 -Name "ModuleName" + $modulePath = $path | Resolve-Path + Assert-AddModuleV3 -Path $modulePath + + # Move to module path to call add functions with no parameters + Push-Location $modulePath + + # Add Sample code on local folder + Add-TT_ToModuleSampleCode + Assert-AddSampleCodes -Path $modulePath + + # Add Testing to module + Add-TT_ToModuleTestModule + Assert-AddTestModuleV3 -Path $modulePath + + # Add Test Sample code + Add-TT_ToModuleTestSampleCode + Assert-AddTestSampleCodes -Path $modulePath + + Pop-Location + + # Run tests + + $result = Invoke-TT_TestingHelper -Path $modulePath + + Assert-AreEqual -Expected 2 -Presented $result.Tests + Assert-AreEqual -Expected 2 -Presented $result.Pass +} + +function TestingHelperTest_Manual_Work_Testing{ + $path = '.' | Join-Path -ChildPath "ModuleName" + + # Create module + $null = Add-TT_ModuleV3 -Name "ModuleName" + $modulePath = $path | Resolve-Path + Assert-AddModuleV3 -Path $modulePath + + # Move to module path to call add functions with no parameters + Push-Location $modulePath + + Add-TT_ToModuleAll + Assert-AddAll -Path $modulePath + + Pop-Location + + # Run tests + + $result = Invoke-TestingHelper -Path $modulePath + + Assert-AreEqual -Expected 2 -Presented $result.Tests + Assert-AreEqual -Expected 2 -Presented $result.Pass +} + +function TestingHelperTest_Manual_Work_Testing{ + + $moduleName = "modulename_{0}" -f (New-Guid).ToString().Substring(0,8) + + $result = New-TT_ModuleV3 -Name $moduleName -AddTesting + + $result = Invoke-TestingHelper -Path $result + Assert-AreEqual -Expected 2 -Presented $result.Tests + Assert-AreEqual -Expected 2 -Presented $result.Pass +} \ No newline at end of file diff --git a/TestingHelperTest/public/Tests-TestingFilesFolders.ps1 b/TestingHelperTest/public/Tests-TestingFilesFolders.ps1 index 7bac659..f14c89d 100644 --- a/TestingHelperTest/public/Tests-TestingFilesFolders.ps1 +++ b/TestingHelperTest/public/Tests-TestingFilesFolders.ps1 @@ -1,7 +1,7 @@ function TestingHelperTest_RemoveTestingFile_Root { - $tested = Get-TestedModuleHandle + $tested = Get-TestingHelperTestedModuleHandle $filename = "Filename.txt" @@ -92,7 +92,7 @@ function TestingHelperTest_RemoveTestingFolder_FolderNotExist{ function TestingHelperTest_RemoveTestingFolder_Not_TestRunFolder{ - $tested = Get-TestedModuleHandle + $tested = Get-TestingHelperTestedModuleHandle $t = New-Item -Name "NotStandardName" -ItemType Directory diff --git a/TestingHelperTest/public/Tests-test.ps1 b/TestingHelperTest/public/Tests-test.ps1 index 30097f4..4c20bac 100644 --- a/TestingHelperTest/public/Tests-test.ps1 +++ b/TestingHelperTest/public/Tests-test.ps1 @@ -13,15 +13,22 @@ function TestingHelperTest_TestPS1{ Assert-AreEqual -Expected "ModuleName" -Presented $result.Name Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName + + Remove-ImportedModule -Module "ModuleName" } function TestingHelperTest_TestPS1_WithPath{ - New-TT_Module -Name "ModuleName" -Description "description of the Module" -AddTesting + $moduleName = "ModuleName_{0}" -f (New-Guid).ToString().Substring(0,8) - $result = Invoke-TT_TestingHelper -Path "./ModuleName" + New-TT_Module -Name $moduleName -Description "description of the Module" -AddTesting + + $result = Invoke-TT_TestingHelper -Path "./$moduleName" + + Assert-AreEqual -Expected $moduleName -Presented $result.Name + Assert-AreEqual -Expected ("{0}Test" -f $moduleName) -Presented $result.TestModule + Assert-AreEqual -Expected ("{0}Test_*" -f $moduleName) -Presented $result.TestsName + Assert-AreEqual -Expected 2 -Presented $result.Tests + Assert-AreEqual -Expected 2 -Presented $result.Pass - Assert-AreEqual -Expected "ModuleName" -Presented $result.Name - Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule - Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName } \ No newline at end of file diff --git a/deploy_module_on_release.yml b/deploy_module_on_release.yml deleted file mode 100644 index e73346b..0000000 --- a/deploy_module_on_release.yml +++ /dev/null @@ -1,48 +0,0 @@ -name: Deploy on Release Published - -on: - workflow_dispatch: - release: - types: [published] - -permissions: - contents: read - -jobs: - deploy_to_powershellgallery: - runs-on: ubuntu-latest - environment: powershellgallery - steps: - - uses: actions/checkout@v3 - - env: - EVENT_CONTEXT: ${{ toJSON(github.event) }} - run: | - echo $EVENT_CONTEXT - - - name: deploy_ps1 - shell: pwsh - env: - NUGETAPIKEY: ${{ secrets.NUGETAPIKEY }} - EVENT_REF: ${{ github.event.ref }} - RELEASE_TAG: ${{ github.event.release.tag_name }} - RELEASE_NAME: ${{ github.event.release.name }} - run: | - $env:EVENT_REF = $env:REF - - If ([string]::IsNullOrEmpty($env:EVENT_REF)) { - # Release published trigger - $tag = $env:RELEASE_TAG - write-host -message "Release [$env:RELEASE_NAME] on tag [$tag]" - } else { - # Read Tag o Branch name - $tag = $env:EVENT_REF.Split('/')[2] - write-host "workflow_dispatch triggered on ref leaf [$tag]" - } - - If([string]::IsNullorwhitespace($tag)) { - # Tag name is empty, exit - write-error "Tag name is empty" - exit 1 - } - - ./deploy.ps1 -VersionTag $tag -NugetApiKey $env:NUGETAPIKEY diff --git a/docs/Create-a-Module.md b/docs/Create-a-Module.md new file mode 100644 index 0000000..55a6673 --- /dev/null +++ b/docs/Create-a-Module.md @@ -0,0 +1,45 @@ +# Create a Module + +Apis to create a module with the sections that you need + +## Create a Module with sample code + +- New-Module (Alias New-ModuleV3) +- New-ModuleV1 +- New-ModuleV2 +- New-ModuleV3 + +- Add-ModuleV3 + +- Add-ToModuleAll +- Add-ToModuleSampleCode + +## Add Testing for sample tests + +- New-TestingModule +- Add-TestModuleV3 +- Add-ToModuleTestAll + +- Add-ToModuleLaunchJson +- Add-ToModuleTestScript +- Add-ToModuleTestModule +- Add-ToModuleTestSampleCode + +## Add Module gearing + +- Add-ToModuleAbout +- Add-ToModuleReadme +- Add-ToModuleLicense +- Add-ToModuleDevContainerJson + +## Add Helper scripts to create releases and deploy + +- Add-ToModuleDeployScript +- Add-ToModuleReleaseScript +- Add-ToModuleSyncScript + +## Add GitHub Worflows for Code Analysis, Testing and Deploy + +- Add-ToModuleDeployWorkflow +- Add-ToModulePSScriptAnalyzerWorkflow +- Add-ToModuleTestWorkflow diff --git a/docs/api-and-asserts.md b/docs/api-and-asserts.md new file mode 100644 index 0000000..bfeb125 --- /dev/null +++ b/docs/api-and-asserts.md @@ -0,0 +1,84 @@ + +# Testing + +- Invoke-TestingHelper +- Import-TestingModule +- Test-Module (*ObsoleteAttribute*) +- Test-ModulelocalPSD1 (*ObsoleteAttribute*) + +# Tracing + +- Trace-Message +- Write-AssertionSectionEnd + +# Files and Folders + +- New-TestingFile +- New-TestingFolder +- Remove-TestingFile +- Remove-TestingFolder + +# New Module + +- New-Module (Alias New-ModuleV3) +- New-ModuleV1 +- New-ModuleV2 +- New-ModuleV3 +- New-TestingModule + +- Add-ModuleV3 +- Add-TestModuleV3 + +# AddToModule* + +- Add-ToModuleAbout +- Add-ToModuleAll +- Add-ToModuleDeployScript +- Add-ToModuleDeployWorkflow +- Add-ToModuleDevContainerJson +- Add-ToModuleLaunchJson +- Add-ToModuleLicense +- Add-ToModulePSScriptAnalyzerWorkflow +- Add-ToModuleReadme +- Add-ToModuleReleaseScript +- Add-ToModuleSampleCode +- Add-ToModuleSyncScript + +- Add-ToModuleTestAll +- Add-ToModuleTestModule +- Add-ToModuleTestSampleCode +- Add-ToModuleTestScript +- Add-ToModuleTestWorkflow + +# Asserts + +- Assert-AreEqual +- Assert-AreEqualContent +- Assert-AreEqualPath +- Assert-AreEqualSecureString +- Assert-AreNotEqual +- Assert-AreNotEqualContent +- Assert-AreNotEqualPath +- Assert-CollectionIsNotNullOrEmpty +- Assert-CollectionIsNullOrEmpty +- Assert-ContainedXOR +- Assert-Contains +- Assert-ContainsPath +- Assert-Count +- Assert-CountTimes +- Assert-FileContains +- Assert-FilesAreEqual +- Assert-FilesAreNotEqual +- Assert-IsFalse +- Assert-IsGuid +- Assert-IsNotNull +- Assert-IsNull +- Assert-IsTrue +- Assert-ItemExist +- Assert-ItemNotExist +- Assert-NotContains +- Assert-NotContainsPath +- Assert-NotImplemented +- Assert-SkipTest +- Assert-StringIsNotNullOrEmpty +- Assert-StringIsNullOrEmpty diff --git a/docs/deploying-with-testinghelper.md b/docs/deploying-with-testinghelper.md new file mode 100644 index 0000000..4aff802 --- /dev/null +++ b/docs/deploying-with-testinghelper.md @@ -0,0 +1,5 @@ +# Deploying with TestingHelper + +- deploy on release workflow +- using Release script to trigger the release +- Managing Tags diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..fc08b84 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,13 @@ +# TestingHelper Documentation + +> This documentation is under development + +- [Structure of a Module](structure-of-a-module.md) +- [Create and adding features to a Module](create-a-module.md) +- [Writing tests with TestingHelper](writing-tests-with-testinghelper.md) +- [Deploying with TestingHelper](deploying-with-testinghelper.md) + +## Project + +- [GitHub project](https://github.com/rulasg/TestingHelper/) +- [PsModuleGallery](https://www.powershellgallery.com/packages/TestingHelper/) diff --git a/docs/structure-of-a-module.md b/docs/structure-of-a-module.md new file mode 100644 index 0000000..28ea493 --- /dev/null +++ b/docs/structure-of-a-module.md @@ -0,0 +1,32 @@ +# Structure of a Module + +## Module + + 1. Module Manifest + 1. Module Script(s) + 2. About + 3. license (MIT) + 4. devcontainer.json + +## Testing + + 1. Testing + 1. Test module + 1. Module manifest + 2. Module script(s) + 2. launch.json + 3. test.ps1 + +## Tools Scripts + + 1. publish.ps1 + 1. publish-helper.ps1 + 2. release.ps1 + 3. sync.ps1 + 1. sync-helper.ps1 + +## Workflows + + 1. PSScriptAnalyzer.yml + 2. publish_module_on_release.yml - publish.ps1 + 3. test_with_TestingHelper.yml - test.ps1 diff --git a/docs/table-of-assert-AddtoModule.md b/docs/table-of-assert-AddtoModule.md new file mode 100644 index 0000000..13a2439 --- /dev/null +++ b/docs/table-of-assert-AddtoModule.md @@ -0,0 +1,28 @@ +# Asserts + +| tag | Add-ToModule* (Add-) | Switch New-ModuleV3 | Add-ToModuleAll (11) | Assert-AddToModule* (Assert-) | Assert-AddAll(10) | Add-ToModuleTestAll | Assert-AddTestAll | +| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | +| AddModule | Add-ModuleV3 | None | x | Assert-AddModuleV3 | | | x | +| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | +| DeployScript | Add-ToModuleDeployScript | AddDeployScript | x | Assert-AddDeployScript | x | | | +| About | Add-ToModuleAbout | AddAbout | x | Assert-AddToModuleAbout | x | | | +| DevContainer | Add-ToModuleDevContainerJson | AddDevContainerJson | x | Assert-AddDevContainerJson | x | | | +| License | Add-ToModuleLicense | AddLicense | x | Assert-AddLicense | x | | | +| Readme | Add-ToModuleReadme | AddReadme | x | Assert-AddReadMe | x | | | +| Release | Add-ToModuleReleaseScript | AddReleaseScript | x | Assert-AddReleaseScript | x | | | +| Sync | Add-ToModuleSyncScript | AddSyncScript | x | Assert-AddSyncScript | x | | | +| PSScriptAnalyzer | Add-ToModulePSScriptAnalyzerWorkflow | AddPSScriptAnalyzerWorkflow | x | Assert-AddPSScriptAnalyzerWorkflow | x | | | +| TestWorkflow | Add-ToModuleTestWorkflow | AddTestWorkflow | x | Assert-AddTestWorkflow | x | | | +| DeployWorkflow | Add-ToModuleDeployWorkflow | AddDeployWorkfloW | x | Assert-AddDeployWorkflow | x | | | +| SampleCode | Add-ToModuleSampleCode | AddSampleCode | Bug | Assert-AddSampleCodes | x | | | +| TestAll | Add-ToModuleTestAll | AddTesting | x | Assert-AddTestAll | x | Original | Original | +| TestSampleCode | Add-ToModuleTestSampleCode | AddTesting + AddSampleCode | x | Assert-AddTestSampleCodes | x | x | x | +| TestScript | Add-ToModuleTestScript | | Add-ToModuleTestAll | Assert-AddTestTestScript | | x | x | +| LaunchJson | Add-ToModuleLaunchJson | | Add-ToModuleTestAll | Assert-AddTestLaunchJson | | x | x | +| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | +| AddTestModule | Add-ToModuleTestModule -> Add-TestModuleV3 | | Add-ToModuleTestAll | Assert-AddTestModuleV3 | | x | | +| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | +| All | Add-ToModuleAll | AddAll | Original | Assert-AddAll | | | | +| | | | | | | | | +| | | | | | | | | +| | | | | | | | | \ No newline at end of file diff --git a/docs/writing-tests-with-testinghelper.md b/docs/writing-tests-with-testinghelper.md new file mode 100644 index 0000000..f8ad2c3 --- /dev/null +++ b/docs/writing-tests-with-testinghelper.md @@ -0,0 +1,8 @@ +# Writing tests with TestingHelper + +- Adding a test module +- Naming convention +- Testing functions +- Asserts +- Tracing +- Return values \ No newline at end of file diff --git a/powershell.yml b/powershell.yml deleted file mode 100644 index 96d32d8..0000000 --- a/powershell.yml +++ /dev/null @@ -1,47 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. -# -# https://github.com/microsoft/action-psscriptanalyzer -# For more information on PSScriptAnalyzer in general, see -# https://github.com/PowerShell/PSScriptAnalyzer - -name: PSScriptAnalyzer - -on: - workflow_dispatch: - pull_request: - -permissions: - contents: read - -jobs: - build: - permissions: - contents: read # for actions/checkout to fetch code - security-events: write # for github/codeql-action/upload-sarif to upload SARIF results - actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status - name: PSScriptAnalyzer - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - - name: Run PSScriptAnalyzer - uses: microsoft/psscriptanalyzer-action@v1.1 - with: - # Check https://github.com/microsoft/action-psscriptanalyzer for more info about the options. - # The below set up runs PSScriptAnalyzer to your entire repository and runs some basic security rules. - path: .\ - recurse: true - severity: 'Error' - # Include your own basic security rules. Removing this option will run all the rules - # includeRule: '"PSAvoidGlobalAliases", "PSAvoidUsingConvertToSecureStringWithPlainText"' - output: results.sarif - - # Upload the SARIF file generated in the previous step - - name: Upload SARIF results file - uses: github/codeql-action/upload-sarif@v2 - with: - sarif_file: results.sarif - diff --git a/private/Add-ToModule.Helper.ps1 b/private/Add-ToModule.Helper.ps1 new file mode 100644 index 0000000..355ebf5 --- /dev/null +++ b/private/Add-ToModule.Helper.ps1 @@ -0,0 +1,31 @@ + +# Create the return value to allow AddToModule pipe chaining +function ReturnValue($Path,$Force, $Passthru){ + # create object with the two parameters as properties + + if($Passthru){ + return [pscustomobject]@{ + Path = $Path + Force = $Force + Passthru = $Passthru + } + } +} + +# Normalize $Path and returns $null if not valid +function NormalizePath($Path){ + # Path returned should be the folder where the module is located. + + # Aceot local folder as default + $Path = [string]::isnullorwhitespace($Path) ? '.' : $Path + + # We may input the RootModule as if we pipe Get-Module command. + # check if $Path is a file and get the parent of it + if(Test-Path -Path $Path -PathType Leaf){ + $ret = $Path | Split-Path -Parent + } else { + $ret = $Path + } + + return $ret | Convert-Path +} \ No newline at end of file diff --git a/private/Helper-NewModuleV3.ps1 b/private/Helper-NewModuleV3.ps1 deleted file mode 100644 index 3c5c642..0000000 --- a/private/Helper-NewModuleV3.ps1 +++ /dev/null @@ -1,162 +0,0 @@ - -# function Get-ModulePath{ -# [CmdletBinding()] -# param( -# [Parameter()][string]$Name, -# [Parameter()][string]$Path -# ) - -# # Asumes current as the default path -# $path = [string]::IsNullOrWhiteSpace($Path) ? (Get-Location | Convert-Path) : $Path - -# # Use dot local path -# $modulePath = [string]::IsNullOrWhiteSpace($Name) ? $path : ($path | Join-Path -ChildPath $Name) - -# return $modulePath -# } - -# function Get-ModulePath{ -# [CmdletBinding()] -# param( -# [Parameter()][string]$Name, -# [Parameter()][string]$Path -# ) - -# # | path/name | null | Name | -# # | --------- | ----- | ----------- | -# # | null | Error | Name | -# # | Path | Path | Path + Name | - -# if ([string]::IsNullOrWhiteSpace($Path) -and [string]::IsNullOrWhiteSpace($Name)) { -# write-Error "Path and Name cannot be null or empty at the same time." -# return $null -# } - -# $modulePath = [string]::IsNullOrWhiteSpace($Path) ? (Get-Location | Convert-Path) : $Path - -# $ret = ([string]::IsNullOrWhiteSpace($Name)) ? $modulePath : ($modulePath | Join-Path -ChildPath $Name) - -# return $ret -# } - -# function Get-ModulePath{ -# [CmdletBinding()] -# param( -# [Parameter()][string]$Name, -# [Parameter()][string]$Path -# ) - -# # | path/name | null | Name | -# # | --------- | ----- | ----------- | -# # | null | Error | Name | -# # | Path | Path | Path | - -# if ([string]::IsNullOrWhiteSpace($Path) -and [string]::IsNullOrWhiteSpace($Name)) { -# write-Error "Path and Name cannot be null or empty at the same time." -# return $null -# } - -# $ret = [string]::IsNullOrWhiteSpace($Path) ? $Name : $Path - -# return $ret -# } - -function Get-ModulePath{ - [CmdletBinding()] - param( - [Parameter()][string]$Name, - [Parameter()][string]$Path, - [Parameter()][switch]$AppendName - ) - - # | path/name | null | Name | - # | --------- | ----- | ----------- | - # | null | Error | Name | - # | Path | Path | Path | - - if ([string]::IsNullOrWhiteSpace($Path) -and [string]::IsNullOrWhiteSpace($Name)) { - write-Error "Path and Name cannot be null or empty at the same time." - return $null - } - - $ret = ([string]::IsNullOrWhiteSpace($Path) ? $Name : ` - ([string]::IsNullOrWhiteSpace($Name) ? $Path : ` - ( !$AppendName ? $Path : ` - ($Path | Join-Path -ChildPath $Name)))) - - - return $ret -} - -function Get-TestModulePath{ - [CmdletBinding()] - param( - [Parameter()][string]$Name, - [Parameter()][string]$Path - ) - - $modulepath = Get-ModulePath -Name $Name -Path $Path - $moduleName = Get-ModuleName -Name $Name -ModulePath $Path - - $testModuleName = Get-TestModuleName -Name $moduleName - $tesModulePath = $modulepath | Join-Path -ChildPath $testModuleName - - return $tesModulePath -} - -function Get-ModuleName{ - [CmdletBinding()] - param( - [Parameter()][string]$Name, - [Parameter()][string]$ModulePath - ) - - #Return Name if provided - if(![string]::IsNullOrWhiteSpace($Name)){ - return $Name - } - - # extract name from path - $retName = Get-ModulePath -Name $Name -Path $ModulePath | Split-Path -Leaf - - return $retName -} - -function Get-TestModuleName { - [CmdletBinding()] - param ( - [parameter(Mandatory)] [string] $Name - ) - - return ($Name + "Test") -} - -function Add-Folder{ - [CmdletBinding(SupportsShouldProcess)] - param( - [Parameter(Mandatory,ValueFromPipeline)][string]$Path - ) - - process { - - try { - #test if path exists - if($Path | Test-Path){ - Write-Error "Path already exists." - return $false - } else { - if ($PSCmdlet.ShouldProcess($Path, "New-Item -ItemType Directory")) { - $null = New-Item -ItemType Directory -Path $Path - } - - return $true - } - } - catch { - Write-Error -Message "Failed to add path." - return $false - } - } - -} - diff --git a/private/Manifest-Helper.ps1 b/private/Manifest.Helper.ps1 similarity index 65% rename from private/Manifest-Helper.ps1 rename to private/Manifest.Helper.ps1 index 45c5c74..304d910 100644 --- a/private/Manifest-Helper.ps1 +++ b/private/Manifest.Helper.ps1 @@ -19,6 +19,27 @@ function Get-ModuleManifest($Path){ return $manifest } +function Import-ModuleManifest ($Path){ + + $manifestPath = Get-ModuleManifestPath -Path $Path + + $ret = $manifestPath ? (Import-PowerShellDataFile -Path $manifestPath) : $null + + return $ret +} + +function Get-ModuleManifestPath ($Path){ + $localPath = $Path | Convert-Path + + $Name = $localPath | Split-Path -leafbase + + $manifestPath = $Path | Join-Path -ChildPath "$Name.psd1" + + $ret = ($manifestPath | Test-Path) ? $manifestPath : $null + + return $ret +} + # returns the manifest of the testing module of the module on Path function Get-TestingModuleManifest($ModulePath){ diff --git a/private/Microsoft.PowerShell.Core-Dependency.ps1 b/private/Microsoft.PowerShell.Core.Dependency.ps1 similarity index 100% rename from private/Microsoft.PowerShell.Core-Dependency.ps1 rename to private/Microsoft.PowerShell.Core.Dependency.ps1 diff --git a/private/Module-Helper.ps1 b/private/Module.Helper.ps1 similarity index 100% rename from private/Module-Helper.ps1 rename to private/Module.Helper.ps1 diff --git a/private/New-ModuleV3.Helper.ps1 b/private/New-ModuleV3.Helper.ps1 new file mode 100644 index 0000000..677a71b --- /dev/null +++ b/private/New-ModuleV3.Helper.ps1 @@ -0,0 +1,96 @@ + + +function Get-ModulePath{ + [CmdletBinding()] + param( + [Parameter()][string]$Name, + [Parameter()][string]$RootPath + ) + + # | path/name | null | Name | + # | --------- | ----- | ----------- | + # | null | Error | ./Name | + # | Path | '.' | Path/Name | + + if ([string]::IsNullOrWhiteSpace($Path) -and [string]::IsNullOrWhiteSpace($Name)) { + write-Error "Path and Name cannot be null or empty at the same time." + return $null + } + + #check if path is null + $path = [string]::IsNullOrWhiteSpace($Path) ? (Get-Location | Convert-Path) : $Path + $ret = $path | Join-Path -ChildPath $Name + return $ret +} + +function Get-ModuleName{ + [CmdletBinding()] + param( + [Parameter()][string]$Path + ) + + # null if path is null + if([string]::IsNullOrWhiteSpace($Path)){ + return $null + } + + $name = $Path | Split-Path -LeafBase + + return $name +} + +function Get-TestModulePath{ + [CmdletBinding()] + param( + [Parameter()][string]$Path + ) + + $moduleName = Get-ModuleName -Path $Path + + $testModulePath = $path | Join-Path -ChildPath ($moduleName + "Test") + + return $testModulePath +} + +function Get-TestModuleName { + [CmdletBinding()] + param ( + [parameter(Mandatory)] [string] $Path + ) + + $testPath = Get-TestModulePath -Path $Path + $name = Get-ModuleName -Path $testPath + + return $name +} + +function New-Folder{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipeline)][string]$Path + ) + + process { + + try { + #test if path exists + if($Path | Test-Path){ + Write-Error "Path already exists." + return $null + } else { + if ($PSCmdlet.ShouldProcess($Path, "New-Item -ItemType Directory")) { + $null = New-Item -ItemType Directory -Path $Path + } + + # Converting to Provider path + return $Path | Convert-Path + } + } + catch { + Write-Error -Message "Failed to add path." + return $null + } + } + +} + diff --git a/private/Templates-Helper.ps1 b/private/Templates-Helper.ps1 deleted file mode 100644 index a468029..0000000 --- a/private/Templates-Helper.ps1 +++ /dev/null @@ -1,29 +0,0 @@ -function Import-Template { - [CmdletBinding(SupportsShouldProcess)] - param( - [Parameter(Mandatory)][string]$Path, - [Parameter(Mandatory)][string]$File, - [Parameter(Mandatory)][string]$Template, - [Parameter()][hashtable]$Replaces - ) - - # test if $path exists - if(!($Path | Test-Path)){ - if ($PSCmdlet.ShouldProcess($Path, "New-Item -Directory -Force")) { - $null = New-Item -ItemType Directory -Force -Path $Path - } - } - - $content = Get-Content -Path ($PSScriptRoot | Join-Path -ChildPath templates -AdditionalChildPath $Template) - - if ($Replaces) { - $Replaces.Keys | ForEach-Object { - $content = $content.Replace($_, $Replaces.$_) - } - } - - $destination = Join-Path -Path $Path -ChildPath $File - if ($PSCmdlet.ShouldProcess($destination, "Set-Content")) { - $content | Set-Content -Path $destination - } -} diff --git a/private/Templates.Helper.ps1 b/private/Templates.Helper.ps1 new file mode 100644 index 0000000..f3b2013 --- /dev/null +++ b/private/Templates.Helper.ps1 @@ -0,0 +1,60 @@ +# Imports a template to a file and replace content if $Force +function Import-Template { + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory)][string]$File, + [Parameter(Mandatory)][string]$Template, + [Parameter()][hashtable]$Replaces, + [Parameter()][switch]$Force + ) + + $destination = Join-Path -Path $Path -ChildPath $File + + if (($destination | Test-Path) -and !$Force) { + Write-Warning -Message "File $destination already exists. Use -Force to overwrite" + return $false + } + + # test if $path exists + if(!($Path | Test-Path)){ + if ($PSCmdlet.ShouldProcess($Path, "New-Item -Directory -Force")) { + $null = New-Item -ItemType Directory -Force -Path $Path + } + } + + $templatePath = $PSScriptRoot | Join-Path -ChildPath templates -AdditionalChildPath $Template + $content = Get-Content -Path $templatePath + + if ($Replaces) { + $Replaces.Keys | ForEach-Object { + $content = $content.Replace($_, $Replaces.$_) + } + } + + $destination = Join-Path -Path $Path -ChildPath $File + if ($PSCmdlet.ShouldProcess($destination, "Set-Content")) { + $content | Set-Content -Path $destination + } +} + +# function Add-FileFromTemplate { +# [CmdletBinding(SupportsShouldProcess)] +# param( +# [Parameter(Mandatory)][string]$Path, +# [Parameter(Mandatory)][string]$File, +# [Parameter(Mandatory)][string]$Template, +# [Parameter()][hashtable]$Replaces, +# [Parameter()][switch]$Force +# ) + +# $destination = Join-Path -Path $Path -ChildPath $File + +# if (($destionation | Test-Path) -and !$Force) { +# Write-Warning -Message "File $destination already exists. Use -Force to overwrite" +# return $false +# } + +# # Import will create File and folder and overwrite if exists +# Import-Template -Path $Path -File $File -Template $Template -Replaces $Replaces +# } \ No newline at end of file diff --git a/private/TestingFilesFolders.Helper.ps1 b/private/TestingFilesFolders.Helper.ps1 new file mode 100644 index 0000000..458ddc1 --- /dev/null +++ b/private/TestingFilesFolders.Helper.ps1 @@ -0,0 +1,50 @@ +function GetRooTestingFolderPath{ + # get the first 6 char of a guid + $random = (New-Guid).ToString().Substring(0,6) + $rd = Get-Date -Format yyMMdd + $path = Join-Path -Path "Temp:" -ChildPath ("Posh_Testing_{0}_{1}" -f $rd,$random) + return $path +} + +function Push-TestingFolder { + [CmdletBinding()] + param ( + [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][string] $Path + ) + + $originalPath = Resolve-Path . + + if ($Path) { + $testFolderName = $Path + } + else { + $testFolderName = Join-Path -Path (GetRooTestingFolderPath) -ChildPath $TestRunFolderName + } + New-TestingFolder $testFolderName + $TestRunPath = Resolve-Path -Path $testFolderName + + if (Test-Path -Path $TestRunPath) { Remove-Testingfolder -Path $TestRunPath } + + New-Item -Path $TestRunPath -ItemType "directory" -Force | Out-Null + + Set-Location -Path $TestRunPath + + return $originalPath +} + +function Pop-TestingFolder { + [CmdletBinding()] + param ( + [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][string] $Path, + [switch] $Force + ) + + $local = Get-Location | Resolve-Path + $localLeaf = $local | Split-Path -Leaf + + Set-Location -Path $Path + + if (($localLeaf -eq $TestRunFolderName) -or $Force) { + Remove-TestingFolder -Path $local + } +} \ No newline at end of file diff --git a/private/templates/template.v3.deploy-helper.ps1 b/private/templates/template.v3.deploy-helper.ps1 index cb29901..96ef449 100644 --- a/private/templates/template.v3.deploy-helper.ps1 +++ b/private/templates/template.v3.deploy-helper.ps1 @@ -35,7 +35,7 @@ function Invoke-DeployModuleToPSGallery{ ) # look for psd1 file on the same folder as this script - $moduleName = $PSScriptRoot | Split-Path -leaf + $moduleName = $PSScriptRoot | Split-Path -LeafBase $psdPath = $PSScriptRoot | Join-Path -ChildPath "$moduleName.psd1" # check if $psd is set @@ -149,7 +149,7 @@ function Get-DeployModuleManifestPath { param() # look for psd1 file on the same folder as this script - $moduleName = $PSScriptRoot | Split-Path -leaf + $moduleName = $PSScriptRoot | Split-Path -LeafBase $psdPath = $PSScriptRoot | Join-Path -ChildPath "$moduleName.psd1" # check if $psd is set diff --git a/public/New-ModuleV3_Add-ModuleV3.ps1 b/public/Add-ModuleV3.ps1 similarity index 58% rename from public/New-ModuleV3_Add-ModuleV3.ps1 rename to public/Add-ModuleV3.ps1 index 6420ee9..afe4030 100644 --- a/public/New-ModuleV3_Add-ModuleV3.ps1 +++ b/public/Add-ModuleV3.ps1 @@ -10,39 +10,33 @@ function Add-ModuleV3 { [CmdletBinding()] Param ( - [Parameter()][string]$Path, [Parameter(Mandatory)][string]$Name, + [Parameter()][string]$RootPath, [Parameter()][hashtable]$Metadata, [Parameter()][switch]$AddSampleCode ) - # Resolve Path. Check if fails - $modulePath = Get-ModulePath -Path $Path -Name $Name - if(!$modulePath){return $null} + # Resolve Path. Check if fails. + $modulePathString = Get-ModulePath -RootPath $Path -Name $Name + if(!$modulePathString){return $null} # Create the module folder. Fail if exists - if(!($modulePath | Add-Folder)){ - return $null - } + # This will filter if Path already exist to avoid overwriting an existing module + $modulePath = $modulePathString | New-Folder + if( !$modulePath ){ return $null } + + $moduleName = Get-ModuleName -Path $modulePath # PSM1 - $rootModule = "$Name.psm1" - Import-Template -Path $modulePath -File $rootModule -Template "template.module.psm1" + $rootModule = "$moduleName.psm1" + Import-Template -Path $modulePath -File $rootModule -Template "template.module.psm1" -Force:$Force # public private $null = New-Item -ItemType Directory -Force -Path ($modulePath | Join-Path -ChildPath "public") $null = New-Item -ItemType Directory -Force -Path ($modulePath | Join-Path -ChildPath "private") - # Sample code - if ($AddSampleCode) { - $destination = $modulePath | Join-Path -ChildPath "public" - Import-Template -Path $destination -File "samplePublicFunction.ps1" -Template "template.module.functions.public.ps1" - $destination = $modulePath | Join-Path -ChildPath "private" - Import-Template -Path $destination -File "samplePrivateFunction.ps1" -Template "template.module.functions.private.ps1" - } - # PSD1 - $psd1Path = ($modulePath | Join-Path -ChildPath "$Name.psd1") + $psd1Path = ($modulePath | Join-Path -ChildPath "$moduleName.psd1") try { # Create the PSD1 file @@ -58,6 +52,6 @@ function Add-ModuleV3 { return $null } - return $modulePath + return $modulePath | Convert-Path } Export-ModuleMember -Function Add-ModuleV3 \ No newline at end of file diff --git a/public/Add-TestModuleV3.ps1 b/public/Add-TestModuleV3.ps1 new file mode 100644 index 0000000..6d6361d --- /dev/null +++ b/public/Add-TestModuleV3.ps1 @@ -0,0 +1,37 @@ +function Add-TestModuleV3 { + [CmdletBinding()] + Param + ( + [Parameter(Mandatory)][string]$Path, + [Parameter()][switch]$Force + ) + + $testingModuleName = Get-TestModuleName -Path $Path + $testingModulePath = Get-TestModulePath -Path $Path + + # Generate metadata from the module if exists + $manifestPath = Get-ModuleManifestPath -Path $Path + if($manifestPath){ + $manifest = Import-PowerShellDataFile -Path $manifestPath + + # Probably we need to tune the metadata fo the testing module + $manifest.Remove('Path') + $manifest.Remove('PsdPath') + $manifest.Remove('Name') + $manifest.Remove('RootModule') + $manifest.Remove('GUID') + } + + # TODO: Not sure how to mangage Force on Add-Module functions + + $result = Add-ModuleV3 -Name $testingModuleName -RootPath $testingModulePath -Metadata $manifest + + if(!$result){ + Write-Error -Message ("Error creating the module [$testingModuleName].") + return $null + } + + return $result + +} Export-ModuleMember -Function Add-TestModuleV3 + diff --git a/public/Add-ToModule.ps1 b/public/Add-ToModule.ps1 new file mode 100644 index 0000000..d0802be --- /dev/null +++ b/public/Add-ToModule.ps1 @@ -0,0 +1,447 @@ +# This is a set of functions that will add sections to a given module +# The intention is to allow the functions to be piped between them or even with the output of Get-Module +# The Ad-Module* functions will take 2 parametst +# -Path: The path of the module PS1 or PSM1 as Get-Module returns, But we will allow also the root of a module +# -Force: If the file already exists, it will be overwritten withe the default values +# The output will be the Path of the module updated. This way we may pipe with next Add-ToModule* function + + + +function Add-ToModuleSampleCode{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + + # Function works based on modulePath + $modulePath = NormalizePath -Path:$Path ?? return $null + + $destination = $modulePath | Join-Path -ChildPath "public" + Import-Template -Path $destination -File "samplePublicFunction.ps1" -Template "template.module.functions.public.ps1" + + $destination = $modulePath | Join-Path -ChildPath "private" + Import-Template -Path $destination -File "samplePrivateFunction.ps1" -Template "template.module.functions.private.ps1" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleSampleCode + +# Add devcontainer.json file +function Add-ToModuleDevContainerJson{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $destination = $Path | Join-Path -ChildPath ".devcontainer" + Import-Template -Force:$Force -Path $destination -File "devcontainer.json" -Template "template.devcontainer.json" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleDevContainerJson + +# Add License file +function Add-ToModuleLicense{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + Import-Template -Force:$Force -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleLicense + +# Add Readme file +function Add-ToModuleReadme{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $moduleManifest = Import-ModuleManifest -Path $Path + # try{$moduleManifest = Get-ModuleManifest -Path $Path }catch{$moduleManifest = $null} + $moduleName = $Path | Split-Path -LeafBase + Import-Template -Force:$Force -Path $Path -File "README.md" -Template "template.README.md" -Replaces @{ + "_MODULE_NAME_" = $moduleName + "_MODULE_DESCRIPTION_" = ($moduleManifest.Description ?? "A powershell module that will hold Powershell functionality.") + } + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleReadme + +# Add about +function Add-ToModuleAbout{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $moduleManifest = Import-ModuleManifest -Path $Path + # try{$moduleManifest = Get-ModuleManifest -Path $Path} catch{$moduleManifest = $null} + $moduleName = $Path | Split-Path -LeafBase + $destination = $Path | Join-Path -ChildPath "en-US" + Import-Template -Force:$Force -Path $destination -File "about_$moduleName.help.txt" -Template "template.about.help.txt" -Replaces @{ + "_MODULE_NAME_" = ($moduleName ?? "") + "_MODULE_DESCRIPTION_" = ($moduleManifest.Description ?? "") + "_AUTHOR_" = ($moduleManifest.Author ?? "") + "_COPYRIGHT_" = ($moduleManifest.CopyRight ?? "") + + } + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleAbout + +# Add deploying +function Add-ToModuleDeployScript{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + Import-Template -Force:$Force -Path $Path -File "deploy.ps1" -Template "template.v3.deploy.ps1" + Import-Template -Force:$Force -Path $Path -File "deploy-helper.ps1" -Template "template.v3.deploy-helper.ps1" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleDeployScript + +# Add Release +function Add-ToModuleReleaseScript{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + Import-Template -Force:$Force -Path $Path -File "release.ps1" -Template "template.v3.release.ps1" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleReleaseScript + +# Add Sync +function Add-ToModuleSyncScript{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + Import-Template -Force:$Force -Path $Path -File "sync.ps1" -Template "template.v3.sync.ps1" + Import-Template -Force:$Force -Path $Path -File "sync-helper.ps1" -Template "template.v3.sync-helper.ps1" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleSyncScript + +# Add PSScriptAnalyzer +function Add-ToModulePSScriptAnalyzerWorkflow{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" + Import-Template -Force:$Force -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModulePSScriptAnalyzerWorkflow + +# Add Testing +function Add-ToModuleTestWorkflow{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" + Import-Template -Force:$Force -Path $destination -File "test_with_TestingHelper.yml" -Template "template.v3.test_with_TestingHelper.yml" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleTestWorkflow + +# Add deploy Workflow +function Add-ToModuleDeployWorkflow{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" + Import-Template -Force:$Force -Path $destination -File "deploy_module_on_release.yml" -Template "template.v3.deploy_module_on_release.yml" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleDeployWorkflow + + +###### TEST ###### + +# Add test.ps1 script +function Add-ToModuleTestScript{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + Import-Template -Force:$Force -Path $Path -File "test.ps1" -Template "template.v3.test.ps1" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleTestScript + +# Add launch.json to module +function Add-ToModuleLaunchJson{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + $destination = $Path | Join-Path -ChildPath ".vscode" + + Import-Template -Force:$Force -Path $destination -File "launch.json" -Template "template.launch.json" + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleLaunchJson + + +function Add-ToModuleTestSampleCode{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $moduleName = Get-ModuleName -Path $Path + $testModulePath = Get-TestModulePath -Path $Path + $testModulename = Get-TestModuleName -Path $Path + $destination = $testModulePath | Join-Path -ChildPath "public" + + Import-Template -Path $destination -Force:$Force -File "SampleFunctionTests.ps1" -Template "template.testmodule.functions.public.ps1" -Replaces @{ + '_MODULE_TESTING_' = $testModulename + '_MODULE_TESTED_' = $ModuleName + } + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleTestSampleCode + + +###### Add All ###### + +function Add-ToModuleTestModule{ + [CmdletBinding()] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + process { + + $Path = NormalizePath -Path:$Path ?? return $null + + #if Path is null return + $modulePath = Get-ModulePath -RootPath $Path + if(!$modulePath){return $null} + + # Test Module + $testModulePath = Add-TestModuleV3 -Path $Path -Force:$Force + + if (!$testModulePath) { + $name = Get-ModuleName -Path $Path + Write-Error -Message ("Error creating Testing for Module [$Name].") + return $null + } + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } +} Export-ModuleMember -Function Add-ToModuleTestModule + + +# Add TestModuleV3 and the rest of the dressing +function Add-ToModuleTestAll{ + [CmdletBinding()] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + + $Path = NormalizePath -Path:$Path ?? return $null + + #if Path is null return + $modulePath = Get-ModulePath -RootPath $Path + if(!$modulePath){return $null} + + # Test Module + $result = Add-ToModuleTestModule -Path $modulePath -Force:$Force -Passthru + + if (!$result) { + $name = Get-ModuleName -Path $Path + Write-Error -Message ("Error creating Testing for Module [$Name].") + return $null + } + + # Sample Code + Add-ToModuleTestSampleCode -Path $modulePath -Force:$Force + + # Add test.ps1 + Add-ToModuleTestScript -Path $modulePath -Force:$Force + + # Add launch.json + Add-ToModuleLaunchJson -Path $modulePath -Force:$Force + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } + +} Export-ModuleMember -Function Add-ToModuleTestAll + +function Add-ToModuleAll{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $Path | Add-ToModuleTestAll -Force:$Force + + $Path | Add-ToModuleSampleCode -Force:$Force + + $Path | Add-ToModuleDevContainerJson -Force:$Force + $Path | Add-ToModuleLicense -Force:$Force + $Path | Add-ToModuleReadme -Force:$Force + $Path | Add-ToModuleAbout -Force:$Force + + # scripts + $Path | Add-ToModuleDeployScript -Force:$Force + $Path | Add-ToModuleReleaseScript -Force:$Force + $Path | Add-ToModuleSyncScript -Force:$Force + + # workflows + $Path | Add-ToModulePSScriptAnalyzerWorkflow -Force:$Force + $Path | Add-ToModuleTestWorkflow -Force:$Force + $Path | Add-ToModuleDeployWorkflow -Force:$Force + + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru + } + +} Export-ModuleMember -Function Add-ToModuleAll diff --git a/public/New-ModuleV1.ps1 b/public/New-ModuleV1.ps1 index 8ed4748..02b74f0 100644 --- a/public/New-ModuleV1.ps1 +++ b/public/New-ModuleV1.ps1 @@ -4,6 +4,7 @@ function New-ModuleV1 { .Synopsis Created a Powershell module with BiT21 format. #> + [System.ObsoleteAttribute("Use New-ModuleV3 instead", $true)] [CmdletBinding()] [OutputType([System.IO.FileInfo])] Param @@ -55,8 +56,8 @@ function New-ModuleV1 { # Testing module if (-Not $AvoidTestFile) { - New-TestingModule -Path $modulePath -ModuleName $ModuleName - New-TestingVsCodeLaunchJson -Path $modulePath -ModuleName $ModuleName + New-TestingModule -Path $modulePath -ModuleName $ModuleName -warningAction SilentlyContinue # Supress Obsolete Warning + New-TestingVsCodeLaunchJsonV1 -Path $modulePath -ModuleName $ModuleName } return $modulePath @@ -129,10 +130,10 @@ Export-ModuleMember -Function _MODULE_TESTING__* $toAppend = $toAppend.Replace('_MODULE_TESTING_',$testingModuleName) - $null = New-ModuleV1 -Path $Path -Name $testingModuleName -Description "Testing module for $ModuleName" -AvoidTestFile -AppendToModuleFile $toAppend + $null = New-ModuleV1 -Path $Path -Name $testingModuleName -Description "Testing module for $ModuleName" -AvoidTestFile -AppendToModuleFile $toAppend -WarningAction SilentlyContinue # Supress Obsolete Warning } Export-ModuleMember -Function New-TestingModule -function New-TestingVsCodeLaunchJson($Path, $ModuleName){ +function New-TestingVsCodeLaunchJsonV1($Path, $ModuleName){ $testScript = @' { @@ -160,4 +161,4 @@ function New-TestingVsCodeLaunchJson($Path, $ModuleName){ -Value $testScript ` -Force ` | Out-Null -} Export-ModuleMember -Function New-TestingVsCodeLaunchJson \ No newline at end of file +} \ No newline at end of file diff --git a/public/New-ModuleV2.ps1 b/public/New-ModuleV2.ps1 index bbfd903..9179af6 100644 --- a/public/New-ModuleV2.ps1 +++ b/public/New-ModuleV2.ps1 @@ -31,6 +31,7 @@ function New-ModuleV2 { .Synopsis Created a Powershell module with V2 format. #> + [System.ObsoleteAttribute("Use New-ModuleV3 instead", $true)] [CmdletBinding()] Param ( diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 4d99b85..69a3005 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -24,15 +24,19 @@ function New-ModuleV3 { Param ( # Name of the module - [Parameter()][string]$Name, + [Parameter(Mandatory,ParameterSetName="Named")][string]$Name, # Description of the module - [Parameter()][string]$Description, + [Parameter(ParameterSetName="Named")][string]$Description, # Author of the module - [Parameter()][string]$Author, + [Parameter(ParameterSetName="Named")][string]$Author, # Version of the module - [Parameter()][string]$ModuleVersion, + [Parameter(ParameterSetName="Named")][string]$ModuleVersion, + [Parameter(ParameterSetName="Named")] # Path where the module will be created. Default is current folder - [Parameter()][string]$Path, + [Parameter(ParameterSetName="WithPath")] + [string]$Path, + # Add all the sections of the module + [Parameter()][switch]$AddAll, # Add Testing module [Parameter()][switch]$AddTesting, # Add Sample Code to the module and test @@ -54,15 +58,23 @@ function New-ModuleV3 { # Add PSScriptAnalyzer workflow [Parameter()][switch]$AddPSScriptAnalyzerWorkflow, # Add testing workflow - [Parameter()][switch]$AddTestingWorkflow, + [Parameter()][switch]$AddTestWorkflow, # Add deploy workflow - [Parameter()][switch]$AdddeployWorkflow + [Parameter()][switch]$AddDeployWorkflow ) $retModulePath = $null - $modulePath = Get-ModulePath -Name $Name -Path $Path -AppendName - $moduleName = Get-ModuleName -Name $Name -ModulePath $modulePath + $modulePath = Get-ModulePath -Name $Name -RootPath $Path + $moduleName = Get-ModuleName -Path $modulePath + + # check $modulePath and return if null + if(!$modulePath -or !$moduleName){ + return $null + } + + # If asked for testing add sample code on both modules + $AddSampleCode = $AddSampleCode -or $AddTesting # Create the module if ($moduleName) { @@ -71,90 +83,57 @@ function New-ModuleV3 { $metadata = @{} if($Description){ $metadata.Description = $Description} if($Author){ $metadata.Author = $Author} - if($ModuleVersion){ $metadata.ModuleVersion = $Version} - - $retModulePath = Add-ModuleV3 -Name $moduleName -Path $modulePath -Metadata $metadata -AddSampleCode:$AddSampleCode + if($ModuleVersion){ $metadata.ModuleVersion = $ModuleVersion} + $retModulePath = Add-ModuleV3 -Name $moduleName -RootPath $modulePath -Metadata $metadata + if(!$retModulePath){ return $null } + + # Add Sample Code + if($AddSampleCode){ $modulePath | Add-ToModuleSampleCode } } - if ($AddTesting) { - $result = Add-TestingToModuleV3 -Name $Name -Path $modulePath -AddSampleCode:$AddSampleCode - - # Check if the module was created - if(! $result){ - return $null - } + # Add All + if($AddAll){ + $modulePath | Add-ToModuleAll + + return $retModulePath } + # Add Testing + if ($AddTesting){ $modulePath | Add-ToModuleTestAll } + # Add devcontainer.json file - if($AddDevContainerJson){ - $destination = $modulePath | Join-Path -ChildPath ".devcontainer" - Import-Template -Path $destination -File "devcontainer.json" -Template "template.devcontainer.json" - } + if($AddDevContainerJson){ $modulePath | Add-ToModuleDevContainerJson } # Add License file - if($AddLicense){ - Import-Template -Path $modulePath -File "LICENSE" -Template "template.LICENSE.txt" - } + if($AddLicense){ $modulePath | Add-ToModuleLicense } # Add Readme file - if($AddReadme){ - $moduleManifest = Get-ModuleManifest -Path $modulePath - Import-Template -Path $modulePath -File "README.md" -Template "template.README.md" -Replaces @{ - "_MODULE_NAME_" = $moduleName - "_MODULE_DESCRIPTION_" = ($moduleManifest.Description ?? "A powershell module that will hold Powershell functionality.") - } - } + if($AddReadme){ $modulePath | Add-ToModuleReadme } # Add about - if($AddAbout){ - $moduleManifest = Get-ModuleManifest -Path $modulePath - $destination = $modulePath | Join-Path -ChildPath "en-US" - Import-Template -Path $destination -File "about_$moduleName.help.txt" -Template "template.about.help.txt" -Replaces @{ - "_MODULE_NAME_" = ($moduleName ?? "") - "_MODULE_DESCRIPTION_" = ($moduleManifest.Description ?? "") - "_AUTHOR_" = ($moduleManifest.Author ?? "") - "_COPYRIGHT_" = ($moduleManifest.CopyRight ?? "") - } - } + if($AddAbout){ $modulePath | Add-ToModuleAbout } # Add deploying - if($AdddeployScript){ - Import-Template -Path $modulePath -File "deploy.ps1" -Template "template.v3.deploy.ps1" - Import-Template -Path $modulePath -File "deploy-helper.ps1" -Template "template.v3.deploy-helper.ps1" - } + if($AddDeployScript){ $modulePath | Add-ToModuleDeployScript } # Add Release - if($AddReleaseScript){ - Import-Template -Path $modulePath -File "release.ps1" -Template "template.v3.release.ps1" - } + if($AddReleaseScript){ $modulePath | Add-ToModuleReleaseScript } # Add Sync - if($AddSyncScript){ - Import-Template -Path $modulePath -File "sync.ps1" -Template "template.v3.sync.ps1" - Import-Template -Path $modulePath -File "sync-helper.ps1" -Template "template.v3.sync-helper.ps1" - } + if($AddSyncScript){ $modulePath | Add-ToModuleSyncScript } # Add PSScriptAnalyzer - if($AddPSScriptAnalyzerWorkflow){ - $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" - } + if($AddPSScriptAnalyzerWorkflow){ $modulePath | Add-ToModulePSScriptAnalyzerWorkflow } # Add Testing - if($AddTestingWorkflow){ - $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Import-Template -Path $destination -File "test_with_TestingHelper.yml" -Template "template.v3.test_with_TestingHelper.yml" - } + if($AddTestWorkflow){ $modulePath | Add-ToModuleTestWorkflow } # Add deploy Workflow - if($AdddeployWorkflow){ - $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Import-Template -Path $destination -File "deploy_module_on_release.yml" -Template "template.v3.deploy_module_on_release.yml" - } + if($AddDeployWorkflow){ $modulePath | Add-ToModuledeployWorkflow } return $retModulePath diff --git a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 b/public/New-ModuleV3_Add-TestingToModuleV3.ps1 deleted file mode 100644 index 49aa4c5..0000000 --- a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 +++ /dev/null @@ -1,77 +0,0 @@ -function Add-TestModuleV3 { - [CmdletBinding()] - Param - ( - [Parameter()][string]$Path, - [Parameter(Mandatory)][string]$Name, - [Parameter()][hashtable]$Metadata - ) - - $testingModuleName = Get-TestModuleName -Name $Name - $testingModulePath = Get-TestModulePath -Name $Name -Path $Path - - $result = Add-ModuleV3 -Name $testingModuleName -Path $testingModulePath -Metadata $Metadata - - if(!$result){ - Write-Error -Message ("Error creating the module [$testingModuleName].") - return $null - } - - return $result - -} Export-ModuleMember -Function Add-TestModuleV3 - - - -function Add-TestingToModuleV3{ - [CmdletBinding()] - Param - ( - [Parameter(Mandatory)][string]$Path, - [Parameter(Mandatory)][string]$Name, - [Parameter()][hashtable]$Metadata, - [Parameter()][switch]$AddSampleCode - ) - - $testModulePath = Add-TestModuleV3 -Path $Path -Name $Name -Metadata $Metadata - - if (!$testModulePath) { - Write-Error -Message ("Error creating Testing for Module [$Name].") - return $null - } - - # Sample test - if ($AddSampleCode) { - $destination = $testModulePath | Join-Path -ChildPath "public" - - Import-Template -Path $destination -File "SampleFunctionTests.ps1" -Template "template.testmodule.functions.public.ps1" -Replaces @{ - '_MODULE_TESTING_' = $testingModuleName - '_MODULE_TESTED_' = $ModuleName - } - } - - # Get root folder - $modulePath = Get-ModulePath -Path $Path - - # test.ps1 script - $testScriptPath = $modulePath | Join-Path -ChildPath "test.ps1" - if($testScriptPath | Test-Path){ - Write-Warning "test.ps1 already exists." - } - else{ - Import-Template -Path $modulePath -File "test.ps1" -Template "template.test.ps1" - } - - # launch.json - $launchJsonPath = $modulePath | Join-Path -ChildPath '.vscode' -AdditionalChildPath 'launch.json' - if($launchJsonPath | Test-Path){ - Write-Warning "launch.json already exists." - } - else{ - $destination = $modulePath | Join-Path -ChildPath '.vscode' - Import-Template -Path $destination -File "launch.json" -Template "template.launch.json" - } - - return $modulePath - -} Export-ModuleMember -Function Add-TestingToModuleV3 \ No newline at end of file diff --git a/public/TestringFilesFolders.ps1 b/public/TestingFilesFolders.ps1 similarity index 64% rename from public/TestringFilesFolders.ps1 rename to public/TestingFilesFolders.ps1 index 9bedfb2..9dcbf94 100644 --- a/public/TestringFilesFolders.ps1 +++ b/public/TestingFilesFolders.ps1 @@ -16,19 +16,6 @@ function New-TestingFolder { $finalPath = $Path | Join-Path -ChildPath $Name } - # if ($Path -and $Name) { - # $finalPath = $Path | Join-Path -ChildPath $Name - # } - - # if (!$Path -and $Name) { - # $finalPath = '.' | Join-Path -ChildPath $Name - # } - - # if (!$Path -and !$Name) { - # $finalPath = '.' | Join-Path -ChildPath (New-Guid).ToString() - # } - - # Need to consolidate as mkdir behaves diferent on PC or Mac $result = New-Item -ItemType Directory -Path $finalPath @@ -114,53 +101,3 @@ function Remove-TestingFile { Assert-itemNotExist -Path $target } Export-ModuleMember -Function Remove-TestingFile - -function GetRooTestingFolderPath{ - # get the first 6 char of a guid - $random = (New-Guid).ToString().Substring(0,6) - $rd = Get-Date -Format yyMMdd - $path = Join-Path -Path "Temp:" -ChildPath ("Posh_Testing_{0}_{1}" -f $rd,$random) - return $path -} -function Push-TestingFolder { - [CmdletBinding()] - param ( - [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][string] $Path - ) - - $originalPath = Resolve-Path . - - if ($Path) { - $testFolderName = $Path - } - else { - $testFolderName = Join-Path -Path (GetRooTestingFolderPath) -ChildPath $TestRunFolderName - } - New-TestingFolder $testFolderName - $TestRunPath = Resolve-Path -Path $testFolderName - - if (Test-Path -Path $TestRunPath) { Remove-Testingfolder -Path $TestRunPath } - - New-Item -Path $TestRunPath -ItemType "directory" -Force | Out-Null - - Set-Location -Path $TestRunPath - - return $originalPath -} - -function Pop-TestingFolder { - [CmdletBinding()] - param ( - [Parameter( ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)][string] $Path, - [switch] $Force - ) - - $local = Get-Location | Resolve-Path - $localLeaf = $local | Split-Path -Leaf - - Set-Location -Path $Path - - if (($localLeaf -eq $TestRunFolderName) -or $Force) { - Remove-TestingFolder -Path $local - } -} \ No newline at end of file diff --git a/public/Write-Assert.ps1 b/public/Write-Assertion.ps1 similarity index 100% rename from public/Write-Assert.ps1 rename to public/Write-Assertion.ps1 diff --git a/readme.md b/readme.md index 75d3b8f..156f8a8 100644 --- a/readme.md +++ b/readme.md @@ -26,55 +26,105 @@ This `module` contains `functions` to help create and run Unit Testing for Power ## How to use it -This library will allow you to create quick unit testing for a given module. It will provide Asset statements too for the assertion section of your tests. - -Use `New-Module` to create the full set with testing and `.vscode/launth.json` - -If you have a module you want to add testing too use `New-TestingModule` - -Follow naming convention for easier use. Function names `ModuleNameTest_*` will be consideres test to PASS. - -## Version 2.0 - -> Moving to `2.0`as I am not sure if this version will break compatibility with the previous one. - -- Make development compatible with Codespaces -- Remove unecesary dependencies -- First version of about help -- Improve testing output -- Assert Functions - - Assert-AreEqual - - Assert-AreEqualContent - - Assert-AreEqualPath - - Assert-AreEqualSecureString - - Assert-AreNotEqual - - Assert-AreNotEqualContent - - Assert-AreNotEqualPath - - Assert-CollectionIsNotNullOrEmpty - - Assert-CollectionIsNullOrEmpty - - Assert-ContainedXOR - - Assert-Contains - - Assert-ContainsPath - - Assert-Count - - Assert-CountTimes - - Assert-FileContains - - Assert-FilesAreEqual - - Assert-FilesAreNotEqual - - Assert-IsFalse - - Assert-IsGuid - - Assert-IsNotNull - - Assert-IsNull - - Assert-IsTrue - - Assert-ItemExist - - Assert-ItemNotExist - - Assert-NotContains - - Assert-NotContainsPath - - Assert-NotImplemented - - Assert-SkipTest - - Assert-StringIsNotNullOrEmpty - - Assert-StringIsNullOrEmpty -- Functions to help on the arrangement - - New-TestingFile - - Remove-TestingFile - - New-TestingFolder +This library will allow you to create a PowerShell Module with all the gearing for a full SDLC on GitHub platform. +- Create a Module with sample code +- Add Testing for sample tests +- Add Module gearing +- Add Helper scripts to create releases and deploy +- Add GitHub Worflows for Code Analysis, Testing and Deploy + +[Information on how to use it on the Docs](docs/index.md) + +## API V3 + +### Testing + +- Invoke-TestingHelper +- Import-TestingModule +- Test-Module (*ObsoleteAttribute*) +- Test-ModulelocalPSD1 (*ObsoleteAttribute*) + +### Tracing + +- Trace-Message +- Write-AssertionSectionEnd + +### Files and Folders + +- New-TestingFile +- New-TestingFolder +- Remove-TestingFile +- Remove-TestingFolder + +### New Module + +- New-Module (Alias New-ModuleV3) +- New-ModuleV1 +- New-ModuleV2 +- New-ModuleV3 + +- Add-ModuleV3 + +### Add Testing + +- New-TestingModule +- Add-TestModuleV3 + +### AddToModule* + +- Add-ToModuleAll +- Add-ToModuleSampleCode + +- Add-ToModuleAbout +- Add-ToModuleReadme +- Add-ToModuleLicense +- Add-ToModuleDevContainerJson + +- Add-ToModuleDeployScript +- Add-ToModuleReleaseScript +- Add-ToModuleSyncScript + +- Add-ToModuleTestAll + +- Add-ToModuleLaunchJson +- Add-ToModuleTestScript +- Add-ToModuleTestModule +- Add-ToModuleTestSampleCode + +- Add-ToModuleDeployWorkflow +- Add-ToModulePSScriptAnalyzerWorkflow +- Add-ToModuleTestWorkflow + +### Asserts + +- Assert-AreEqual +- Assert-AreEqualContent +- Assert-AreEqualPath +- Assert-AreEqualSecureString +- Assert-AreNotEqual +- Assert-AreNotEqualContent +- Assert-AreNotEqualPath +- Assert-CollectionIsNotNullOrEmpty +- Assert-CollectionIsNullOrEmpty +- Assert-ContainedXOR +- Assert-Contains +- Assert-ContainsPath +- Assert-Count +- Assert-CountTimes +- Assert-FileContains +- Assert-FilesAreEqual +- Assert-FilesAreNotEqual +- Assert-IsFalse +- Assert-IsGuid +- Assert-IsNotNull +- Assert-IsNull +- Assert-IsTrue +- Assert-ItemExist +- Assert-ItemNotExist +- Assert-NotContains +- Assert-NotContainsPath +- Assert-NotImplemented +- Assert-SkipTest +- Assert-StringIsNotNullOrEmpty +- Assert-StringIsNullOrEmpty diff --git a/test.ps1 b/test.ps1 index ff4e3bb..9497236 100644 --- a/test.ps1 +++ b/test.ps1 @@ -47,4 +47,7 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors \ No newline at end of file +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTestToModuleAll_Simple +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddModule_DefaultManifest +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddAll +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors diff --git a/test_with_TestingHelper.yml b/test_with_TestingHelper.yml deleted file mode 100644 index d08b89d..0000000 --- a/test_with_TestingHelper.yml +++ /dev/null @@ -1,45 +0,0 @@ -# This is a workflow to test you PowerShell module with TestingHelper -# https://github.com/rulasg/DemoPsModule/blob/main/.github/workflows/test_with_TestingHelper.yml - -name: Test with TestingHelper - -# Controls when the workflow will run -on: - - # Run as check on pull request - pull_request: - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -permissions: - # To run test we only need to read the repository - contents: read - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "build" - test: - # The type of runner that the job will run on - runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v3 - - # Runs a single command using the runners shell - - name: Run test.ps1 - shell: pwsh - run: | - $result = ./test.ps1 -ShowTestErrors - $result - - # Allow Not Implemented and Skipped tests to pass - $passed = $result.Tests -eq $result.Pass + $result.NotImplemented + $result.Skipped - # $passed = $result.Tests -eq $result.Pass - - if($passed) - { "All test passed" | Write-Verbose -verbose ; exit 0 } - else - { "Not all tests passed" | Write-Verbose -verbose ; exit 1 }