Skip to content

Commit

Permalink
Merge pull request #69 from mlocati/simplify-composer-version
Browse files Browse the repository at this point in the history
Simplify handling of Composer version to be installed
  • Loading branch information
mlocati authored Oct 29, 2020
2 parents 877e4f8 + 645cf4e commit 4f24e62
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
26 changes: 3 additions & 23 deletions PhpManager/public/Install-Composer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
$installerUrl = 'https://getcomposer.org/installer';
$installer = ''
$tempPhar = ''
$actualPharUrl = ''
$pathCreatedHere = $false
try {
if ($NoCache) {
Expand All @@ -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 = @()
Expand All @@ -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
Expand Down
27 changes: 25 additions & 2 deletions test/tests/Install-Composer.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 4f24e62

Please sign in to comment.