Files
LinkPort/scripts/node-toolchain.test.ps1

64 lines
3.8 KiB
PowerShell

$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot 'node-toolchain.ps1')
$fixture = Join-Path ([IO.Path]::GetTempPath()) ('linkport-node-test-' + [Guid]::NewGuid().ToString('N') + ' with spaces')
$project = Join-Path $fixture 'project'
$previousPath = $env:PATH
$previousNode = $env:LINKPORT_NODE
$script:commands = @{}
$script:assertions = 0
function Get-Command {
[CmdletBinding()]
param([string]$Name, [string]$CommandType)
if ($CommandType -ne 'Application') { throw 'Node lookup must exclude aliases and functions' }
if ($script:commands[$Name]) { [PSCustomObject]@{ Source = $script:commands[$Name] } }
}
function Assert-NodeTest([bool]$Condition, [string]$Message) {
if (!$Condition) { throw $Message }
$script:assertions++
}
function New-NodeFixture([string]$Directory, [bool]$WithNpm = $true) {
New-Item -ItemType Directory -Path $Directory -Force | Out-Null
$node = Join-Path $Directory 'node.exe'
[IO.File]::WriteAllText($node, 'resolution fixture, not executable')
if ($WithNpm) { [IO.File]::WriteAllText((Join-Path $Directory 'npm.cmd'), 'fixture') }
return $node
}
try {
$env:LINKPORT_NODE = $null
$pathNode = New-NodeFixture (Join-Path $fixture 'on path')
$script:commands['node.exe'] = $pathNode
Assert-NodeTest ((Initialize-LinkPortNodeRuntime $project) -eq $pathNode) 'PATH Node lookup failed'
$portable = New-NodeFixture (Join-Path $project 'tools/node')
Assert-NodeTest ((Initialize-LinkPortNodeRuntime $project) -eq $portable) 'Portable Node did not take priority over PATH'
Assert-NodeTest ($env:PATH.StartsWith((Split-Path $portable -Parent) + ';')) 'Child processes do not use selected Node'
$explicit = New-NodeFixture (Join-Path $project 'custom node')
$env:LINKPORT_NODE = 'custom node/node.exe'
Assert-NodeTest ((Initialize-LinkPortNodeRuntime $project) -eq $explicit) 'Project-relative Node override failed'
$env:LINKPORT_NODE = 'missing/node.exe'
$rejected = $false
try { Initialize-LinkPortNodeRuntime $project | Out-Null } catch { $rejected = $_.Exception.Message.Contains('Node.js was not found') }
Assert-NodeTest $rejected 'Invalid Node override silently fell back'
$incomplete = New-NodeFixture (Join-Path $project 'node only') $false
$script:commands['npm.cmd'] = Join-Path (Split-Path $pathNode -Parent) 'npm.cmd'
$env:LINKPORT_NODE = 'node only/node.exe'
$rejected = $false
try { Initialize-LinkPortNodeRuntime $project | Out-Null } catch { $rejected = $_.Exception.Message.Contains('complete Node.js distribution') }
Assert-NodeTest $rejected 'Incomplete portable Node was mixed with unrelated npm'
$env:LINKPORT_NODE = $null
$script:commands['node.exe'] = $incomplete
Assert-NodeTest ((Initialize-LinkPortNodeRuntime (Join-Path $fixture 'empty')) -eq $incomplete) 'Existing split PATH Node/npm stopped working'
$script:commands.Clear()
$rejected = $false
try { Initialize-LinkPortNodeRuntime (Join-Path $fixture 'empty') | Out-Null } catch { $rejected = $_.Exception.Message.Contains('tools/node') }
Assert-NodeTest $rejected 'Missing Node guidance is incomplete'
Write-Host "Node discovery tests passed: $script:assertions assertions. No runtime was executed or installed."
} finally {
$env:PATH = $previousPath
$env:LINKPORT_NODE = $previousNode
Remove-Item -LiteralPath Function:\Get-Command
$resolved = [IO.Path]::GetFullPath($fixture)
$tempPrefix = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()).TrimEnd('\', '/') + [IO.Path]::DirectorySeparatorChar
if (!$resolved.StartsWith($tempPrefix, [StringComparison]::OrdinalIgnoreCase) -or !(Split-Path $resolved -Leaf).StartsWith('linkport-node-test-')) { throw 'Unexpected fixture cleanup path' }
if (Test-Path -LiteralPath $resolved) { Remove-Item -LiteralPath $resolved -Recurse -Force }
}