Skip to content

Commit

Permalink
switch to pester
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Mar 28, 2023
1 parent d6f5358 commit 695a601
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 142 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/script-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,3 @@ jobs:
- run: make test
working-directory: updater

cli-integration:
name: CLI integration @ ${{ matrix.host }}
runs-on: ${{ matrix.host }}-latest
strategy:
fail-fast: false
matrix:
host:
- ubuntu
- macos
- windows
defaults:
run:
working-directory: sentry-cli/integration-test
steps:
- run: git config --global core.autocrlf false
working-directory: .
- uses: actions/checkout@v3
- run: pwsh ./execute.ps1 tests/run-script.ps1
- run: git diff --no-index tests/script-output.txt tests/script-output.expected
- run: git diff --no-index tests/server-output.txt tests/server-output.expected
15 changes: 10 additions & 5 deletions .github/workflows/workflow-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ jobs:
- run: "[[ '${{ needs.updater-test-args.outputs.prUrl }}' == '' ]]"
- run: "[[ '${{ needs.updater-test-args.outputs.prBranch }}' == '' ]]"

cli-test-args:
runs-on: ubuntu-latest
cli-integration:
runs-on: ${{ matrix.host }}-latest
strategy:
fail-fast: false
matrix:
host:
- ubuntu
- macos
- windows
steps:
- uses: actions/checkout@v3

- uses: ./sentry-cli/integration-test/
with:
script: sentry-cli/integration-test/tests/run-script.ps1
script-output-expected: sentry-cli/integration-test/tests/script-output.expected
server-output-expected: sentry-cli/integration-test/tests/server-output.expected
path: sentry-cli/integration-test/tests/
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
# Executes the script at the given path after starting a dummy Sentry server that collects and logs requests.
# The script is given the server URL as a first argument.
param(
[Parameter(Mandatory = $true)][string] $Script,
[Parameter(Mandatory = $false)][string] $OutputDirectory
)

if ("$OutputDirectory" -eq "")
{
$OutputDirectory = (Get-Item $Script).DirectoryName
}

$ErrorActionPreference = "Stop"
Set-StrictMode -Version latest
# Executes the given block starting a dummy Sentry server that collects and logs requests.
# The block is given the server URL as a first argument.
# Returns the dummy server logs.

$ServerUri = "http://127.0.0.1:8000"
$ServerOutFile = "$OutputDirectory/server-output.txt"
$ScriptOutFile = "$OutputDirectory/script-output.txt"

Remove-Item -Path $ServerOutFile -ErrorAction SilentlyContinue
Remove-Item -Path $ScriptOutFile -ErrorAction SilentlyContinue

function RunApiServer([string] $ServerScript, [string] $Uri = $ServerUri)
{
Expand Down Expand Up @@ -50,7 +34,7 @@ function RunApiServer([string] $ServerScript, [string] $Uri = $ServerUri)

Remove-Item $result.outFile -ErrorAction Continue
Remove-Item $result.errFile -ErrorAction Continue
return "$stdout`n$stderr".Trim() -replace $Uri, "<ServerUri>" -replace "`r`n", "`n"
return ("$stdout`n$stderr".Trim() -replace $Uri, "<ServerUri>" -replace "`r`n", "`n") -split "`n" | ForEach-Object { $_.Trim() }
}.GetNewClosure()

$result.stop = {
Expand Down Expand Up @@ -97,71 +81,32 @@ function RunApiServer([string] $ServerScript, [string] $Uri = $ServerUri)

if ($result.process.HasExited -or $startupFailed)
{
Write-Host "Couldn't start the $ServerScript" -ForegroundColor Red
$result.stop.Invoke()
$result.dispose.Invoke()
exit 1
throw Write-Host "Couldn't start the $ServerScript"
}

return $result
}

function RunWithApiServer([ScriptBlock] $Callback)
function With-Sentry-Server([ScriptBlock] $Callback)
{
# start the server
$httpServer = RunApiServer "sentry-server"
# run the test

$result = @()
try
{
$Callback.Invoke()
# run the test
Invoke-Command -ScriptBlock $Callback -ArgumentList $ServerUri
}
finally
{
$httpServer.stop.Invoke()
$result = $httpServer.dispose.Invoke()
}

return $httpServer.dispose.Invoke()
}

