Skip to content

Commit 4f24e62

Browse files
authored
Merge pull request #69 from mlocati/simplify-composer-version
Simplify handling of Composer version to be installed
2 parents 877e4f8 + 645cf4e commit 4f24e62

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

PhpManager/public/Install-Composer.ps1

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
$installerUrl = 'https://getcomposer.org/installer';
6767
$installer = ''
6868
$tempPhar = ''
69-
$actualPharUrl = ''
7069
$pathCreatedHere = $false
7170
try {
7271
if ($NoCache) {
@@ -75,7 +74,7 @@
7574
Write-Verbose "Downloading from $installerUrl"
7675
Invoke-WebRequest -UseBasicParsing -Uri $installerUrl -OutFile $installer
7776
} else {
78-
$installer = Get-FileFromUrlOrCache -Url $installerUrl -CachedFileName 'composer-installer.php'
77+
$installer, $foo = Get-FileFromUrlOrCache -Url $installerUrl -CachedFileName 'composer-installer.php'
7978
}
8079
$tempPhar = [System.IO.Path]::GetTempFileName();
8180
$arguments = @()
@@ -86,35 +85,16 @@
8685
if ($Version -match '\.') {
8786
$arguments += '--version=' + $Version
8887
} else {
89-
$actualPharUrl = "https://getcomposer.org/composer-$Version.phar"
90-
switch ($Version) {
91-
'1' {
92-
$testVersion = '1.10.16'
93-
}
94-
'2' {
95-
$testVersion = '2.0.1'
96-
}
97-
default {
98-
$testVersion = "$Version.0.1"
99-
}
100-
}
101-
$arguments += "--version=$testVersion"
102-
$arguments += '--check'
88+
$arguments += '--' + $Version
10389
}
10490
}
10591
$arguments += '2>&1'
106-
Write-Verbose "Launching Composer installer"
92+
Write-Verbose ('Launching Composer installer with: ' + ($arguments -join ' '))
10793
$installerResult = & $phpVersion.ExecutablePath $arguments
10894
if ($LASTEXITCODE -ne 0) {
10995
throw $installerResult
11096
}
11197
Write-Verbose "Composer installed succeeded"
112-
if ($actualPharUrl -ne '') {
113-
Write-Verbose "Downloading Composer"
114-
Set-NetSecurityProtocolType
115-
Write-Verbose "Downloading from $actualPharUrl"
116-
Invoke-WebRequest -UseBasicParsing -Uri $actualPharUrl -OutFile $tempPhar
117-
}
11898
Write-Verbose "Installing to $Path"
11999
If (-Not(Test-Path -LiteralPath $Path)) {
120100
New-Item -ItemType Directory -Path $Path | Out-Null

test/tests/Install-Composer.Tests.ps1

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
Describe 'Install-Composer' {
2+
BeforeAll {
3+
function GetInstalledComposerVersion()
4+
{
5+
param (
6+
[Parameter(Mandatory = $true, Position = 0)]
7+
[string] $composerBatPath
8+
)
9+
$output = & $composerBat @('--version')
10+
if ($LASTEXITCODE -ne 0) {
11+
return "Failed to launch composer. Its output is`n$output"
12+
}
13+
$match = $output | Select-String -Pattern 'Composer\s+(?:v(?:er(?:s(?:ion)?)?)?\.?\s*)?(\d\S*)'
14+
if (-not($match)) {
15+
return "Failed to detect Composer version from`n$output"
16+
}
17+
return $match.Matches.Groups[1].Value
18+
}
19+
}
220
Mock -ModuleName PhpManager Get-PhpDownloadCache { return Join-Path -Path $Global:PHPMANAGER_TESTPATH -ChildPath download-cache }
321
$phpPath = Join-Path -Path $Global:PHPMANAGER_TESTINSTALLS -ChildPath (New-Guid).Guid
422
$composerPath = Join-Path -Path $Global:PHPMANAGER_TESTINSTALLS -ChildPath (New-Guid).Guid
@@ -16,8 +34,13 @@
1634
Update-PhpCAInfo -Path $phpPath
1735
Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache
1836
$composerBat | Should -Exist
19-
& $composerBat @('--version')
20-
$LASTEXITCODE | Should -Be 0
37+
GetInstalledComposerVersion($composerBat) | Should -Match '^([2-9]\d*|1\d+)\.'
38+
Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache -Version 1
39+
GetInstalledComposerVersion($composerBat) | Should -Match '^1\.'
40+
Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache -Version 2
41+
GetInstalledComposerVersion($composerBat) | Should -Match '^2\.'
42+
Install-Composer -Path $composerPath -PhpPath $phpPath -Scope User -NoAddToPath -NoCache -Version '1.10.15'
43+
GetInstalledComposerVersion($composerBat) | Should -Be '1.10.15'
2144
} finally {
2245
try {
2346
if (Test-Path -LiteralPath $composerPath) {

0 commit comments

Comments
 (0)