Search code examples
powershellpester

In need of some assistance with PowerShell functions and modules


I have this module that I am working with:

Function Get-BuildVersion {
param(
    [string] $MajorMinorVersion,
    [int] $BuildCounter,
    [int] $Offset=30,
    [string] $Suffix = "-hotfix"
 )

return "$MajorMinorVersion.$BuildCounter"
}

Export-ModuleMember -Function @(
"Get-BuildVersion"
)

and I am using this Pester script to test against:

$here = Convert-Path $PSScriptRoot
Import-Module $here\BuildNumber -Force

InModuleScope BuildNumber {

Describe "Get-BuildVersion is triggered" {
    $testCases = @(
        @{ MajorMinorVersion = "1.0"; BuildCounter = 0; ExpectedResult = "1.0.0" }
        @{ MajorMinorVersion = "2.0"; BuildCounter = 0; ExpectedResult = "2.0.0" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 0; ExpectedResult = "2.1.0" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 50; ExpectedResult = "2.1.50" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 50; Offset = 30; ExpectedResult = "2.1.80" }
        @{ MajorMinorVersion = "2.1"; BuildCounter = 50; Offset = 30; Suffix = "-hotfix"; ExpectedResult = "2.1.80-hotfix" }
    )

    Context "When getting build number" {
        foreach ($test in $testCases) {

            $result = Get-BuildVersion @test

            It "should return the expected version number: $($test.ExpectedResult)" {
                $result | Should Be $test.ExpectedResult
             }
         }
      }
   }
}

When I run my test, this is the output that I get:

PS C:\users\Reddit\PowerShell> .\BuildNumber.Tests.ps1
Describing Get-BuildVersion is triggered
Context When getting build number
 [+] should return the expected version number: 1.0.0 60ms
 [+] should return the expected version number: 2.0.0 36ms
 [+] should return the expected version number: 2.1.0 36ms
 [+] should return the expected version number: 2.1.50 31ms
 [-] should return the expected version number: 2.1.80 45ms
   String lengths are both 6. Strings differ at index 4.
   Expected: {2.1.80}
   But was:  {2.1.50}
   ---------------^
   22:                     $result | Should Be $test.ExpectedResult
   at <ScriptBlock>, C:\users\Reddit\PowerShell\BuildNumber.Tests.ps1: line 22
 [-] should return the expected version number: 2.1.80-hotfix 74ms
   Expected string length 13 but was 6. Strings differ at index 4.
   Expected: {2.1.80-hotfix}
   But was:  {2.1.50}
   ---------------^
   22:                     $result | Should Be $test.ExpectedResult
   at <ScriptBlock>, C:\users\Reddit\PowerShell\BuildNumber.Tests.ps1: line 22

Any ideas as to what I can do to get the expected 2.1.80 and 2.1.80-hotfix values? I have tried formatting a string but to no avail.

I can't seem to figure out how to pass the other variables $Offset and $Suffix without throwing arguments.


Solution

  • If the intention is to get your tests to pass (as a sort of test driven development exercise), you need to modify your function as follows:

    Function Get-BuildVersion {
    param(
        [string] $MajorMinorVersion,
        [int] $BuildCounter,
        [int] $Offset=0,
        [string] $Suffix
     )
    
    return "$MajorMinorVersion.$($BuildCounter+$Offset)$Suffix"
    }
    

    Your tests should then all pass as currently defined.

    Changes:

    • The default value of $Offset is 0, unless overridden by a provided value.
    • There is no default value for $Suffix so its empty by default.
    • The string returned has been modified to include these two parameters in its output.

    Also FYI the Return keyword is technically redundant here, you can just remove it.