From 8579cb3541775d33e6fc15e0fdbd67ea1a264e39 Mon Sep 17 00:00:00 2001 From: felickz <1760475+felickz@users.noreply.github.com> Date: Mon, 29 Apr 2024 00:53:18 -0400 Subject: [PATCH 1/4] Init the test suite --- .github/workflows/ci.yml | 4 +- .vscode/settings.json | 3 + tests/GitHubActions_tests.ps1 | 183 ---------------------------------- tests/action_tests.ps1 | 106 ++++++++++++++++++++ 4 files changed, 111 insertions(+), 185 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 tests/GitHubActions_tests.ps1 create mode 100644 tests/action_tests.ps1 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1bd8655..0a089fa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: - name: pester tests uses: zyborg/pester-tests-report@bb711b31e93f78f7423086d57f81928325829765 # v1.5.0 with: - include_paths: ./tests/GitHubActions_tests.ps1 + include_paths: ./tests/action_tests.ps1 exclude_tags: SkipCI report_name: action_base_tests report_title: Action Base Tests @@ -66,7 +66,7 @@ jobs: - name: pester tests uses: zyborg/pester-tests-report@bb711b31e93f78f7423086d57f81928325829765 # v1.5.0 with: - include_paths: ./tests/GitHubActions_tests.ps1 + include_paths: ./tests/action_tests.ps1 exclude_tags: SkipCI report_name: action_base_tests-on-win report_title: Action Base Tests (On Windows) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..04ee7db --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "sarif-viewer.connectToGithubCodeScanning": "on" +} \ No newline at end of file diff --git a/tests/GitHubActions_tests.ps1 b/tests/GitHubActions_tests.ps1 deleted file mode 100644 index e15f8a3..0000000 --- a/tests/GitHubActions_tests.ps1 +++ /dev/null @@ -1,183 +0,0 @@ - -Import-Module Pester -Import-Module GitHubActions - -Set-Variable -Scope Script -Option Constant -Name EOL -Value ([System.Environment]::NewLine) -ErrorAction Ignore - -Describe 'Set-ActionVariable' { - $testCases = @( - @{ Name = 'varName1' ; Value = 'varValue1' } - @{ Name = 'var name 2'; Value = 'var value 2' } - @{ Name = 'var,name;3'; Value = 'var,value;3' - Expected = "::set-env name=var%2Cname%3B3::var,value;3$EOL" } - ) - It 'Given valid -Name and -Value, and -SkipLocal' -TestCases $testCases { - param($Name, $Value, $Expected) - - if (-not $Expected) { - $Expected = "::set-env name=$($Name)::$($Value)$EOL" - } - - $output = Set-ActionVariable $Name $Value -SkipLocal - $output | Should -Be $Expected - [System.Environment]::GetEnvironmentVariable($Name) | Should -BeNullOrEmpty - } - It 'Given valid -Name and -Value, and NOT -SkipLocal' -TestCases $testCases { - param($Name, $Value, $Expected) - - if (-not $Expected) { - $Expected = "::set-env name=$($Name)::$($Value)$EOL" - } - - Set-ActionVariable $Name $Value | Should -Be $Expected - [System.Environment]::GetEnvironmentVariable($Name) | Should -Be $Value - } -} - -Describe 'Add-ActionSecretMask' { - It 'Given a valid -Secret' { - $secret = 'f00B@r!' - Add-ActionSecretMask $secret | Should -Be "::add-mask::$($secret)$EOL" - } -} - -Describe 'Add-ActionPath' { - It 'Given a valid -Path and -SkipLocal' { - $addPath = '/to/some/path' - $oldPath = [System.Environment]::GetEnvironmentVariable('PATH') - Add-ActionPath $addPath -SkipLocal | Should -Be "::add-path::$($addPath)$EOL" - [System.Environment]::GetEnvironmentVariable('PATH') | Should -Be $oldPath - } - - It 'Given a valid -Path and NOT -SkipLocal' { - $addPath = '/to/some/path' - $oldPath = [System.Environment]::GetEnvironmentVariable('PATH') - $newPath = "$($addPath)$([System.IO.Path]::PathSeparator)$($oldPath)" - Add-ActionPath $addPath | Should -Be "::add-path::$($addPath)$EOL" - [System.Environment]::GetEnvironmentVariable('PATH') | Should -Be $newPath - } -} - -Describe 'Get-ActionInput' { - [System.Environment]::SetEnvironmentVariable('INPUT_INPUT1', 'Value 1') - [System.Environment]::SetEnvironmentVariable('INPUT_INPUT3', 'Value 3') - - $testCases = @( - @{ Name = 'input1' ; Should = @{ Be = $true; ExpectedValue = 'Value 1' } } - @{ Name = 'INPUT1' ; Should = @{ Be = $true; ExpectedValue = 'Value 1' } } - @{ Name = 'Input1' ; Should = @{ Be = $true; ExpectedValue = 'Value 1' } } - @{ Name = 'input2' ; Should = @{ BeNullOrEmpty = $true } } - @{ Name = 'INPUT2' ; Should = @{ BeNullOrEmpty = $true } } - @{ Name = 'Input2' ; Should = @{ BeNullOrEmpty = $true } } - ) - - It 'Given valid -Name' -TestCases $testCases { - param($Name, $Should) - - Get-ActionInput $Name | Should @Should - Get-ActionInput $Name | Should @Should - Get-ActionInput $Name | Should @Should - Get-ActionInput $Name | Should @Should - Get-ActionInput $Name | Should @Should - Get-ActionInput $Name | Should @Should - } -} - -Describe 'Get-ActionInputs' { - [System.Environment]::SetEnvironmentVariable('INPUT_INPUT1', 'Value 1') - [System.Environment]::SetEnvironmentVariable('INPUT_INPUT3', 'Value 3') - - $testCases = @( - @{ Name = 'InPut1' ; Should = @{ Be = $true; ExpectedValue = "Value 1" } } - @{ Name = 'InPut2' ; Should = @{ BeNullOrEmpty = $true } } - @{ Name = 'InPut3' ; Should = @{ Be = $true; ExpectedValue = "Value 3" } } - ) - - ## We skip this test during CI build because we can't be sure of the actual - ## number of INPUT_ environment variables in the real GH Workflow environment - It 'Given 2 predefined inputs' -Tag 'SkipCI' { - $inputs = Get-ActionInputs - $inputs.Count | Should -Be 2 - } - - It 'Given 2 predefined inputs, and a -Name in any case' -TestCases $testCases { - param($Name, $Should) - - $inputs = Get-ActionInputs - - $key = $Name - $inputs[$key] | Should @Should - $inputs.$key | Should @Should - $key = $Name.ToUpper() - $inputs[$key] | Should @Should - $inputs.$key | Should @Should - $key = $Name.ToLower() - $inputs[$key] | Should @Should - $inputs.$key | Should @Should - } -} - -Describe 'Set-ActionOuput' { - It 'Given a valid -Name and -Value' { - $output = Set-ActionOutput 'foo_bar' 'foo bar value' - $output | Should -Be "::set-output name=foo_bar::foo bar value$EOL" - } -} - -Describe 'Write-ActionDebug' { - It 'Given a valid -Message' { - $output = Write-ActionDebug 'This is a sample message' - $output | Should -Be "::debug::This is a sample message$EOL" - } -} - -Describe 'Write-ActionError' { - It 'Given a valid -Message' { - $output = Write-ActionError 'This is a sample message' - $output | Should -Be "::error::This is a sample message$EOL" - } -} - -Describe 'Write-ActionWarning' { - It 'Given a valid -Message' { - $output = Write-ActionWarning 'This is a sample message' - $output | Should -Be "::warning::This is a sample message$EOL" - } -} - -Describe 'Write-ActionInfo' { - It 'Given a valid -Message' { - $output = Write-ActionInfo 'This is a sample message' - $output | Should -Be "This is a sample message$EOL" - } -} - -Describe 'Enter-ActionOutputGroup' { - It 'Given a valid -Name' { - $output = Enter-ActionOutputGroup 'Sample Group' - $output | Should -Be "::group::Sample Group$EOL" - } -} - -Describe 'Exit-ActionOutputGroup' { - It 'Given everything is peachy' { - $output = Exit-ActionOutputGroup - $output | Should -Be "::endgroup::$EOL" - } -} - -Describe 'Invoke-ActionWithinOutputGroup' { - It 'Given a valid -Name and -ScriptBlock' { - $output = Invoke-ActionWithinOutputGroup 'Sample Group' { - Write-ActionInfo "Message 1" - Write-ActionInfo "Message 2" - } - - $output | Should -Be @( - "::group::Sample Group$EOL" - "Message 1$EOL" - "Message 2$EOL" - "::endgroup::$EOL" - ) - } -} diff --git a/tests/action_tests.ps1 b/tests/action_tests.ps1 new file mode 100644 index 0000000..7c48051 --- /dev/null +++ b/tests/action_tests.ps1 @@ -0,0 +1,106 @@ +# Import the Pester module +Import-Module Pester + +# Define the tests +Describe "Require a token" { + BeforeAll { + # Set up any variables or conditions needed before running the tests + #$env:GITHUB_TOKEN = 'test_token' + + # Run the script and capture the exit code + $output = .\..\action.ps1 + } + + It "action.ps1 should fail and require a token" { + $errorMessageFound = $output | Where-Object { $_ -match "::error::GitHubToken is not set" } + $errorMessageFound | Should -Not -BeNullOrEmpty + $LASTEXITCODE | Should -Be 1 + } +} + +Describe "Install Dependencies" { + BeforeAll { + # Set up any variables or conditions needed before running the tests + $env:GITHUB_REPOSITORY = 'octodemo/demo-vulnerabilities-ghas' + $env:GITHUB_REF = 'refs/pull/120/merge' + $env:GITHUB_STEP_SUMMARY = $(New-Item -Name /_temp/_runner_file_commands/step_summary_a01d8a3b-1412-4059-9cf1-f7c4b54cff76 -ItemType File -Force).FullName + $env:SSR_FAIL_ON_ALERT = "true" + $env:SSR_FAIL_ON_ALERT_EXCLUDE_CLOSED = "true" + + # Delete all environment variables + # $env:GITHUB_TOKEN = $null + # $env:GITHUB_REPOSITORY = $null + # $env:GITHUB_REF = $null + # $env:GITHUB_STEP_SUMMARY = $null + # $env:SSR_FAIL_ON_ALERT = $null + # $env:SSR_FAIL_ON_ALERT_EXCLUDE_CLOSED = $null + + + # Run the script and capture the exit code + $output = .\..\action.ps1 + } + + + It "GitHubActions module is installed" { + $module = Get-Module -ListAvailable -Name GitHubActions -ErrorAction SilentlyContinue + $module | Should -Not -BeNullOrEmpty + } + + It "PowerShellForGitHub module is installed" { + $module = Get-Module -ListAvailable -Name PowerShellForGitHub -ErrorAction SilentlyContinue + $module | Should -Not -BeNullOrEmpty + } + + +} + +# Import the module that contains the function to test +#Import-Module GitHubActions + +# Define the tests +Describe "Mocked Tests" { + # Mock the Get-GitHubPullRequest function to return a predefined object + # Get-GitHubPullRequest -ModuleName GitHubActions { + # Mock Get-GitHubPullRequest{ + # return @{ + # Title = 'Test PR' + # commits = 1 + # } + # } + + # BeforeAll { + # # Set up any variables or conditions needed before running the tests + # $env:GITHUB_REPOSITORY = 'octodemo/demo-vulnerabilities-ghas' + # $env:GITHUB_REF = 'refs/pull/120/merge' + # $env:GITHUB_STEP_SUMMARY = $(New-Item -Name /_temp/_runner_file_commands/step_summary_a01d8a3b-1412-4059-9cf1-f7c4b54cff76 -ItemType File -Force).FullName + # $env:SSR_FAIL_ON_ALERT = "true" + # $env:SSR_FAIL_ON_ALERT_EXCLUDE_CLOSED = "true" + # } + + + # Context "context"{ + # function Get-GitHubPullRequest { + # "placeholder function" + # } + + # Mock Get-GitHubPullRequest{ + # return @{ + # Title = 'Test PR' + # commits = 1 + # } + # } + + # It "GitHubActions module is installed" { + # # Run the script and capture the exit code + # $output = & .\..\action.ps1 -GitHubToken 'test_token' + + + # Write-Host $output + # Assert-MockCalled Get-GitHubPullRequest -Times 1 -ModuleName 'GitHubActions' + + + # } + + # } + +} \ No newline at end of file From 6415cee91d03f8ff71079befe00a7350b994760c Mon Sep 17 00:00:00 2001 From: felickz <1760475+felickz@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:27:56 -0400 Subject: [PATCH 2/4] cross plat invocations :D `\` to `/` --- tests/action_tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/action_tests.ps1 b/tests/action_tests.ps1 index 7c48051..c4dacfd 100644 --- a/tests/action_tests.ps1 +++ b/tests/action_tests.ps1 @@ -8,7 +8,7 @@ Describe "Require a token" { #$env:GITHUB_TOKEN = 'test_token' # Run the script and capture the exit code - $output = .\..\action.ps1 + $output = ./../action.ps1 } It "action.ps1 should fail and require a token" { @@ -37,7 +37,7 @@ Describe "Install Dependencies" { # Run the script and capture the exit code - $output = .\..\action.ps1 + $output = ./../action.ps1 } @@ -92,7 +92,7 @@ Describe "Mocked Tests" { # It "GitHubActions module is installed" { # # Run the script and capture the exit code - # $output = & .\..\action.ps1 -GitHubToken 'test_token' + # $output = & ./../action.ps1 -GitHubToken 'test_token' # Write-Host $output From c622d0474aef53da5fbd4b99b54cfe1d2fdc893f Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:08:08 -0400 Subject: [PATCH 3/4] Remove pester secrets --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a089fa..ae5c111 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,8 +26,8 @@ jobs: report_name: action_base_tests report_title: Action Base Tests gist_name: pwsh-github-action-base_tests.md - github_token: ${{ secrets.GITHUB_TOKEN }} - gist_token: ${{ secrets.GIST_TOKEN }} + # github_token: ${{ secrets.GITHUB_TOKEN }} + # gist_token: ${{ secrets.GIST_TOKEN }} gist_badge_label: Tests %ExecutedAt% # - name: pester tests manually @@ -70,4 +70,4 @@ jobs: exclude_tags: SkipCI report_name: action_base_tests-on-win report_title: Action Base Tests (On Windows) - github_token: ${{ secrets.GITHUB_TOKEN }} + # github_token: ${{ secrets.GITHUB_TOKEN }} From cddd32b76a22fe4afcb9d80fc71f2b377632fd39 Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Mon, 29 Apr 2024 10:08:31 -0400 Subject: [PATCH 4/4] No gists --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae5c111..4280559 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,10 +25,10 @@ jobs: exclude_tags: SkipCI report_name: action_base_tests report_title: Action Base Tests - gist_name: pwsh-github-action-base_tests.md + #gist_name: pwsh-github-action-base_tests.md # github_token: ${{ secrets.GITHUB_TOKEN }} # gist_token: ${{ secrets.GIST_TOKEN }} - gist_badge_label: Tests %ExecutedAt% + #gist_badge_label: Tests %ExecutedAt% # - name: pester tests manually # shell: pwsh