AL-Go/Tests/CheckForUpdates.Action.Test...

126 строки
5.6 KiB
PowerShell

Get-Module TestActionsHelper | Remove-Module -Force
Import-Module (Join-Path $PSScriptRoot 'TestActionsHelper.psm1')
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
Describe "CheckForUpdates Action Tests" {
BeforeAll {
$actionName = "CheckForUpdates"
$scriptRoot = Join-Path $PSScriptRoot "..\Actions\$actionName" -Resolve
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'actionScript', Justification = 'False positive.')]
$actionScript = GetActionScript -scriptRoot $scriptRoot -scriptName "$actionName.ps1"
}
It 'Compile Action' {
Invoke-Expression $actionScript
}
It 'Test action.yaml matches script' {
$permissions = [ordered]@{
"contents" = "write"
"pull-requests" = "write"
"workflows" = "write"
}
$outputs = [ordered]@{
}
YamlTest -scriptRoot $scriptRoot -actionName $actionName -actionScript $actionScript -permissions $permissions -outputs $outputs
}
It 'Test YamlClass' {
. (Join-Path $scriptRoot "yamlclass.ps1")
$yaml = [Yaml]::load((Join-Path $PSScriptRoot 'YamlSnippet.txt'))
# Yaml file should have 77 entries
$yaml.content.Count | Should -be 73
$start = 0; $count = 0
# Locate lines for permissions section (including permissions: line)
$yaml.Find('permissions:', [ref] $start, [ref] $count) | Should -be $true
$start | Should -be 17
$count | Should -be 5
# Locate lines for permissions section (excluding permissions: line)
$yaml.Find('permissions:/', [ref] $start, [ref] $count) | Should -be $true
$start | Should -be 18
$count | Should -be 4
# Get Yaml class for permissions section (excluding permissions: line)
$yaml.Get('permissions:/').content | ForEach-Object { $_ | Should -not -belike ' *' }
# Locate section called permissionos (should return false)
$yaml.Find('permissionos:', [ref] $start, [ref] $count) | Should -Not -be $true
# Check checkout step
($yaml.Get('jobs:/Initialization:/steps:/- name: Checkout').content -join '') | Should -be "- name: Checkout uses: actions/checkout@v4 with: lfs: true"
# Get Shell line in read Settings step
($yaml.Get('jobs:/Initialization:/steps:/- name: Read settings/with:/shell:').content -join '') | Should -be "shell: powershell"
# Get Jobs section (without the jobs: line)
$jobsYaml = $yaml.Get('jobs:/')
# Locate CheckForUpdates
$jobsYaml.Find('CheckForUpdates:', [ref] $start, [ref] $count) | Should -be $true
$start | Should -be 23
$count | Should -be 19
# Replace all occurances of 'shell: powershell' with 'shell: pwsh'
$yaml.ReplaceAll('shell: powershell','shell: pwsh')
$yaml.content[46].Trim() | Should -be 'shell: pwsh'
# Replace Permissions
$yaml.Replace('Permissions:/',@('contents: write','actions: read'))
$yaml.content[44].Trim() | Should -be 'shell: pwsh'
$yaml.content.Count | Should -be 71
# Get Jobs section (without the jobs: line)
$jobsYaml = $yaml.Get('jobs:/')
($jobsYaml.Get('Initialization:/steps:/- name: Read settings/with:/shell:').content -join '') | Should -be "shell: pwsh"
}
It 'Test YamlClass Remove' {
. (Join-Path $scriptRoot "yamlclass.ps1")
$yamlSnippet = @(
"permissions:",
" contents: read",
" actions: read",
" pull-requests: write",
" checks: write"
)
$permissionsYaml = [Yaml]::new($yamlSnippet)
$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(1, 0) # Remove nothing
$permissionsContent.content.Count | Should -be 4
$permissionsContent.content[0].Trim() | Should -be 'contents: read'
$permissionsContent.content[1].Trim() | Should -be 'actions: read'
$permissionsContent.content[2].Trim() | Should -be 'pull-requests: write'
$permissionsContent.content[3].Trim() | Should -be 'checks: write'
$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(0, 3) # Remove first 3 lines
$permissionsContent.content.Count | Should -be 1
$permissionsContent.content[0].Trim() | Should -be 'checks: write'
$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(2, 1) # Remove only the 3rd line
$permissionsContent.content.Count | Should -be 3
$permissionsContent.content[0].Trim() | Should -be 'contents: read'
$permissionsContent.content[1].Trim() | Should -be 'actions: read'
$permissionsContent.content[2].Trim() | Should -be 'checks: write'
$permissionsContent = $permissionsYaml.Get('permissions:/')
$permissionsContent.content.Count | Should -be 4
$permissionsContent.Remove(2, 4) # Remove more than the number of lines
$permissionsContent.content.Count | Should -be 2 # Only the first two lines should remain
$permissionsContent.content[0].Trim() | Should -be 'contents: read'
$permissionsContent.content[1].Trim() | Should -be 'actions: read'
}
# Call action
}