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

68 lines
4.4 KiB
PowerShell

$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot 'go-toolchain.ps1')
$fixture = Join-Path ([IO.Path]::GetTempPath()) ('linkport-go-test-' + [Guid]::NewGuid().ToString('N') + ' with spaces')
$project = Join-Path $fixture 'project'
$previous = @{}
$names = @('LINKPORT_GO')
foreach ($name in $names) { $previous[$name] = [Environment]::GetEnvironmentVariable($name, 'Process') }
$script:commandSource = $null
$script:assertions = 0
function Get-Command {
[CmdletBinding()]
param([string]$Name, [string]$CommandType)
if ($Name -ne 'go.exe' -or $CommandType -ne 'Application') { throw 'Go lookup must exclude functions and aliases' }
if ($script:commandSource) { [PSCustomObject]@{ Source = $script:commandSource } }
}
function Assert-GoTest([bool]$Condition, [string]$Message) {
if (!$Condition) { throw $Message }
$script:assertions++
}
function New-GoFixture([string]$Path) {
New-Item -ItemType Directory -Path (Split-Path $Path -Parent) -Force | Out-Null
[IO.File]::WriteAllText($Path, 'resolution fixture, not an executable')
return [IO.Path]::GetFullPath($Path)
}
try {
New-Item -ItemType Directory -Path $project -Force | Out-Null
foreach ($name in $names) { [Environment]::SetEnvironmentVariable($name, $null, 'Process') }
$pathGo = New-GoFixture (Join-Path $fixture 'on path/go.exe')
$script:commandSource = $pathGo
Assert-GoTest ((Get-LinkPortGoExecutable $project) -eq $pathGo) 'PATH lookup failed'
$explicit = New-GoFixture (Join-Path $project 'custom Go/bin/go.exe')
$env:LINKPORT_GO = 'custom Go/bin/go.exe'
Assert-GoTest ((Get-LinkPortGoExecutable $project) -eq $explicit) 'Project-relative override did not take priority over PATH'
$env:LINKPORT_GO = '"' + $explicit + '"'
Assert-GoTest ((Get-LinkPortGoExecutable $project) -eq $explicit) 'Quoted absolute override failed'
$env:LINKPORT_GO = 'missing/go.exe'
$rejected = $false
try { Get-LinkPortGoExecutable $project | Out-Null } catch { $rejected = $_.Exception.Message -like 'LINKPORT_GO does not point to a file:*' }
Assert-GoTest $rejected 'Invalid explicit override silently fell back to PATH'
$env:LINKPORT_GO = $project
$rejected = $false
try { Get-LinkPortGoExecutable $project | Out-Null } catch { $rejected = $_.Exception.Message -like 'LINKPORT_GO does not point to a file:*' }
Assert-GoTest $rejected 'Directory accepted as an executable'
$env:LINKPORT_GO = $null
$script:commandSource = $null
$missingMessage = ''
try { Get-LinkPortGoExecutable $project | Out-Null } catch { $missingMessage = $_.Exception.Message }
Assert-GoTest ($missingMessage.Contains('Go 1.27.1') -and $missingMessage.Contains('LINKPORT_GO') -and $missingMessage.Contains('prebuilt linkport.exe') -and $missingMessage.Contains($project)) 'Missing Go guidance is incomplete'
$portable = New-GoFixture (Join-Path $project 'tools/go/bin/go.exe')
Assert-GoTest ((Get-LinkPortGoExecutable $project) -eq $portable) 'Project portable Go lookup failed'
$script:commandSource = $pathGo
Assert-GoTest ((Get-LinkPortGoExecutable $project) -eq $portable) 'Portable Go did not take priority over machine PATH'
$env:LINKPORT_GO = 'custom Go/bin/go.exe'
Assert-GoTest ((Get-LinkPortGoExecutable $project) -eq $explicit) 'Explicit Go did not take priority over portable Go'
foreach ($entry in @('build.ps1', 'package.ps1', 'test-matrix.ps1')) {
$content = [IO.File]::ReadAllText((Join-Path $PSScriptRoot $entry))
Assert-GoTest ($content.Contains('Get-LinkPortGoExecutable -ProjectRoot') -and !$content.Contains("throw 'Go is required'")) ('Entry point does not use the common resolver: ' + $entry)
}
Write-Host "Go discovery tests passed: $script:assertions assertions. No toolchain was executed or installed."
} finally {
foreach ($name in $names) { [Environment]::SetEnvironmentVariable($name, $previous[$name], 'Process') }
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-go-test-')) { throw 'Unexpected fixture cleanup path' }
if (Test-Path -LiteralPath $resolved) { Remove-Item -LiteralPath $resolved -Recurse -Force }
}