Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/powershell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
# The below set up runs PSScriptAnalyzer to your entire repository and runs some basic security rules.
path: .\
recurse: true
severity: 'Error'
severity: "Error"
# Include your own basic security rules. Removing this option will run all the rules
# includeRule: '"PSAvoidGlobalAliases", "PSAvoidUsingConvertToSecureStringWithPlainText"'
output: results.sarif
Expand All @@ -45,4 +45,4 @@ jobs:
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif
sarif_file: results.sarif
6 changes: 3 additions & 3 deletions TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ function Assert-AddModuleV3 {

# Deploy
if ($AddDeployScript) {
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy.ps1") -Comment "Deploy-Module.ps1"
Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy-helper.ps1") -Comment "Deploy-Module.ps1"
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
Expand All @@ -94,7 +94,7 @@ function Assert-AddModuleV3 {
# PSScriptAnalyzer
if ($AddPSScriptAnalyzerWorkflow) {
$destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows"
Assert-ItemExist -Path ($destination | Join-Path -ChildPath "PSScriptAnalyzer.yml") -Comment "PSScriptAnalyzer.yml"
Assert-ItemExist -Path ($destination | Join-Path -ChildPath "powershell.yml") -Comment "powershell.yml"
}

# TestingWorkflow
Expand Down
2 changes: 1 addition & 1 deletion TestingHelperTest/public/Tests-deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ $SCRITPBLOCK_WITHNOEXCEPTION = {
return 0
}
}
$EXCEPTION_MESSAGE = 'Some throw exception comming from Deploy-Module Injection'
$EXCEPTION_MESSAGE = 'Some exception message thown on Invoke-DeployModule injected function'
$SCRITPBLOCK_WITHEXCEPTION = {

function Invoke-DeployModule {
Expand Down
4 changes: 2 additions & 2 deletions deploy-helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Invoke-DeployModuleToPSGallery{
}

# Deploy the module with ShouldProcess (-whatif, -confirm)
if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) {
if ($PSCmdlet.ShouldProcess($psdPath, "Invoke-DeployModule")) {
"Deploying {0} {1} {2} to PSGallery ..." -f $($psd1.RootModule), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) | Write-Information
# During testing we should use -WhatIf paarmetre when calling for deploy.
# Just reach this point when testing call failure
Expand Down Expand Up @@ -159,4 +159,4 @@ function Get-DeployModuleManifestPath {
} else {
$psdPath
}
}
}
1 change: 0 additions & 1 deletion deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) {

# Deploy module to PSGallery
Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force

48 changes: 48 additions & 0 deletions deploy_module_on_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ jobs:
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif

4 changes: 2 additions & 2 deletions private/templates/template.deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ if ($Force -and -not $Confirm){
}

# Deploy the module with ShouldProcess (-whatif, -confirm)
if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) {
if ($PSCmdlet.ShouldProcess($psdPath, "Publish-Module")) {
$message ="Deploying {0} {1} {2} to PSGallery ..." -f $($psdPath.Name), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease)
# show an empty line
Write-Information -InformationAction Continue -Message ""
Write-Information -InformationAction Continue -Message $message
Deploy-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy
Publish-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy
}
2 changes: 1 addition & 1 deletion private/templates/template.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ function Import-TestingHelper{
Import-TestingHelper -AllowPrerelease

# Run test by PSD1 file
Invoke-TestingHelper
Test-ModulelocalPSD1
6 changes: 3 additions & 3 deletions private/templates/template.v3.deploy-helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function Invoke-DeployModuleToPSGallery{
}

# Deploy the module with ShouldProcess (-whatif, -confirm)
if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) {
if ($PSCmdlet.ShouldProcess($psdPath, "Invoke-DeployModule")) {
"Deploying {0} {1} {2} to PSGallery ..." -f $($psd1.RootModule), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) | Write-Information
# During testing we should use -WhatIf paarmetre when calling for deploy.
# Just reach this point when testing call failure
Expand Down Expand Up @@ -107,7 +107,7 @@ function Invoke-DeployModule {
Force = $Force
}

Deploy-Module @parameters
Publish-Module @parameters

if($?){
Write-Information -MessageData "Deployed module [$Name] to PSGallery"
Expand Down Expand Up @@ -159,4 +159,4 @@ function Get-DeployModuleManifestPath {
} else {
$psdPath
}
}
}
3 changes: 1 addition & 2 deletions private/templates/template.v3.deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,4 @@ if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) {
}

# Deploy module to PSGallery
Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force

Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force
46 changes: 46 additions & 0 deletions private/templates/template.v3.powershell.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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
17 changes: 12 additions & 5 deletions private/templates/template.v3.sync-helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ function Out-ContentToFile {
[Parameter(Mandatory=$true)][string]$filePath
)

if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) {
$content | Out-File -FilePath $filePath -Force
process{
if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) {
$content | Out-File -FilePath $filePath -Force
}
}
}

Expand All @@ -44,7 +46,12 @@ function Save-UrlContentToFile {
)

$fileContent = Get-UrlContent -Url $url
$fileContent | Out-ContentToFile -FilePath $filePath

