Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template placeholders not expanded the same way as in pester itself #247

Open
csandfeld opened this issue Mar 15, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@csandfeld
Copy link

There is a mismatch between how placeholders are expanded in the "Pester Tests" (vscode-adapter) extension, and how they are expanded in Pester. I would expect them to be expanded the same by both.

As an example these tests ...

Template.tests.ps1

BeforeDiscovery {
    $arrayOfHashtables = @(
        @{ Emoji = '🌵' ; Description = 'cactus' }
        @{ Emoji = '🦒' ; Description = 'giraffe' }
        @{ Emoji = '🍎' ; Description = 'apple' }
        @{ Emoji = '🐧' ; Description = 'penguin' }
        @{ Emoji = '😊' ; Description = 'smiling face with smiling eyes' }
    )

    $arrayOfObjects = @(
        [pscustomobject]@{ Emoji = '🌵' ; Description = 'cactus' }
        [pscustomobject]@{ Emoji = '🦒' ; Description = 'giraffe' }
        [pscustomobject]@{ Emoji = '🍎' ; Description = 'apple' }
        [pscustomobject]@{ Emoji = '🐧' ; Description = 'penguin' }
        [pscustomobject]@{ Emoji = '😊' ; Description = 'smiling face with smiling eyes' }
    )
}


Describe 'Template expansion' {
    Context 'Array of hashtables' {
        It 'Using PropertyName:   Returns <Emoji> (<Description>)' -ForEach $arrayOfHashtables {}
        It 'Using _.PropertyName: Returns <_.Emoji> (<_.Description>)' -ForEach $arrayOfHashtables {}
    }

    Context 'Array of objects' {
        It 'Using PropertyName:   Returns <Emoji> (<Description>)' -ForEach $arrayOfObjects {}
        It 'Using _.PropertyName: Returns <_.Emoji> (<_.Description>)' -ForEach $arrayOfObjects {}
    }
}

... are expanded and rendered like this by the "Pester Tests" extension:

image

... whereas they are expanded and rendered like this by Pester:

image

PowerShell version (tested with PS5 as well)

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Pester Tests Extension version

Published
2023-07-31, 06:15:27
Last released
2023-08-01, 17:24:12
Last updated
2023-10-19, 19:25:47
Identifier
pspester.pester-test

Pester version

Get-Module -Name Pester

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     5.5.0                 Pester                              ...

@JustinGrote
Copy link
Collaborator

JustinGrote commented Mar 15, 2024

It uses the same name processing as Pester, but using hooks it occurs prior to final output for performance, so there may be an issue here especially with that referencing of a property rather than a direct object.

Thanks for the note! I'll see if I can sort this but in the meantime look in the tests directly for tested/supported name cases.
https://github.com/pester/vscode-adapter/blob/main/sample/Tests/Basic.Tests.ps1

@JustinGrote JustinGrote added the bug Something isn't working label Mar 15, 2024
@PsychoData
Copy link

I also had some good success with just creating a HashTable manually, so that it could parse directly from a template in the meantime
Previously, I was trying to use } -ForEach $AllAccountsT2 and Context "<_.GivenName> <_.SurName>-<_.SamAccountName>" {, but it was not populating any of the templates since they relied on Properties off the template.

To work around that I just created a HashTable from each returned case, then fed that Array of HashTables to the next block, and everything worked well for me and the templates filled in.

Describe "Tier 2 Accounts" {
    BeforeDiscovery -Scriptblock {
        $AllAccountsT2 = Get-ADUser -SearchBase 'OU=Tier 2,DC=CompanyDom,DC=org' -Filter * -Properties *
        $AllAccountsT2HashT = $AllAccountsT2 | Foreach {
            @{
                GivenName       = $_.GivenName
                SurName         = $_.SurName
                SamAccountName  = $_.SamAccountName
                CurrAdUserObj   = $_
            }
        }
    }
    Context "<GivenName> <SurName> - <SamAccountName> " {
        IT "tests" {} 
    } -ForEach $AllAccountsT2HashT
}

I'm passing $CurrAdUserObj so that I can be lazy inside my tests and just call for other attributes like $CurrAdUserObj.MemberOf or $CurrAdUserObj.DisplayName without having them declared in the "full" hashtable passed in.

Before, with the properties, these were just $_.MemberOf and $_.DisplayName, but it isn't much trouble with the $CurrAdUserObj to use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants