diff --git a/TestingHelperTest/DummyModule1/DummyModule1Test/DummyModule1Test.psd1 b/TestingHelperTest/DummyModule1/Test/Test.psd1 similarity index 93% rename from TestingHelperTest/DummyModule1/DummyModule1Test/DummyModule1Test.psd1 rename to TestingHelperTest/DummyModule1/Test/Test.psd1 index aca5485..3c8c435 100644 --- a/TestingHelperTest/DummyModule1/DummyModule1Test/DummyModule1Test.psd1 +++ b/TestingHelperTest/DummyModule1/Test/Test.psd1 @@ -1,5 +1,5 @@ # -# Module manifest for module 'DummyModule1Test' +# Module manifest for module 'Test' # # Generated by: raulg # @@ -9,7 +9,7 @@ @{ # Script module or binary module file associated with this manifest. -RootModule = 'DummyModule1Test.psm1' +RootModule = 'Test.psm1' # Version number of this module. ModuleVersion = '1.0' @@ -126,7 +126,7 @@ PrivateData = @{ # HelpInfoURI = '' # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. -DefaultCommandPrefix = 'DummyModule1Test' +DefaultCommandPrefix = 'Test' } diff --git a/TestingHelperTest/DummyModule1/DummyModule1Test/DummyModule1Test.psm1 b/TestingHelperTest/DummyModule1/Test/Test.psm1 similarity index 60% rename from TestingHelperTest/DummyModule1/DummyModule1Test/DummyModule1Test.psm1 rename to TestingHelperTest/DummyModule1/Test/Test.psm1 index 469e750..4d5aa11 100644 --- a/TestingHelperTest/DummyModule1/DummyModule1Test/DummyModule1Test.psm1 +++ b/TestingHelperTest/DummyModule1/Test/Test.psm1 @@ -1,16 +1,16 @@ -Write-Host "Loading DummyModule1Test ..." -ForegroundColor DarkYellow +Write-Host "Loading Test ..." -ForegroundColor DarkYellow $InstanceId = New-Guid function Get-InstanceId{ return $InstanceId } -function DummyModule1Test_Intro{ +function Test_Intro{ Assert-IsTrue -Condition $true } -function DummyModule1Test_Description{ +function Test_Description{ [string] $result = Get-DummyModule1Description diff --git a/TestingHelperTest/private/Add-ModuleSections.Tests.Asserts.ps1 b/TestingHelperTest/private/Add-ModuleSections.Tests.Asserts.ps1 index 828e6cc..0f4fa3d 100644 --- a/TestingHelperTest/private/Add-ModuleSections.Tests.Asserts.ps1 +++ b/TestingHelperTest/private/Add-ModuleSections.Tests.Asserts.ps1 @@ -218,8 +218,7 @@ function Assert-AddTestSampleCodes{ process{ $Path = $Path | Convert-Path - $name = $Path | Split-Path -LeafBase - $testingModuleName = $Name + "Test" + $testingModuleName = "Test" $testingModulePath = $path | Join-Path -ChildPath $testingModuleName $samplePublicPath = $testingModulePath | Join-Path -ChildPath "public" -AdditionalChildPath SampleFunctionTests.ps1 @@ -355,10 +354,7 @@ function Assert-AddTestModuleV3{ process{ $Path = $Path | Convert-Path - $name = $Path | Split-Path -LeafBase - - # $modulePath = $Path | Join-Path -ChildPath $Name - $testingModuleName = $name + "Test" + $testingModuleName = "Test" $testingModulePath = $path | Join-Path -ChildPath $testingModuleName Assert-AddModuleV3 -Path $testingModulePath @@ -375,10 +371,8 @@ function Assert-AddTestAll { process{ $Path = $Path | Convert-Path - $name = $Path | Split-Path -LeafBase - # $modulePath = $Path | Join-Path -ChildPath $Name - $testingModuleName = $name + "Test" + $testingModuleName = "Test" $testingModulePath = $path | Join-Path -ChildPath $testingModuleName Assert-AddModuleV3 -Path $testingModulePath diff --git a/TestingHelperTest/public/Copy-FunctionsToModule.Test.ps1 b/TestingHelperTest/public/Copy-FunctionsToModule.Test.ps1 index 1ca5bd1..017a6a3 100644 --- a/TestingHelperTest/public/Copy-FunctionsToModule.Test.ps1 +++ b/TestingHelperTest/public/Copy-FunctionsToModule.Test.ps1 @@ -7,8 +7,8 @@ function TestingHelperTest_FunctionsToModule_Copy{ New-TestingFile -Name Function1.ps1 -Path sourceModule/public New-TestingFile -Name Function6.txt -Path sourceModule/private/childFolder New-TestingFile -Name Function2.ps1 -Path sourceModule/private - New-TestingFile -Name Function3.ps1 -Path sourceModule/sourceModuleTest/public - New-TestingFile -Name Function4.ps1 -Path sourceModule/sourceModuleTest/private + New-TestingFile -Name Function3.ps1 -Path sourceModule/Test/public + New-TestingFile -Name Function4.ps1 -Path sourceModule/Test/private Copy-TT_FunctionsToModule -Source sourceModule -Destination destinationModule @@ -16,8 +16,8 @@ function TestingHelperTest_FunctionsToModule_Copy{ Assert-ItemExist -path destinationModule/private/Function2.ps1 Assert-ItemExist -path destinationModule/private/childFolder/Function6.txt - Assert-ItemExist -path destinationModule/destinationModuleTest/public/Function3.ps1 - Assert-ItemExist -path destinationModule/destinationModuleTest/private/Function4.ps1 + Assert-ItemExist -path destinationModule/Test/public/Function3.ps1 + Assert-ItemExist -path destinationModule/Test/private/Function4.ps1 } @@ -29,17 +29,17 @@ function TestingHelperTest_FunctionsToModule_Copy_FilesExist{ New-TestingFile -Name Function1.ps1 -Content 'Function1 source' -Path sourceModule/public New-TestingFile -Name Function6.txt -Content 'Function6 source' -Path sourceModule/private/childFolder New-TestingFile -Name Function2.ps1 -Content 'Function2 source' -Path sourceModule/private - New-TestingFile -Name Function3.ps1 -Content 'Function3 source' -Path sourceModule/sourceModuleTest/public - New-TestingFile -Name Function4.ps1 -Content 'Function4 source' -Path sourceModule/sourceModuleTest/private + New-TestingFile -Name Function3.ps1 -Content 'Function3 source' -Path sourceModule/Test/public + New-TestingFile -Name Function4.ps1 -Content 'Function4 source' -Path sourceModule/Test/private # Adding files on destination New-TestingFile -Name Function1.ps1 -Content 'Function1 destination' -Path destinationModule/public New-TestingFile -Name Function6.txt -Content 'Function6 destination' -Path destinationModule/private/childFolder - New-TestingFile -Name Function3.ps1 -Content 'Function3 destination' -Path destinationModule/destinationModuleTest/public + New-TestingFile -Name Function3.ps1 -Content 'Function3 destination' -Path destinationModule/Test/public Copy-TT_FunctionsToModule -Source sourceModule -Destination destinationModule @InfoParameters Assert-AreEqual -Expected 'Function1 source' -Presented $(Get-Content -Path destinationModule/public/Function1.ps1) Assert-AreEqual -Expected 'Function6 source' -Presented $(Get-Content -Path destinationModule/private/childFolder/Function6.txt) - Assert-AreEqual -Expected 'Function3 source' -Presented $(Get-Content -Path destinationModule/destinationModuleTest/public/Function3.ps1) + Assert-AreEqual -Expected 'Function3 source' -Presented $(Get-Content -Path destinationModule/Test/public/Function3.ps1) } diff --git a/TestingHelperTest/public/Import-Modules.Tests.ps1 b/TestingHelperTest/public/Import-Modules.Tests.ps1 index e58fbfc..64adf4c 100644 --- a/TestingHelperTest/public/Import-Modules.Tests.ps1 +++ b/TestingHelperTest/public/Import-Modules.Tests.ps1 @@ -9,13 +9,13 @@ function TestingHelperTest_ImportTestingModule_TargetModule{ Import-TT_TestingModule -TargetModule $Dummy1 - Assert-IsNotNull -Object (Get-Module -Name ($Dummy1 +"Test")) + Assert-IsNotNull -Object (Get-Module -Name "Test") - $instance1 = Get-DummyModule1TestInstanceId + $instance1 = Get-TestInstanceId Import-TT_TestingModule -TargetModule $Dummy1 -Force - $instance2 = Get-DummyModule1TestInstanceId + $instance2 = Get-TestInstanceId Assert-AreNotEqual -Expected $instance1 -Presented $instance2 } @@ -45,7 +45,7 @@ function TestingHelperTest_ImportTestingModule_TargetModule_AlreadyLoaded{ Import-TT_TestingModule -TargetModule $Dummy1 @WarningParameters - Assert-IsNotNull -Object (Get-Module -Name ($Dummy1 +"Test")) + Assert-IsNotNull -Object (Get-Module -Name "Test") Assert-Count -Expected 1 -Presented $WarningVar Assert-AreEqual -Presented $WarningVar[0].Message -Expected ("[Import-TestingModule] TargetModule {0} is already loaded" -f $Dummy1) @@ -54,7 +54,7 @@ function TestingHelperTest_ImportTestingModule_TargetModule_AlreadyLoaded{ function TestingHelperTest_ImportTestingModule_TestingModule { [CmdletBinding()] param () - $TestDummy1 = $Dummy1 + "Test" + $TestDummy1 = "Test" Import-Module -name $DUMMY_1_PATH -Global @@ -68,15 +68,17 @@ function TestingHelperTest_ImportTestingModule_TestingModule { Assert-IsNotNull -Object (Get-Module -Name $TestDummy1) - $instance1 = Get-DummyModule1TestInstanceId + $instance1 = Get-TestInstanceId Import-TT_TestingModule -Name $TestDummyPath -Force - $instance2 = Get-DummyModule1TestInstanceId + $instance2 = Get-TestInstanceId Assert-AreNotEqual -Expected $instance1 -Presented $instance2 - Get-Module -Name $Dummy1* | Remove-Module -Force + Get-Module -Name $Dummy1 | Remove-Module -Force + Assert-IsNull -Object (Get-Module -Name $Dummy1) - Assert-IsNull -Object (Get-Module -Name $TestDummy1*) + Get-module -Name $TestDummy1 | Remove-Module -Force + Assert-IsNull -Object (Get-Module -Name $TestDummy1) } \ No newline at end of file diff --git a/TestingHelperTest/public/testPS1.Tests.ps1 b/TestingHelperTest/public/testPS1.Tests.ps1 index 4c20bac..49de649 100644 --- a/TestingHelperTest/public/testPS1.Tests.ps1 +++ b/TestingHelperTest/public/testPS1.Tests.ps1 @@ -11,8 +11,10 @@ function TestingHelperTest_TestPS1{ $result = & $test @InfoParameters Assert-AreEqual -Expected "ModuleName" -Presented $result.Name - Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule - Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName + # Assert-AreEqual -Expected "ModuleNameTest" -Presented $result.TestModule + Assert-AreEqual -Expected "Test" -Presented $result.TestModule + # Assert-AreEqual -Expected "ModuleNameTest_*" -Presented $result.TestsName + Assert-AreEqual -Expected "Test_*" -Presented $result.TestsName Remove-ImportedModule -Module "ModuleName" } @@ -26,8 +28,8 @@ function TestingHelperTest_TestPS1_WithPath{ $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 "Test" -Presented $result.TestModule + Assert-AreEqual -Expected "Test_*" -Presented $result.TestsName Assert-AreEqual -Expected 2 -Presented $result.Tests Assert-AreEqual -Expected 2 -Presented $result.Pass diff --git a/private/Module.Helper.ps1 b/private/Module.Helper.ps1 index fe7f79e..b540180 100644 --- a/private/Module.Helper.ps1 +++ b/private/Module.Helper.ps1 @@ -12,10 +12,11 @@ function Get-ModuleHeader { function Get-TestingModuleName { [CmdletBinding()] + [OutputType([string])] param ( [parameter(Mandatory)] [string] $TargetModule ) - - return ($TargetModule + "Test") + + return "Test" } diff --git a/private/New-ModuleV3.Helper.ps1 b/private/New-ModuleV3.Helper.ps1 index 677a71b..ed46f05 100644 --- a/private/New-ModuleV3.Helper.ps1 +++ b/private/New-ModuleV3.Helper.ps1 @@ -45,9 +45,7 @@ function Get-TestModulePath{ [Parameter()][string]$Path ) - $moduleName = Get-ModuleName -Path $Path - - $testModulePath = $path | Join-Path -ChildPath ($moduleName + "Test") + $testModulePath = $path | Join-Path -ChildPath ("Test") return $testModulePath } @@ -57,9 +55,8 @@ function Get-TestModuleName { param ( [parameter(Mandatory)] [string] $Path ) - - $testPath = Get-TestModulePath -Path $Path - $name = Get-ModuleName -Path $testPath + + $name = "Test" return $name } diff --git a/public/Copy-functionsToModule.ps1 b/public/Copy-functionsToModule.ps1 index e261401..63ea5e8 100644 --- a/public/Copy-functionsToModule.ps1 +++ b/public/Copy-functionsToModule.ps1 @@ -14,14 +14,14 @@ function Copy-FunctionsToModule { $sourcePublic = $Source | Join-Path -ChildPath 'public' $sourcePrivate = $Source | Join-Path -ChildPath 'private' - $sourceTestPublic = $Source | Join-Path -ChildPath $($sourceModuleName + 'Test') -AdditionalChildPath 'public' - $sourceTestPrivate = $Source | Join-Path -ChildPath $($sourceModuleName + 'Test') -AdditionalChildPath 'private' + $sourceTestPublic = $Source | Join-Path -ChildPath 'Test' -AdditionalChildPath 'public' + $sourceTestPrivate = $Source | Join-Path -ChildPath 'Test' -AdditionalChildPath 'private' $destinationPublic = $Destination | Join-Path -ChildPath 'public' $destinationPrivate = $Destination | Join-Path -ChildPath 'private' - $destinationTestPublic = $Destination | Join-Path -ChildPath $($destinationModuleName + 'Test') -AdditionalChildPath 'public' - $destinationTestPrivate = $Destination | Join-Path -ChildPath $($destinationModuleName + 'Test') -AdditionalChildPath 'private' + $destinationTestPublic = $Destination | Join-Path -ChildPath 'Test' -AdditionalChildPath 'public' + $destinationTestPrivate = $Destination | Join-Path -ChildPath 'Test' -AdditionalChildPath 'private' Copy-Item -Path $sourcePublic/* -Destination $destinationPublic -Recurse -Force Copy-Item -Path $sourcePrivate/* -Destination $destinationPrivate -Recurse -Force diff --git a/test.ps1 b/test.ps1 index 6379ca5..811850f 100644 --- a/test.ps1 +++ b/test.ps1 @@ -19,6 +19,9 @@ param ( function Set-TestName{ [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Scope='Function')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Scope='Function')] [Alias("st")] param ( [Parameter(Position=0,ValueFromPipeline)][string]$TestName @@ -29,8 +32,22 @@ function Set-TestName{ } } +function Get-TestName{ + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Scope='Function')] + [Alias("gt")] + param ( + ) + + process{ + $global:TestName + } +} + function Clear-TestName{ [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Scope='Function')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function')] [Alias("ct")] param ( ) @@ -38,40 +55,90 @@ function Clear-TestName{ $global:TestName = $null } -function Import-TestingHelper{ +function Import-RequiredModule{ [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Scope='Function')] param ( - [Parameter()][string]$Version, - [Parameter()][switch]$AllowPrerelease, - [Parameter()][switch]$PassThru + [Parameter(ParameterSetName = "HT", ValueFromPipeline)][hashtable]$RequiredModule, + [Parameter(ParameterSetName = "RM",Position = 0)][string]$ModuleName, + [Parameter(ParameterSetName = "RM")][string]$ModuleVersion, + [Parameter(ParameterSetName = "HT")] + [Parameter(ParameterSetName = "RM")] + [switch]$AllowPrerelease, + [Parameter(ParameterSetName = "HT")] + [Parameter(ParameterSetName = "RM")] + [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 + process{ + # Powershell module manifest does not allow versions with prerelease tags on them. + # Powershell modle manifest does not allow to add a arbitrary field to specify prerelease versions. + # Valid value (ModuleName, ModuleVersion, RequiredVersion, GUID) + # There is no way to specify a prerelease required module. + + if($RequiredModule){ + $ModuleName = $RequiredModule.ModuleName + $ModuleVersion = [string]::IsNullOrWhiteSpace($RequiredModule.RequiredVersion) ? $RequiredModule.ModuleVersion : $RequiredModule.RequiredVersion + } + + "Importing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $ModuleName, $ModuleVersion, $AllowPrerelease | Write-Host -ForegroundColor DarkGray + + # Following semVer we can manually specidy a taged version to specify that is prerelease + # Extract the semVer from it and set AllowPrerelease to true + if ($ModuleVersion) { + $V = $ModuleVersion.Split('-') + $semVer = $V[0] + $AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1])) + } + + $module = Import-Module $ModuleName -PassThru -ErrorAction SilentlyContinue -MinimumVersion:$semVer + + if ($null -eq $module) { + "Installing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $ModuleName, $ModuleVersion, $AllowPrerelease | Write-Host -ForegroundColor DarkGray + $installed = Install-Module -Name $ModuleName -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$ModuleVersion + $module = $installed | ForEach-Object {Import-Module -Name $_.Name -RequiredVersion ($_.Version.Split('-')[0]) -Force -PassThru} + } + + "Imported module Name[{0}] Version[{1}] PreRelease[{2}]" -f $module.Name, $module.Version, $module.privatedata.psdata.prerelease | Write-Host -ForegroundColor DarkGray + + if ($PassThru) { + $module + } } +} - if ($PassThru) { - $module +<# +. SYNOPSIS + Extracts the required modules from the module manifest +#> +function Get-RequiredModule{ + [CmdletBinding()] + [OutputType([Object[]])] + param() + + # Required Modules + $localPath = $PSScriptRoot + $manifest = $localPath | Join-Path -child "*.psd1" | Get-Item | Import-PowerShellDataFile + $requiredModule = $null -eq $manifest.RequiredModules ? @() : $manifest.RequiredModules + + # Convert to hashtable + $ret = @() + $requiredModule | ForEach-Object{ + $ret += $_ -is [string] ? @{ ModuleName = $_ } : $_ } + + return $ret } -Import-TestingHelper -AllowPrerelease +# Install and load TestingHelper +# Import-RequiredModule -Name TestingHelper -AllowPrerelease +Import-RequiredModule "TestingHelper" -AllowPrerelease -# Run test by PSD1 file -# Test-ModulelocalPSD1 -ShowTestErrors:$ShowTestErrors -# Test-ModulelocalPSD1 -ShowTestErrors:$ShowTestErrors -TestName StagingModuleTest_* +# Install and Load Module dependencies +Get-RequiredModule | Import-RequiredModule -AllowPrerelease if($TestName){ - Invoke-TestingHelper -TestName $TestName + Invoke-TestingHelper -TestName $TestName -ShowTestErrors:$ShowTestErrors } else { - Invoke-TestingHelper + Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors } \ No newline at end of file