From 9d529a72e0c2137020367b2d8d3f74b7b8411ed2 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sat, 24 Jun 2023 02:25:43 +0530 Subject: [PATCH] Only check castor version if castor.php is present --- __tests__/tools.test.ts | 11 +++++++++-- dist/index.js | 3 +++ src/scripts/tools/add_tools.ps1 | 9 +++++---- src/scripts/tools/add_tools.sh | 2 +- src/tools.ts | 3 +++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/__tests__/tools.test.ts b/__tests__/tools.test.ts index 408e3bdd..28b98f44 100644 --- a/__tests__/tools.test.ts +++ b/__tests__/tools.test.ts @@ -1,3 +1,4 @@ +import fs = require('fs'); import * as tools from '../src/tools'; interface IData { @@ -533,7 +534,7 @@ describe('Tools tests', () => { ${'latest'} | ${'darwin'} | ${'releases/latest/download/castor.darwin-amd64.phar'} ${'0.5.1'} | ${'darwin'} | ${'releases/download/v0.5.1/castor.darwin-amd64.phar'} ${'latest'} | ${'win32'} | ${'releases/latest/download/castor.windows-amd64.phar'} - ${'0.5.1'} | ${'win32'} | ${'releases/download/v0.5.1/castor.windows-amd64.phar'} + ${'0.5.1'} | ${'win32'} | ${'releases/download/v0.5.1/castor.windows-amd64.phar castor -V'} ${'latest'} | ${'openbsd'} | ${'Platform openbsd is not supported'} `('checking addCastor: $version, $os', async ({version, os, uri}) => { const data = getData({ @@ -543,7 +544,13 @@ describe('Tools tests', () => { version: version, os: os }); - expect(await tools.addCastor(data)).toContain(uri); + if (os === 'win32' && version === '0.5.1') { + fs.writeFileSync('castor.php', ''); + expect(await tools.addCastor(data)).toContain(uri); + fs.unlinkSync('castor.php'); + } else { + expect(await tools.addCastor(data)).toContain(uri); + } }); it.each` diff --git a/dist/index.js b/dist/index.js index 1a18d389..5e2ea6ea 100644 --- a/dist/index.js +++ b/dist/index.js @@ -841,6 +841,9 @@ async function addCastor(data) { data['tool'] = 'castor.' + data['os'].replace('win32', 'windows') + '-amd64'; data['url'] = await getUrl(data); data['tool'] = 'castor'; + data['version_parameter'] = fs_1.default.existsSync('castor.php') + ? data['version_parameter'] + : ''; return await addArchive(data); } exports.addCastor = addCastor; diff --git a/src/scripts/tools/add_tools.ps1 b/src/scripts/tools/add_tools.ps1 index 0ea82430..8a79a04a 100644 --- a/src/scripts/tools/add_tools.ps1 +++ b/src/scripts/tools/add_tools.ps1 @@ -57,7 +57,7 @@ Function Get-ToolVersion() { Param ( [Parameter(Position = 0, Mandatory = $true)] $tool, - [Parameter(Position = 1, Mandatory = $true)] + [Parameter(Position = 1, Mandatory = $false)] $param ) $alp = "[a-zA-Z0-9\.]" @@ -72,7 +72,9 @@ Function Get-ToolVersion() { Set-Variable -Name 'composer_version' -Value $composer_version -Scope Global return "$composer_version" } - return . $tool $param 2> $null | ForEach-Object { $_ -replace "composer $version_regex", '' } | Select-String -Pattern $version_regex | Select-Object -First 1 | ForEach-Object { $_.matches.Value } + if($null -ne $param) { + return . $tool $param 2> $null | ForEach-Object { $_ -replace "composer $version_regex", '' } | Select-String -Pattern $version_regex | Select-Object -First 1 | ForEach-Object { $_.matches.Value } + } } # Helper function to configure tools. @@ -135,8 +137,7 @@ Function Add-Tool() { [Parameter(Position = 1, Mandatory = $true)] [ValidateNotNull()] $tool, - [Parameter(Position = 2, Mandatory = $true)] - [ValidateNotNull()] + [Parameter(Position = 2, Mandatory = $false)] $ver_param ) if (Test-Path $bin_dir\$tool) { diff --git a/src/scripts/tools/add_tools.sh b/src/scripts/tools/add_tools.sh index 09445ed7..0530533c 100644 --- a/src/scripts/tools/add_tools.sh +++ b/src/scripts/tools/add_tools.sh @@ -19,7 +19,7 @@ get_tool_version() { composer_version="$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex")" fi echo "$composer_version" | sudo tee /tmp/composer_version - else + elif [ -n "$param" ]; then $tool "$param" 2>/dev/null | sed -Ee "s/[Cc]omposer(.)?$version_regex//g" | grep -Eo "$version_regex" | head -n 1 fi } diff --git a/src/tools.ts b/src/tools.ts index eed82704..dc4cad4c 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -239,6 +239,9 @@ export async function addCastor(data: RS): Promise { data['tool'] = 'castor.' + data['os'].replace('win32', 'windows') + '-amd64'; data['url'] = await getUrl(data); data['tool'] = 'castor'; + data['version_parameter'] = fs.existsSync('castor.php') + ? data['version_parameter'] + : ''; return await addArchive(data); }