@@ -254,33 +254,33 @@ pub fn powershell_with_path(script: &str, variable: &str, path: &Path) -> std::p
254254 command
255255}
256256
257+ /// The environment variable the all-users install reads the installer's command line from.
258+ pub const INSTALLER_ARGUMENTS : & str = "OPENBOT_INSTALLER_ARGUMENTS" ;
259+
257260/// The exit code that means the person said no to Windows' approval prompt: `ERROR_CANCELLED`, as
258- /// the WSL fix reads it (`windows::DECLINED`) .
259- const ERROR_CANCELLED : i32 = 1223 ;
261+ /// the WSL fix reads it.
262+ const ERROR_CANCELLED : i32 = crate :: windows :: DECLINED ;
260263
261264/// The all-users install: Windows' approval prompt, then Docker's installer as an administrator,
262- /// waited for, its exit code PowerShell's. The installer's path is `INSTALLER_PATH`.
265+ /// waited for, its exit code PowerShell's, and `ERROR_CANCELLED` for a No (`windows::runas_script`).
266+ /// The installer's path is `INSTALLER_PATH` and its command line `INSTALLER_ARGUMENTS`.
263267pub fn elevated_install ( installer : & Path , arguments : & [ String ] ) -> std:: process:: Command {
264- let quoted: Vec < String > = arguments
265- . iter ( )
266- . map ( |argument| format ! ( "'{}'" , argument. replace( '\'' , "''" ) ) )
267- . collect ( ) ;
268- let start = format ! (
269- "$p = Start-Process -FilePath $env:{INSTALLER_PATH} -ArgumentList {} -Verb RunAs -Wait \
270- -PassThru",
271- quoted. join( "," )
268+ let mut command = powershell_with_path (
269+ & install_script ( crate :: windows:: PROCESS_START ) ,
270+ INSTALLER_PATH ,
271+ installer,
272272 ) ;
273- powershell_with_path ( & elevated_script ( & start) , INSTALLER_PATH , installer)
273+ command. env ( INSTALLER_ARGUMENTS , crate :: windows:: command_line ( arguments) ) ;
274+ command
274275}
275276
276- /// `start`, which raises Windows' approval prompt and sets `$p`, exiting with `$p`'s exit code. A
277- /// no to the prompt exits `ERROR_CANCELLED`, found by its Win32 code in the error, never by its
278- /// words, which are in Windows' own language.
279- fn elevated_script ( start : & str ) -> String {
280- format ! (
281- "$ErrorActionPreference = 'Stop'; try {{ {start}; exit $p.ExitCode }} catch {{ \
282- $e = $_.Exception; while ($e) {{ if ($e.NativeErrorCode -eq {ERROR_CANCELLED}) \
283- {{ exit {ERROR_CANCELLED} }}; $e = $e.InnerException }}; throw }}"
277+ /// The script `elevated_install` runs, with `starting` raising the prompt (`windows::runas_script`).
278+ fn install_script ( starting : & str ) -> String {
279+ crate :: windows:: runas_script (
280+ & format ! ( "$env:{INSTALLER_PATH}" ) ,
281+ & format ! ( "$env:{INSTALLER_ARGUMENTS}" ) ,
282+ false ,
283+ starting,
284284 )
285285}
286286
@@ -792,7 +792,7 @@ fn run_installer(mounted: &Mounted) -> Installed {
792792}
793793
794794/// Whether `elevated_install` stopped because the person said no to Windows' approval prompt: it
795- /// then exits `ERROR_CANCELLED` (`elevated_script `). What PowerShell said is in Windows' own
795+ /// then exits `ERROR_CANCELLED` (`windows::runas_script `). What PowerShell said is in Windows' own
796796/// language, so it is never read for this.
797797#[ cfg_attr( not( target_os = "windows" ) , allow( dead_code) ) ]
798798fn closed_the_prompt ( code : Option < i32 > ) -> bool {
@@ -1497,9 +1497,15 @@ Internal requirements count=1 size=180
14971497 . to_string_lossy ( )
14981498 . into_owned ( ) ;
14991499 assert ! (
1500- script. contains( "-ArgumentList 'install','--quiet' -Verb RunAs " ) ,
1500+ script. contains( "$start.Arguments = $env:OPENBOT_INSTALLER_ARGUMENTS " ) ,
15011501 "{script}"
15021502 ) ;
1503+ assert ! ( script. contains( "$start.Verb = 'runas'" ) , "{script}" ) ;
1504+ let envs: Vec < _ > = install. get_envs ( ) . collect ( ) ;
1505+ assert ! ( envs. contains( & (
1506+ std:: ffi:: OsStr :: new( INSTALLER_ARGUMENTS ) ,
1507+ Some ( std:: ffi:: OsStr :: new( "install --quiet" ) )
1508+ ) ) ) ;
15031509 }
15041510
15051511 /*
@@ -1746,41 +1752,84 @@ Internal requirements count=1 size=180
17461752 assert ! ( !closed_the_prompt( Some ( 1 ) ) ) ;
17471753 assert ! ( !closed_the_prompt( Some ( 0 ) ) ) ;
17481754 assert ! ( !closed_the_prompt( None ) ) ;
1749- // The script reads the Win32 code, never the words.
1750- let script = elevated_script ( "$p = Start-Process x -Verb RunAs -Wait -PassThru" ) ;
1755+ // The script reads the Win32 code, never the words, and asks through the call that keeps
1756+ // the code (`windows::PROCESS_START`), not Start-Process, which loses it.
1757+ let script = install_script ( crate :: windows:: PROCESS_START ) ;
17511758 assert ! ( script. contains( "NativeErrorCode -eq 1223" ) , "{script}" ) ;
17521759 assert ! ( script. contains( "exit 1223" ) , "{script}" ) ;
17531760 assert ! (
17541761 script. contains( "$ErrorActionPreference = 'Stop'" ) ,
17551762 "{script}"
17561763 ) ;
17571764 assert ! ( !script. contains( "canceled" ) , "{script}" ) ;
1765+ assert ! ( !script. contains( "Start-Process" ) , "{script}" ) ;
17581766 }
17591767
1760- /// The script `elevated_install` runs, in real Windows PowerShell, with `Start-Process` standing
1761- /// in for by the error it throws when the person says no: a Win32 `ERROR_CANCELLED` inside the
1762- /// `InvalidOperationException`, worded in German. It exits 1223. Any other error exits 1.
1768+ /// The script `elevated_install` runs, in real Windows PowerShell, answered No as Windows
1769+ /// answers it. With `Start-Process -Verb RunAs`, a No on Windows Server 2022 (PowerShell
1770+ /// 5.1.20348) came as an `InvalidOperationException` (HResult 0x80131509) with no inner
1771+ /// exception and no Win32 code, so the script exited 1 and the update went on to open Docker's
1772+ /// installer. It now asks through `Process.Start`, whose No carries `ERROR_CANCELLED`.
17631773 #[ test]
17641774 #[ cfg( windows) ]
1765- fn the_elevated_install_script_exits_1223_on_a_no_in_any_language ( ) {
1766- let run = |native : i32 | {
1767- let start = format ! (
1768- "throw [System.InvalidOperationException]::new('Dieser Befehl kann aufgrund des \
1769- folgenden Fehlers nicht ausgeführt werden.', \
1770- [System.ComponentModel.Win32Exception]::new({native}))"
1775+ fn the_elevated_install_script_exits_1223_on_a_no_as_windows_says_it ( ) {
1776+ let script = format ! (
1777+ "{}{}" ,
1778+ crate :: windows:: SAYS_NO_TYPE ,
1779+ install_script( crate :: windows:: SAYS_NO )
1780+ ) ;
1781+ let code = crate :: windows:: powershell_exit_for_test (
1782+ & script,
1783+ & [
1784+ ( INSTALLER_PATH , r"C:\nowhere\Docker Desktop Installer.exe" ) ,
1785+ ( INSTALLER_ARGUMENTS , "install --quiet" ) ,
1786+ ] ,
1787+ ) ;
1788+ assert_eq ! ( code, Some ( ERROR_CANCELLED ) ) ;
1789+ assert ! ( closed_the_prompt( code) ) ;
1790+ }
1791+
1792+ /// The real install command on real Windows, with `Start-Process` answering No as it did on
1793+ /// Windows Server 2022: the install never asks through it. The runner's Windows asks nobody,
1794+ /// so the program runs elevated and its exit code comes back; one that isn't there is a
1795+ /// failure, not a No.
1796+ #[ test]
1797+ #[ cfg( windows) ]
1798+ fn the_elevated_install_runs_the_installer_and_hands_back_its_exit_code ( ) {
1799+ let cmd = std:: path:: PathBuf :: from ( std:: env:: var ( "ComSpec" ) . unwrap ( ) ) ;
1800+ let ran = |program : & Path , arguments : & [ & str ] | {
1801+ let command = elevated_install (
1802+ program,
1803+ & arguments. iter ( ) . map ( |a| a. to_string ( ) ) . collect :: < Vec < _ > > ( ) ,
17711804 ) ;
1772- let output = powershell_with_path (
1773- & elevated_script ( & start) ,
1774- INSTALLER_PATH ,
1775- Path :: new ( r"C:\nowhere\Docker Desktop Installer.exe" ) ,
1805+ let script = command
1806+ . get_args ( )
1807+ . nth ( 3 )
1808+ . unwrap ( )
1809+ . to_string_lossy ( )
1810+ . into_owned ( ) ;
1811+ let envs: Vec < ( String , String ) > = command
1812+ . get_envs ( )
1813+ . filter_map ( |( name, value) | {
1814+ Some ( (
1815+ name. to_string_lossy ( ) . into_owned ( ) ,
1816+ value?. to_string_lossy ( ) . into_owned ( ) ,
1817+ ) )
1818+ } )
1819+ . collect ( ) ;
1820+ let envs: Vec < ( & str , & str ) > =
1821+ envs. iter ( ) . map ( |( n, v) | ( n. as_str ( ) , v. as_str ( ) ) ) . collect ( ) ;
1822+ crate :: windows:: powershell_exit_for_test (
1823+ & format ! ( "{}{script}" , crate :: windows:: START_PROCESS_SAID_NO ) ,
1824+ & envs,
17761825 )
1777- . output ( )
1778- . unwrap ( ) ;
1779- output. status . code ( )
17801826 } ;
1781- assert_eq ! ( run( ERROR_CANCELLED ) , Some ( ERROR_CANCELLED ) ) ;
1782- let other = run ( 2 ) ;
1783- assert_ne ! ( other, Some ( ERROR_CANCELLED ) ) ;
1784- assert_ne ! ( other, Some ( 0 ) ) ;
1827+ assert_eq ! ( ran( cmd. as_path( ) , & [ "/c" , "exit 7" ] ) , Some ( 7 ) ) ;
1828+ let missing = ran (
1829+ Path :: new ( r"C:\nowhere\Docker Desktop Installer.exe" ) ,
1830+ & [ "install" ] ,
1831+ ) ;
1832+ assert_ne ! ( missing, Some ( 0 ) ) ;
1833+ assert ! ( !closed_the_prompt( missing) , "{missing:?}" ) ;
17851834 }
17861835}
0 commit comments