diff --git a/install.ps1 b/install.ps1 index 08acf45..f6c05ca 100644 --- a/install.ps1 +++ b/install.ps1 @@ -303,6 +303,23 @@ function Normalize-PathForCompare($Path) { ) } +function PathList-Contains($PathValue, $PathToFind) { + if (-not $PathValue) { + return $false + } + $wanted = Normalize-PathForCompare $PathToFind + foreach ($entry in ($PathValue -split ';' | Where-Object { $_ })) { + try { + if ((Normalize-PathForCompare $entry) -ieq $wanted) { + return $true + } + } + catch { + } + } + return $false +} + function Assert-NoRelativePathSegments($Path) { foreach ($segment in ($Path -split '[\\/]')) { if ($segment -eq "." -or $segment -eq "..") { @@ -339,7 +356,7 @@ function Add-UserPath($PathToAdd) { $entries = @($userPath -split ';' | Where-Object { $_ }) } $wanted = Normalize-PathForCompare $PathToAdd - if (-not (";$env:Path;").Contains(";$PathToAdd;")) { + if (-not (PathList-Contains $env:Path $PathToAdd)) { $env:Path = "$PathToAdd;$env:Path" } foreach ($entry in $entries) { diff --git a/tests/release_scripts.rs b/tests/release_scripts.rs index 9d7216b..bd66142 100644 --- a/tests/release_scripts.rs +++ b/tests/release_scripts.rs @@ -276,14 +276,16 @@ fn windows_prebuilt_install_requires_client_but_not_bench() { fn windows_installer_adds_user_path_idempotently() { let ps1 = include_str!("../install.ps1"); assert!(ps1.contains("function Add-UserPath($PathToAdd)")); + assert!(ps1.contains("function PathList-Contains($PathValue, $PathToFind)")); + assert!(ps1.contains("$wanted = Normalize-PathForCompare $PathToFind")); + assert!(ps1.contains("foreach ($entry in ($PathValue -split ';' | Where-Object { $_ }))")); assert!(ps1.contains("$wanted = Normalize-PathForCompare $PathToAdd")); assert!(ps1.contains("-ieq $wanted")); assert!(ps1.contains("(@($entries) + $PathToAdd) -join ';'")); - assert!(ps1.contains("(\";$env:Path;\").Contains(\";$PathToAdd;\")")); + assert!(ps1.contains("PathList-Contains $env:Path $PathToAdd")); assert!(ps1.contains("$env:Path = \"$PathToAdd;$env:Path\"")); assert!( - ps1.find("(\";$env:Path;\").Contains(\";$PathToAdd;\")") - .unwrap() + ps1.find("PathList-Contains $env:Path $PathToAdd").unwrap() < ps1.find("foreach ($entry in $entries)").unwrap(), "Windows installer must refresh the current shell PATH even when user PATH already contains Dosh" );