Testing/Invoke-TestsFromVSCode.ps1
<# .synopsis Execute tests in the container from VS Code .description Executes test in the container based on the current file and cursor position in VS Code .parameter CurrentFile Path to the file that should be used for testing. If empty, it will test everything .parameter CurrentLine The line number in the file. Based on this, it will determine the function to test .example Invoke-TestsFromVSCode -CurrentFile "C:\test.al" -CurrentLine 12 #> function Invoke-TestsFromVSCode { param( # The current file the tests are invoked from [Parameter(Mandatory=$false)] [string] $CurrentFile, # The current line no. in the current file [Parameter(Mandatory=$false)] [string] $CurrentLine ) $ContainerName = Get-NewContainerName -ContainerName $ContainerName if (!$CurrentFile) { Invoke-BCTest -ContainerName $containerName } else { # determine if the current file is a test codeunit if ((Get-Content $CurrentFile -Raw).Contains('Subtype = Test')) { $TestCodeunit = [Regex]::Match((Get-Content $CurrentFile).Item(0),' \d+ ').Value.Trim() if ($null -ne $CurrentLine) { $Method = Get-TestFromLine -Path $CurrentFile -LineNumber $CurrentLine if ($null -ne $Method) { Invoke-BCTest -ContainerName $containerName -TestCodeunit $TestCodeunit -TestMethod $Method } else { Invoke-BCTest -ContainerName $containerName -TestCodeunit $TestCodeunit -TestMethod '*' } } } else { Invoke-BCTest -ContainerName $containerName } } } function Get-TestFromLine { param ( # file path to search [Parameter(Mandatory=$true)] [string] $Path, # line number to start at [Parameter(Mandatory=$true)] [int] $LineNumber ) $Lines = Get-Content $Path for ($i = ($LineNumber - 1); $i -ge 0; $i--) { if ($Lines.Item($i).Contains('[Test]')) { # search forwards for the procedure declaration (it might not be the following line) for ($j = $i; $j -le $Lines.Count; $j++) { if ($Lines.Item($j).Contains('procedure')) { $ProcDeclaration = $Lines.Item($j) $ProcDeclaration = $ProcDeclaration.Substring($ProcDeclaration.IndexOf('procedure') + 10) $ProcDeclaration = $ProcDeclaration.Substring(0,$ProcDeclaration.IndexOf('(')) return $ProcDeclaration } } } } } Export-ModuleMember Invoke-TestsFromVSCode |