Skip to content

Commit 5da6ddc

Browse files
committed
βœ…πŸ”¨ Refactor dev scripts and tests
1 parent 4643a61 commit 5da6ddc

11 files changed

Lines changed: 104 additions & 30 deletions

β€Žscripts/Add-Tests.ps1β€Ž

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Begin
1515
{
1616
[CmdletBinding()] Param()
1717
$Script:NL = [Environment]::NewLine
18-
$src = Join-Path ($PSScriptRoot |Split-Path) src
19-
$Script:ModuleName = Join-Path $src *.psd1 |Resolve-Path |Split-Path -LeafBase
18+
$reporoot = $PSScriptRoot |Split-Path # faster than git rev-parse --show-toplevel ?
19+
$Script:ModuleName = Join-Path $reporoot src *.psd1 |Resolve-Path |Split-Path -LeafBase
2020
if(Get-Module $Script:ModuleName) {Remove-Module $Script:ModuleName}
21-
& (Join-Path $PSScriptRoot Build-Module.ps1)
22-
Join-Path $src .publish *.psd1 |Resolve-Path |Import-Module
21+
& (Join-Path $PSScriptRoot Build-ThisModule.ps1)
22+
Join-Path $reporoot .publish *.psd1 |Resolve-Path |Import-Module
2323
Write-Debug "Imported commands: $(Get-Command -Module $Script:ModuleName)"
2424
}
2525

