Skip to content

Commit c636e90

Browse files
committed
Add some tests for SourceDirectories
Caught a bug in the exports and fixed that
1 parent 3697269 commit c636e90

5 files changed

Lines changed: 66 additions & 5 deletions

File tree

Source/Public/Build-Module.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function Build-Module {
210210
# If there is a PublicFilter, update ExportedFunctions
211211
if ($ModuleInfo.PublicFilter) {
212212
# SilentlyContinue because there don't *HAVE* to be public functions
213-
if (($PublicFunctions = Get-ChildItem $ModuleInfo.PublicFilter -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty BaseName)) {
213+
if (($PublicFunctions = Get-ChildItem $ModuleInfo.PublicFilter -Recurse -ErrorAction SilentlyContinue | Where-Object BaseName -in $AllScripts.BaseName | Select-Object -ExpandProperty BaseName)) {
214214
Update-Metadata -Path $OutputManifest -PropertyName FunctionsToExport -Value $PublicFunctions
215215
}
216216
}

Tests/Integration/Source1.Tests.ps1

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#requires -Module ModuleBuilder
22

33
Describe "Build-Module With Source1" {
4-
Write-Host "Current Modules: `n$(Get-Module | Out-String)" -ForegroundColor DarkGreen -BackgroundColor Yellow
54
Context "When we call Build-Module" {
65
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
76
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
@@ -13,7 +12,7 @@ Describe "Build-Module With Source1" {
1312
$Metadata = Import-Metadata $Output.Path
1413

1514
It "Should update FunctionsToExport in the manifest" {
16-
$Metadata.FunctionsToExport | Should -Be @("Get-Finale", "Get-Source")
15+
$Metadata.FunctionsToExport | Should -Be @("Get-Source")
1716
}
1817

1918
It "Should update AliasesToExport in the manifest" {
@@ -25,6 +24,56 @@ Describe "Build-Module With Source1" {
2524
}
2625

2726
It "Will comment out the original using statements in their original positions" {
27+
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 3
28+
}
29+
}
30+
31+
Context "Regression test for #55: I can pass SourceDirectories" {
32+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -Passthru
33+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
34+
35+
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
36+
$Module | Should -Not -FileContentMatch '^Source$'
37+
}
38+
39+
$Metadata = Import-Metadata $Output.Path
40+
41+
It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
42+
$Metadata.FunctionsToExport | Should -Be @()
43+
}
44+
45+
It "Should de-dupe and move using statements to the top of the file" {
46+
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
47+
}
48+
49+
It "Will comment out the original using statement in the original positions" {
50+
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
51+
}
52+
}
53+
54+
Context "Regression test for #55: I can pass SourceDirectories and PublicFilter" {
55+
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -PublicFilter "P*\*" -Passthru
56+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
57+
58+
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
59+
$Module | Should -Not -FileContentMatch '^Source$'
60+
}
61+
62+
$Metadata = Import-Metadata $Output.Path
63+
64+
It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
65+
$Metadata.FunctionsToExport | Should -Be @("GetFinale", "GetPreview")
66+
}
67+
68+
It "Should update AliasesToExport in the manifest" {
69+
$Metadata.AliasesToExport | Should -Be @("GF", "GP")
70+
}
71+
72+
It "Should de-dupe and move using statements to the top of the file" {
73+
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
74+
}
75+
76+
It "Will comment out the original using statement in the original positions" {
2877
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
2978
}
3079
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using module ModuleBuilder
22

3-
function Get-Finale {
3+
function GetFinale {
44
[CmdletBinding()]
5-
[Alias("gs")]
5+
[Alias("gf")]
66
param()
77
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using module ModuleBuilder
2+
3+
function GetPreview {
4+
[CmdletBinding()]
5+
[Alias("gp")]
6+
param()
7+
}

Tests/Integration/build.psd1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@{
2+
Path = "Source1\Source1.psd1"
3+
OutputDirectory = "..\Result1"
4+
VersionedOutputDirectory = $true
5+
}

0 commit comments

Comments
 (0)