diff --git a/install.ps1 b/install.ps1 index 7d6c8c8..5824ad7 100644 --- a/install.ps1 +++ b/install.ps1 @@ -299,6 +299,26 @@ function Assert-SafeUpdateCache($Path) { $full } +function Add-UserPath($PathToAdd) { + $userPath = [Environment]::GetEnvironmentVariable("Path", "User") + $entries = @() + if ($userPath) { + $entries = @($userPath -split ';' | Where-Object { $_ }) + } + $wanted = Normalize-PathForCompare $PathToAdd + foreach ($entry in $entries) { + if ((Normalize-PathForCompare $entry) -ieq $wanted) { + return + } + } + $next = if ($entries.Count -gt 0) { + (@($entries) + $PathToAdd) -join ';' + } else { + $PathToAdd + } + [Environment]::SetEnvironmentVariable("Path", $next, "User") +} + $bindir = Join-Path $Prefix "bin" $configDir = Join-Path $HOME ".config\dosh" New-Item -ItemType Directory -Force -Path $bindir, $configDir | Out-Null @@ -447,10 +467,7 @@ predict = true "@ | Set-Content -NoNewline -Encoding utf8 $hostsConfig } -$userPath = [Environment]::GetEnvironmentVariable("Path", "User") -if (-not (($userPath -split ';') -contains $bindir)) { - [Environment]::SetEnvironmentVariable("Path", "$userPath;$bindir", "User") -} +Add-UserPath $bindir Write-Host "Installed Dosh client to $bindir" Write-Host "Configured UDP port $Port" diff --git a/tests/release_scripts.rs b/tests/release_scripts.rs index 8ae15e3..8a24171 100644 --- a/tests/release_scripts.rs +++ b/tests/release_scripts.rs @@ -145,6 +145,20 @@ fn windows_installer_reuses_persistent_source_update_cache() { ); } +#[test] +fn windows_installer_adds_user_path_idempotently() { + let ps1 = include_str!("../install.ps1"); + assert!(ps1.contains("function Add-UserPath($PathToAdd)")); + assert!(ps1.contains("$wanted = Normalize-PathForCompare $PathToAdd")); + assert!(ps1.contains("-ieq $wanted")); + assert!(ps1.contains("(@($entries) + $PathToAdd) -join ';'")); + assert!( + !ps1.contains("\"$userPath;$bindir\""), + "Windows installer must not write a leading semicolon when user PATH is empty" + ); + assert!(ps1.contains("Add-UserPath $bindir")); +} + #[test] fn unix_installer_rejects_unsafe_source_update_cache_paths() { let install = include_str!("../install.sh");