@@ -73,13 +73,10 @@ $('"@')
7373
Tests $Synopsis
7474
#>
7575
76-
if((Test-Path .changes -Type Leaf) -and
77-
!@(Get-Content .changes |Get-Item |Select-Object -ExpandProperty Name |
78-
Where-Object {`$_.StartsWith("`$((`$MyInvocation.MyCommand.Name -split '\.',2)[0]).")})) {return}
76+
if(!(&"`$PSScriptRoot/../scripts/Test-RelevantTest.ps1")) {return}
7977
BeforeAll {
8078
Set-StrictMode -Version Latest
81-
`$module = Join-Path (`$PSScriptRoot |Split-Path) src .publish *.psd1 |Get-Item
82-
Import-Module `$module -Force
79+
&"`$PSScriptRoot/../scripts/Import-ThisModule.ps1"
8380
}
8481
Describe '$Name' -Tag $Name,$($Name -replace '-',',') {
8582
Context '$($Synopsis -replace "'","''")' -Tag Example {
@@ -88,7 +85,7 @@ $($Examples.example |Format-ExampleTest)
8885
$($CmdInfo.ParameterSets |Where-Object Name -ne __AllParameterSets |Format-ParameterSetContext)
8986
}
9087
AfterAll {
91-
Remove-Module `$module.BaseName -Force
88+
&"$PSScriptRoot/../scripts/Remove-ThisModule.ps1"
9289
}
9390
"@
9491
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ $(Get-Content $FullName -Raw)
2727
{
2828
[CmdletBinding()] Param()
2929
$Local:OFS = [Environment]::NewLine
30-
$public = Get-Item public/*.ps1
31-
$psm1 = [path]::ChangeExtension((Resolve-Path .publish/*.psd1),'psm1')
30+
$public = Join-Path src public *.ps1 |Get-Item
31+
$psm1 = [path]::ChangeExtension((Join-Path .publish *.psd1 |Resolve-Path),'psm1')
3232
return @"
33-
$(Get-Item private/*.ps1 -ErrorAction Ignore |Format-Function)
33+
$(Join-Path src private *.ps1 |Get-Item -ErrorAction Ignore |Format-Function)
3434
$($public |Format-Function)
3535
Export-ModuleMember -Function $($public.BaseName -join ',')
3636
"@ |Out-File $psm1 utf8BOM
3737
}
3838

39-
Push-Location "$PSScriptRoot/../src"
39+
$PSScriptRoot |Split-Path |Push-Location
4040
New-Item .publish -Type Directory -ErrorAction Ignore |Out-Null
41-
Copy-Item *.psd1 .publish
41+
Copy-Item (Join-Path src *.psd1) .publish
4242
}
4343
Process
4444
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<#
2+
.SYNOPSIS
3+
Imports this repository's module, installing and importing required modules first.
4+
#>
5+
6+
#Requires -Version 7
7+
[CmdletBinding()] Param()
8+
if(!($PSScriptRoot |Split-Path |Join-Path -ChildPath .publish |Test-Path -Type Container))
9+
{
10+
$name = $PSScriptRoot |Split-Path |Join-Path -ChildPath src -AdditionalChildPath *.psd1 |Split-Path -LeafBase
11+
if(Get-Module $name) {Remove-Module $name -Force}
12+
& (Join-Path $PSScriptRoot Build-ThisModule.ps1)
13+
}
14+
$module = $PSScriptRoot |Split-Path |Join-Path -ChildPath .publish -AdditionalChildPath *.psd1 |Get-Item
15+
$manifest = Import-PowerShellDataFile $module.FullName
16+
if($manifest.PSObject.Properties.Name -contains 'RequiredModules' -and $manifest.RequiredModules)
17+
{
18+
$manifest.RequiredModules |ForEach-Object {
19+
if(!(Get-Module $_ -ListAvailable -wa Ignore))
20+
{
21+
Install-PSResource $_ -Scope CurrentUser -Repository PSGallery -TrustRepository -wa Ignore
22+
}
23+
Import-Module $_
24+
}
25+
}
26+
Import-Module $module -Force

β€Žscripts/Publish-ThisModule.ps1β€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Publishes the module if it has been updated.
1111
)
1212
Process
1313
{
14-
Push-Location "$PSScriptRoot/../src/.publish"
14+
$PSScriptRoot |Split-Path |Join-Path -ChildPath .publish |Push-Location
1515
$name = Get-Item *.psd1 |Test-ModuleManifest |Select-Object -ExpandProperty Name
1616
[version] $publishedVersion = (Find-PSResource -Name $name -Repository PSGallery -ErrorAction Ignore |
1717
Select-Object -ExpandProperty Version) ?? '0.0.0.0'
@@ -24,7 +24,7 @@ Process
2424
else
2525
{
2626
Update-ModuleManifest -Path *.psd1 -ModuleVersion $ModuleVersion -FunctionsToExport (
27-
Get-Item ../public/*.ps1 |Split-Path -LeafBase)
27+
Join-Path .. src public *.ps1 |Get-Item |Split-Path -LeafBase)
2828
# see bug https://github.com/PowerShell/PSResourceGet/issues/1806#issuecomment-2992975199
2929
Get-PSResourceRepository -Name PSGallery
3030
Publish-PSResource -Path *.psd1 -Repository PSGallery -ApiKey $GalleryKey
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<#
2+
.SYNOPSIS
3+
Unloads this repository's module.
4+
#>
5+
6+
#Requires -Version 7
7+
[CmdletBinding()] Param()
8+
$PSScriptRoot |
9+
Split-Path |
10+
Join-Path -ChildPath .publish -AdditionalChildPath *.psd1 |
11+
Get-Item |
12+
Split-Path -LeafBase |
13+
Remove-Module -Force
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<#
2+
.SYNOPSIS
3+
Determines whether the current Pester test script is in scope, when run via workflow.
4+
#>
5+
6+
#Requires -Version 7
7+
[CmdletBinding()] Param(
8+
[Parameter(Position=0)][string] $Name =
9+
(Get-Variable MyInvocation -Scope 1 -ValueOnly).MyCommand.Name
10+
)
11+
Begin {$PSScriptRoot |Split-Path |Push-Location}
12+
Process
13+
{
14+
if(!$Name) {return}
15+
if(!(Test-Path .changes -Type Leaf)) {return $true}
16+
$target = ($Name -split '\.',2)[0]
17+
return [bool]@(Get-Content .changes |Get-Item |Select-Object -ExpandProperty Name |
18+
Where-Object {$_.StartsWith("$target.")}).Count
19+
}
20+
Clean {Pop-Location}

β€Žscripts/Update-Readme.ps1β€Ž

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ Updates the README.md file with the list of public cmdlets.
77
[CmdletBinding()] Param()
88
Begin
99
{
10-
Push-Location "$PSScriptRoot/.."
10+
$PSScriptRoot |Split-Path |Push-Location
1111
}
1212
Process
1313
{
14-
$ModuleName = Get-Item src/*.psd1 |Split-Path -LeafBase
14+
$ModuleName = Join-Path src *.psd1 |Get-Item |Split-Path -LeafBase
1515
$readme = Get-Content README.md -Raw
16-
$cmdlets = (Get-Item src/public/*.ps1 |
16+
$cmdlets = (Join-Path src public *.ps1 |
17+
Get-Item |
1718
Show-Progress 'Listing cmdlets' {$_.BaseName} |
1819
ForEach-Object {
1920
$cmdlet,$file = $_.BaseName,$_.FullName

β€Žscripts/Write-Wiki.ps1β€Ž

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ Begin
1111
{
1212
Install-PSResource PlatyPS -Repository PSGallery -Scope CurrentUser -TrustRepository
1313
}
14-
Push-Location "$PSScriptRoot/.."
14+
$PSScriptRoot |Split-Path |Push-Location
1515
}
1616
Process
1717
{
18-
$ModuleName = Get-Item src/*.psd1 |Split-Path -LeafBase
19-
& './scripts/Build-Module.ps1'
20-
Import-Module (Get-Item src/.publish/*.psd1)
21-
New-MarkdownHelp -Module $ModuleName -OutputFolder .github/wiki -ErrorAction Ignore
18+
$ModuleName = Join-Path src *.psd1 |Get-Item |Split-Path -LeafBase
19+
& (Join-Path scripts Build-ThisModule.ps1)
20+
$psd1 = Join-Path .publish *.psd1 |Get-Item
21+
& (Join-Path scripts Import-ThisModule.ps1)
22+
$manifest = Test-ModuleManifest $psd1.FullName
23+
if($manifest.PSObject.Properties.Name -contains 'RequiredModules' -and $manifest.RequiredModules)
24+
{
25+
$manifest.RequiredModules.Name |ForEach-Object {
26+
Install-PSResource $_ -Scope CurrentUser -Repository PSGallery -TrustRepository -wa Ignore
27+
Import-Module $_
28+
}
29+
}
30+
New-MarkdownHelp -Module $ModuleName -OutputFolder (Join-Path .github wiki) -ErrorAction Ignore
2231
}
2332
Clean {Pop-Location}

β€Žtest/Export-Json.Tests.ps1β€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ Tests exporting a portion of a JSON document, recursively importing references.
66
$basename = "$(($MyInvocation.MyCommand.Name -split '\.',2)[0])."
77
$skip = !(Test-Path .changes -Type Leaf) ? $false :
88
!@(Get-Content .changes |Get-Item |Select-Object -ExpandProperty Name |Where-Object {$_.StartsWith($basename)})
9-
if($skip) {Write-Information "No changes to $basename" -infa Continue}
9+
if(!(&"$PSScriptRoot/../scripts/Test-RelevantTest.ps1")) {return}
10+
BeforeAll {
11+
Set-StrictMode -Version Latest
12+
&"$PSScriptRoot/../scripts/Import-ThisModule.ps1"
13+
}
1014
Describe 'Export-Json' -Tag Export-Json -Skip:$skip {
1115
BeforeAll {
1216
$scriptsdir,$sep = (Split-Path $PSScriptRoot),[io.path]::PathSeparator
@@ -28,4 +32,4 @@ Describe 'Export-Json' -Tag Export-Json -Skip:$skip {
2832
Should -BeExactly $Result -Because 'parameter should work'
2933
}
3034
}
31-
}
35+
}

β€Žtest/Placeholder.Tests.ps1β€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
It 'should have tests' {
33
Set-ItResult -Inconclusive -Because 'no tests have been written yet'
44
}
5-
}
5+
}

0 commit comments

Comments
Β (0)