Created
January 4, 2019 18:32
-
-
Save Stephanevg/34cff3425bebb7467bfe4c00f315f5ed to your computer and use it in GitHub Desktop.
Code to scan for pester tests in a file. It return IT desccribe blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $Path = "PahtToFile.Tests.Ps1" | |
| Function Get-PesterITBlock { | |
| [CmdletBinding()] | |
| Param( | |
| $Path, | |
| $InputObject | |
| ) | |
| if($Path){ | |
| $P = Get-Item -path $Path | |
| $Raw = [System.Management.Automation.Language.Parser]::ParseFile($p.FullName, [ref]$null, [ref]$Null) | |
| }elseif($InputObject){ | |
| $Raw = [System.Management.Automation.Language.Parser]::ParseInput($InputObject,[ref]$null, [ref]$Null) | |
| } | |
| $String = $Raw.FindAll( {$args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst]}, $true) | |
| $Data = @() | |
| $Data += $String | ? {$_.StringConstantType -eq "BareWord" -and $_.Value -eq 'it'} | |
| Foreach($d in $data){ | |
| $Hash = @{value='';content=''} | |
| $Hash.ElementType = $d.Value | |
| $Hash.Content = $d.Parent.Extent.Text | |
| $Hash.Name = $d.Parent.commandElements.Value[1] | |
| $Hash.Value = $d.Parent.CommandElements.Scriptblock.Extent | |
| $Hash.TestCases = $d.Parent.Parent.PipelineElements.CommandElements[-1].Elements | |
| $Obj = New-Object psobject -Property $Hash | |
| $obj.PSObject.TypeNames.Insert(0,'Powershell.Pester.It') | |
| $Obj | |
| } | |
| } | |
| Function Get-PesterDescribeBlock { | |
| [CmdletBinding()] | |
| Param( | |
| $Path = "C:\Users\taavast3\OneDrive\Repo\Projects\OpenSource\PSClassUtils\PSClassUtils\Pester.TEsts.Ps1", | |
| $InputObject | |
| ) | |
| if($Path){ | |
| $P = Get-Item -path $Path | |
| $Raw = [System.Management.Automation.Language.Parser]::ParseFile($p.FullName, [ref]$null, [ref]$Null) | |
| }elseif($InputObject){ | |
| $Raw = [System.Management.Automation.Language.Parser]::ParseInput($InputObject,[ref]$null, [ref]$Null) | |
| } | |
| $String = $Raw.FindAll( {$args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst]}, $true) | |
| $Data = @() | |
| $Data += $String | ? {$_.StringConstantType -eq "BareWord" -and $_.Value -eq 'Describe'} | |
| Foreach($d in $data){ | |
| $Hash = @{} | |
| $Hash.ElementType = $d.Value | |
| $Hash.ItBlocks = @() | |
| $Hash.Tags = "" | |
| $Hash.ItBlocks += Get-PesterITBlock -InputObject $d.Parent.Extent.Text | |
| $Hash.Content = $d.Parent.Extent.Text | |
| $Pattern = '^.*-tag(?<Tags>.*$)' | |
| $Options = @() | |
| $Options += [System.Text.RegularExpressions.RegexOptions]::Multiline | |
| $Options += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase | |
| $rgx = [regex]::New($Pattern,$Options) | |
| $MyMatches = $rgx.Match($d.Parent.Extent.Text) | |
| $Hash.Tags = $MyMatches.Groups['Tags'].Value | |
| $Obj = New-Object psobject -Property $Hash | |
| $obj.PSObject.TypeNames.Insert(0,'Powershell.Pester.Describe') | |
| #$obj.PsObject.TypeNames.Add('Powershell.Pester.Describe') | |
| $obj | |
| } | |
| } | |
| Function Get-PesterScript { | |
| [CmdletBinding()] | |
| Param( | |
| [System.IO.FileInfo]$path | |
| ) | |
| $Hash = @{} | |
| $Hash.Path = $Path.FullName | |
| $Hash.Data = Get-PesterDescribeBlock -Path $path.FullName | |
| $obj = New-Object psobject -Property $Hash | |
| $obj.PSObject.TypeNames.Insert(0,'Powershell.Pester.Script') | |
| #$obj.PsObject.TypeNames.Add('Powershell.Pester.Script') | |
| return $obj | |
| } | |
| $PesterScript = Get-PesterScript -Path $Path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment