Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests to verify prefix works right
  • Loading branch information
Jaykul committed Jan 11, 2020
commit d28c47de58e4aa108fef1300e6a7cb6c9d8e08e1
1 change: 1 addition & 0 deletions Source/Private/SetModuleContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function SetModuleContent {
[string]$Encoding = $(if($IsCoreCLR) { "UTF8Bom" } else { "UTF8" })
)
begin {
Write-Debug "SetModuleContent WorkingDirectory $WorkingDirectory"
Push-Location $WorkingDirectory -StackName SetModuleContent
$ContentStarted = $false # There has been no content yet

Expand Down
177 changes: 93 additions & 84 deletions Tests/Integration/Source1.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,124 +1,133 @@
#requires -Module ModuleBuilder

Describe "Build-Module With Source1" {
Context "When we call Build-Module" {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
Describe "When we call Build-Module" -Tag Integration {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")

It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
$Module | Should -Not -FileContentMatch '^Source$'
}
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
$Module | Should -Not -FileContentMatch '^Source$'
}

$Metadata = Import-Metadata $Output.Path
$Metadata = Import-Metadata $Output.Path

It "Should update FunctionsToExport in the manifest" {
$Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
}
It "Should update FunctionsToExport in the manifest" {
$Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
}

It "Should update AliasesToExport in the manifest" {
$Metadata.AliasesToExport -match "GS" | Should -Not -BeNullOrEmpty
}
It "Should update AliasesToExport in the manifest" {
$Metadata.AliasesToExport -match "GS" | Should -Not -BeNullOrEmpty
}

It "Should de-dupe and move using statements to the top of the file" {
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
}
It "Should de-dupe and move using statements to the top of the file" {
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
}

It "Will comment out the original using statements in their original positions" {
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 3
}
It "Will comment out the original using statements in their original positions" {
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 3
}
}

Context "Regression test for #55: I can pass SourceDirectories" {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -Passthru
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
Describe "Regression test for #55: I can pass SourceDirectories" -Tag Integration, Regression {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -Passthru
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")

It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
$Module | Should -Not -FileContentMatch '^Source$'
}
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
$Module | Should -Not -FileContentMatch '^Source$'
}

$Metadata = Import-Metadata $Output.Path
$Metadata = Import-Metadata $Output.Path

It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
$Metadata.FunctionsToExport | Should -Be @()
}
It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
$Metadata.FunctionsToExport | Should -Be @()
}

It "Should de-dupe and move using statements to the top of the file" {
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
}
It "Should de-dupe and move using statements to the top of the file" {
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
}

It "Will comment out the original using statement in the original positions" {
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
}
It "Will comment out the original using statement in the original positions" {
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
}
}

Context "Regression test for #55: I can pass SourceDirectories and PublicFilter" {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -PublicFilter "P*\*" -Passthru
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
Describe "Regression test for #55: I can pass SourceDirectories and PublicFilter" -Tag Integration, Regression {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -SourceDirectories "Private" -PublicFilter "P*\*" -Passthru
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")

It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
$Module | Should -Not -FileContentMatch '^Source$'
}
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
$Module | Should -Not -FileContentMatch '^Source$'
}

$Metadata = Import-Metadata $Output.Path
$Metadata = Import-Metadata $Output.Path

It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
$Metadata.FunctionsToExport | Should -Be @("GetFinale", "GetPreview")
}
It "Should not have any FunctionsToExport if SourceDirectories don't match the PublicFilter" {
$Metadata.FunctionsToExport | Should -Be @("GetFinale", "GetPreview")
}

It "Should update AliasesToExport in the manifest" {
$Metadata.AliasesToExport | Should -Be @("GF", "GP")
}
It "Should update AliasesToExport in the manifest" {
$Metadata.AliasesToExport | Should -Be @("GF", "GP")
}

It "Should de-dupe and move using statements to the top of the file" {
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
}
It "Should de-dupe and move using statements to the top of the file" {
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
}

It "Will comment out the original using statement in the original positions" {
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
}
It "Will comment out the original using statement in the original positions" {
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
}
}

Context "Regression test for #84: Multiple Aliases per command will Export" {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
Describe "Regression test for #84: Multiple Aliases per command will Export" -Tag Integration, Regression {
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru

$Metadata = Import-Metadata $Output.Path
$Metadata = Import-Metadata $Output.Path

It "Should update AliasesToExport in the manifest" {
$Metadata.AliasesToExport | Should -Be @("GS","GSou", "SS", "SSou")
}
It "Should update AliasesToExport in the manifest" {
$Metadata.AliasesToExport | Should -Be @("GS","GSou", "SS", "SSou")
}
}

Context "Supports building without a build.psd1" {
Copy-Item $PSScriptRoot\Source1 $PSScriptRoot\Copy1 -Recurse
Remove-Item $PSScriptRoot\Copy1\build.psd1
Rename-Item $PSScriptRoot\Copy1\Source1.psd1 Copy1.psd1

$Build = @{ }
Describe "Supports building without a build.psd1" -Tag Integration {
Copy-Item $PSScriptRoot\Source1 $PSScriptRoot\Copy1 -Recurse
Remove-Item $PSScriptRoot\Copy1\build.psd1
Rename-Item $PSScriptRoot\Copy1\Source1.psd1 Copy1.psd1

It "No longer fails if there's no build.psd1" {
$BuildParameters = @{
SourcePath = "$PSScriptRoot\Copy1\Copy1.psd1"
OutputDirectory = "..\Result1"
VersionedOutputDirectory = $true
}
$Build = @{ }

$Build.Output = Build-Module @BuildParameters -Passthru
It "No longer fails if there's no build.psd1" {
$BuildParameters = @{
SourcePath = "$PSScriptRoot\Copy1\Copy1.psd1"
OutputDirectory = "..\Result1"
VersionedOutputDirectory = $true
}

Remove-Item -Recurse $PSScriptRoot\Copy1
$Build.Output = Build-Module @BuildParameters -Passthru
}

Remove-Item -Recurse $PSScriptRoot\Copy1

It "Creates the same module as with a build.psd1" {
$Build.Metadata = Import-Metadata $Build.Output.Path
}

It "Should update AliasesToExport in the manifest" {
$Build.Metadata.AliasesToExport | Should -Be @("GS", "GSou", "SS", "SSou")
}
It "Creates the same module as with a build.psd1" {
$Build.Metadata = Import-Metadata $Build.Output.Path
}

It "Should update FunctionsToExport in the manifest" {
$Build.Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
}
It "Should update AliasesToExport in the manifest" {
$Build.Metadata.AliasesToExport | Should -Be @("GS", "GSou", "SS", "SSou")
}

It "Should update FunctionsToExport in the manifest" {
$Build.Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
}
}

Describe "Regression test for #88 not copying prefix files" -Tag Integration, Regression {
$Output = Build-Module $PSScriptRoot\build.psd1 -Passthru

$Metadata = Import-Metadata $Output.Path

It "Should update AliasesToExport in the manifest" {
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
$ModuleInfo = Get-Content $Module
$ModuleInfo[0] | Should -be "using module Configuration"
}
}
3 changes: 3 additions & 0 deletions Tests/Integration/build.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
Path = "Source1\Source1.psd1"
OutputDirectory = "..\Result1"
VersionedOutputDirectory = $true

# This file is not in Source1 it's here next to this build
Prefix = "..\using.ps1"
}
1 change: 1 addition & 0 deletions Tests/Integration/using.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using module Configuration