Files
node-versions/tests/Node.Tests.ps1
Haritha b845aa0df4 Log retrieval logic update (#232)
* Update log retrieval logic in Node.Tests.ps1

Refactor Get-UseNodeLogs function to find logs in runner root directory instead of home directory.

* Update comment to reflect the correct directory

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Modify path retrieval logic

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-13 21:48:59 -06:00

88 lines
3.8 KiB
PowerShell

Import-Module (Join-Path $PSScriptRoot "../helpers/pester-extensions.psm1")
Describe "Node.js" {
BeforeAll {
function Get-UseNodeLogs {
$runnerProc = Get-Process -Name "Runner.Listener" -ErrorAction SilentlyContinue | Select-Object -First 1
#Write-Host "`$runnerProc: $($runnerProc | Out-String)"
if (-not $runnerProc -or -not $runnerProc.Path) {
Write-Error "Runner.Listener process not found."
return
}
# Go up two directories to get runner root
$runnerRoot = Split-Path (Split-Path $runnerProc.Path -Parent) -Parent
#Write-Host "`$runnerRoot: $runnerRoot"
# Recursively find all _diag/pages folders under the runner root directory
$possiblePaths = Get-ChildItem -Path $runnerRoot -Directory -Recurse -Depth 4 -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*_diag\pages" -or $_.FullName -like "*_diag/pages" }
Write-Host "LogsPaths:"
$possiblePaths | ForEach-Object { Write-Host $_.FullName }
$logsFolderPath = $possiblePaths | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($logsFolderPath) {
$resolvedPath = Resolve-Path -Path $logsFolderPath -ErrorAction SilentlyContinue
}
if ($resolvedPath -and -not [string]::IsNullOrEmpty($resolvedPath.Path) -and (Test-Path $resolvedPath.Path)) {
$useNodeLogFile = Get-ChildItem -Path $resolvedPath | Where-Object {
$logContent = Get-Content $_.Fullname -Raw
return $logContent -match "setup-node@v"
} | Select-Object -First 1
# Return the file name if a match is found
if ($useNodeLogFile) {
return $useNodeLogFile.FullName
} else {
Write-Error "No matching log file found in the specified path: $($resolvedPath.Path)"
}
} else {
Write-Error "The provided logs folder path is null, empty, or does not exist: $logsFolderPath"
}
}
}
It "is available" {
"node --version" | Should -ReturnZeroExitCode
}
It "version is correct" {
$versionOutput = Invoke-Expression "node --version"
$versionOutput | Should -Match $env:VERSION
}
It "is used from tool-cache" {
$nodePath = (Get-Command "node").Path
$nodePath | Should -Not -BeNullOrEmpty
# GitHub Windows images don't have `AGENT_TOOLSDIRECTORY` variable
$toolcacheDir = $env:AGENT_TOOLSDIRECTORY ?? $env:RUNNER_TOOL_CACHE
$expectedPath = Join-Path -Path $toolcacheDir -ChildPath "node"
$nodePath.startsWith($expectedPath) | Should -BeTrue -Because "'$nodePath' is not started with '$expectedPath'"
}
It "cached version is used without downloading" {
if ($env:RUNNER_TYPE -eq "self-hosted") {
# Get the installed version of Node.js
$nodeVersion = Invoke-Expression "node --version"
# Check if Node.js is installed
$nodeVersion | Should -Not -BeNullOrEmpty
# Check if the installed version of Node.js is the expected version
$nodeVersion | Should -Match $env:VERSION
}else {
# Analyze output of previous steps to check if Node.js was consumed from cache or downloaded
$useNodeLogFile = Get-UseNodeLogs
$useNodeLogFile | Should -Exist
$useNodeLogContent = Get-Content $useNodeLogFile -Raw
$useNodeLogContent | Should -Match "Found in cache"
}
}
It "Run simple code" {
"node ./simple-test.js" | Should -ReturnZeroExitCode
}
}