-
Notifications
You must be signed in to change notification settings - Fork 200
RunTests action #2343
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
Draft
spetersenms
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
spetersenms:spetersen/separateTestAction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
RunTests action #2343
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de9d2ca
Add separate RunTests action gated by useSeparateTestAction
spetersenms 6550465
Documentation cleanup and skip action when relevant.
spetersenms d375a8b
Merge branch 'main' into spetersen/separateTestAction
spetersenms cd2c03a
Fix failing tests
spetersenms d3b320f
Merge branch 'main' into spetersen/separateTestAction
spetersenms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Run tests | ||
|
|
||
| Run the normal tests (testFolders) for an AL-Go project against the build container created and kept alive by the RunPipeline action. | ||
|
|
||
| This action only does anything when the `useSeparateTestAction` setting is enabled. In that case, the RunPipeline action compiles, publishes and installs the apps, skips the normal tests and keeps the build container alive. This action then runs the normal tests against that same container and writes the results to `TestResults.xml` in the project folder. | ||
|
|
||
| Only normal tests (testFolders) are handled here. BCPT and page scripting tests continue to be executed by the RunPipeline action. | ||
|
|
||
| ## INPUT | ||
|
|
||
| ### ENV variables | ||
|
|
||
| | Name | Description | | ||
| | :-- | :-- | | ||
| | Settings | env.Settings must be set by a prior call to the ReadSettings Action | | ||
| | containerName | env.containerName is set by the RunPipeline action and identifies the container to run tests against (the container name is otherwise derived from the project) | | ||
|
|
||
| ### Parameters | ||
|
|
||
| | Name | Required | Description | Default value | | ||
| | :-- | :-: | :-- | :-- | | ||
| | shell | | The shell (powershell or pwsh) in which the PowerShell script should run | powershell | | ||
| | project | | Project folder | '.' | | ||
| | installTestAppsJson | | Path to a JSON file containing a list of test apps to run tests in | '' | | ||
|
|
||
| ## OUTPUT | ||
|
|
||
| None |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| Param( | ||
| [Parameter(HelpMessage = "The GitHub token running the action", Mandatory = $false)] | ||
| [string] $token, | ||
| [Parameter(HelpMessage = "Project folder", Mandatory = $false)] | ||
| [string] $project = "", | ||
| [Parameter(HelpMessage = "A path to a JSON-formatted list of test apps to run tests in", Mandatory = $false)] | ||
| [string] $installTestAppsJson = '' | ||
| ) | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Runs the normal tests (testFolders) for an AL-Go project against the build container | ||
| created and kept alive by the RunPipeline action. | ||
| .DESCRIPTION | ||
| Runs the normal tests (testFolders) of an AL-Go project against the build container that the | ||
| RunPipeline action created and kept alive. This runs as part of the build when the | ||
| useSeparateTestAction setting is enabled; when it is not, RunPipeline runs the tests instead. | ||
| Results are written to TestResults.xml in the project folder. | ||
|
|
||
| Only normal tests (testFolders) are run here. BCPT and page scripting tests are run by the | ||
| RunPipeline action. | ||
| .PARAMETER token | ||
| The GitHub token running the action. It is exposed as the _token environment variable so | ||
| downstream test override scripts (for example, BCApps test tolerance, which downloads the | ||
| unstable-tests artifact) can authenticate against GitHub. | ||
| .PARAMETER project | ||
| Project folder. | ||
| .PARAMETER installTestAppsJson | ||
| A path to a JSON-formatted list of test apps (produced by previous jobs) to run tests in. | ||
| .EXAMPLE | ||
| RunTests.ps1 -project 'MyProject' | ||
| #> | ||
|
|
||
| . (Join-Path -Path $PSScriptRoot -ChildPath "..\AL-Go-Helper.ps1" -Resolve) | ||
| Import-Module (Join-Path $PSScriptRoot '..\TelemetryHelper.psm1' -Resolve) | ||
| Import-Module (Join-Path $PSScriptRoot 'RunTests.psm1' -Resolve) -DisableNameChecking -Force | ||
| DownloadAndImportBcContainerHelper | ||
|
|
||
| function Get-TestRunnerCredential { | ||
| <# | ||
| .SYNOPSIS | ||
| Returns the credential used by the test runner to connect to the build container. | ||
| .DESCRIPTION | ||
| RunPipeline creates the container and keeps it alive when useSeparateTestAction is set. | ||
| When RunPipeline surfaces the container credential (masked, as base64-encoded JSON in | ||
| the containerCredential environment variable), it is used here so the test runner can | ||
| connect to the same container. Otherwise a default credential is used. | ||
| #> | ||
| [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'The container credential is surfaced by RunPipeline as plain text')] | ||
| param() | ||
| if ($ENV:containerCredential) { | ||
| $credentialJson = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($ENV:containerCredential)) | ConvertFrom-Json | ||
| $securePassword = ConvertTo-SecureString -String $credentialJson.password -AsPlainText -Force | ||
| return New-Object System.Management.Automation.PSCredential($credentialJson.username, $securePassword) | ||
| } | ||
| $securePassword = ConvertTo-SecureString -String ([GUID]::NewGuid().ToString()) -AsPlainText -Force | ||
| return New-Object System.Management.Automation.PSCredential("admin", $securePassword) | ||
| } | ||
|
|
||
| if ($project -eq ".") { $project = "" } | ||
|
|
||
| $baseFolder = $ENV:GITHUB_WORKSPACE | ||
| $projectPath = Join-Path $baseFolder $project | ||
|
|
||
| Write-Host "Use settings" | ||
| $settings = $env:Settings | ConvertFrom-Json | ConvertTo-HashTable | ||
|
|
||
| # Surface the token so RunTestsInBcContainer override scripts (e.g. BCApps test tolerance) can authenticate against GitHub. | ||
| $ENV:_token = $token | ||
|
|
||
| # Analyze the repository to determine the test folders (and other test related settings) | ||
| $settings = AnalyzeRepo -settings $settings -baseFolder $baseFolder -project $project -doNotCheckArtifactSetting | ||
|
|
||
| # Resolve the container kept alive by RunPipeline (name is deterministic per project, also exported to the environment). | ||
| $containerName = $ENV:containerName | ||
| if (-not $containerName) { | ||
| $containerName = GetContainerName($project) | ||
| } | ||
|
|
||
| # Credentials used to connect to the build container. | ||
| $credential = Get-TestRunnerCredential | ||
|
|
||
| # A RunTestsInBcContainer override script, if present, replaces the built-in BcContainerHelper test runner. | ||
| $overrideParams = Get-ScriptOverrides -ALGoFolderName (Join-Path $projectPath ".AL-Go") -OverrideScriptNames @("RunTestsInBcContainer") | ||
|
|
||
| Invoke-AlGoTestRun ` | ||
| -settings $settings ` | ||
| -projectPath $projectPath ` | ||
| -containerName $containerName ` | ||
| -credential $credential ` | ||
| -installTestAppsJson $installTestAppsJson ` | ||
| -runTestsOverride $overrideParams['RunTestsInBcContainer'] | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Helper module for the RunTests action. | ||
| .DESCRIPTION | ||
| Contains the logic for running the normal tests (testFolders) of an AL-Go project against a | ||
| build container that was created and kept alive by the RunPipeline action. Kept in a module | ||
| so the logic can be unit tested independently of the action entry script. | ||
| #> | ||
|
|
||
| function Get-TestAppsToRun { | ||
| <# | ||
| .SYNOPSIS | ||
| Determines the set of test app files to run tests in. | ||
| .DESCRIPTION | ||
| Collects the test apps compiled for the project (found in the build artifacts TestApps | ||
| folder) and, when runTestsInAllInstalledTestApps is enabled, the test apps installed from | ||
| previous jobs (listed in installTestAppsJson). Test apps wrapped in parentheses are | ||
| unwrapped (matching Run-AlPipeline semantics where such apps are otherwise not tested). | ||
| .PARAMETER settings | ||
| The (analyzed) AL-Go settings hashtable. | ||
| .PARAMETER projectPath | ||
| The full path to the project folder. | ||
| .PARAMETER installTestAppsJson | ||
| Path to a JSON file with the list of installed test apps. | ||
| #> | ||
| Param( | ||
| [hashtable] $settings, | ||
| [string] $projectPath, | ||
| [string] $installTestAppsJson = '' | ||
| ) | ||
|
|
||
| $testAppOutputFolder = Join-Path $projectPath ".buildartifacts\TestApps" | ||
|
|
||
| $testApps = @() | ||
| if (Test-Path $testAppOutputFolder) { | ||
| $testApps += @(Get-ChildItem -Path $testAppOutputFolder -Filter "*.app" -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName }) | ||
| } | ||
|
|
||
| if ($settings.runTestsInAllInstalledTestApps -and $installTestAppsJson -and (Test-Path $installTestAppsJson)) { | ||
| try { | ||
| $installedTestApps = @(Get-Content -Path $installTestAppsJson -Raw | ConvertFrom-Json) | ||
| } | ||
| catch { | ||
| throw "Failed to parse JSON file at path '$installTestAppsJson'. Error: $($_.Exception.Message)" | ||
| } | ||
| $testApps += @($installedTestApps | ForEach-Object { $_.TrimStart("(").TrimEnd(")") } | Where-Object { $_ -and (Test-Path $_) }) | ||
| } | ||
|
|
||
| return @($testApps | Select-Object -Unique) | ||
| } | ||
|
|
||
| function Invoke-AlGoTestRun { | ||
| <# | ||
| .SYNOPSIS | ||
| Runs the normal tests for an AL-Go project against a kept-alive build container. | ||
| .DESCRIPTION | ||
| Runs tests in each test app against the given container and writes the results to | ||
| testResultsFile in JUnit format. Honors the treatTestFailuresAsWarnings setting. When a | ||
| RunTestsInBcContainer override script is provided, it is used instead of the built-in | ||
| BcContainerHelper test runner. | ||
| .PARAMETER settings | ||
| The (analyzed) AL-Go settings hashtable. | ||
| .PARAMETER projectPath | ||
| The full path to the project folder. | ||
| .PARAMETER containerName | ||
| The name of the build container to run the tests against. | ||
| .PARAMETER credential | ||
| The credential used to connect to the build container. | ||
| .PARAMETER installTestAppsJson | ||
| Path to a JSON file with the list of installed test apps. | ||
| .PARAMETER runTestsOverride | ||
| Optional scriptblock overriding the BcContainerHelper test runner (RunTestsInBcContainer). | ||
| #> | ||
| Param( | ||
| [hashtable] $settings, | ||
| [string] $projectPath, | ||
| [string] $containerName, | ||
| [System.Management.Automation.PSCredential] $credential, | ||
| [string] $installTestAppsJson = '', | ||
| [scriptblock] $runTestsOverride = $null | ||
| ) | ||
|
|
||
| $testApps = Get-TestAppsToRun -settings $settings -projectPath $projectPath -installTestAppsJson $installTestAppsJson | ||
| if (@($testApps).Count -eq 0) { | ||
| Write-Host "No test apps found to run tests in. Skipping test execution." | ||
| return | ||
| } | ||
|
|
||
| Write-Host "Running tests against container '$containerName'" | ||
|
|
||
| $testResultsFile = Join-Path $projectPath "TestResults.xml" | ||
| if (Test-Path $testResultsFile) { | ||
| Remove-Item $testResultsFile -Force | ||
| } | ||
|
|
||
| # Test failures surface as warnings when treatTestFailuresAsWarnings is set, otherwise as errors. | ||
| $gitHubActionsSeverity = if ($settings.treatTestFailuresAsWarnings) { 'warning' } else { 'error' } | ||
|
|
||
| $allTestsPassed = $true | ||
| Push-Location $projectPath | ||
| try { | ||
| foreach ($testApp in $testApps) { | ||
| $appJson = Get-AppJsonFromAppFile -appFile $testApp | ||
| Write-Host "Running tests in $($appJson.name) ($($appJson.id))" | ||
|
|
||
| $runTestsParams = @{ | ||
| "containerName" = $containerName | ||
| "credential" = $credential | ||
| "companyName" = $settings.companyName | ||
| "extensionId" = $appJson.id | ||
| "appName" = $appJson.name | ||
| "JUnitResultFileName" = $testResultsFile | ||
| "AppendToJUnitResultFile" = $true | ||
| "detailed" = $true | ||
| "GitHubActions" = $gitHubActionsSeverity | ||
| "returnTrueIfAllPassed" = $true | ||
| } | ||
|
|
||
| if ($runTestsOverride) { | ||
| $passed = & $runTestsOverride -parameters $runTestsParams | ||
| } | ||
| else { | ||
| $passed = Run-TestsInBcContainer @runTestsParams | ||
| } | ||
|
|
||
| if (-not $passed) { | ||
| $allTestsPassed = $false | ||
| } | ||
| } | ||
| } | ||
| finally { | ||
| Pop-Location | ||
| } | ||
|
|
||
| if (-not $allTestsPassed) { | ||
| if ($settings.treatTestFailuresAsWarnings) { | ||
| OutputWarning -message "There are test failures, but they are treated as warnings (treatTestFailuresAsWarnings is set)." | ||
| } | ||
| else { | ||
| throw "There are test failures." | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Export-ModuleMember -Function Invoke-AlGoTestRun, Get-TestAppsToRun |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| name: Run Tests | ||
| author: Microsoft Corporation | ||
| inputs: | ||
| shell: | ||
| description: Shell in which you want to run the action (powershell or pwsh) | ||
| required: false | ||
| default: powershell | ||
| token: | ||
| description: The GitHub token running the action | ||
| required: false | ||
| default: ${{ github.token }} | ||
| project: | ||
| description: Project folder | ||
| required: false | ||
| default: '.' | ||
| installTestAppsJson: | ||
| description: A path to a JSON-formatted list of test apps to run tests in | ||
| required: false | ||
| default: '' | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: run | ||
| shell: ${{ inputs.shell }} | ||
| env: | ||
| _token: ${{ inputs.token }} | ||
| _project: ${{ inputs.project }} | ||
| _installTestAppsJson: ${{ inputs.installTestAppsJson }} | ||
| run: | | ||
| ${{ github.action_path }}/../Invoke-AlGoAction.ps1 -ActionName "RunTests" -Action { | ||
| ${{ github.action_path }}/RunTests.ps1 -token $ENV:_token -project $ENV:_project -installTestAppsJson $ENV:_installTestAppsJson | ||
| } | ||
| branding: | ||
| icon: terminal | ||
| color: blue |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.