if (Get-Command 'chmod' -ErrorAction SilentlyContinue)
{
chmod +x $Script
if ($LastExitCode -ne 0)
{
throw "chmod failed";
}
}

function Append([string] $File, $Value)
{
$info = Get-Item $file -ErrorAction SilentlyContinue
if ($null -ne $info -and $info.Length -gt 0)
{
"`n" | Out-File $file -Encoding utf8 -Append -NoNewline
}
$value | Out-File $file -Encoding utf8 -Append -NoNewline
}

$serverOutput = RunWithApiServer -Callback {
try
{
Write-Host "Running $Script $ServerUri" -ForegroundColor DarkYellow
& $Script $ServerUri | ForEach-Object {
Write-Host " $_"
Append -File $ScriptOutFile -Value $_
}
if (-not $?)
{
throw "Script execution failed"
}
}
catch
{
Append -File $ScriptOutFile -Value $_
Write-Error " $_"
}
Write-Host "Script finished successfully" -ForegroundColor Green
return $result
}

Append -File $ServerOutFile -Value $serverOutput
Write-Host "Outputs written to '$ServerOutFile' and '$ScriptOutFile'" -ForegroundColor Green
Export-ModuleMember -Function With-Sentry-Server
39 changes: 9 additions & 30 deletions sentry-cli/integration-test/action.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,21 @@
name: Sentry CLI integration test

description: |
Action to test Sentry CLI integration & symbol upload.
Executes the script at the given path after starting a dummy Sentry server that collects and logs requests.
The script is given the server URL as a first argument. Server and script output are saved in `server-output.txt` and
`script-output.txt` respectively.
Action to test Sentry CLI integration & symbol upload. This action simply runs all the https://github.com/pester/Pester
tests in the given directory. The tests can make use of a dummy Sentry server that collects uploaded symbols.
This server is made available as a PowerShell module to your tests.
inputs:
script:
description: Script to run.
path:
description: The directory containing all the tests.
required: true
server-output-expected:
description: If given, the server-produced output must match the file at this path.
required: false
default: ''
script-output-expected:
description: If given, the script-produced output must match the file at this path.
required: false
default: ''

runs:
using: composite

steps:
- name: Run ${{ inputs.script }}
id: run
- name: Run tests
shell: pwsh
run: |
directory=$(dirname '${{ inputs.script }}')
echo "directory=$directory" >> $GITHUB_OUTPUT
pwsh '${{ github.action_path }}/execute.ps1' -Script '${{ inputs.script }}' -OutputDirectory "$directory"
shell: bash

- name: Check script-output.txt
run: git diff --no-index ${{ steps.run.outputs.directory }}/script-output.txt ${{ inputs.script-output-expected }}
if: ${{ inputs.script-output-expected }}
shell: bash

- name: Check server-output.txt
run: git diff --no-index ${{ steps.run.outputs.directory }}/server-output.txt ${{ inputs.server-output-expected }}
if: ${{ inputs.server-output-expected }}
shell: bash
Import-Module -Name ${{ github.action_path }}/action.psm1
Invoke-Pester -Output Detailed '${{ inputs.path }}'
21 changes: 21 additions & 0 deletions sentry-cli/integration-test/tests/action.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# In CI, the module is expected to be loaded
if (!(Test-Path env:CI ))
{
Import-Module $PSScriptRoot/../action.psm1
}

Describe 'With-Sentry-Server' {
It "works fine with a simple callback" {
$output = With-Sentry-Server {
Param([string]$url)
$url | Should -Be "http://127.0.0.1:8000"
}
$output | Should -BeOfType [string]
$output | Should -Match 'HTTP server listening on <ServerUri>'
}
It "rethrows an exception and recovers" {
{ With-Sentry-Server { throw "hello there" } } | Should -Throw "hello there"
$output = With-Sentry-Server {}
$output | Should -Match 'HTTP server listening on <ServerUri>'
}
}
13 changes: 0 additions & 13 deletions sentry-cli/integration-test/tests/run-script.ps1

This file was deleted.

1 change: 0 additions & 1 deletion sentry-cli/integration-test/tests/script-output.expected

This file was deleted.

4 changes: 0 additions & 4 deletions sentry-cli/integration-test/tests/server-output.expected

This file was deleted.

0 comments on commit 695a601

Please sign in to comment.