From 881f6ee4423aef890126601623151caaa39f8299 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 13:15:16 +0200 Subject: [PATCH 01/31] delete wf from root. not sure why are here --- deploy_module_on_release.yml | 48 ------------------------------------ powershell.yml | 47 ----------------------------------- test_with_TestingHelper.yml | 45 --------------------------------- 3 files changed, 140 deletions(-) delete mode 100644 deploy_module_on_release.yml delete mode 100644 powershell.yml delete mode 100644 test_with_TestingHelper.yml 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/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/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 } From 573b47d6da41d857c0cb70a66b52825a31416bb9 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 13:16:03 +0200 Subject: [PATCH 02/31] Update Import-template --- private/Templates-Helper.ps1 | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/private/Templates-Helper.ps1 b/private/Templates-Helper.ps1 index a468029..8d8dbd0 100644 --- a/private/Templates-Helper.ps1 +++ b/private/Templates-Helper.ps1 @@ -1,12 +1,21 @@ +# 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()][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")) { @@ -27,3 +36,24 @@ function Import-Template { $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 From a32c0b124c9fb337ecb2448f70b183275b49f246 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 13:17:29 +0200 Subject: [PATCH 03/31] Add-ModuleLicense --- public/Add-ModuleSection.ps1 | 12 ++++++++++++ public/New-ModuleV3.ps1 | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 public/Add-ModuleSection.ps1 diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 new file mode 100644 index 0000000..5f038a5 --- /dev/null +++ b/public/Add-ModuleSection.ps1 @@ -0,0 +1,12 @@ +function Add-ModuleLicense{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Mandatory)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + Import-Template -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" -Force:$Force + } +} Export-ModuleMember -Function Add-ModuleLicense \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 4d99b85..b2511fe 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -97,7 +97,8 @@ function New-ModuleV3 { # Add License file if($AddLicense){ - Import-Template -Path $modulePath -File "LICENSE" -Template "template.LICENSE.txt" + # Import-Template -Path $modulePath -File "LICENSE" -Template "template.LICENSE.txt" + Add-ModuleLicense -Path $modulePath } # Add Readme file From f2f3c84dd758ec9ffab4f5ee19dc573b73204973 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 13:36:05 +0200 Subject: [PATCH 04/31] Add-ModuleReleaseScript , Add-ModulePSScriptAnalyzerWorkflow --- public/Add-ModuleSection.ps1 | 29 ++++++++++++++++++++++++++++- public/New-ModuleV3.ps1 | 8 +++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index 5f038a5..978076e 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -9,4 +9,31 @@ function Add-ModuleLicense{ process{ Import-Template -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" -Force:$Force } -} Export-ModuleMember -Function Add-ModuleLicense \ No newline at end of file +} Export-ModuleMember -Function Add-ModuleLicense + +function Add-ModuleReleaseScript{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Mandatory)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + Import-Template -Path $modulePath -File "release.ps1" -Template "template.v3.release.ps1" -Force:$Force + } +} Export-ModuleMember -Function Add-ModuleReleaseScript + +function Add-ModulePSScriptAnalyzerWorkflow{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Mandatory)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" + Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" + } +} Export-ModuleMember -Function Add-ModuleReleaseScript \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index b2511fe..91774dc 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -130,7 +130,8 @@ function New-ModuleV3 { # Add Release if($AddReleaseScript){ - Import-Template -Path $modulePath -File "release.ps1" -Template "template.v3.release.ps1" + # Import-Template -Path $modulePath -File "release.ps1" -Template "template.v3.release.ps1" + Add-ModuleReleaseScript -Path $modulePath } # Add Sync @@ -141,8 +142,9 @@ function New-ModuleV3 { # Add PSScriptAnalyzer if($AddPSScriptAnalyzerWorkflow){ - $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" + # $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" + # Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" + Add-ModulePSScriptAnalyzerWorkflow -Path $modulePath } # Add Testing From 7316406dfae871d03873d8f3751ddd743c0ee58e Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 14:48:36 +0200 Subject: [PATCH 05/31] test typo --- TestingHelperTest/public/Tests-New-ModuleV3.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 index 27e6579..63b3d62 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -149,7 +149,7 @@ function TestingHelperTest_NewModuleV3_AddAbout{ Assert-IsTrue -Condition ($aboutContent.Contains("Powershell Testing UnitTest Module TestingHelper")) } -function TestingHelperTest_NewModuleV33_AddDeployScript{ +function TestingHelperTest_NewModuleV3_AddDeployScript{ $moduleName = "MyModule" $path = '.' From 20c381b808e70945ede666c3d94ed1583a008ceb Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 14:49:18 +0200 Subject: [PATCH 06/31] Add-Module* and 1 test --- .../public/Tests-Add-moduleSectiond.ps1 | 8 + public/Add-ModuleSection.ps1 | 192 +++++++++++++++++- public/New-ModuleV3.ps1 | 46 ++--- 3 files changed, 208 insertions(+), 38 deletions(-) create mode 100644 TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 new file mode 100644 index 0000000..7253ca1 --- /dev/null +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -0,0 +1,8 @@ + +function TestingHelperTest_AddModuleSection_PipeCalls{ + $modulePath = New-ModuleV3 -Name "module1" + + $modulePath | Add-TT_ModuleLicense + + Assert-AddModuleV3 -Name "module1" -Path $modulePath -AddLicense +} \ No newline at end of file diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index 978076e..e0f9e1a 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -1,16 +1,135 @@ +# 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-Module* function + + + +function ReturnValue($Path,$Force){ + # create object with the two parameters as properties + return [pscustomobject]@{ + Path = $Path + Force = $Force + } +} + +function GetPath($Path){ + # Path should be a file or the folder where the module is located. + # check if $Path is a file + if(Test-Path -Path $Path -PathType Leaf){ + return $Path + } else { + # if not a file + return Get-ModuleManifest -Path $Path + } +} + + +# Add devcontainer.json file +function Add-ModuleDevContainerJson{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -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 + } +} Export-ModuleMember -Function Add-ModuleDevContainerJson + +# Add License file function Add-ModuleLicense{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory)][string]$Path, + [Parameter(ValueFromPipelineByPropertyName,ValueFromPipeline)][string]$Path, [Parameter()][switch]$Force ) process{ - Import-Template -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" -Force:$Force + if(!(GetPath -Path:$Path)){return $null} + + Import-Template -Force:$Force -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" + + return ReturnValue -Path $Path -Force:$Force } } Export-ModuleMember -Function Add-ModuleLicense +# Add Readme file +function Add-ModuleReadme{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -Path:$Path)){return $null} + + $moduleManifest = Get-ModuleManifest -Path $Path + $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 + } +} Export-ModuleMember -Function Add-ModuleReadme + +# Add about +function Add-ModuleAbout{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -Path:$Path)){return $null} + + $moduleManifest = Get-ModuleManifest -Path $Path + $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 + } +} Export-ModuleMember -Function Add-ModuleAbout + +# Add deploying +function Add-ModuleDeployScript{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -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 + } +} Export-ModuleMember -Function Add-ModuleDeployScript + +# Add Release function Add-ModuleReleaseScript{ [CmdletBinding(SupportsShouldProcess)] Param @@ -20,10 +139,33 @@ function Add-ModuleReleaseScript{ ) process{ - Import-Template -Path $modulePath -File "release.ps1" -Template "template.v3.release.ps1" -Force:$Force + if(!(GetPath -Path:$Path)){return $null} + + Import-Template -Force:$Force -Path $Path -File "release.ps1" -Template "template.v3.release.ps1" + + return ReturnValue -Path $Path -Force:$Force } } Export-ModuleMember -Function Add-ModuleReleaseScript +# Add Sync +function Add-ModuleSyncScript{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -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 + } +} Export-ModuleMember -Function Add-ModuleSyncScript + +# Add PSScriptAnalyzer function Add-ModulePSScriptAnalyzerWorkflow{ [CmdletBinding(SupportsShouldProcess)] Param @@ -33,7 +175,47 @@ function Add-ModulePSScriptAnalyzerWorkflow{ ) process{ + if(!(GetPath -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 + } +} Export-ModuleMember -Function Add-ModulePSScriptAnalyzerWorkflow + +# Add Testing +function Add-ModuleTestingWorkflow{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -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 + } +} Export-ModuleMember -Function Add-ModuleTestingWorkflow + +# Add deploy Workflow +function Add-ModuledeployWorkflow{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter()][switch]$Force + ) + + process{ + if(!(GetPath -Path:$Path)){return $null} + $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" + 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 } -} Export-ModuleMember -Function Add-ModuleReleaseScript \ No newline at end of file +} Export-ModuleMember -Function Add-ModuledeployWorkflow \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 91774dc..d6bfbe7 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -56,7 +56,7 @@ function New-ModuleV3 { # Add testing workflow [Parameter()][switch]$AddTestingWorkflow, # Add deploy workflow - [Parameter()][switch]$AdddeployWorkflow + [Parameter()][switch]$AddDeployWorkflow ) $retModulePath = $null @@ -91,72 +91,52 @@ function New-ModuleV3 { # Add devcontainer.json file if($AddDevContainerJson){ - $destination = $modulePath | Join-Path -ChildPath ".devcontainer" - Import-Template -Path $destination -File "devcontainer.json" -Template "template.devcontainer.json" + $null = Add-ModuleDevContainerJson -Path $modulePath } # Add License file if($AddLicense){ - # Import-Template -Path $modulePath -File "LICENSE" -Template "template.LICENSE.txt" - Add-ModuleLicense -Path $modulePath + $null = Add-ModuleLicense -Path $modulePath } # 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.") - } + $null = Add-ModuleReadme -Path $modulePath } # 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 ?? "") - } + $null = Add-ModuleAbout -Path $modulePath } # 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){ + $null = Add-ModuleDeployScript -Path $modulePath } # Add Release if($AddReleaseScript){ - # Import-Template -Path $modulePath -File "release.ps1" -Template "template.v3.release.ps1" - Add-ModuleReleaseScript -Path $modulePath + $null = Add-ModuleReleaseScript -Path $modulePath } # 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" + $null = Add-ModuleSyncScript -Path $modulePath } # Add PSScriptAnalyzer if($AddPSScriptAnalyzerWorkflow){ - # $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - # Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" - Add-ModulePSScriptAnalyzerWorkflow -Path $modulePath + $null = Add-ModulePSScriptAnalyzerWorkflow -Path $modulePath } # 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" + $null = Add-ModuleTestingWorkflow -Path $modulePath } # 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){ + $null = Add-ModuledeployWorkflow -Path $modulePath } return $retModulePath From 34d82bf74fb061e35cdc9e8b59eaf167cc991fe4 Mon Sep 17 00:00:00 2001 From: rulasg Date: Sat, 24 Jun 2023 22:31:57 +0200 Subject: [PATCH 07/31] Implementation New Add-Module pattern - Pass[87] --- .../Assert-Add-ModuleSections.Helper.ps1 | 420 ++++++++++++++++++ .../private/tests-Helper-New-ModuleV3.ps1 | 178 +------- .../public/Tests-Add-moduleSectiond.ps1 | 72 ++- .../Tests-New-ModuleV3-Add-ModuleV3.ps1 | 4 +- ...Tests-New-ModuleV3-Add-TestingModuleV3.ps1 | 2 +- .../public/Tests-New-ModuleV3.ps1 | 158 +------ private/Helper-NewModuleV3.ps1 | 124 ++---- .../templates/template.v3.deploy-helper.ps1 | 4 +- public/Add-ModuleSection.ps1 | 159 +++++-- public/New-ModuleV3.ps1 | 22 +- public/New-ModuleV3_Add-ModuleV3.ps1 | 26 +- public/New-ModuleV3_Add-TestingToModuleV3.ps1 | 18 +- test.ps1 | 5 +- 13 files changed, 696 insertions(+), 496 deletions(-) create mode 100644 TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 diff --git a/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 b/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 new file mode 100644 index 0000000..d5e40f8 --- /dev/null +++ b/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 @@ -0,0 +1,420 @@ + +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 +} + +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 +} + + +# Devcontainer.json +function Assert-AddDevContainerJson{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + process{ + Assert-ItemExist -Path ($Path | Join-Path -ChildPath ".devcontainer" | Join-Path -ChildPath "devcontainer.json") -Comment "devcontainer.json" + + return $Passthru ? $Path : $null + } +} + + +# License +function Assert-AddLicense{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + process{ + Assert-ItemExist -Path ($Path | Join-Path -ChildPath "LICENSE") -Comment "LICENSE" + + return $Passthru ? $Path : $null + } +} + +# ReadMe +function Assert-AddReadMe{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + } + + return $Passthru ? $Path : $null + } +} + +# AddAbout +function Assert-AddAbout{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + } + + return $Passthru ? $Path : $null + } +} + +# Deploy +function Assert-AddDeployScript{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + + return $Passthru ? $Path : $null + } +} + +# release script +function Assert-AddReleaseScript{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + process{ + Assert-ItemExist -Path ($Path | Join-Path -ChildPath "release.ps1") -Comment "release.ps1" + + return $Passthru ? $Path : $null + } +} + +# sync script +function Assert-AddSyncScript{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + + return $Passthru ? $Path : $null + } +} + + +# PSScriptAnalyzer +function Assert-AddPSScriptAnalyzerWorkflow{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + process{ + $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" + Assert-ItemExist -Path ($destination | Join-Path -ChildPath "powershell.yml") -Comment "powershell.yml" + + return $Passthru ? $Path : $null + } +} + + +# TestingWorkflow +function Assert-AddTestingWorkflow{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + + return $Passthru ? $Path : $null + } +} + + +# DeployWorkflow +function Assert-AddDeployWorkflow{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + + return $Passthru ? $Path : $null + } +} + +# SampleCodes +function Assert-AddSampleCodes{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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" + + return $Passthru ? $Path : $null + } +} + +#Testing SampleCode +function Assert-AddTestingSampleCodes{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + 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 + + return $Passthru ? $Path : $null + } +} + +# Testing launch.json +function Assert-AddTestingLaunchJson{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [ValidateSet("Pester","PesterWithTestingHelper")] + [string] $TestingFramework, + [Parameter()][switch]$Passthru + ) + 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 '') + + return $Passthru ? $Path : $null + } +} + +# Testing TestScript +function Assert-AddTestingTestScript{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [ValidateSet("Pester","PesterWithTestingHelper")] + [string] $TestingFramework, + [Parameter()][switch]$Passthru + ) + process{ + + $testps1Path = $Path | Join-Path -ChildPath "test.ps1" + + Assert-ItemExist -Path $testps1Path -Comment "test.ps1 exists" + + return $Passthru ? $Path : $null + } +} + +# 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, + [Parameter()][switch]$Passthru + ) + 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 + + return $Passthru ? $Path : $null + } +} + +function Assert-AddTestingV3 { + param( + [Parameter()][string]$Path, + [Parameter()][hashtable]$Expected + ) + + 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 -Expected $Expected + } + +} + +# Full +function Assert-AddFull{ + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Passthru + ) + process{ + $Path | Assert-AddDevContainerJson + $Path | Assert-AddLicense + $Path | Assert-AddReadMe + $Path | Assert-AddAbout + $Path | Assert-AddDeployScript + $Path | Assert-AddReleaseScript + $Path | Assert-AddSyncScript + $Path | Assert-AddPSScriptAnalyzerWorkflow + $Path | Assert-AddTestingWorkflow + $Path | Assert-AddDeployWorkflow + + return $Passthru ? $Path : $null + } +} + + + + diff --git a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 index c0f2f58..d081c52 100644 --- a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 +++ b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 @@ -4,190 +4,16 @@ function Get-DefaultsManifest { 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 + [Parameter()][hashtable]$Expected ) - $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-AddTestingV3 -Path $Path -Expected $Expected - 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 index 7253ca1..f9fc712 100644 --- a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -1,8 +1,70 @@ -function TestingHelperTest_AddModuleSection_PipeCalls{ - $modulePath = New-ModuleV3 -Name "module1" +function TestingHelperTest_AddModuleSection_PipeCalls_NewModuleV3{ + + $result = New-TT_ModuleV3 -Name "MyModule" | Add-TT_ModuleLicense + + $result | Assert-Addlicense +} + +function TestingHelperTest_AddModuleSection_PipeCalls_Folder{ + + New-TestingFolder -Path "folderName" + + $result = Get-Item -path "folderName" | Add-TT_ModuleLicense + $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_ModuleLicense + + $result | Assert-Addlicense + + Remove-Module -Name $moduleName +} + +function TestingHelperTest_AddModuleSection_PipeCalls_Chain{ + + $modulePath = New-TT_ModuleV3 -Name "MyModule" + + $result1 = $modulePath | Add-TT_ModuleLicense | Add-TT_ModuleAbout + + $result1 | Assert-Addlicense -PassThru | Assert-AddAbout +} + +function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ + + New-TestingFolder -Path "folderName" + + $result = Add-TT_ModuleFull -Path "./folderName" + $result | Assert-AddFull +} + +function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ + + New-TestingFolder -Path "folderName" + + $result = Get-Item -path "folderName" | Add-TT_ModuleFull + $result | Assert-AddFull +} + +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_ModuleFull + $result | Assert-AddFull + + Remove-Module -Name "MyModule" +} - $modulePath | Add-TT_ModuleLicense - Assert-AddModuleV3 -Name "module1" -Path $modulePath -AddLicense -} \ No newline at end of file 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 index 8399c6b..4db223b 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3-Add-TestingModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3-Add-TestingModuleV3.ps1 @@ -7,7 +7,7 @@ function TestingHelperTest_NewModuleV3_AddTestingToModule_AddTestingModuleV3_Sim $modulePath = $folderName $param = @{ - RootModule = "MyModuleTest.psm1" + # RootModule = "MyModuleTest.psm1" Author = "Me" CompanyName = "MyCompany" ModuleVersion = "6.6.6" diff --git a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 index 63b3d62..d7d3b90 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -15,7 +15,7 @@ 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 { @@ -27,12 +27,12 @@ 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 @@ -65,154 +65,10 @@ function TestingHelperTest_NewModuleV3_AddModuleV3_AddTestingToModuleV3_AddSampl 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{ + Assert-AddModuleV3 -Path $modulePath + Assert-AddSampleCodes -Path $modulePath - $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 + Assert-TestingV3 -Name $moduleName -Path $modulePath + Assert-AddTestingSampleCodes -Path $modulePath } -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{ - - $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")) -} - -function TestingHelperTest_NewModuleV3_AddDeployScript{ - - $moduleName = "MyModule" - $path = '.' - $modulePath = $path | Join-Path -ChildPath $moduleName - - $result = New-TT_ModuleV3 -Name $moduleName -AddDeployScript - - 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 - - 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-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/private/Helper-NewModuleV3.ps1 b/private/Helper-NewModuleV3.ps1 index 3c5c642..677a71b 100644 --- a/private/Helper-NewModuleV3.ps1 +++ b/private/Helper-NewModuleV3.ps1 @@ -1,137 +1,70 @@ -# 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 + [Parameter()][string]$RootPath ) # | path/name | null | Name | # | --------- | ----- | ----------- | - # | null | Error | Name | - # | Path | Path | Path | + # | 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 } - $ret = ([string]::IsNullOrWhiteSpace($Path) ? $Name : ` - ([string]::IsNullOrWhiteSpace($Name) ? $Path : ` - ( !$AppendName ? $Path : ` - ($Path | Join-Path -ChildPath $Name)))) - - - return $ret + #check if path is null + $path = [string]::IsNullOrWhiteSpace($Path) ? (Get-Location | Convert-Path) : $Path + $ret = $path | Join-Path -ChildPath $Name + return $ret } -function Get-TestModulePath{ +function Get-ModuleName{ [CmdletBinding()] param( - [Parameter()][string]$Name, [Parameter()][string]$Path ) - $modulepath = Get-ModulePath -Name $Name -Path $Path - $moduleName = Get-ModuleName -Name $Name -ModulePath $Path + # null if path is null + if([string]::IsNullOrWhiteSpace($Path)){ + return $null + } - $testModuleName = Get-TestModuleName -Name $moduleName - $tesModulePath = $modulepath | Join-Path -ChildPath $testModuleName + $name = $Path | Split-Path -LeafBase - return $tesModulePath + return $name } -function Get-ModuleName{ +function Get-TestModulePath{ [CmdletBinding()] param( - [Parameter()][string]$Name, - [Parameter()][string]$ModulePath + [Parameter()][string]$Path ) - #Return Name if provided - if(![string]::IsNullOrWhiteSpace($Name)){ - return $Name - } + $moduleName = Get-ModuleName -Path $Path - # extract name from path - $retName = Get-ModulePath -Name $Name -Path $ModulePath | Split-Path -Leaf + $testModulePath = $path | Join-Path -ChildPath ($moduleName + "Test") - return $retName + return $testModulePath } function Get-TestModuleName { [CmdletBinding()] param ( - [parameter(Mandatory)] [string] $Name + [parameter(Mandatory)] [string] $Path ) - return ($Name + "Test") + $testPath = Get-TestModulePath -Path $Path + $name = Get-ModuleName -Path $testPath + + return $name } -function Add-Folder{ +function New-Folder{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,ValueFromPipeline)][string]$Path @@ -143,18 +76,19 @@ function Add-Folder{ #test if path exists if($Path | Test-Path){ Write-Error "Path already exists." - return $false + return $null } else { if ($PSCmdlet.ShouldProcess($Path, "New-Item -ItemType Directory")) { $null = New-Item -ItemType Directory -Path $Path } - return $true + # Converting to Provider path + return $Path | Convert-Path } } catch { Write-Error -Message "Failed to add path." - return $false + return $null } } 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/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index e0f9e1a..d75076b 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -15,28 +15,82 @@ function ReturnValue($Path,$Force){ } } +# Normalize $Path and returns $null if not valid function GetPath($Path){ - # Path should be a file or the folder where the module is located. - # check if $Path is a file + # Path returned should be the folder where the module is located. + # 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){ - return $Path + $ret = $Path | Split-Path -Parent } else { - # if not a file - return Get-ModuleManifest -Path $Path + $ret = $Path } + + return $ret | Convert-Path } +function Add-ModuleSampleCode{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + process{ + $Path = GetPath -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 + } +} Export-ModuleMember -Function Add-ModuleSampleCode + +function Add-TestSampleCode{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + process{ + $Path = GetPath -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 -File "SampleFunctionTests.ps1" -Template "template.testmodule.functions.public.ps1" -Replaces @{ + '_MODULE_TESTING_' = $testModulename + '_MODULE_TESTED_' = $ModuleName + } + + return ReturnValue -Path $Path -Force:$Force + } +} Export-ModuleMember -Function Add-ModuleSampleCode + # Add devcontainer.json file function Add-ModuleDevContainerJson{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -Path:$Path ?? return $null $destination = $Path | Join-Path -ChildPath ".devcontainer" Import-Template -Force:$Force -Path $destination -File "devcontainer.json" -Template "template.devcontainer.json" @@ -50,12 +104,14 @@ function Add-ModuleLicense{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(ValueFromPipelineByPropertyName,ValueFromPipeline)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -Path:$Path ?? return $null Import-Template -Force:$Force -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" @@ -67,14 +123,16 @@ function Add-ModuleLicense{ function Add-ModuleReadme{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -Path:$Path ?? return $null - $moduleManifest = Get-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 @@ -89,14 +147,16 @@ function Add-ModuleReadme{ function Add-ModuleAbout{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -Path:$Path ?? return $null - $moduleManifest = Get-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 @{ @@ -115,12 +175,14 @@ function Add-ModuleAbout{ function Add-ModuleDeployScript{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -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" @@ -134,12 +196,14 @@ function Add-ModuleReleaseScript{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -Path:$Path ?? return $null Import-Template -Force:$Force -Path $Path -File "release.ps1" -Template "template.v3.release.ps1" @@ -151,12 +215,14 @@ function Add-ModuleReleaseScript{ function Add-ModuleSyncScript{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -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" @@ -170,12 +236,14 @@ function Add-ModulePSScriptAnalyzerWorkflow{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -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" @@ -188,12 +256,14 @@ function Add-ModulePSScriptAnalyzerWorkflow{ function Add-ModuleTestingWorkflow{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -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" @@ -203,19 +273,50 @@ function Add-ModuleTestingWorkflow{ } Export-ModuleMember -Function Add-ModuleTestingWorkflow # Add deploy Workflow -function Add-ModuledeployWorkflow{ +function Add-ModuleDeployWorkflow{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,ValueFromPipelineByPropertyName)][string]$Path, + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, [Parameter()][switch]$Force ) process{ - if(!(GetPath -Path:$Path)){return $null} + $Path = GetPath -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 } -} Export-ModuleMember -Function Add-ModuledeployWorkflow \ No newline at end of file +} Export-ModuleMember -Function Add-ModuleDeployWorkflow + +function Add-ModuleFull{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + process{ + $Path = GetPath -Path:$Path ?? return $null + + $null = $Path | Add-ModuleDevContainerJson -Force:$Force + $null = $Path | Add-ModuleLicense -Force:$Force + $null = $Path | Add-ModuleReadme -Force:$Force + $null = $Path | Add-ModuleAbout -Force:$Force + $null = $Path | Add-ModuleDeployScript -Force:$Force + $null = $Path | Add-ModuleReleaseScript -Force:$Force + $null = $Path | Add-ModuleSyncScript -Force:$Force + $null = $Path | Add-ModulePSScriptAnalyzerWorkflow -Force:$Force + $null = $Path | Add-ModuleTestingWorkflow -Force:$Force + $null = $Path | Add-ModuleDeployWorkflow -Force:$Force + + return ReturnValue -Path $Path -Force:$Force + + } + +} Export-ModuleMember -Function Add-ModuleFull \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index d6bfbe7..73e1cb8 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -61,8 +61,8 @@ function New-ModuleV3 { $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 # Create the module if ($moduleName) { @@ -71,22 +71,32 @@ 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){ + $null = Add-ModuleSampleCode -Path $modulePath + } } if ($AddTesting) { - $result = Add-TestingToModuleV3 -Name $Name -Path $modulePath -AddSampleCode:$AddSampleCode + $result = Add-TestingToModuleV3 -Name $Name -Path $modulePath # Check if the module was created if(! $result){ return $null } + + # Add Sample Code + if($AddSampleCode){ + $null = Add-TestSampleCode -Path $modulePath + } } # Add devcontainer.json file diff --git a/public/New-ModuleV3_Add-ModuleV3.ps1 b/public/New-ModuleV3_Add-ModuleV3.ps1 index 6420ee9..2cd538a 100644 --- a/public/New-ModuleV3_Add-ModuleV3.ps1 +++ b/public/New-ModuleV3_Add-ModuleV3.ps1 @@ -10,37 +10,31 @@ 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" + $rootModule = "$moduleName.psm1" Import-Template -Path $modulePath -File $rootModule -Template "template.module.psm1" # 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") diff --git a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 b/public/New-ModuleV3_Add-TestingToModuleV3.ps1 index 49aa4c5..e156935 100644 --- a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 +++ b/public/New-ModuleV3_Add-TestingToModuleV3.ps1 @@ -3,14 +3,13 @@ function Add-TestModuleV3 { Param ( [Parameter()][string]$Path, - [Parameter(Mandatory)][string]$Name, [Parameter()][hashtable]$Metadata ) - $testingModuleName = Get-TestModuleName -Name $Name - $testingModulePath = Get-TestModulePath -Name $Name -Path $Path + $testingModuleName = Get-TestModuleName -Path $Path + $testingModulePath = Get-TestModulePath -Path $Path - $result = Add-ModuleV3 -Name $testingModuleName -Path $testingModulePath -Metadata $Metadata + $result = Add-ModuleV3 -Name $testingModuleName -RootPath $testingModulePath -Metadata $Metadata if(!$result){ Write-Error -Message ("Error creating the module [$testingModuleName].") @@ -33,7 +32,7 @@ function Add-TestingToModuleV3{ [Parameter()][switch]$AddSampleCode ) - $testModulePath = Add-TestModuleV3 -Path $Path -Name $Name -Metadata $Metadata + $testModulePath = Add-TestModuleV3 -Path $Path -Metadata $Metadata if (!$testModulePath) { Write-Error -Message ("Error creating Testing for Module [$Name].") @@ -42,16 +41,11 @@ function Add-TestingToModuleV3{ # 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 - } + Add-TestSampleCode -Path $testModulePath } # Get root folder - $modulePath = Get-ModulePath -Path $Path + $modulePath = Get-ModulePath -RootPath $Path # test.ps1 script $testScriptPath = $modulePath | Join-Path -ChildPath "test.ps1" diff --git a/test.ps1 b/test.ps1 index ff4e3bb..63d2167 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_NewModuleV3_AddTestingToModule_AddTestingModuleV3_Simple +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddModuleSection_FULL_* +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddModule_DefaultManifest +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors From 873739f139d84c1c6016aac953e9c1cfafb470ed Mon Sep 17 00:00:00 2001 From: rulasg Date: Sun, 25 Jun 2023 11:20:40 +0200 Subject: [PATCH 08/31] Rename Add-ModuleFull to Add-ModuleAll --- TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 | 6 +++--- public/Add-ModuleSection.ps1 | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 index f9fc712..bc56a7e 100644 --- a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -42,7 +42,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ New-TestingFolder -Path "folderName" - $result = Add-TT_ModuleFull -Path "./folderName" + $result = Add-TT_ModuleAll -Path "./folderName" $result | Assert-AddFull } @@ -50,7 +50,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ New-TestingFolder -Path "folderName" - $result = Get-Item -path "folderName" | Add-TT_ModuleFull + $result = Get-Item -path "folderName" | Add-TT_ModuleAll $result | Assert-AddFull } @@ -61,7 +61,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Module{ Import-Module -Name $modulePath $module = Get-Module -Name "MyModule" - $result = $module | Add-TT_ModuleFull + $result = $module | Add-TT_ModuleAll $result | Assert-AddFull Remove-Module -Name "MyModule" diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index d75076b..f496d5e 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -292,7 +292,7 @@ function Add-ModuleDeployWorkflow{ } } Export-ModuleMember -Function Add-ModuleDeployWorkflow -function Add-ModuleFull{ +function Add-ModuleAll{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -319,4 +319,4 @@ function Add-ModuleFull{ } -} Export-ModuleMember -Function Add-ModuleFull \ No newline at end of file +} Export-ModuleMember -Function Add-ModuleAll \ No newline at end of file From 71a41728fab17f6992fc00a7e689a703df1e921a Mon Sep 17 00:00:00 2001 From: rulasg Date: Sun, 25 Jun 2023 11:20:59 +0200 Subject: [PATCH 09/31] bug avoid leaving module loaded after test --- TestingHelperTest/public/Tests-test.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TestingHelperTest/public/Tests-test.ps1 b/TestingHelperTest/public/Tests-test.ps1 index 30097f4..c12710a 100644 --- a/TestingHelperTest/public/Tests-test.ps1 +++ b/TestingHelperTest/public/Tests-test.ps1 @@ -13,6 +13,8 @@ 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-Module -Name "ModuleName" -Force } function TestingHelperTest_TestPS1_WithPath{ @@ -24,4 +26,6 @@ function TestingHelperTest_TestPS1_WithPath{ Assert-AreEqual -Expected "ModuleName" -Presented $result.Name Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName + + Remove-Module -Name "ModuleName" -Force } \ No newline at end of file From 7df94ad99901b788636491bac9c406196492d94e Mon Sep 17 00:00:00 2001 From: rulasg Date: Sun, 25 Jun 2023 11:29:23 +0200 Subject: [PATCH 10/31] Rename Ad-Module* to Add-ToModule* --- .../public/Tests-Add-moduleSectiond.ps1 | 14 ++-- public/Add-ModuleSection.ps1 | 72 +++++++++---------- public/New-ModuleV3.ps1 | 22 +++--- 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 index bc56a7e..09cf02c 100644 --- a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -1,7 +1,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_NewModuleV3{ - $result = New-TT_ModuleV3 -Name "MyModule" | Add-TT_ModuleLicense + $result = New-TT_ModuleV3 -Name "MyModule" | Add-TT_ToModuleLicense $result | Assert-Addlicense } @@ -10,7 +10,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Folder{ New-TestingFolder -Path "folderName" - $result = Get-Item -path "folderName" | Add-TT_ModuleLicense + $result = Get-Item -path "folderName" | Add-TT_ToModuleLicense $result | Assert-Addlicense } @@ -22,7 +22,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Module{ Import-Module -Name $modulePath $module = Get-Module -Name $moduleName - $result = $module | Add-TT_ModuleLicense + $result = $module | Add-TT_ToModuleLicense $result | Assert-Addlicense @@ -33,7 +33,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Chain{ $modulePath = New-TT_ModuleV3 -Name "MyModule" - $result1 = $modulePath | Add-TT_ModuleLicense | Add-TT_ModuleAbout + $result1 = $modulePath | Add-TT_ToModuleLicense | Add-TT_ToModuleAbout $result1 | Assert-Addlicense -PassThru | Assert-AddAbout } @@ -42,7 +42,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ New-TestingFolder -Path "folderName" - $result = Add-TT_ModuleAll -Path "./folderName" + $result = Add-TT_ToModuleAll -Path "./folderName" $result | Assert-AddFull } @@ -50,7 +50,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ New-TestingFolder -Path "folderName" - $result = Get-Item -path "folderName" | Add-TT_ModuleAll + $result = Get-Item -path "folderName" | Add-TT_ToModuleAll $result | Assert-AddFull } @@ -61,7 +61,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Module{ Import-Module -Name $modulePath $module = Get-Module -Name "MyModule" - $result = $module | Add-TT_ModuleAll + $result = $module | Add-TT_ToModuleAll $result | Assert-AddFull Remove-Module -Name "MyModule" diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index f496d5e..85558e7 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -3,7 +3,7 @@ # 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-Module* function +# The output will be the Path of the module updated. This way we may pipe with next Add-ToModule* function @@ -30,7 +30,7 @@ function GetPath($Path){ return $ret | Convert-Path } -function Add-ModuleSampleCode{ +function Add-ToModuleSampleCode{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -50,7 +50,7 @@ function Add-ModuleSampleCode{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleSampleCode +} Export-ModuleMember -Function Add-ToModuleSampleCode function Add-TestSampleCode{ [CmdletBinding(SupportsShouldProcess)] @@ -76,11 +76,11 @@ function Add-TestSampleCode{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleSampleCode +} Export-ModuleMember -Function Add-ToModuleSampleCode # Add devcontainer.json file -function Add-ModuleDevContainerJson{ +function Add-ToModuleDevContainerJson{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -97,10 +97,10 @@ function Add-ModuleDevContainerJson{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleDevContainerJson +} Export-ModuleMember -Function Add-ToModuleDevContainerJson # Add License file -function Add-ModuleLicense{ +function Add-ToModuleLicense{ [CmdletBinding(SupportsShouldProcess)] Param ( @@ -117,10 +117,10 @@ function Add-ModuleLicense{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleLicense +} Export-ModuleMember -Function Add-ToModuleLicense # Add Readme file -function Add-ModuleReadme{ +function Add-ToModuleReadme{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -141,10 +141,10 @@ function Add-ModuleReadme{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleReadme +} Export-ModuleMember -Function Add-ToModuleReadme # Add about -function Add-ModuleAbout{ +function Add-ToModuleAbout{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -169,10 +169,10 @@ function Add-ModuleAbout{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleAbout +} Export-ModuleMember -Function Add-ToModuleAbout # Add deploying -function Add-ModuleDeployScript{ +function Add-ToModuleDeployScript{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -189,10 +189,10 @@ function Add-ModuleDeployScript{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleDeployScript +} Export-ModuleMember -Function Add-ToModuleDeployScript # Add Release -function Add-ModuleReleaseScript{ +function Add-ToModuleReleaseScript{ [CmdletBinding(SupportsShouldProcess)] Param ( @@ -209,10 +209,10 @@ function Add-ModuleReleaseScript{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleReleaseScript +} Export-ModuleMember -Function Add-ToModuleReleaseScript # Add Sync -function Add-ModuleSyncScript{ +function Add-ToModuleSyncScript{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -229,10 +229,10 @@ function Add-ModuleSyncScript{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleSyncScript +} Export-ModuleMember -Function Add-ToModuleSyncScript # Add PSScriptAnalyzer -function Add-ModulePSScriptAnalyzerWorkflow{ +function Add-ToModulePSScriptAnalyzerWorkflow{ [CmdletBinding(SupportsShouldProcess)] Param ( @@ -250,10 +250,10 @@ function Add-ModulePSScriptAnalyzerWorkflow{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModulePSScriptAnalyzerWorkflow +} Export-ModuleMember -Function Add-ToModulePSScriptAnalyzerWorkflow # Add Testing -function Add-ModuleTestingWorkflow{ +function Add-ToModuleTestingWorkflow{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -270,10 +270,10 @@ function Add-ModuleTestingWorkflow{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleTestingWorkflow +} Export-ModuleMember -Function Add-ToModuleTestingWorkflow # Add deploy Workflow -function Add-ModuleDeployWorkflow{ +function Add-ToModuleDeployWorkflow{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -290,9 +290,9 @@ function Add-ModuleDeployWorkflow{ return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleDeployWorkflow +} Export-ModuleMember -Function Add-ToModuleDeployWorkflow -function Add-ModuleAll{ +function Add-ToModuleAll{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -304,19 +304,19 @@ function Add-ModuleAll{ process{ $Path = GetPath -Path:$Path ?? return $null - $null = $Path | Add-ModuleDevContainerJson -Force:$Force - $null = $Path | Add-ModuleLicense -Force:$Force - $null = $Path | Add-ModuleReadme -Force:$Force - $null = $Path | Add-ModuleAbout -Force:$Force - $null = $Path | Add-ModuleDeployScript -Force:$Force - $null = $Path | Add-ModuleReleaseScript -Force:$Force - $null = $Path | Add-ModuleSyncScript -Force:$Force - $null = $Path | Add-ModulePSScriptAnalyzerWorkflow -Force:$Force - $null = $Path | Add-ModuleTestingWorkflow -Force:$Force - $null = $Path | Add-ModuleDeployWorkflow -Force:$Force + $null = $Path | Add-ToModuleDevContainerJson -Force:$Force + $null = $Path | Add-ToModuleLicense -Force:$Force + $null = $Path | Add-ToModuleReadme -Force:$Force + $null = $Path | Add-ToModuleAbout -Force:$Force + $null = $Path | Add-ToModuleDeployScript -Force:$Force + $null = $Path | Add-ToModuleReleaseScript -Force:$Force + $null = $Path | Add-ToModuleSyncScript -Force:$Force + $null = $Path | Add-ToModulePSScriptAnalyzerWorkflow -Force:$Force + $null = $Path | Add-ToModuleTestingWorkflow -Force:$Force + $null = $Path | Add-ToModuleDeployWorkflow -Force:$Force return ReturnValue -Path $Path -Force:$Force } -} Export-ModuleMember -Function Add-ModuleAll \ No newline at end of file +} Export-ModuleMember -Function Add-ToModuleAll \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 73e1cb8..ec18d7c 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -81,7 +81,7 @@ function New-ModuleV3 { # Add Sample Code if($AddSampleCode){ - $null = Add-ModuleSampleCode -Path $modulePath + $null = Add-ToModuleSampleCode -Path $modulePath } } @@ -101,52 +101,52 @@ function New-ModuleV3 { # Add devcontainer.json file if($AddDevContainerJson){ - $null = Add-ModuleDevContainerJson -Path $modulePath + $null = Add-ToModuleDevContainerJson -Path $modulePath } # Add License file if($AddLicense){ - $null = Add-ModuleLicense -Path $modulePath + $null = Add-ToModuleLicense -Path $modulePath } # Add Readme file if($AddReadme){ - $null = Add-ModuleReadme -Path $modulePath + $null = Add-ToModuleReadme -Path $modulePath } # Add about if($AddAbout){ - $null = Add-ModuleAbout -Path $modulePath + $null = Add-ToModuleAbout -Path $modulePath } # Add deploying if($AddDeployScript){ - $null = Add-ModuleDeployScript -Path $modulePath + $null = Add-ToModuleDeployScript -Path $modulePath } # Add Release if($AddReleaseScript){ - $null = Add-ModuleReleaseScript -Path $modulePath + $null = Add-ToModuleReleaseScript -Path $modulePath } # Add Sync if($AddSyncScript){ - $null = Add-ModuleSyncScript -Path $modulePath + $null = Add-ToModuleSyncScript -Path $modulePath } # Add PSScriptAnalyzer if($AddPSScriptAnalyzerWorkflow){ - $null = Add-ModulePSScriptAnalyzerWorkflow -Path $modulePath + $null = Add-ToModulePSScriptAnalyzerWorkflow -Path $modulePath } # Add Testing if($AddTestingWorkflow){ - $null = Add-ModuleTestingWorkflow -Path $modulePath + $null = Add-ToModuleTestingWorkflow -Path $modulePath } # Add deploy Workflow if($AddDeployWorkflow){ - $null = Add-ModuledeployWorkflow -Path $modulePath + $null = Add-ToModuledeployWorkflow -Path $modulePath } return $retModulePath From c8512d459ab110e27f2049d50b2567e511a8abd4 Mon Sep 17 00:00:00 2001 From: rulasg Date: Sun, 25 Jun 2023 18:36:38 +0200 Subject: [PATCH 11/31] Pass[85] --- .../Assert-Add-ModuleSections.Helper.ps1 | 21 +++--- .../private/tests-Helper-New-ModuleV3.ps1 | 5 +- ...Tests-New-ModuleV3-Add-TestingModuleV3.ps1 | 57 --------------- .../Tests-New-ModuleV3-AddTestToModuleAll.ps1 | 35 ++++++++++ .../public/Tests-New-ModuleV3.ps1 | 21 ++---- private/Templates-Helper.ps1 | 3 +- public/Add-ModuleSection.ps1 | 43 +++++++++++- public/New-ModuleV3.ps1 | 5 +- public/New-ModuleV3_Add-ModuleV3.ps1 | 2 +- public/New-ModuleV3_Add-TestingToModuleV3.ps1 | 69 +++++++++---------- test.ps1 | 6 +- 11 files changed, 138 insertions(+), 129 deletions(-) delete mode 100644 TestingHelperTest/public/Tests-New-ModuleV3-Add-TestingModuleV3.ps1 create mode 100644 TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 diff --git a/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 b/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 index d5e40f8..dd91464 100644 --- a/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 +++ b/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 @@ -182,7 +182,7 @@ function Assert-AddPSScriptAnalyzerWorkflow{ # TestingWorkflow -function Assert-AddTestingWorkflow{ +function Assert-AddTestWorkflow{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] @@ -231,7 +231,7 @@ function Assert-AddSampleCodes{ } #Testing SampleCode -function Assert-AddTestingSampleCodes{ +function Assert-AddTestSampleCodes{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] @@ -251,12 +251,11 @@ function Assert-AddTestingSampleCodes{ } # Testing launch.json -function Assert-AddTestingLaunchJson{ +function Assert-AddTestLaunchJson{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [ValidateSet("Pester","PesterWithTestingHelper")] - [string] $TestingFramework, + [string] $Path, [Parameter()][switch]$Passthru ) process{ @@ -281,12 +280,11 @@ function Assert-AddTestingLaunchJson{ } # Testing TestScript -function Assert-AddTestingTestScript{ +function Assert-AddTestTestScript{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [ValidateSet("Pester","PesterWithTestingHelper")] - [string] $TestingFramework, + [string] $Path, [Parameter()][switch]$Passthru ) process{ @@ -373,7 +371,7 @@ function Assert-AddModuleV3{ } } -function Assert-AddTestingV3 { +function Assert-AddTestV3 { param( [Parameter()][string]$Path, [Parameter()][hashtable]$Expected @@ -387,6 +385,9 @@ function Assert-AddTestingV3 { $testingModulePath = $path | Join-Path -ChildPath $testingModuleName Assert-AddModuleV3 -Path $testingModulePath -Expected $Expected + + Assert-AddTestTestScript -Path $Path + Assert-AddTestLaunchJson -Path $Path } } @@ -408,7 +409,7 @@ function Assert-AddFull{ $Path | Assert-AddReleaseScript $Path | Assert-AddSyncScript $Path | Assert-AddPSScriptAnalyzerWorkflow - $Path | Assert-AddTestingWorkflow + $Path | Assert-AddTestWorkflow $Path | Assert-AddDeployWorkflow return $Passthru ? $Path : $null diff --git a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 index d081c52..be8c183 100644 --- a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 +++ b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 @@ -6,14 +6,13 @@ function Get-DefaultsManifest { -function Assert-TestingV3 { +function Assert-TestModuleV3 { param( - [Parameter()][string]$Name, [Parameter()][string]$Path, [Parameter()][hashtable]$Expected ) - Assert-AddTestingV3 -Path $Path -Expected $Expected + Assert-AddTestV3 -Path $Path -Expected $Expected } 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 4db223b..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..1ca63ee --- /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_TestToModuleAll + + Assert-AreEqualPath -Expected $modulePath -Presented $result + Assert-TestModuleV3 -Path $modulePath -Expected $param +} + + +function TestingHelperTest_AddTestToModuleAll_Simple{ + + $moduleName = "MyModule" + $modulePath = New-ModuleV3 -Name $moduleName + + $result = Add-TT_TestToModuleAll -Path $modulePath + + Assert-AreEqualPath -Expected $modulePath -Presented $result + Assert-TestModuleV3 -Path $modulePath -Expected $param + +} + +# 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 d7d3b90..4cc1722 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -18,7 +18,7 @@ function TestingHelperTest_NewModuleV3_WithName { $result | Assert-AddModuleV3 } -function TestingHelperTest_NewModuleV3_WithNameRemotePath { +function TestingHelperTest_NewModuleV3_WithName_RemotePath { $moduleName = "MyModule" $folderName = "FolderName" @@ -42,7 +42,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,23 +52,10 @@ 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 -Path $modulePath Assert-AddSampleCodes -Path $modulePath - Assert-TestingV3 -Name $moduleName -Path $modulePath - Assert-AddTestingSampleCodes -Path $modulePath + Assert-TestModuleV3 -Path $modulePath + Assert-AddTestSampleCodes -Path $modulePath } diff --git a/private/Templates-Helper.ps1 b/private/Templates-Helper.ps1 index 8d8dbd0..f3b2013 100644 --- a/private/Templates-Helper.ps1 +++ b/private/Templates-Helper.ps1 @@ -23,7 +23,8 @@ function Import-Template { } } - $content = Get-Content -Path ($PSScriptRoot | Join-Path -ChildPath templates -AdditionalChildPath $Template) + $templatePath = $PSScriptRoot | Join-Path -ChildPath templates -AdditionalChildPath $Template + $content = Get-Content -Path $templatePath if ($Replaces) { $Replaces.Keys | ForEach-Object { diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index 85558e7..70d3781 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -319,4 +319,45 @@ function Add-ToModuleAll{ } -} Export-ModuleMember -Function Add-ToModuleAll \ No newline at end of file +} Export-ModuleMember -Function Add-ToModuleAll + +# Add test.ps1 script +function Add-ToModuleTestScript{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + process{ + $Path = GetPath -Path:$Path ?? return $null + + Import-Template -Force:$Force -Path $Path -File "test.ps1" -Template "template.v3.test.ps1" + + return ReturnValue -Path $Path -Force:$Force + } +} Export-ModuleMember -Function Add-ToModuleTestScript + +# Add launch.json to module +function Add-ToModuleLaunchJson{ + [CmdletBinding(SupportsShouldProcess)] + Param + ( + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + process{ + $Path = GetPath -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 + } +} Export-ModuleMember -Function Add-ToModuleLaunchJson diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index ec18d7c..e1908c1 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -64,6 +64,9 @@ function New-ModuleV3 { $modulePath = Get-ModulePath -Name $Name -RootPath $Path $moduleName = Get-ModuleName -Path $modulePath + # If asked for testing add sample code on both modules + $AddSampleCode = $AddSampleCode -or $AddTesting + # Create the module if ($moduleName) { @@ -86,7 +89,7 @@ function New-ModuleV3 { } if ($AddTesting) { - $result = Add-TestingToModuleV3 -Name $Name -Path $modulePath + $result = Add-TestToModuleAll -Path $modulePath # Check if the module was created if(! $result){ diff --git a/public/New-ModuleV3_Add-ModuleV3.ps1 b/public/New-ModuleV3_Add-ModuleV3.ps1 index 2cd538a..d9acc4a 100644 --- a/public/New-ModuleV3_Add-ModuleV3.ps1 +++ b/public/New-ModuleV3_Add-ModuleV3.ps1 @@ -52,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/New-ModuleV3_Add-TestingToModuleV3.ps1 b/public/New-ModuleV3_Add-TestingToModuleV3.ps1 index e156935..c7f414b 100644 --- a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 +++ b/public/New-ModuleV3_Add-TestingToModuleV3.ps1 @@ -3,69 +3,68 @@ function Add-TestModuleV3 { Param ( [Parameter()][string]$Path, - [Parameter()][hashtable]$Metadata + [Parameter()][switch]$AddSampleCode ) $testingModuleName = Get-TestModuleName -Path $Path $testingModulePath = Get-TestModulePath -Path $Path - $result = Add-ModuleV3 -Name $testingModuleName -RootPath $testingModulePath -Metadata $Metadata + # Generate metadata from the module + $manifest = Get-ModuleManifest -Path $Path + # 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') + + + $result = Add-ModuleV3 -Name $testingModuleName -RootPath $testingModulePath -Metadata $manifest if(!$result){ Write-Error -Message ("Error creating the module [$testingModuleName].") return $null } + # AddSampleCode + if ($AddSampleCode) { + $null = Add-TestSampleCode -Path $result + } + return $result } Export-ModuleMember -Function Add-TestModuleV3 -function Add-TestingToModuleV3{ +function Add-TestToModuleAll{ [CmdletBinding()] Param ( - [Parameter(Mandatory)][string]$Path, - [Parameter(Mandatory)][string]$Name, - [Parameter()][hashtable]$Metadata, - [Parameter()][switch]$AddSampleCode + [Parameter()][string]$Path ) - $testModulePath = Add-TestModuleV3 -Path $Path -Metadata $Metadata + $Path = [string]::IsNullOrWhiteSpace($Path) ? '.' : $Path + #if Path is null return + $modulePath = Get-ModulePath -RootPath $Path + if(!$modulePath){return $null} + + # Test Module + $testModulePath = Add-TestModuleV3 -Path $Path -AddSampleCode + if (!$testModulePath) { + $name = Get-ModuleName -Path $Path Write-Error -Message ("Error creating Testing for Module [$Name].") return $null } - # Sample test - if ($AddSampleCode) { - Add-TestSampleCode -Path $testModulePath - } + # Add test.ps1 + $null = Add-ToModuleTestScript -Path $modulePath - # Get root folder - $modulePath = Get-ModulePath -RootPath $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" - } + # Add launch.json + $null = Add-ToModuleLaunchJson -Path $modulePath - return $modulePath + return $modulePath | Convert-Path -} Export-ModuleMember -Function Add-TestingToModuleV3 \ No newline at end of file +} Export-ModuleMember -Function Add-TestToModuleAll \ No newline at end of file diff --git a/test.ps1 b/test.ps1 index 63d2167..0e3ab67 100644 --- a/test.ps1 +++ b/test.ps1 @@ -47,7 +47,7 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddTestingToModule_AddTestingModuleV3_Simple -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddModuleSection_FULL_* +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTestToModuleAll_Simple # Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddModule_DefaultManifest -Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTest* +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors From 04389d2af45a5919bc015a9950e1b5a6881b8f0c Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 08:21:54 +0200 Subject: [PATCH 12/31] Pass all but TestingHelperTest_Manual* --- .../private/tests-Helper-New-ModuleV3.ps1 | 1 + .../Tests-New-ModuleV3-AddTestToModuleAll.ps1 | 4 +- .../public/Tests-NewModule_operations.ps1 | 60 +++++ ...anifest-Helper.ps1 => Manifest.Helper.ps1} | 12 + public/Add-ModuleSection.ps1 | 255 ++++++++++++------ ...leV3_Add-ModuleV3.ps1 => Add-ModuleV3.ps1} | 2 +- public/Add-TestModuleV3.ps1 | 43 +++ public/New-ModuleV3.ps1 | 6 +- public/New-ModuleV3_Add-TestingToModuleV3.ps1 | 70 ----- 9 files changed, 295 insertions(+), 158 deletions(-) create mode 100644 TestingHelperTest/public/Tests-NewModule_operations.ps1 rename private/{Manifest-Helper.ps1 => Manifest.Helper.ps1} (76%) rename public/{New-ModuleV3_Add-ModuleV3.ps1 => Add-ModuleV3.ps1} (97%) create mode 100644 public/Add-TestModuleV3.ps1 delete mode 100644 public/New-ModuleV3_Add-TestingToModuleV3.ps1 diff --git a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 index be8c183..bd56109 100644 --- a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 +++ b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 @@ -1,6 +1,7 @@ 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/Tests-New-ModuleV3-AddTestToModuleAll.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 index 1ca63ee..04803d5 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 @@ -7,7 +7,7 @@ function TestingHelperTest_AddTestToModuleAll_NoParam{ $modulePath | Set-Location # Should find module on current location and add test to it - $result = Add-TT_TestToModuleAll + $result = Add-TT_ToModuleTestAll Assert-AreEqualPath -Expected $modulePath -Presented $result Assert-TestModuleV3 -Path $modulePath -Expected $param @@ -19,7 +19,7 @@ function TestingHelperTest_AddTestToModuleAll_Simple{ $moduleName = "MyModule" $modulePath = New-ModuleV3 -Name $moduleName - $result = Add-TT_TestToModuleAll -Path $modulePath + $result = Add-TT_ToModuleTestAll -Path $modulePath Assert-AreEqualPath -Expected $modulePath -Presented $result Assert-TestModuleV3 -Path $modulePath -Expected $param diff --git a/TestingHelperTest/public/Tests-NewModule_operations.ps1 b/TestingHelperTest/public/Tests-NewModule_operations.ps1 new file mode 100644 index 0000000..dbe5504 --- /dev/null +++ b/TestingHelperTest/public/Tests-NewModule_operations.ps1 @@ -0,0 +1,60 @@ + +function TestingHelperTest_Manual_Work_StepsToFull{ + + open . + + $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-TestModuleV3 -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-AddFull -Path $modulePath + + Pop-Location + + # Run tests + + $result = Invoke-TT_TestingHelper -Path $modulePath + + Assert-AreEqual -Expected 2 -Presented $result.Test + Assert-AreEqual -Expected 2 -Presented $result.Pass +} \ No newline at end of file diff --git a/private/Manifest-Helper.ps1 b/private/Manifest.Helper.ps1 similarity index 76% rename from private/Manifest-Helper.ps1 rename to private/Manifest.Helper.ps1 index 45c5c74..2349376 100644 --- a/private/Manifest-Helper.ps1 +++ b/private/Manifest.Helper.ps1 @@ -19,6 +19,18 @@ function Get-ModuleManifest($Path){ return $manifest } +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/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index 70d3781..1ad00bc 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -16,11 +16,14 @@ function ReturnValue($Path,$Force){ } # Normalize $Path and returns $null if not valid -function GetPath($Path){ +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 { @@ -33,14 +36,16 @@ function GetPath($Path){ function Add-ToModuleSampleCode{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + + # 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" @@ -52,45 +57,20 @@ function Add-ToModuleSampleCode{ } } Export-ModuleMember -Function Add-ToModuleSampleCode -function Add-TestSampleCode{ - [CmdletBinding(SupportsShouldProcess)] - param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] - [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Force - ) - - process{ - $Path = GetPath -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 -File "SampleFunctionTests.ps1" -Template "template.testmodule.functions.public.ps1" -Replaces @{ - '_MODULE_TESTING_' = $testModulename - '_MODULE_TESTED_' = $ModuleName - } - - return ReturnValue -Path $Path -Force:$Force - } -} Export-ModuleMember -Function Add-ToModuleSampleCode # Add devcontainer.json file function Add-ToModuleDevContainerJson{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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" @@ -104,14 +84,14 @@ function Add-ToModuleLicense{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $Path = NormalizePath -Path:$Path ?? return $null Import-Template -Force:$Force -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" @@ -123,14 +103,14 @@ function Add-ToModuleLicense{ function Add-ToModuleReadme{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $Path = NormalizePath -Path:$Path ?? return $null try{$moduleManifest = Get-ModuleManifest -Path $Path }catch{$moduleManifest = $null} $moduleName = $Path | Split-Path -LeafBase @@ -147,14 +127,14 @@ function Add-ToModuleReadme{ function Add-ToModuleAbout{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $Path = NormalizePath -Path:$Path ?? return $null try{$moduleManifest = Get-ModuleManifest -Path $Path} catch{$moduleManifest = $null} $moduleName = $Path | Split-Path -LeafBase @@ -175,14 +155,14 @@ function Add-ToModuleAbout{ function Add-ToModuleDeployScript{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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" @@ -196,14 +176,14 @@ function Add-ToModuleReleaseScript{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $Path = NormalizePath -Path:$Path ?? return $null Import-Template -Force:$Force -Path $Path -File "release.ps1" -Template "template.v3.release.ps1" @@ -215,14 +195,14 @@ function Add-ToModuleReleaseScript{ function Add-ToModuleSyncScript{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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" @@ -236,14 +216,14 @@ function Add-ToModulePSScriptAnalyzerWorkflow{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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" @@ -253,37 +233,37 @@ function Add-ToModulePSScriptAnalyzerWorkflow{ } Export-ModuleMember -Function Add-ToModulePSScriptAnalyzerWorkflow # Add Testing -function Add-ToModuleTestingWorkflow{ +function Add-ToModuleTestWorkflow{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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 } -} Export-ModuleMember -Function Add-ToModuleTestingWorkflow +} Export-ModuleMember -Function Add-ToModuleTestWorkflow # Add deploy Workflow function Add-ToModuleDeployWorkflow{ [CmdletBinding(SupportsShouldProcess)] param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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" @@ -292,48 +272,22 @@ function Add-ToModuleDeployWorkflow{ } } Export-ModuleMember -Function Add-ToModuleDeployWorkflow -function Add-ToModuleAll{ - [CmdletBinding(SupportsShouldProcess)] - param( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] - [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Force - ) - - process{ - $Path = GetPath -Path:$Path ?? return $null - - $null = $Path | Add-ToModuleDevContainerJson -Force:$Force - $null = $Path | Add-ToModuleLicense -Force:$Force - $null = $Path | Add-ToModuleReadme -Force:$Force - $null = $Path | Add-ToModuleAbout -Force:$Force - $null = $Path | Add-ToModuleDeployScript -Force:$Force - $null = $Path | Add-ToModuleReleaseScript -Force:$Force - $null = $Path | Add-ToModuleSyncScript -Force:$Force - $null = $Path | Add-ToModulePSScriptAnalyzerWorkflow -Force:$Force - $null = $Path | Add-ToModuleTestingWorkflow -Force:$Force - $null = $Path | Add-ToModuleDeployWorkflow -Force:$Force - - return ReturnValue -Path $Path -Force:$Force - - } -} Export-ModuleMember -Function Add-ToModuleAll +###### TEST ###### # Add test.ps1 script function Add-ToModuleTestScript{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $Path = NormalizePath -Path:$Path ?? return $null Import-Template -Force:$Force -Path $Path -File "test.ps1" -Template "template.v3.test.ps1" @@ -346,14 +300,14 @@ function Add-ToModuleLaunchJson{ [CmdletBinding(SupportsShouldProcess)] Param ( - [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, [Parameter()][switch]$Force ) process{ - $Path = GetPath -Path:$Path ?? return $null + $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" @@ -361,3 +315,140 @@ function Add-ToModuleLaunchJson{ return ReturnValue -Path $Path -Force:$Force } } Export-ModuleMember -Function Add-ToModuleLaunchJson + + +function Add-ToModuleTestSampleCode{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + 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 + } +} Export-ModuleMember -Function Add-ToModuleTestSampleCode + + +###### Add All ###### + +function Add-ToModuleTestModule{ + [CmdletBinding()] + Param + ( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + process { + + $Path = NormalizePath -Path:$Path ?? return $null + + #if Path is null return + $modulePath = Get-ModulePath -RootPath $Path + if(!$modulePath){return $null} + + # Test Module + # TODO : Not sure how to implemente the Forice flag with Add full modules + $testModulePath = Add-TestModuleV3 -Path $Path -AddSampleCode + + if (!$testModulePath) { + $name = Get-ModuleName -Path $Path + Write-Error -Message ("Error creating Testing for Module [$Name].") + return $null + } + + return ReturnValue -Path $Path -Force:$Force + } +} 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()][switch]$Force + ) + + process{ + + $Path = NormalizePath -Path:$Path ?? return $null + + #if Path is null return + $modulePath = Get-ModulePath -RootPath $Path + if(!$modulePath){return $null} + + # Test Module + # TODO : Not sure how to implemente the Forice flag with Add full modules + $testModulePath = Add-TestModuleV3 -Path $Path -AddSampleCode + + if (!$testModulePath) { + $name = Get-ModuleName -Path $Path + Write-Error -Message ("Error creating Testing for Module [$Name].") + return $null + } + + # Add test.ps1 + $null = Add-ToModuleTestScript -Path $modulePath -Force:$Force + + # Add launch.json + $null = Add-ToModuleLaunchJson -Path $modulePath -Force:$Force + + return ReturnValue -Path $modulePath -Force:$Force + } + +} Export-ModuleMember -Function Add-ToModuleTestAll + +function Add-ToModuleAll{ + [CmdletBinding(SupportsShouldProcess)] + param( + [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path, + [Parameter()][switch]$Force + ) + + process{ + $Path = NormalizePath -Path:$Path ?? return $null + + $null = $Path | Add-ToModuleTestAll -Force:$Force + + $null = $Path | Add-ToModuleDevContainerJson -Force:$Force + $null = $Path | Add-ToModuleLicense -Force:$Force + $null = $Path | Add-ToModuleReadme -Force:$Force + $null = $Path | Add-ToModuleAbout -Force:$Force + + # scripts + $null = $Path | Add-ToModuleDeployScript -Force:$Force + $null = $Path | Add-ToModuleReleaseScript -Force:$Force + $null = $Path | Add-ToModuleSyncScript -Force:$Force + + # workflows + $null = $Path | Add-ToModulePSScriptAnalyzerWorkflow -Force:$Force + $null = $Path | Add-ToModuleTestWorkflow -Force:$Force + $null = $Path | Add-ToModuleDeployWorkflow -Force:$Force + + return ReturnValue -Path $Path -Force:$Force + + } + +} Export-ModuleMember -Function Add-ToModuleAll diff --git a/public/New-ModuleV3_Add-ModuleV3.ps1 b/public/Add-ModuleV3.ps1 similarity index 97% rename from public/New-ModuleV3_Add-ModuleV3.ps1 rename to public/Add-ModuleV3.ps1 index d9acc4a..e2520b0 100644 --- a/public/New-ModuleV3_Add-ModuleV3.ps1 +++ b/public/Add-ModuleV3.ps1 @@ -29,7 +29,7 @@ function Add-ModuleV3 { # PSM1 $rootModule = "$moduleName.psm1" - Import-Template -Path $modulePath -File $rootModule -Template "template.module.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") diff --git a/public/Add-TestModuleV3.ps1 b/public/Add-TestModuleV3.ps1 new file mode 100644 index 0000000..b64c116 --- /dev/null +++ b/public/Add-TestModuleV3.ps1 @@ -0,0 +1,43 @@ +function Add-TestModuleV3 { + [CmdletBinding()] + Param + ( + [Parameter(Mandatory)][string]$Path, + [Parameter()][switch]$AddSampleCode, + [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 + } + + # AddSampleCode + if ($AddSampleCode) { + $null = Add-ToModuleTestSampleCode -Path $Path -Force:$Force + } + + return $result + +} Export-ModuleMember -Function Add-TestModuleV3 + diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index e1908c1..fc7db55 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -89,7 +89,7 @@ function New-ModuleV3 { } if ($AddTesting) { - $result = Add-TestToModuleAll -Path $modulePath + $result = Add-ToModuleTestAll -Path $modulePath # Check if the module was created if(! $result){ @@ -98,7 +98,7 @@ function New-ModuleV3 { # Add Sample Code if($AddSampleCode){ - $null = Add-TestSampleCode -Path $modulePath + $null = Add-ToModuleTestSampleCode -Path $modulePath } } @@ -144,7 +144,7 @@ function New-ModuleV3 { # Add Testing if($AddTestingWorkflow){ - $null = Add-ToModuleTestingWorkflow -Path $modulePath + $null = Add-ToModuleTestWorkflow -Path $modulePath } # Add deploy Workflow diff --git a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 b/public/New-ModuleV3_Add-TestingToModuleV3.ps1 deleted file mode 100644 index c7f414b..0000000 --- a/public/New-ModuleV3_Add-TestingToModuleV3.ps1 +++ /dev/null @@ -1,70 +0,0 @@ -function Add-TestModuleV3 { - [CmdletBinding()] - Param - ( - [Parameter()][string]$Path, - [Parameter()][switch]$AddSampleCode - ) - - $testingModuleName = Get-TestModuleName -Path $Path - $testingModulePath = Get-TestModulePath -Path $Path - - # Generate metadata from the module - $manifest = Get-ModuleManifest -Path $Path - # 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') - - - $result = Add-ModuleV3 -Name $testingModuleName -RootPath $testingModulePath -Metadata $manifest - - if(!$result){ - Write-Error -Message ("Error creating the module [$testingModuleName].") - return $null - } - - # AddSampleCode - if ($AddSampleCode) { - $null = Add-TestSampleCode -Path $result - } - - return $result - -} Export-ModuleMember -Function Add-TestModuleV3 - - - -function Add-TestToModuleAll{ - [CmdletBinding()] - Param - ( - [Parameter()][string]$Path - ) - - $Path = [string]::IsNullOrWhiteSpace($Path) ? '.' : $Path - - #if Path is null return - $modulePath = Get-ModulePath -RootPath $Path - if(!$modulePath){return $null} - - # Test Module - $testModulePath = Add-TestModuleV3 -Path $Path -AddSampleCode - - if (!$testModulePath) { - $name = Get-ModuleName -Path $Path - Write-Error -Message ("Error creating Testing for Module [$Name].") - return $null - } - - # Add test.ps1 - $null = Add-ToModuleTestScript -Path $modulePath - - # Add launch.json - $null = Add-ToModuleLaunchJson -Path $modulePath - - return $modulePath | Convert-Path - -} Export-ModuleMember -Function Add-TestToModuleAll \ No newline at end of file From ebd2b168b10385260699b3e22dedc7e75ca76be8 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 08:41:43 +0200 Subject: [PATCH 13/31] Fix two small test bugs from Asserts. --- TestingHelperTest/public/Tests-Assert-guid.ps1 | 2 +- TestingHelperTest/public/Tests-Assert-path.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 From a493cf18475b91af9653a805f88cabcd078bca55 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 08:53:00 +0200 Subject: [PATCH 14/31] Ref: Rename files and order some functions --- ...per.ps1 => Add-ModuleSections.Asserts.ps1} | 16 ----- ...onstants.ps1 => DUMMY.Tests.Constants.ps1} | 0 .../private/Manifest.Tests.Helper.ps1 | 36 +++++++++++ ...leV3.ps1 => New-ModuleV3.Tests.Helper.ps1} | 0 ...ams.ps1 => WarInfoParams.Tests.Helper.ps1} | 0 ... Microsoft.PowerShell.Core.Dependency.ps1} | 0 .../{Module-Helper.ps1 => Module.Helper.ps1} | 0 ...ewModuleV3.ps1 => New-ModuleV3.Helper.ps1} | 0 ...plates-Helper.ps1 => Templates.Helper.ps1} | 0 private/TestingFilesFolders.Helper.ps1 | 50 +++++++++++++++ ...lesFolders.ps1 => TestingFilesFolders.ps1} | 63 ------------------- .../{Write-Assert.ps1 => Write-Assertion.ps1} | 0 test.ps1 | 4 +- 13 files changed, 88 insertions(+), 81 deletions(-) rename TestingHelperTest/private/{Assert-Add-ModuleSections.Helper.ps1 => Add-ModuleSections.Asserts.ps1} (97%) rename TestingHelperTest/private/{tests-helper-DUMMY_Constants.ps1 => DUMMY.Tests.Constants.ps1} (100%) create mode 100644 TestingHelperTest/private/Manifest.Tests.Helper.ps1 rename TestingHelperTest/private/{tests-Helper-New-ModuleV3.ps1 => New-ModuleV3.Tests.Helper.ps1} (100%) rename TestingHelperTest/private/{tests-helper-WarInfoParams.ps1 => WarInfoParams.Tests.Helper.ps1} (100%) rename private/{Microsoft.PowerShell.Core-Dependency.ps1 => Microsoft.PowerShell.Core.Dependency.ps1} (100%) rename private/{Module-Helper.ps1 => Module.Helper.ps1} (100%) rename private/{Helper-NewModuleV3.ps1 => New-ModuleV3.Helper.ps1} (100%) rename private/{Templates-Helper.ps1 => Templates.Helper.ps1} (100%) create mode 100644 private/TestingFilesFolders.Helper.ps1 rename public/{TestringFilesFolders.ps1 => TestingFilesFolders.ps1} (64%) rename public/{Write-Assert.ps1 => Write-Assertion.ps1} (100%) diff --git a/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 similarity index 97% rename from TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 rename to TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 index dd91464..44c9de4 100644 --- a/TestingHelperTest/private/Assert-Add-ModuleSections.Helper.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -13,22 +13,6 @@ function TestingHelperTest_AssertAddSection_throwOnNull{ Assert-IsTrue -Condition $hasthrown } -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 -} - - # Devcontainer.json function Assert-AddDevContainerJson{ param( 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/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/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 similarity index 100% rename from TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 rename to TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 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/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/Helper-NewModuleV3.ps1 b/private/New-ModuleV3.Helper.ps1 similarity index 100% rename from private/Helper-NewModuleV3.ps1 rename to private/New-ModuleV3.Helper.ps1 diff --git a/private/Templates-Helper.ps1 b/private/Templates.Helper.ps1 similarity index 100% rename from private/Templates-Helper.ps1 rename to private/Templates.Helper.ps1 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/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/test.ps1 b/test.ps1 index 0e3ab67..2267cb5 100644 --- a/test.ps1 +++ b/test.ps1 @@ -49,5 +49,5 @@ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file # Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTestToModuleAll_Simple # Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddModule_DefaultManifest -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTest* -Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_Manual* +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors From a1deca2ba8a63d3b6e0726649be87ef9cb4cba72 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 11:16:18 +0200 Subject: [PATCH 15/31] file rename --- .../TestingHelper_TestedManifest.Helper.ps1} | 13 +++++++------ TestingHelperTest/public/Tests-Assert.ps1 | 2 +- TestingHelperTest/public/Tests-ModuleHelperTest.ps1 | 8 ++++---- .../public/Tests-NewModule_operations.ps1 | 2 -- .../public/Tests-TestingFilesFolders.ps1 | 4 ++-- private/Manifest.Helper.ps1 | 9 +++++++++ public/Add-ModuleSection.ps1 | 6 ++++-- 7 files changed, 27 insertions(+), 17 deletions(-) rename TestingHelperTest/{public/Tested-Manifest.ps1 => private/TestingHelper_TestedManifest.Helper.ps1} (62%) 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/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-NewModule_operations.ps1 b/TestingHelperTest/public/Tests-NewModule_operations.ps1 index dbe5504..ba21ec5 100644 --- a/TestingHelperTest/public/Tests-NewModule_operations.ps1 +++ b/TestingHelperTest/public/Tests-NewModule_operations.ps1 @@ -1,8 +1,6 @@ function TestingHelperTest_Manual_Work_StepsToFull{ - open . - $path = '.' | Join-Path -ChildPath "ModuleName" # Create module 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/private/Manifest.Helper.ps1 b/private/Manifest.Helper.ps1 index 2349376..304d910 100644 --- a/private/Manifest.Helper.ps1 +++ b/private/Manifest.Helper.ps1 @@ -19,6 +19,15 @@ 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 diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index 1ad00bc..8682b5e 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -112,7 +112,8 @@ function Add-ToModuleReadme{ process{ $Path = NormalizePath -Path:$Path ?? return $null - try{$moduleManifest = Get-ModuleManifest -Path $Path }catch{$moduleManifest = $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 @@ -136,7 +137,8 @@ function Add-ToModuleAbout{ process{ $Path = NormalizePath -Path:$Path ?? return $null - try{$moduleManifest = Get-ModuleManifest -Path $Path} catch{$moduleManifest = $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 @{ From d730f08230efc45235cfc3f0872bc0f70c22e8fe Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 11:32:34 +0200 Subject: [PATCH 16/31] Make New-TestingVsCodeLaunchJsonV1 private --- TestingHelperTest/public/Tests-New-ModuleV1.ps1 | 15 --------------- public/New-ModuleV1.ps1 | 6 +++--- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/TestingHelperTest/public/Tests-New-ModuleV1.ps1 b/TestingHelperTest/public/Tests-New-ModuleV1.ps1 index 01aeaf4..40e4ef6 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV1.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV1.ps1 @@ -56,19 +56,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/public/New-ModuleV1.ps1 b/public/New-ModuleV1.ps1 index 8ed4748..8a6ce4a 100644 --- a/public/New-ModuleV1.ps1 +++ b/public/New-ModuleV1.ps1 @@ -56,7 +56,7 @@ function New-ModuleV1 { if (-Not $AvoidTestFile) { New-TestingModule -Path $modulePath -ModuleName $ModuleName - New-TestingVsCodeLaunchJson -Path $modulePath -ModuleName $ModuleName + New-TestingVsCodeLaunchJsonV1 -Path $modulePath -ModuleName $ModuleName } return $modulePath @@ -132,7 +132,7 @@ Export-ModuleMember -Function _MODULE_TESTING__* $null = New-ModuleV1 -Path $Path -Name $testingModuleName -Description "Testing module for $ModuleName" -AvoidTestFile -AppendToModuleFile $toAppend } Export-ModuleMember -Function New-TestingModule -function New-TestingVsCodeLaunchJson($Path, $ModuleName){ +function New-TestingVsCodeLaunchJsonV1($Path, $ModuleName){ $testScript = @' { @@ -160,4 +160,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 From ef7bda3d4b635a49c9829189a011f56bf8eec192 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 12:34:57 +0200 Subject: [PATCH 17/31] docs - structure --- docs/Create-a-Module.md | 45 +++++++ docs/How-to-Use-TestingHelper.md | 8 ++ docs/deploying-with-testinghelper.md | 5 + docs/structure-of-a-module.md | 32 +++++ docs/writing-tests-with-testinghelper.md | 8 ++ readme.md | 152 +++++++++++++++-------- 6 files changed, 199 insertions(+), 51 deletions(-) create mode 100644 docs/Create-a-Module.md create mode 100644 docs/How-to-Use-TestingHelper.md create mode 100644 docs/deploying-with-testinghelper.md create mode 100644 docs/structure-of-a-module.md create mode 100644 docs/writing-tests-with-testinghelper.md 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/How-to-Use-TestingHelper.md b/docs/How-to-Use-TestingHelper.md new file mode 100644 index 0000000..1d79ca3 --- /dev/null +++ b/docs/How-to-Use-TestingHelper.md @@ -0,0 +1,8 @@ +# How to use TestingHelper + +> This documentation is under development + +- [Structure of a Module](structure-of-a-module.md) +- [Create a Module](create-a-module.md) +- [Writing tests with TestingHelper](writing-tests-with-testinghelper.md) +- [Deploying with TestingHelper](deploying-with-testinghelper.md) \ No newline at end of file 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/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/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/readme.md b/readme.md index 75d3b8f..8586b0e 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/How-to-Use-TestingHelper.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 From 5786b50e2eced56e75eec271b2f9fa06d44654d0 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 14:17:01 +0200 Subject: [PATCH 18/31] docs - set index.md as entry point --- docs/How-to-Use-TestingHelper.md | 8 -------- docs/index.md | 13 +++++++++++++ readme.md | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) delete mode 100644 docs/How-to-Use-TestingHelper.md create mode 100644 docs/index.md diff --git a/docs/How-to-Use-TestingHelper.md b/docs/How-to-Use-TestingHelper.md deleted file mode 100644 index 1d79ca3..0000000 --- a/docs/How-to-Use-TestingHelper.md +++ /dev/null @@ -1,8 +0,0 @@ -# How to use TestingHelper - -> This documentation is under development - -- [Structure of a Module](structure-of-a-module.md) -- [Create a Module](create-a-module.md) -- [Writing tests with TestingHelper](writing-tests-with-testinghelper.md) -- [Deploying with TestingHelper](deploying-with-testinghelper.md) \ No newline at end of file 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/readme.md b/readme.md index 8586b0e..156f8a8 100644 --- a/readme.md +++ b/readme.md @@ -34,7 +34,7 @@ This library will allow you to create a PowerShell Module with all the gearing f - 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/How-to-Use-TestingHelper.md) +[Information on how to use it on the Docs](docs/index.md) ## API V3 From cc725ddd28b51018eeaf1764f2147b1d0ec0c9f7 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 26 Jun 2023 14:24:48 +0200 Subject: [PATCH 19/31] Mark New-Module V2 and V1 as Obsolete --- public/New-ModuleV1.ps1 | 1 + public/New-ModuleV2.ps1 | 1 + 2 files changed, 2 insertions(+) diff --git a/public/New-ModuleV1.ps1 b/public/New-ModuleV1.ps1 index 8a6ce4a..b1b19c5 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 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 ( From b8565d182ebe77acc9fea0b1efa2e286a7f55d63 Mon Sep 17 00:00:00 2001 From: rulasg Date: Tue, 27 Jun 2023 10:42:45 +0200 Subject: [PATCH 20/31] Docs and Rename functions --- .../private/Add-ModuleSections.Asserts.ps1 | 8 +- .../private/New-ModuleV3.Tests.Helper.ps1 | 2 +- .../public/Tests-Add-moduleSectiond.ps1 | 8 +- .../public/Tests-NewModule_operations.ps1 | 2 +- docs/api-and-asserts.md | 84 +++++++++++++++++++ docs/table-of-assert-AddtoModule.md | 27 ++++++ public/New-ModuleV3.ps1 | 4 +- 7 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 docs/api-and-asserts.md create mode 100644 docs/table-of-assert-AddtoModule.md diff --git a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 index 44c9de4..2f44387 100644 --- a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -71,7 +71,7 @@ function Assert-AddReadMe{ } # AddAbout -function Assert-AddAbout{ +function Assert-AddToModuleAbout{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] @@ -355,7 +355,7 @@ function Assert-AddModuleV3{ } } -function Assert-AddTestV3 { +function Assert-AddTestAll { param( [Parameter()][string]$Path, [Parameter()][hashtable]$Expected @@ -377,7 +377,7 @@ function Assert-AddTestV3 { } # Full -function Assert-AddFull{ +function Assert-AddAll{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] @@ -388,7 +388,7 @@ function Assert-AddFull{ $Path | Assert-AddDevContainerJson $Path | Assert-AddLicense $Path | Assert-AddReadMe - $Path | Assert-AddAbout + $Path | Assert-AddToModuleAbout $Path | Assert-AddDeployScript $Path | Assert-AddReleaseScript $Path | Assert-AddSyncScript diff --git a/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 index bd56109..c5bca4b 100644 --- a/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 +++ b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 @@ -13,7 +13,7 @@ function Assert-TestModuleV3 { [Parameter()][hashtable]$Expected ) - Assert-AddTestV3 -Path $Path -Expected $Expected + Assert-AddTestAll -Path $Path -Expected $Expected } diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 index 09cf02c..8adb25d 100644 --- a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -35,7 +35,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Chain{ $result1 = $modulePath | Add-TT_ToModuleLicense | Add-TT_ToModuleAbout - $result1 | Assert-Addlicense -PassThru | Assert-AddAbout + $result1 | Assert-Addlicense -PassThru | Assert-AddToModuleAbout } function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ @@ -43,7 +43,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ New-TestingFolder -Path "folderName" $result = Add-TT_ToModuleAll -Path "./folderName" - $result | Assert-AddFull + $result | Assert-AddAll } function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ @@ -51,7 +51,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ New-TestingFolder -Path "folderName" $result = Get-Item -path "folderName" | Add-TT_ToModuleAll - $result | Assert-AddFull + $result | Assert-AddAll } function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Module{ @@ -62,7 +62,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Module{ $module = Get-Module -Name "MyModule" $result = $module | Add-TT_ToModuleAll - $result | Assert-AddFull + $result | Assert-AddAll Remove-Module -Name "MyModule" } diff --git a/TestingHelperTest/public/Tests-NewModule_operations.ps1 b/TestingHelperTest/public/Tests-NewModule_operations.ps1 index ba21ec5..d393ca1 100644 --- a/TestingHelperTest/public/Tests-NewModule_operations.ps1 +++ b/TestingHelperTest/public/Tests-NewModule_operations.ps1 @@ -45,7 +45,7 @@ function TestingHelperTest_Manual_Work_Testing{ Push-Location $modulePath Add-TT_ToModuleAll - Assert-AddFull -Path $modulePath + Assert-AddAll -Path $modulePath Pop-Location 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/table-of-assert-AddtoModule.md b/docs/table-of-assert-AddtoModule.md new file mode 100644 index 0000000..a1aedb8 --- /dev/null +++ b/docs/table-of-assert-AddtoModule.md @@ -0,0 +1,27 @@ +# Asserts + +| tag | Add-ToModule* (Add-) | New-ModuleV3 | Add-ToModuleAll (11) | Assert-AddToModule* (Assert-) | Assert-AddAll(10) | Add-ToModuleTestAll | Assert-AddTestAll | +| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------- | ----------------- | ------------------- | ----------------- | +| 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 | | | +| LaunchJson | Add-ToModuleLaunchJson | | | Assert-AddTestLaunchJson | | x | x | +| TestScript | Add-ToModuleTestScript | | | Assert-AddTestTestScript | | x | | +| SampleCode | Add-ToModuleSampleCode | AddSampleCode | x | Assert-AddSampleCodes | | | x | +| TestSampleCode | Add-ToModuleTestSampleCode | | | Assert-AddTestSampleCodes | | x | x | +| ---------------- | ------------------------------------ | --------------------------- | | ---------------------------------- | | | | +| AddModule | Add-ModuleV3 | | x | Assert-AddModuleV3 | | | | +| AddTestModule | Add-ToModuleTestModule -> Add-TestModuleV3 | | | Assert-AddTestAll | | x | | +| ---------------- | ------------------------------------ | --------------------------- | | ---------------------------------- | | | | +| All | Add-ToModuleAll | | | Assert-AddAll | | | | +| TestAll | Add-ToModuleTestAll | AddTesting | x | | | | | +| | | | | | | | | +| | | | | | | | | +| | | | | | | | | \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index fc7db55..4ace1a5 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -54,7 +54,7 @@ function New-ModuleV3 { # Add PSScriptAnalyzer workflow [Parameter()][switch]$AddPSScriptAnalyzerWorkflow, # Add testing workflow - [Parameter()][switch]$AddTestingWorkflow, + [Parameter()][switch]$AddTestWorkflow, # Add deploy workflow [Parameter()][switch]$AddDeployWorkflow ) @@ -143,7 +143,7 @@ function New-ModuleV3 { } # Add Testing - if($AddTestingWorkflow){ + if($AddTestWorkflow){ $null = Add-ToModuleTestWorkflow -Path $modulePath } From 9d436bbbc53dd4a4875d1f78228052de35e2709e Mon Sep 17 00:00:00 2001 From: rulasg Date: Tue, 27 Jun 2023 13:45:33 +0200 Subject: [PATCH 21/31] function cleaning --- .../private/Add-ModuleSections.Asserts.ps1 | 10 ++-- .../private/New-ModuleV3.Tests.Helper.ps1 | 5 +- .../Tests-New-ModuleV3-AddTestToModuleAll.ps1 | 4 +- docs/table-of-assert-AddtoModule.md | 51 ++++++++++--------- public/Add-ModuleSection.ps1 | 15 +++--- public/Add-TestModuleV3.ps1 | 6 --- 6 files changed, 46 insertions(+), 45 deletions(-) diff --git a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 index 2f44387..5263567 100644 --- a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -357,8 +357,7 @@ function Assert-AddModuleV3{ function Assert-AddTestAll { param( - [Parameter()][string]$Path, - [Parameter()][hashtable]$Expected + [Parameter()][string]$Path ) process{ @@ -368,7 +367,8 @@ function Assert-AddTestAll { $testingModuleName = $name + "Test" $testingModulePath = $path | Join-Path -ChildPath $testingModuleName - Assert-AddModuleV3 -Path $testingModulePath -Expected $Expected + Assert-AddModuleV3 -Path $testingModulePath + Assert-AddTestSampleCodes -Path $Path Assert-AddTestTestScript -Path $Path Assert-AddTestLaunchJson -Path $Path @@ -395,6 +395,10 @@ function Assert-AddAll{ $Path | Assert-AddPSScriptAnalyzerWorkflow $Path | Assert-AddTestWorkflow $Path | Assert-AddDeployWorkflow + $Path | Assert-AddSampleCodes + + $Path | Assert-AddTestAll + $Path | Assert-AddTestSampleCodes return $Passthru ? $Path : $null } diff --git a/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 index c5bca4b..bce9bc5 100644 --- a/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 +++ b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 @@ -9,11 +9,10 @@ function Get-DefaultsManifest { function Assert-TestModuleV3 { param( - [Parameter()][string]$Path, - [Parameter()][hashtable]$Expected + [Parameter()][string]$Path ) - Assert-AddTestAll -Path $Path -Expected $Expected + Assert-AddTestAll -Path $Path } diff --git a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 index 04803d5..ca88072 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 @@ -10,7 +10,7 @@ function TestingHelperTest_AddTestToModuleAll_NoParam{ $result = Add-TT_ToModuleTestAll Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestModuleV3 -Path $modulePath -Expected $param + Assert-TestModuleV3 -Path $modulePath } @@ -22,7 +22,7 @@ function TestingHelperTest_AddTestToModuleAll_Simple{ $result = Add-TT_ToModuleTestAll -Path $modulePath Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestModuleV3 -Path $modulePath -Expected $param + Assert-TestModuleV3 -Path $modulePath } diff --git a/docs/table-of-assert-AddtoModule.md b/docs/table-of-assert-AddtoModule.md index a1aedb8..6cbf69a 100644 --- a/docs/table-of-assert-AddtoModule.md +++ b/docs/table-of-assert-AddtoModule.md @@ -1,27 +1,28 @@ # Asserts -| tag | Add-ToModule* (Add-) | New-ModuleV3 | Add-ToModuleAll (11) | Assert-AddToModule* (Assert-) | Assert-AddAll(10) | Add-ToModuleTestAll | Assert-AddTestAll | -| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------- | ----------------- | ------------------- | ----------------- | -| 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 | | | -| LaunchJson | Add-ToModuleLaunchJson | | | Assert-AddTestLaunchJson | | x | x | -| TestScript | Add-ToModuleTestScript | | | Assert-AddTestTestScript | | x | | -| SampleCode | Add-ToModuleSampleCode | AddSampleCode | x | Assert-AddSampleCodes | | | x | -| TestSampleCode | Add-ToModuleTestSampleCode | | | Assert-AddTestSampleCodes | | x | x | -| ---------------- | ------------------------------------ | --------------------------- | | ---------------------------------- | | | | -| AddModule | Add-ModuleV3 | | x | Assert-AddModuleV3 | | | | -| AddTestModule | Add-ToModuleTestModule -> Add-TestModuleV3 | | | Assert-AddTestAll | | x | | -| ---------------- | ------------------------------------ | --------------------------- | | ---------------------------------- | | | | -| All | Add-ToModuleAll | | | Assert-AddAll | | | | -| TestAll | Add-ToModuleTestAll | AddTesting | x | | | | | -| | | | | | | | | -| | | | | | | | | -| | | | | | | | | \ No newline at end of file +| 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-TestModuleV3 -> Assert-AddTestAll | | x | | +| ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | +| All | Add-ToModuleAll | | Original | Assert-AddAll | | | | +| | | | | | | | | +| | | | | | | | | +| | | | | | | | | \ No newline at end of file diff --git a/public/Add-ModuleSection.ps1 b/public/Add-ModuleSection.ps1 index 8682b5e..416b5b2 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ModuleSection.ps1 @@ -366,8 +366,7 @@ function Add-ToModuleTestModule{ if(!$modulePath){return $null} # Test Module - # TODO : Not sure how to implemente the Forice flag with Add full modules - $testModulePath = Add-TestModuleV3 -Path $Path -AddSampleCode + $testModulePath = Add-TestModuleV3 -Path $Path -Force:$Force if (!$testModulePath) { $name = Get-ModuleName -Path $Path @@ -400,15 +399,17 @@ function Add-ToModuleTestAll{ if(!$modulePath){return $null} # Test Module - # TODO : Not sure how to implemente the Forice flag with Add full modules - $testModulePath = Add-TestModuleV3 -Path $Path -AddSampleCode - - if (!$testModulePath) { + $result = Add-ToModuleTestModule -Path $modulePath -Force:$Force + + if (!$result) { $name = Get-ModuleName -Path $Path Write-Error -Message ("Error creating Testing for Module [$Name].") return $null } + # Sample Code + $null = Add-ToModuleTestSampleCode -Path $modulePath -Force:$Force + # Add test.ps1 $null = Add-ToModuleTestScript -Path $modulePath -Force:$Force @@ -434,6 +435,8 @@ function Add-ToModuleAll{ $null = $Path | Add-ToModuleTestAll -Force:$Force + $null = $Path | Add-ToModuleSampleCode -Force:$Force + $null = $Path | Add-ToModuleDevContainerJson -Force:$Force $null = $Path | Add-ToModuleLicense -Force:$Force $null = $Path | Add-ToModuleReadme -Force:$Force diff --git a/public/Add-TestModuleV3.ps1 b/public/Add-TestModuleV3.ps1 index b64c116..6d6361d 100644 --- a/public/Add-TestModuleV3.ps1 +++ b/public/Add-TestModuleV3.ps1 @@ -3,7 +3,6 @@ function Add-TestModuleV3 { Param ( [Parameter(Mandatory)][string]$Path, - [Parameter()][switch]$AddSampleCode, [Parameter()][switch]$Force ) @@ -31,11 +30,6 @@ function Add-TestModuleV3 { Write-Error -Message ("Error creating the module [$testingModuleName].") return $null } - - # AddSampleCode - if ($AddSampleCode) { - $null = Add-ToModuleTestSampleCode -Path $Path -Force:$Force - } return $result From a8c9c1143ec6d59f87d95ed68fbaca0a3e46b12e Mon Sep 17 00:00:00 2001 From: rulasg Date: Tue, 27 Jun 2023 14:18:25 +0200 Subject: [PATCH 22/31] Pass[86] --- .../private/Add-ModuleSections.Asserts.ps1 | 24 ++++++++++++++++++- .../private/ImportModule.Tests.Helper.ps1 | 7 ++++++ .../private/New-ModuleV3.Tests.Helper.ps1 | 10 -------- .../Tests-New-ModuleV3-AddTestToModuleAll.ps1 | 4 ++-- .../public/Tests-New-ModuleV3.ps1 | 2 +- .../public/Tests-NewModule_operations.ps1 | 4 ++-- TestingHelperTest/public/Tests-test.ps1 | 17 +++++++------ docs/table-of-assert-AddtoModule.md | 2 +- 8 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 TestingHelperTest/private/ImportModule.Tests.Helper.ps1 diff --git a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 index 5263567..3651485 100644 --- a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -355,9 +355,31 @@ function Assert-AddModuleV3{ } } + +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()][string]$Path + [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] + [Alias("PSPath")][ValidateNotNullOrEmpty()] + [string] $Path ) process{ 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/New-ModuleV3.Tests.Helper.ps1 b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 index bce9bc5..a601346 100644 --- a/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 +++ b/TestingHelperTest/private/New-ModuleV3.Tests.Helper.ps1 @@ -6,13 +6,3 @@ function Get-DefaultsManifest { } - -function Assert-TestModuleV3 { - param( - [Parameter()][string]$Path - ) - - Assert-AddTestAll -Path $Path - -} - diff --git a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 index ca88072..9bf5c17 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 @@ -10,7 +10,7 @@ function TestingHelperTest_AddTestToModuleAll_NoParam{ $result = Add-TT_ToModuleTestAll Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestModuleV3 -Path $modulePath + Assert-AddTestModuleV3 -Path $modulePath } @@ -22,7 +22,7 @@ function TestingHelperTest_AddTestToModuleAll_Simple{ $result = Add-TT_ToModuleTestAll -Path $modulePath Assert-AreEqualPath -Expected $modulePath -Presented $result - Assert-TestModuleV3 -Path $modulePath + Assert-AddTestModuleV3 -Path $modulePath } diff --git a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 index 4cc1722..8ce92a6 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -55,7 +55,7 @@ function TestingHelperTest_NewModuleV3_AddTesting{ Assert-AddModuleV3 -Path $modulePath Assert-AddSampleCodes -Path $modulePath - Assert-TestModuleV3 -Path $modulePath + Assert-AddTestModuleV3 -Path $modulePath Assert-AddTestSampleCodes -Path $modulePath } diff --git a/TestingHelperTest/public/Tests-NewModule_operations.ps1 b/TestingHelperTest/public/Tests-NewModule_operations.ps1 index d393ca1..6a8c4a9 100644 --- a/TestingHelperTest/public/Tests-NewModule_operations.ps1 +++ b/TestingHelperTest/public/Tests-NewModule_operations.ps1 @@ -17,7 +17,7 @@ function TestingHelperTest_Manual_Work_StepsToFull{ # Add Testing to module Add-TT_ToModuleTestModule - Assert-TestModuleV3 -Path $modulePath + Assert-AddTestModuleV3 -Path $modulePath # Add Test Sample code Add-TT_ToModuleTestSampleCode @@ -53,6 +53,6 @@ function TestingHelperTest_Manual_Work_Testing{ $result = Invoke-TT_TestingHelper -Path $modulePath - Assert-AreEqual -Expected 2 -Presented $result.Test + 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-test.ps1 b/TestingHelperTest/public/Tests-test.ps1 index c12710a..4c20bac 100644 --- a/TestingHelperTest/public/Tests-test.ps1 +++ b/TestingHelperTest/public/Tests-test.ps1 @@ -14,18 +14,21 @@ function TestingHelperTest_TestPS1{ Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName - Remove-Module -Name "ModuleName" -Force + 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 - Assert-AreEqual -Expected "ModuleName" -Presented $result.Name - Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule - Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName + $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 - Remove-Module -Name "ModuleName" -Force } \ No newline at end of file diff --git a/docs/table-of-assert-AddtoModule.md b/docs/table-of-assert-AddtoModule.md index 6cbf69a..720a1a7 100644 --- a/docs/table-of-assert-AddtoModule.md +++ b/docs/table-of-assert-AddtoModule.md @@ -20,7 +20,7 @@ | 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-TestModuleV3 -> Assert-AddTestAll | | x | | +| AddTestModule | Add-ToModuleTestModule -> Add-TestModuleV3 | | Add-ToModuleTestAll | Assert-AddTestModuleV3 | | x | | | ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | | All | Add-ToModuleAll | | Original | Assert-AddAll | | | | | | | | | | | | | From a80bf82f85cf2130393d348d06482b6fd0286105 Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 09:30:42 +0200 Subject: [PATCH 23/31] Pass[86] --- .../public/Tests-Add-moduleSectiond.ps1 | 17 +-- .../Tests-New-ModuleV3-AddTestToModuleAll.ps1 | 4 +- private/Add-ToModule.Helper.ps1 | 34 ++++++ ...Add-ModuleSection.ps1 => Add-ToModule.ps1} | 115 ++++++++---------- public/New-ModuleV3.ps1 | 12 +- 5 files changed, 99 insertions(+), 83 deletions(-) create mode 100644 private/Add-ToModule.Helper.ps1 rename public/{Add-ModuleSection.ps1 => Add-ToModule.ps1} (79%) diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 index 8adb25d..7ca088c 100644 --- a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -1,7 +1,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_NewModuleV3{ - $result = New-TT_ModuleV3 -Name "MyModule" | Add-TT_ToModuleLicense + $result = New-TT_ModuleV3 -Name "MyModule" | Add-TT_ToModuleLicense -PassThru $result | Assert-Addlicense } @@ -10,7 +10,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Folder{ New-TestingFolder -Path "folderName" - $result = Get-Item -path "folderName" | Add-TT_ToModuleLicense + $result = Get-Item -path "folderName" | Add-TT_ToModuleLicense -PassThru $result | Assert-Addlicense } @@ -22,7 +22,7 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Module{ Import-Module -Name $modulePath $module = Get-Module -Name $moduleName - $result = $module | Add-TT_ToModuleLicense + $result = $module | Add-TT_ToModuleLicense -PassThru $result | Assert-Addlicense @@ -33,16 +33,17 @@ function TestingHelperTest_AddModuleSection_PipeCalls_Chain{ $modulePath = New-TT_ModuleV3 -Name "MyModule" - $result1 = $modulePath | Add-TT_ToModuleLicense | Add-TT_ToModuleAbout + $result = $modulePath | Add-TT_ToModuleLicense -PassThru | Add-TT_ToModuleAbout - $result1 | Assert-Addlicense -PassThru | Assert-AddToModuleAbout + $result | Assert-Addlicense + $result | Assert-AddToModuleAbout } function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ New-TestingFolder -Path "folderName" - $result = Add-TT_ToModuleAll -Path "./folderName" + $result = Add-TT_ToModuleAll -PassThru -Path "./folderName" $result | Assert-AddAll } @@ -50,7 +51,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_GetItem{ New-TestingFolder -Path "folderName" - $result = Get-Item -path "folderName" | Add-TT_ToModuleAll + $result = Get-Item -path "folderName" | Add-TT_ToModuleAll -PassThru $result | Assert-AddAll } @@ -61,7 +62,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Module{ Import-Module -Name $modulePath $module = Get-Module -Name "MyModule" - $result = $module | Add-TT_ToModuleAll + $result = $module | Add-TT_ToModuleAll -PassThru $result | Assert-AddAll Remove-Module -Name "MyModule" diff --git a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 index 9bf5c17..fc7ef54 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3-AddTestToModuleAll.ps1 @@ -7,7 +7,7 @@ function TestingHelperTest_AddTestToModuleAll_NoParam{ $modulePath | Set-Location # Should find module on current location and add test to it - $result = Add-TT_ToModuleTestAll + $result = Add-TT_ToModuleTestAll -Passthru Assert-AreEqualPath -Expected $modulePath -Presented $result Assert-AddTestModuleV3 -Path $modulePath @@ -19,7 +19,7 @@ function TestingHelperTest_AddTestToModuleAll_Simple{ $moduleName = "MyModule" $modulePath = New-ModuleV3 -Name $moduleName - $result = Add-TT_ToModuleTestAll -Path $modulePath + $result = Add-TT_ToModuleTestAll -Path $modulePath -Passthru Assert-AreEqualPath -Expected $modulePath -Presented $result Assert-AddTestModuleV3 -Path $modulePath diff --git a/private/Add-ToModule.Helper.ps1 b/private/Add-ToModule.Helper.ps1 new file mode 100644 index 0000000..3e93d90 --- /dev/null +++ b/private/Add-ToModule.Helper.ps1 @@ -0,0 +1,34 @@ + +# 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 + + } + } else { + return $null + } +} + +# 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/public/Add-ModuleSection.ps1 b/public/Add-ToModule.ps1 similarity index 79% rename from public/Add-ModuleSection.ps1 rename to public/Add-ToModule.ps1 index 416b5b2..7f5bb1b 100644 --- a/public/Add-ModuleSection.ps1 +++ b/public/Add-ToModule.ps1 @@ -7,39 +7,14 @@ -function ReturnValue($Path,$Force){ - # create object with the two parameters as properties - return [pscustomobject]@{ - Path = $Path - Force = $Force - } -} - -# 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 -} - function Add-ToModuleSampleCode{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -53,12 +28,10 @@ function Add-ToModuleSampleCode{ $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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleSampleCode - - # Add devcontainer.json file function Add-ToModuleDevContainerJson{ [CmdletBinding(SupportsShouldProcess)] @@ -66,7 +39,8 @@ function Add-ToModuleDevContainerJson{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -75,7 +49,7 @@ function Add-ToModuleDevContainerJson{ $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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleDevContainerJson @@ -87,7 +61,8 @@ function Add-ToModuleLicense{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -95,7 +70,7 @@ function Add-ToModuleLicense{ Import-Template -Force:$Force -Path $Path -File "LICENSE" -Template "template.LICENSE.txt" - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleLicense @@ -106,7 +81,8 @@ function Add-ToModuleReadme{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -120,7 +96,7 @@ function Add-ToModuleReadme{ "_MODULE_DESCRIPTION_" = ($moduleManifest.Description ?? "A powershell module that will hold Powershell functionality.") } - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleReadme @@ -131,7 +107,8 @@ function Add-ToModuleAbout{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -149,7 +126,7 @@ function Add-ToModuleAbout{ } - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleAbout @@ -160,7 +137,8 @@ function Add-ToModuleDeployScript{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -169,7 +147,7 @@ function Add-ToModuleDeployScript{ 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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleDeployScript @@ -181,7 +159,8 @@ function Add-ToModuleReleaseScript{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -189,7 +168,7 @@ function Add-ToModuleReleaseScript{ Import-Template -Force:$Force -Path $Path -File "release.ps1" -Template "template.v3.release.ps1" - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleReleaseScript @@ -200,7 +179,8 @@ function Add-ToModuleSyncScript{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -209,7 +189,7 @@ function Add-ToModuleSyncScript{ 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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleSyncScript @@ -221,7 +201,8 @@ function Add-ToModulePSScriptAnalyzerWorkflow{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -230,7 +211,7 @@ function Add-ToModulePSScriptAnalyzerWorkflow{ $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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModulePSScriptAnalyzerWorkflow @@ -241,7 +222,8 @@ function Add-ToModuleTestWorkflow{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -250,7 +232,7 @@ function Add-ToModuleTestWorkflow{ $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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleTestWorkflow @@ -261,7 +243,8 @@ function Add-ToModuleDeployWorkflow{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -270,7 +253,7 @@ function Add-ToModuleDeployWorkflow{ $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 + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleDeployWorkflow @@ -285,7 +268,8 @@ function Add-ToModuleTestScript{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -293,7 +277,7 @@ function Add-ToModuleTestScript{ Import-Template -Force:$Force -Path $Path -File "test.ps1" -Template "template.v3.test.ps1" - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleTestScript @@ -305,7 +289,8 @@ function Add-ToModuleLaunchJson{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -314,7 +299,7 @@ function Add-ToModuleLaunchJson{ Import-Template -Force:$Force -Path $destination -File "launch.json" -Template "template.launch.json" - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleLaunchJson @@ -325,7 +310,8 @@ function Add-ToModuleTestSampleCode{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -341,7 +327,7 @@ function Add-ToModuleTestSampleCode{ '_MODULE_TESTED_' = $ModuleName } - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleTestSampleCode @@ -355,7 +341,8 @@ function Add-ToModuleTestModule{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process { @@ -374,7 +361,7 @@ function Add-ToModuleTestModule{ return $null } - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleTestModule @@ -387,7 +374,8 @@ function Add-ToModuleTestAll{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -399,7 +387,7 @@ function Add-ToModuleTestAll{ if(!$modulePath){return $null} # Test Module - $result = Add-ToModuleTestModule -Path $modulePath -Force:$Force + $result = Add-ToModuleTestModule -Path $modulePath -Force:$Force -Passthru if (!$result) { $name = Get-ModuleName -Path $Path @@ -416,7 +404,7 @@ function Add-ToModuleTestAll{ # Add launch.json $null = Add-ToModuleLaunchJson -Path $modulePath -Force:$Force - return ReturnValue -Path $modulePath -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } } Export-ModuleMember -Function Add-ToModuleTestAll @@ -427,7 +415,8 @@ function Add-ToModuleAll{ [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, - [Parameter()][switch]$Force + [Parameter(ValueFromPipelineByPropertyName)][switch]$Force, + [Parameter(ValueFromPipelineByPropertyName)][Switch]$Passthru ) process{ @@ -452,7 +441,7 @@ function Add-ToModuleAll{ $null = $Path | Add-ToModuleTestWorkflow -Force:$Force $null = $Path | Add-ToModuleDeployWorkflow -Force:$Force - return ReturnValue -Path $Path -Force:$Force + return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 4ace1a5..0024414 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -88,18 +88,10 @@ function New-ModuleV3 { } } + # Add Testing if ($AddTesting) { - $result = Add-ToModuleTestAll -Path $modulePath - - # Check if the module was created - if(! $result){ - return $null - } + $null = Add-ToModuleTestAll -Path $modulePath -Passthru - # Add Sample Code - if($AddSampleCode){ - $null = Add-ToModuleTestSampleCode -Path $modulePath - } } # Add devcontainer.json file From f142e79c5fc085d7a436293d2a698a713c4f9e84 Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 09:37:41 +0200 Subject: [PATCH 24/31] Remove Passthru for Assert-Add* functions --- .../private/Add-ModuleSections.Asserts.ps1 | 83 ++++--------------- 1 file changed, 16 insertions(+), 67 deletions(-) diff --git a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 index 3651485..653b549 100644 --- a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -18,13 +18,10 @@ function Assert-AddDevContainerJson{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ Assert-ItemExist -Path ($Path | Join-Path -ChildPath ".devcontainer" | Join-Path -ChildPath "devcontainer.json") -Comment "devcontainer.json" - - return $Passthru ? $Path : $null } } @@ -34,13 +31,10 @@ function Assert-AddLicense{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ Assert-ItemExist -Path ($Path | Join-Path -ChildPath "LICENSE") -Comment "LICENSE" - - return $Passthru ? $Path : $null } } @@ -49,8 +43,7 @@ function Assert-AddReadMe{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ $name = $Path | Split-Path -LeafBase @@ -65,8 +58,6 @@ function Assert-AddReadMe{ $expectedDescription = $manifest.Description ?? "A powershell module that will hold Powershell functionality." Assert-Contains -Expected $expectedDescription -Presented $content -Comment "README.md contains module description" } - - return $Passthru ? $Path : $null } } @@ -75,8 +66,7 @@ function Assert-AddToModuleAbout{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ @@ -95,8 +85,6 @@ function Assert-AddToModuleAbout{ 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" } - - return $Passthru ? $Path : $null } } @@ -105,14 +93,11 @@ function Assert-AddDeployScript{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [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" - - return $Passthru ? $Path : $null } } @@ -121,13 +106,10 @@ function Assert-AddReleaseScript{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ Assert-ItemExist -Path ($Path | Join-Path -ChildPath "release.ps1") -Comment "release.ps1" - - return $Passthru ? $Path : $null } } @@ -136,14 +118,11 @@ function Assert-AddSyncScript{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [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" - - return $Passthru ? $Path : $null } } @@ -153,14 +132,11 @@ function Assert-AddPSScriptAnalyzerWorkflow{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" Assert-ItemExist -Path ($destination | Join-Path -ChildPath "powershell.yml") -Comment "powershell.yml" - - return $Passthru ? $Path : $null } } @@ -170,14 +146,11 @@ function Assert-AddTestWorkflow{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [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" - - return $Passthru ? $Path : $null } } @@ -187,14 +160,11 @@ function Assert-AddDeployWorkflow{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [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" - - return $Passthru ? $Path : $null } } @@ -203,14 +173,11 @@ function Assert-AddSampleCodes{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [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" - - return $Passthru ? $Path : $null } } @@ -219,8 +186,7 @@ function Assert-AddTestSampleCodes{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ $name = $Path | Split-Path -LeafBase @@ -229,8 +195,6 @@ function Assert-AddTestSampleCodes{ $samplePublicPath = $testingModulePath | Join-Path -ChildPath "public" -AdditionalChildPath SampleFunctionTests.ps1 Assert-ItemExist -Path $samplePublicPath - - return $Passthru ? $Path : $null } } @@ -239,8 +203,7 @@ function Assert-AddTestLaunchJson{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ $launchFile = $Path | Join-Path -ChildPath ".vscode" -AdditionalChildPath "launch.json" @@ -258,8 +221,6 @@ function Assert-AddTestLaunchJson{ 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 '') - - return $Passthru ? $Path : $null } } @@ -268,16 +229,13 @@ function Assert-AddTestTestScript{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ $testps1Path = $Path | Join-Path -ChildPath "test.ps1" Assert-ItemExist -Path $testps1Path -Comment "test.ps1 exists" - - return $Passthru ? $Path : $null } } @@ -288,8 +246,7 @@ function Assert-AddModuleV3{ [Alias("PSPath")][ValidateNotNullOrEmpty()] [string] $Path, # Manifest data to check - [Parameter()][hashtable]$Expected, - [Parameter()][switch]$Passthru + [Parameter()][hashtable]$Expected ) process{ $name = $Path | Split-Path -LeafBase @@ -350,12 +307,9 @@ function Assert-AddModuleV3{ } Write-AssertionSectionEnd - - return $Passthru ? $Path : $null } } - function Assert-AddTestModuleV3{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] @@ -372,7 +326,6 @@ function Assert-AddTestModuleV3{ Assert-AddModuleV3 -Path $testingModulePath } - } function Assert-AddTestAll { @@ -395,7 +348,6 @@ function Assert-AddTestAll { Assert-AddTestTestScript -Path $Path Assert-AddTestLaunchJson -Path $Path } - } # Full @@ -403,8 +355,7 @@ function Assert-AddAll{ param( [Parameter(Mandatory,Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Alias("PSPath")][ValidateNotNullOrEmpty()] - [string] $Path, - [Parameter()][switch]$Passthru + [string] $Path ) process{ $Path | Assert-AddDevContainerJson @@ -421,8 +372,6 @@ function Assert-AddAll{ $Path | Assert-AddTestAll $Path | Assert-AddTestSampleCodes - - return $Passthru ? $Path : $null } } From 7fea33f813ec4e6e705aa14da589d72021a58868 Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 11:11:42 +0200 Subject: [PATCH 25/31] Pass[2] --- .../private/Add-ModuleSections.Asserts.ps1 | 1 + .../public/Tests-Add-moduleSectiond.ps1 | 1 + private/Add-ToModule.Helper.ps1 | 5 +- public/Add-ToModule.ps1 | 1 - public/New-ModuleV3.ps1 | 49 +++++-------------- 5 files changed, 15 insertions(+), 42 deletions(-) diff --git a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 index 653b549..9cc0667 100644 --- a/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Asserts.ps1 @@ -358,6 +358,7 @@ function Assert-AddAll{ [string] $Path ) process{ + $Path | Assert-AddDevContainerJson $Path | Assert-AddLicense $Path | Assert-AddReadMe diff --git a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 index 7ca088c..7b121db 100644 --- a/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 +++ b/TestingHelperTest/public/Tests-Add-moduleSectiond.ps1 @@ -43,6 +43,7 @@ function TestingHelperTest_AddModuleSection_FULL_PipeCalls_Folder{ New-TestingFolder -Path "folderName" + $result = Add-TT_ToModuleAll -PassThru -Path "./folderName" $result | Assert-AddAll } diff --git a/private/Add-ToModule.Helper.ps1 b/private/Add-ToModule.Helper.ps1 index 3e93d90..355ebf5 100644 --- a/private/Add-ToModule.Helper.ps1 +++ b/private/Add-ToModule.Helper.ps1 @@ -8,11 +8,8 @@ function ReturnValue($Path,$Force, $Passthru){ Path = $Path Force = $Force Passthru = $Passthru - } - } else { - return $null - } + } } # Normalize $Path and returns $null if not valid diff --git a/public/Add-ToModule.ps1 b/public/Add-ToModule.ps1 index 7f5bb1b..2ae5b0f 100644 --- a/public/Add-ToModule.ps1 +++ b/public/Add-ToModule.ps1 @@ -442,7 +442,6 @@ function Add-ToModuleAll{ $null = $Path | Add-ToModuleDeployWorkflow -Force:$Force return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru - } } Export-ModuleMember -Function Add-ToModuleAll diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 0024414..57b1318 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -83,66 +83,41 @@ function New-ModuleV3 { } # Add Sample Code - if($AddSampleCode){ - $null = Add-ToModuleSampleCode -Path $modulePath - } + if($AddSampleCode){ $modulePath | Add-ToModuleSampleCode } } # Add Testing - if ($AddTesting) { - $null = Add-ToModuleTestAll -Path $modulePath -Passthru - - } + if ($AddTesting){ $modulePath | Add-ToModuleTestAll } # Add devcontainer.json file - if($AddDevContainerJson){ - $null = Add-ToModuleDevContainerJson -Path $modulePath - } + if($AddDevContainerJson){ $modulePath | Add-ToModuleDevContainerJson } # Add License file - if($AddLicense){ - $null = Add-ToModuleLicense -Path $modulePath - } + if($AddLicense){ $modulePath | Add-ToModuleLicense } # Add Readme file - if($AddReadme){ - $null = Add-ToModuleReadme -Path $modulePath - } + if($AddReadme){ $modulePath | Add-ToModuleReadme } # Add about - if($AddAbout){ - $null = Add-ToModuleAbout -Path $modulePath - } + if($AddAbout){ $modulePath | Add-ToModuleAbout } # Add deploying - if($AddDeployScript){ - $null = Add-ToModuleDeployScript -Path $modulePath - } + if($AddDeployScript){ $modulePath | Add-ToModuleDeployScript } # Add Release - if($AddReleaseScript){ - $null = Add-ToModuleReleaseScript -Path $modulePath - } + if($AddReleaseScript){ $modulePath | Add-ToModuleReleaseScript } # Add Sync - if($AddSyncScript){ - $null = Add-ToModuleSyncScript -Path $modulePath - } + if($AddSyncScript){ $modulePath | Add-ToModuleSyncScript } # Add PSScriptAnalyzer - if($AddPSScriptAnalyzerWorkflow){ - $null = Add-ToModulePSScriptAnalyzerWorkflow -Path $modulePath - } + if($AddPSScriptAnalyzerWorkflow){ $modulePath | Add-ToModulePSScriptAnalyzerWorkflow } # Add Testing - if($AddTestWorkflow){ - $null = Add-ToModuleTestWorkflow -Path $modulePath - } + if($AddTestWorkflow){ $modulePath | Add-ToModuleTestWorkflow } # Add deploy Workflow - if($AddDeployWorkflow){ - $null = Add-ToModuledeployWorkflow -Path $modulePath - } + if($AddDeployWorkflow){ $modulePath | Add-ToModuledeployWorkflow } return $retModulePath From 4fcb85cc80c7a61226f601f01e06b8124cded10b Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 11:21:59 +0200 Subject: [PATCH 26/31] remove return value from add-ToModule --- public/Add-ToModule.ps1 | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/public/Add-ToModule.ps1 b/public/Add-ToModule.ps1 index 2ae5b0f..d0802be 100644 --- a/public/Add-ToModule.ps1 +++ b/public/Add-ToModule.ps1 @@ -396,13 +396,13 @@ function Add-ToModuleTestAll{ } # Sample Code - $null = Add-ToModuleTestSampleCode -Path $modulePath -Force:$Force + Add-ToModuleTestSampleCode -Path $modulePath -Force:$Force # Add test.ps1 - $null = Add-ToModuleTestScript -Path $modulePath -Force:$Force + Add-ToModuleTestScript -Path $modulePath -Force:$Force # Add launch.json - $null = Add-ToModuleLaunchJson -Path $modulePath -Force:$Force + Add-ToModuleLaunchJson -Path $modulePath -Force:$Force return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } @@ -422,24 +422,24 @@ function Add-ToModuleAll{ process{ $Path = NormalizePath -Path:$Path ?? return $null - $null = $Path | Add-ToModuleTestAll -Force:$Force + $Path | Add-ToModuleTestAll -Force:$Force - $null = $Path | Add-ToModuleSampleCode -Force:$Force + $Path | Add-ToModuleSampleCode -Force:$Force - $null = $Path | Add-ToModuleDevContainerJson -Force:$Force - $null = $Path | Add-ToModuleLicense -Force:$Force - $null = $Path | Add-ToModuleReadme -Force:$Force - $null = $Path | Add-ToModuleAbout -Force:$Force + $Path | Add-ToModuleDevContainerJson -Force:$Force + $Path | Add-ToModuleLicense -Force:$Force + $Path | Add-ToModuleReadme -Force:$Force + $Path | Add-ToModuleAbout -Force:$Force # scripts - $null = $Path | Add-ToModuleDeployScript -Force:$Force - $null = $Path | Add-ToModuleReleaseScript -Force:$Force - $null = $Path | Add-ToModuleSyncScript -Force:$Force + $Path | Add-ToModuleDeployScript -Force:$Force + $Path | Add-ToModuleReleaseScript -Force:$Force + $Path | Add-ToModuleSyncScript -Force:$Force # workflows - $null = $Path | Add-ToModulePSScriptAnalyzerWorkflow -Force:$Force - $null = $Path | Add-ToModuleTestWorkflow -Force:$Force - $null = $Path | Add-ToModuleDeployWorkflow -Force:$Force + $Path | Add-ToModulePSScriptAnalyzerWorkflow -Force:$Force + $Path | Add-ToModuleTestWorkflow -Force:$Force + $Path | Add-ToModuleDeployWorkflow -Force:$Force return ReturnValue -Path $Path -Force:$Force -Passthru:$Passthru } From 921f2a95643b27ca21105ffdf7a0fba286511277 Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 11:35:15 +0200 Subject: [PATCH 27/31] Supress Obsolete Warning --- TestingHelperTest/public/Tests-New-ModuleV1.ps1 | 3 +-- TestingHelperTest/public/Tests-New-ModuleV2.ps1 | 6 +++--- public/New-ModuleV1.ps1 | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/TestingHelperTest/public/Tests-New-ModuleV1.ps1 b/TestingHelperTest/public/Tests-New-ModuleV1.ps1 index 40e4ef6..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 diff --git a/TestingHelperTest/public/Tests-New-ModuleV2.ps1 b/TestingHelperTest/public/Tests-New-ModuleV2.ps1 index 832261f..34a05ec 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" -waraningAction 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" -waraningAction SilentlyContinue # Supress Obsolete Warning $test = $ModuleName | Join-Path -ChildPath "test.ps1" | Resolve-Path diff --git a/public/New-ModuleV1.ps1 b/public/New-ModuleV1.ps1 index b1b19c5..02b74f0 100644 --- a/public/New-ModuleV1.ps1 +++ b/public/New-ModuleV1.ps1 @@ -56,7 +56,7 @@ function New-ModuleV1 { # Testing module if (-Not $AvoidTestFile) { - New-TestingModule -Path $modulePath -ModuleName $ModuleName + New-TestingModule -Path $modulePath -ModuleName $ModuleName -warningAction SilentlyContinue # Supress Obsolete Warning New-TestingVsCodeLaunchJsonV1 -Path $modulePath -ModuleName $ModuleName } @@ -130,7 +130,7 @@ 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-TestingVsCodeLaunchJsonV1($Path, $ModuleName){ From 31ca19a8f0f7a4696d713b36fa0e9a1a6e59db8d Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 11:38:12 +0200 Subject: [PATCH 28/31] Fix type WarningAction --- TestingHelperTest/public/Tests-New-ModuleV2.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TestingHelperTest/public/Tests-New-ModuleV2.ps1 b/TestingHelperTest/public/Tests-New-ModuleV2.ps1 index 34a05ec..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" -waraningAction SilentlyContinue # Supress Obsolete Warning + 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" -waraningAction SilentlyContinue # Supress Obsolete Warning + 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 From 862f2af6ed81b5bd108ce9cbed1e8ac20b0e1adb Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 12:07:45 +0200 Subject: [PATCH 29/31] New-ModuleV3 -AddAll --- TestingHelperTest/public/Tests-New-ModuleV3.ps1 | 14 ++++++++++++++ docs/table-of-assert-AddtoModule.md | 2 +- public/New-ModuleV3.ps1 | 9 +++++++++ test.ps1 | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 index 8ce92a6..b67a1b6 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -59,3 +59,17 @@ function TestingHelperTest_NewModuleV3_AddTesting{ Assert-AddTestSampleCodes -Path $modulePath } +function TestingHelperTest_NewModuleV3_AddAll{ + + $moduleName = "MyModule" + $path = '.' + $modulePath = $path | Join-Path -ChildPath $moduleName + + $result = New-TT_ModuleV3 -Name $moduleName -AddAll + + Assert-AreEqualPath -Expected $modulePath -Presented $result + + Assert-AddAll -Path $modulePath + +} + diff --git a/docs/table-of-assert-AddtoModule.md b/docs/table-of-assert-AddtoModule.md index 720a1a7..13a2439 100644 --- a/docs/table-of-assert-AddtoModule.md +++ b/docs/table-of-assert-AddtoModule.md @@ -22,7 +22,7 @@ | ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | | AddTestModule | Add-ToModuleTestModule -> Add-TestModuleV3 | | Add-ToModuleTestAll | Assert-AddTestModuleV3 | | x | | | ---------------- | ------------------------------------------ | --------------------------- | -------------------- | ---------------------------------------- | ----------------- | ------------------- | ----------------- | -| All | Add-ToModuleAll | | Original | Assert-AddAll | | | | +| All | Add-ToModuleAll | AddAll | Original | Assert-AddAll | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | \ No newline at end of file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 57b1318..05cd6e2 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -33,6 +33,8 @@ function New-ModuleV3 { [Parameter()][string]$ModuleVersion, # Path where the module will be created. Default is current folder [Parameter()][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 @@ -86,6 +88,13 @@ function New-ModuleV3 { if($AddSampleCode){ $modulePath | Add-ToModuleSampleCode } } + # Add All + if($AddAll){ + $modulePath | Add-ToModuleAll + + return $retModulePath + } + # Add Testing if ($AddTesting){ $modulePath | Add-ToModuleTestAll } diff --git a/test.ps1 b/test.ps1 index 2267cb5..9497236 100644 --- a/test.ps1 +++ b/test.ps1 @@ -49,5 +49,5 @@ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file # Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_AddTestToModuleAll_Simple # Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddModule_DefaultManifest -# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_Manual* +# Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors -TestName TestingHelperTest_NewModuleV3_AddAll Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors From fe1d2fb6d319291c139418fc6081e2e2286ec736 Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 13:02:23 +0200 Subject: [PATCH 30/31] New-ModuleV3 paramset --- TestingHelperTest/public/Tests-New-ModuleV3.ps1 | 3 ++- public/Add-ModuleV3.ps1 | 2 +- public/New-ModuleV3.ps1 | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 index b67a1b6..b7d97fe 100644 --- a/TestingHelperTest/public/Tests-New-ModuleV3.ps1 +++ b/TestingHelperTest/public/Tests-New-ModuleV3.ps1 @@ -34,7 +34,8 @@ function TestingHelperTest_NewModuleV3_WithOutName { # 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 diff --git a/public/Add-ModuleV3.ps1 b/public/Add-ModuleV3.ps1 index e2520b0..afe4030 100644 --- a/public/Add-ModuleV3.ps1 +++ b/public/Add-ModuleV3.ps1 @@ -36,7 +36,7 @@ function Add-ModuleV3 { $null = New-Item -ItemType Directory -Force -Path ($modulePath | Join-Path -ChildPath "private") # PSD1 - $psd1Path = ($modulePath | Join-Path -ChildPath "$Name.psd1") + $psd1Path = ($modulePath | Join-Path -ChildPath "$moduleName.psd1") try { # Create the PSD1 file diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index 05cd6e2..69a3005 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -24,15 +24,17 @@ 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 @@ -66,6 +68,11 @@ function New-ModuleV3 { $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 From 896ba75dc5131aef44c7ac6f93b37401015e018c Mon Sep 17 00:00:00 2001 From: rulasg Date: Wed, 28 Jun 2023 13:40:03 +0200 Subject: [PATCH 31/31] TestingHelperTest_Manual_Work_Testing --- .../public/Tests-NewModule_operations.ps1 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/TestingHelperTest/public/Tests-NewModule_operations.ps1 b/TestingHelperTest/public/Tests-NewModule_operations.ps1 index 6a8c4a9..2fdd3c4 100644 --- a/TestingHelperTest/public/Tests-NewModule_operations.ps1 +++ b/TestingHelperTest/public/Tests-NewModule_operations.ps1 @@ -51,8 +51,19 @@ function TestingHelperTest_Manual_Work_Testing{ # Run tests - $result = Invoke-TT_TestingHelper -Path $modulePath + $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