Using local variables on Pester test running in Appveyor

Welcome Forums Pester Using local variables on Pester test running in Appveyor

Viewing 2 reply threads
  • Author
    Posts
    • #158835
      Participant
      Topics: 12
      Replies: 26
      Points: 99
      Rank: Member

      My question does focus on running tests in Appveyor so hopefully this forum is alright for this.

      I’ve built a module that interacts with the rest api of Last.fm. The way I handle the api and session keys is by storing them in the password vault of the local computer. The

      Get-LFMConfiguration

      function then gets those keys and store them in a script variable that’s available to all functions so it’s not necessary to type them out on every function making an api call. See below:

      function Get-LFMConfiguration {
          # .ExternalHelp PowerLFM.psm1-help.xml
      
          [CmdletBinding()]
          param ()
      
          $module = (Get-Command -Name $MyInvocation.MyCommand.Name).ModuleName
      
          try {
              [Void][Windows.Security.Credentials.PasswordVault,Windows.Security.Credentials,ContentType=WindowsRuntime]
              $vault = New-Object -TypeName Windows.Security.Credentials.PasswordVault -ErrorAction Stop
      
              $ak = $vault.Retrieve($module, 'APIKey').Password
              $sk = $vault.Retrieve($module, 'SessionKey').Password
              $ss = $vault.Retrieve($module, 'SharedSecret').Password
              $script:LFMConfig = [pscustomobject] @{
                  'APIKey' = $ak
                  'SessionKey' = $sk
                  'SharedSecret' = $ss
              }
              Write-Verbose 'LFMConfig is loaded in to the session'
          } catch {
              Write-Error $_.Exception.Message
          }
      }
      

      I’ve built an integration test that seeks to make calls to the rest api. It works fine running locally as the $LFMConfig variable is available but running this in Appveyor obviously fails as that variable doesn’t contain anything and returns null. What is the best way to make those variables available in Appveyor to have a passing test? Below is my current attempt that fails:

      Describe 'Add-LFMArtistTag: Integration' -Tag Integration {
      
          BeforeAll {
              if ($env:APPVEYOR) {
                  $script:LFMConfig = [pscustomobject] @{
                      'APIKey' = $env:LFMApiKey
                      'SessionKey' = $env:LFMSessionKey
                  }
                  Write-Output [$LFMConfig.ApiKey]
              }
              else {
                  Get-LFMConfiguration
              }
      
              $atParams = @{
                  Artist = 'Deftones'
                  Tag = 'randomValue'
                  Confirm = $false
              }
              Remove-LFMArtistTag @atParams
          }
      
          It "Should not contain the random value tag before adding it" {
              $tag = Get-LFMArtistTag -Artist Deftones
              $tag.Tag | Should -Not -Be 'randomValue'
          }
      
          It "Should add the new random value tag to the artist" {
              Add-LFMArtistTag @atParams
              $tag = Get-LFMArtistTag -Artist Deftones
              $tag.Where({$_.Tag -eq 'randomValue'}).Tag | Should -Not -BeNullOrEmpty
              $tag.Where({$_.Tag -eq 'randomValue'}).Tag | Should -Be 'randomValue'
          }
      
          AfterAll {
              Remove-LFMArtistTag @atParams
          }
      }
      

      And the environment variables I added to the appveyor.yml

      environment:
          LFMApiKey:
              secure: 3hqS76v5ydp1RAfqHveKisX1Cs/xFFOISouteoSO69wUuHVrSrqmi3IPpP/NFQmh
          LFMSessionKey:
              secure: gMAfmqAx9wTyeT5qEDB4zW5N6+GexurGriIaWoupOww1aFZGaDJNxUfbznCpA6PD
      
    • #158891
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero

      Script scoped variables life ends when the script ends. So the integration tests which runs as a separate script(I hope so) will not be aware of the script scoped variables in the other script. You could use Export-CLIXML to store the variable and retrieve it in the other script here.

    • #158982
      Participant
      Topics: 12
      Replies: 26
      Points: 99
      Rank: Member

      I figured it out. I did have a couple small mistakes in my code but the main problem was the Appveyor secure variables weren’t being exposed during the build. I didn’t realize there is a setting in the project to enable them in PRs: ‘Enable secure variables in Pull Requests from the same repository only’.

      I now have passing integration tests in Appveyor. 😀

Viewing 2 reply threads
  • The topic ‘Using local variables on Pester test running in Appveyor’ is closed to new replies.