diff --git a/PhpManager/public/Install-Composer.ps1 b/PhpManager/public/Install-Composer.ps1 index d2de36c..ed60172 100644 --- a/PhpManager/public/Install-Composer.ps1 +++ b/PhpManager/public/Install-Composer.ps1 @@ -66,7 +66,6 @@ $installerUrl = 'https://getcomposer.org/installer'; $installer = '' $tempPhar = '' - $actualPharUrl = '' $pathCreatedHere = $false try { if ($NoCache) { @@ -75,7 +74,7 @@ Write-Verbose "Downloading from $installerUrl" Invoke-WebRequest -UseBasicParsing -Uri $installerUrl -OutFile $installer } else { - $installer = Get-FileFromUrlOrCache -Url $installerUrl -CachedFileName 'composer-installer.php' + $installer, $foo = Get-FileFromUrlOrCache -Url $installerUrl -CachedFileName 'composer-installer.php' } $tempPhar = [System.IO.Path]::GetTempFileName(); $arguments = @() @@ -86,35 +85,16 @@ if ($Version -match '\.') { $arguments += '--version=' + $Version } else { - $actualPharUrl = "https://getcomposer.org/composer-$Version.phar" - switch ($Version) { - '1' { - $testVersion = '1.10.16' - } - '2' { - $testVersion = '2.0.1' - } - default { - $testVersion = "$Version.0.1" - } - } - $arguments += "--version=$testVersion" - $arguments += '--check' + $arguments += '--' + $Version } } $arguments += '2>&1' - Write-Verbose "Launching Composer installer" + Write-Verbose ('Launching Composer installer with: ' + ($arguments -join ' ')) $installerResult = & $phpVersion.ExecutablePath $arguments if ($LASTEXITCODE -ne 0) { throw $installerResult } Write-Verbose "Composer installed succeeded" - if ($actualPharUrl -ne '') { - Write-Verbose "Downloading Composer" - Set-NetSecurityProtocolType - Write-Verbose "Downloading from $actualPharUrl" - Invoke-WebRequest -UseBasicParsing -Uri $actualPharUrl -OutFile $tempPhar - } Write-Verbose "Installing to $Path" If (-Not(Test-Path -LiteralPath $Path)) { New-Item -ItemType Directory -Path $Path | Out-Null diff --git a/test/tests/Install-Composer.Tests.ps1 b/test/tests/Install-Composer.Tests.ps1 index c1f2720..0b8b005 100644 --- a/test/tests/Install-Composer.Tests.ps1 +++ b/test/tests/Install-Composer.Tests.ps1 @@ -1,4 +1,22 @@ Describe 'Install-Composer' { + BeforeAll { + function GetInstalledComposerVersion() + { + param ( + [Parameter(Mandatory = $true, Position = 0)] + [string] $composerBatPath + ) + $output = & $composerBat @('--version') + if ($LASTEXITCODE -ne 0) { + return "Failed to launch composer. Its output is`n$output" + } + $match = $output | Select-String -Pattern 'Composer\s+(?:v(?:er(?:s(?:ion)?)?)?\.?\s*)?(\d\S*)' + if (-not($match)) { + return "Failed to detect Composer version from`n$output" + } + return $match.Matches.Groups[1].Value + } + } Mock -ModuleName PhpManager Get-PhpDownloadCache { return Join-Path -Path $Global:PHPMANAGER_TESTPATH -ChildPath download-cache } $phpPath = Join-Path -Path $Global:PHPMANAGER_TESTINSTALLS -ChildPath (New-Guid).Guid $composerPath = Join-Path -Path $Global:PHPMANAGER_TESTINSTALLS -ChildPath (New-Guid).Guid @@ -16,8 +34,13 @@ Update-PhpCAInfo -Path $phpPath Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache $composerBat | Should -Exist - & $composerBat @('--version') - $LASTEXITCODE | Should -Be 0 + GetInstalledComposerVersion($composerBat) | Should -Match '^([2-9]\d*|1\d+)\.' + Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache -Version 1 + GetInstalledComposerVersion($composerBat) | Should -Match '^1\.' + Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache -Version 2 + GetInstalledComposerVersion($composerBat) | Should -Match '^2\.' + Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache -Version '1.10.15' + GetInstalledComposerVersion($composerBat) | Should -Be '1.10.15' } finally { try { if (Test-Path -LiteralPath $composerPath) {