diff --git a/tests/configure-test-env.ps1 b/tests/configure-test-env.ps1 index f329bf797b9..92343c3acd7 100644 --- a/tests/configure-test-env.ps1 +++ b/tests/configure-test-env.ps1 @@ -7,38 +7,42 @@ try { $null = gcm rig -ea stop; $rig=$true } catch { If ($rig) { # rig defaults to admin mode, where installing R raises a UAC prompt. User # mode installs R in the user profile instead; an explicit RIG_MODE wins. - # The variable is removed again at the end so it does not leak into a - # dot-sourcing shell. rig.exe does not need the mode later: rvenv.cfg - # records the absolute path of the R it selected. + # The variable is removed again in `finally`, even after Ctrl+C or an + # error, so it does not leak into a dot-sourcing shell. rig.exe does not + # need the mode later: rvenv.cfg records the absolute path of the R it + # selected. $rigModeWasSet = [bool]$env:RIG_MODE If (-not $rigModeWasSet) { $env:RIG_MODE = 'user' } - # In admin mode rig proj sync installs a missing R by re-running itself - # elevated, which leaves the project library owned by the admin account. - # Install R separately (rig add does nothing when it is already installed), - # then sync as this user. - $rLockVersion = (Select-String -Path rproj.lock -Pattern '^r_version = "(.+)"' | Select-Object -First 1).Matches[0].Groups[1].Value - Write-Host -ForegroundColor green " > Installing R $rLockVersion if missing" - rig add $rLockVersion - If ($LASTEXITCODE -eq 0) { - Write-Host -ForegroundColor green " > Syncing R packages from rproj.lock" - rig proj sync --no-install-r - } - If ($LASTEXITCODE -ne 0) { - Write-Host -ForegroundColor red "Installing R or rig proj sync failed - R tests will run without the locked packages" - } Else { - # Installs on top of the lock must target the project library and the - # exact R the sync used; rig's default is the default R's user library. - $rVersion = (Select-String -Path .rvenv/rvenv.cfg -Pattern '^r-version = (.+)$').Matches[0].Groups[1].Value.Trim() - $rLib = (Resolve-Path .rvenv/lib).Path - Write-Host -ForegroundColor green " > Installing dev knitr and rmarkdown" - foreach ($dev in @(@('rmarkdown', 'https://rstudio.r-universe.dev'), @('knitr', 'https://yihui.r-universe.dev'))) { - rig pkg install --r-version $rVersion --library $rLib --with-repos $dev[1] $dev[0] - If ($LASTEXITCODE -ne 0) { - Write-Host -ForegroundColor yellow "Dev $($dev[0]) not installed - tests use the locked version" + try { + # In admin mode rig proj sync installs a missing R by re-running itself + # elevated, which leaves the project library owned by the admin account. + # Install R separately (rig add does nothing when it is already installed), + # then sync as this user. + $rLockVersion = (Select-String -Path rproj.lock -Pattern '^r_version = "(.+)"' | Select-Object -First 1).Matches[0].Groups[1].Value + Write-Host -ForegroundColor green " > Installing R $rLockVersion if missing" + rig add $rLockVersion + If ($LASTEXITCODE -eq 0) { + Write-Host -ForegroundColor green " > Syncing R packages from rproj.lock" + rig proj sync --no-install-r + } + If ($LASTEXITCODE -ne 0) { + Write-Host -ForegroundColor red "Installing R or rig proj sync failed - R tests will run without the locked packages" + } Else { + # Installs on top of the lock must target the project library and the + # exact R the sync used; rig's default is the default R's user library. + $rVersion = (Select-String -Path .rvenv/rvenv.cfg -Pattern '^r-version = (.+)$').Matches[0].Groups[1].Value.Trim() + $rLib = (Resolve-Path .rvenv/lib).Path + Write-Host -ForegroundColor green " > Installing dev knitr and rmarkdown" + foreach ($dev in @(@('rmarkdown', 'https://rstudio.r-universe.dev'), @('knitr', 'https://yihui.r-universe.dev'))) { + rig pkg install --r-version $rVersion --library $rLib --with-repos $dev[1] $dev[0] + If ($LASTEXITCODE -ne 0) { + Write-Host -ForegroundColor yellow "Dev $($dev[0]) not installed - tests use the locked version" + } } } + } finally { + If (-not $rigModeWasSet) { Remove-Item Env:RIG_MODE } } - If (-not $rigModeWasSet) { Remove-Item Env:RIG_MODE } } # Check python test environment ---