ticketmaster / poshspec

Infrastructure Testing DSL running in Pester
MIT License
183 stars 32 forks source link

Unable to call Custom functions in poshspec. #27

Open SamPosh opened 8 years ago

SamPosh commented 8 years ago

I am using Package resource for my infrastructure validation. As my environment has only powershell v4, Get-package function was not there.

I have written a module named Poshspechelper in my machine and written a custom function Get-InstalledPackage. The function is given below.

function Get-InstalledPackage
{
    [CmdletBinding()]
  param($appName, $appVersion)

  if ((Get-WmiObject win32_operatingsystem).OSArchitecture -notmatch '64')  
  { 
    $keys= (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*')
    $possible_path= 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
  }  
  else  
  { 
    $keys = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*')
    $possible_path= 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
    $possible_path= 'HKCU:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
  }

  if ($appVersion -eq $null) { 
    @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName}).Length -gt 0
  }
  else{
    $IsAppInstalled =    @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName}).Length -gt 0
    $VersionAvailable =   @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName  } | Where-Object {$_.DisplayVersion -eq $appVersion} ).Length -gt 0
    if ($VersionAvailable)
    {
        $Version = $appVersion
    }    
    $object = [Pscustomobject] @{
        IsInstalled= $IsAppInstalled
        Version= $Version   
    }
    Write-Output $object

  }
}

When i run this with pester like below , it worked as expected. .

Import-module pester
Import-Module PoshspecHelper

Describe "Orca msi validation" {
    It "Package validation" {
        Get-InstalledPackage -appName 'Orca' -appVersion '3.1.3790.0000' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'version' | should be '3.1.3790.0000'
    }
}

But when i call this in package.ps1 file instead of Get-package (like given below) , i am getting error given below the code.


#$expression = {Get-Package -Name '$Target' -ErrorAction SilentlyContinue}
    $expression = { Get-InstalledPackage -appName '$Target' -appVersion $Property -ErrorAction SilentlyContinue}

[-] Package2 property 'version' for 'Orca' should be '3.1.3790.0000' 67.04s Expected: {3.1.3790.0000} But was: {} 1: Get-InstalledPackage -appName 'Orca' -appVersion version -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'versio n' | should be '3.1.3790.0000' at , : line 1 at , C:\Program Files\WindowsPowerShell\Modules\Poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: line 12

Please check and let me know what went wrong . when i debugged it ,it always failed in the below line at Invoke-PoshspecExpression.ps1 file

Invoke-Expression $InputObject.Expression