Write-Information -MessageData "Saved content to [$filePath] from [$url]"
}
if ([string]::IsNullOrWhiteSpace($fileContent)) {
Write-Error -Message "Content from [$url] is empty"
return
} else {
$fileContent | Out-ContentToFile -FilePath $filePath
Write-Information -MessageData "Saved content to [$filePath] from [$url]"
}
}
24 changes: 16 additions & 8 deletions private/templates/template.v3.sync.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
<#
.SYNOPSIS
Synchronizes TestingHelper templates files
Synchronizes with TestingHelper templates files

.DESCRIPTION
Synchronizes TestingHelper templates to the local repo.
Synchronizes with TestingHelper templates to the local repo.
TestingHelper uses templates to create a new module.
This script will update the local module with the latest templates.
.LINK
https://raw.githubusercontent.com/rulasg/DemoPsModule/main/sync.ps1
https://raw.githubusercontent.com/rulasg/TestingHelper/main/sync.ps1
#>

[cmdletbinding()]
[cmdletbinding(SupportsShouldProcess)]
param()

. ($PSScriptRoot | Join-Path -ChildPath "sync-helper.ps1")

Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/test.ps1' -FilePath 'test.ps1'
Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/release.ps1' -FilePath 'release.ps1'
Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy.ps1' -FilePath 'deploy.ps1'
Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy-helper.ps1' -FilePath 'deploy-helper.ps1'
Save-UrlContentToFile -FilePath 'deploy_module_on_release.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy_module_on_release.yml'
Save-UrlContentToFile -FilePath 'powershell.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.powershell.yml'
Save-UrlContentToFile -FilePath 'test_with_TestingHelper.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test_with_TestingHelper.yml'

Save-UrlContentToFile -FilePath 'deploy-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy-helper.ps1'
Save-UrlContentToFile -FilePath 'deploy.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.ps1'

Save-UrlContentToFile -FilePath 'sync-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync-helper.ps1'
Save-UrlContentToFile -FilePath 'sync.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.ps1'

Save-UrlContentToFile -FilePath 'release.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1'
Save-UrlContentToFile -FilePath 'test.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1'
50 changes: 50 additions & 0 deletions private/templates/template.v3.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<#
.SYNOPSIS
Run tests
.DESCRIPTION
Run the unit test of the actual module
.NOTES
Using TestingHelper this script will search for a Test module and run the tests
This script will be referenced from launch.json to run the tests on VSCode
.LINK
https://raw.githubusercontent.com/rulasg/DemoPsModule/main/test.ps1
.EXAMPLE
> ./test.ps1
#>

[CmdletBinding()]
param (
#Switch ShowTestErrors
[Parameter()][switch]$ShowTestErrors
)

function Import-TestingHelper{
[CmdletBinding()]
param (
[Parameter()][string]$Version,
[Parameter()][switch]$AllowPrerelease,
[Parameter()][switch]$PassThru
)

if ($Version) {
$V = $Version.Split('-')
$semVer = $V[0]
$AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1]))
}

$module = Import-Module TestingHelper -PassThru -ErrorAction SilentlyContinue -RequiredVersion:$semVer

if ($null -eq $module) {
$installed = Install-Module -Name TestingHelper -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$Version
$module = Import-Module -Name $installed.Name -RequiredVersion ($installed.Version.Split('-')[0]) -Force -PassThru
}

if ($PassThru) {
$module
}
}

Import-TestingHelper -AllowPrerelease

# Run test by PSD1 file
Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors
37 changes: 15 additions & 22 deletions public/Invoke-TestingHelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,15 @@ function Test-Module {
$result | Add-Member -NotePropertyName "TestsName" -NotePropertyValue $functionsTestName
$result | Add-Member -NotePropertyName "Tests" -NotePropertyValue $functionsTest.Length
$result | Add-Member -NotePropertyName "Time" -NotePropertyValue $time

# Display single line result
Write-Host -ForegroundColor DarkCyan
$TestingModuleName | Write-Host -ForegroundColor Green -NoNewline
" results - " | Write-Host -ForegroundColor DarkCyan -NoNewline
Out-SingleResultData -Name "Pass" -Value $result.Pass -Color "Yellow"
Out-SingleResultData -Name "Failed" -Value $result.Failed -Color "Red"
Out-SingleResultData -Name "Skipped" -Value $result.Skipped -Color "Yellow"
Out-SingleResultData -Name "NotImplemented" -Value $result.NotImplemented -Color "Red"
Write-Host -ForegroundColor DarkCyan

# Displayy all results strucutre
$result

# Save result to global variable
$global:ResultTestingHelper = $result

# Save result to global variable
$global:ResultTestingHelper = $result

# Displayy all results strucutre
$global:ResultTestingHelper | Write-Output

# Display single line result
Show-ResultSingleLine -Result $result

# unload testing module
Remove-Module -Name $TestingModuleName -Force
Expand Down Expand Up @@ -135,14 +128,14 @@ function Invoke-TestingHelper {
$result | Add-Member -NotePropertyName "Tests" -NotePropertyValue $functionsTest.Length
$result | Add-Member -NotePropertyName "Time" -NotePropertyValue $time

# Display single line result
Show-ResultSingleLine -Result $result

# Displayy all results strucutre
$result

# Save result to global variable
$global:ResultTestingHelper = $result

# Displayy all results strucutre
$global:ResultTestingHelper | Write-Output

# Display single line result
Show-ResultSingleLine -Result $result

# unload testing module
Remove-Module -Name $TestingModuleName -Force
Expand Down
Loading