From ff0006524427f7e4bb48c4a1a869aeb5f5a81d10 Mon Sep 17 00:00:00 2001 From: badasukerubin Date: Wed, 11 Sep 2024 18:40:27 +0100 Subject: [PATCH 1/3] feat: custom-composer: composer_paths --- bin/composer_paths.sh | 16 ++++- tests/expect/composer_paths_05.exp | 7 +- tests/expect/composer_paths_11.exp | 30 ++++++++ tests/expect/composer_paths_12.exp | 8 +++ tests/expect/composer_paths_13.exp | 31 +++++++++ tests/expect/composer_paths_14.exp | 31 +++++++++ .../custom-composer/composer-gh-actions.json | 18 +++++ .../custom-composer/composer-gh-actions.lock | 68 +++++++++++++++++++ .../invalid-custom-composer/.gitignore | 1 + .../composer-gh-actions.json | 4 ++ .../no-lock-file-custom-composer/.gitignore | 1 + .../composer-gh-actions.json | 18 +++++ .../composer-gh-actions.json | 14 ++++ .../composer-gh-actions.lock | 21 ++++++ 14 files changed, 261 insertions(+), 7 deletions(-) create mode 100755 tests/expect/composer_paths_11.exp create mode 100755 tests/expect/composer_paths_12.exp create mode 100755 tests/expect/composer_paths_13.exp create mode 100755 tests/expect/composer_paths_14.exp create mode 100644 tests/fixtures/custom-composer/composer-gh-actions.json create mode 100644 tests/fixtures/custom-composer/composer-gh-actions.lock create mode 100644 tests/fixtures/invalid-custom-composer/.gitignore create mode 100644 tests/fixtures/invalid-custom-composer/composer-gh-actions.json create mode 100644 tests/fixtures/no-lock-file-custom-composer/.gitignore create mode 100644 tests/fixtures/no-lock-file-custom-composer/composer-gh-actions.json create mode 100644 tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.json create mode 100644 tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.lock diff --git a/bin/composer_paths.sh b/bin/composer_paths.sh index decf725..b913be4 100755 --- a/bin/composer_paths.sh +++ b/bin/composer_paths.sh @@ -3,13 +3,18 @@ composer_path="${1:-$(which composer)}" working_directory="${2:-.}" php_path="${3:-$(which php)}" +custom_composer_filename="${4:-}" function test_composer { "${php_path}" "${composer_path}" --version > /dev/null 2>&1 } function validate_composer { - "${php_path}" "${composer_path}" validate --no-check-publish --no-check-lock --working-dir "${working_directory}" > /dev/null 2>&1 + if [ -n "${custom_composer_filename}" ]; then + export COMPOSER="${custom_composer_filename}.json" + fi + + "${php_path}" "${composer_path}" validate --no-check-publish --no-check-lock --working-dir "${working_directory}" > /dev/null 2>&1 } if ! test_composer; then @@ -20,14 +25,19 @@ fi composer_json="composer.json" composer_lock="composer.lock" +if [ -n "${custom_composer_filename}" ]; then + composer_json="${custom_composer_filename}.json" + composer_lock="${custom_composer_filename}.lock" +fi + if [ -n "${working_directory}" ]; then if [ ! -d "${working_directory}" ]; then echo "::error title=Working Directory Not Found::Unable to find working directory at '${working_directory}'" exit 1 fi - composer_json="${working_directory}/composer.json" - composer_lock="${working_directory}/composer.lock" + composer_json="${working_directory}/${composer_json}" + composer_lock="${working_directory}/${composer_lock}" fi if [ ! -f "${composer_json}" ]; then diff --git a/tests/expect/composer_paths_05.exp b/tests/expect/composer_paths_05.exp index 676476b..99f6449 100755 --- a/tests/expect/composer_paths_05.exp +++ b/tests/expect/composer_paths_05.exp @@ -8,13 +8,12 @@ set timeout 3 spawn ../../bin/composer_paths.sh "" "../fixtures/no-lock-file" match_max 100000 -expect "::debug::Unable to find composer.lock at '../fixtures/no-lock-file/composer.lock'\r -::debug::Composer path is '*'\r +expect "::debug::Unable to find composer.lock at '../fixtures/no-lock-file/composer.lock'\r" +expect "::debug::Composer path is '*'\r ::debug::Composer version *\r ::debug::Composer cache directory found at '*'\r ::debug::File composer.json found at '../fixtures/no-lock-file/composer.json'\r -::debug::File composer.lock path computed as ''\r -" +::debug::File composer.lock path computed as ''\r" expect eof # Verify output variables have been set correctly. diff --git a/tests/expect/composer_paths_11.exp b/tests/expect/composer_paths_11.exp new file mode 100755 index 0000000..75ea7fb --- /dev/null +++ b/tests/expect/composer_paths_11.exp @@ -0,0 +1,30 @@ +#!/usr/bin/env -S expect -f + +# For testing outputs variables written to GITHUB_OUTPUT +set gitHubOutputFile composer_paths_11.txt +set ::env(GITHUB_OUTPUT) $gitHubOutputFile + +set timeout 3 +spawn ../../bin/composer_paths.sh "" "../fixtures/custom-composer" "" "composer-gh-actions" +match_max 100000 + +expect "::debug::Composer path is '*'\r +::debug::Composer version *\r +::debug::Composer cache directory found at '*'\r +::debug::File composer.json found at '../fixtures/custom-composer/composer-gh-actions.json'\r +::debug::File composer.lock path computed as '../fixtures/custom-composer/composer-gh-actions.lock'\r" +expect eof + +# Verify output variables have been set correctly. +set fp [open $gitHubOutputFile r] +set fileData [read $fp] +close $fp + +if {[regexp {^composer_command=\S*\s*cache-dir=\S*\s*json=\.\./fixtures/custom-composer/composer-gh-actions\.json\s*lock=\.\./fixtures/custom-composer/composer-gh-actions\.lock\s*$} $fileData] == 0} { + puts "\nExpected output variable does not match. Received:\n" + puts $fileData + exit 1 +} + +# Clean up +file delete $gitHubOutputFile diff --git a/tests/expect/composer_paths_12.exp b/tests/expect/composer_paths_12.exp new file mode 100755 index 0000000..6f95f6a --- /dev/null +++ b/tests/expect/composer_paths_12.exp @@ -0,0 +1,8 @@ +#!/usr/bin/env -S expect -f + +set timeout 3 +spawn ../../bin/composer_paths.sh "" "../fixtures/invalid-custom-composer" "" "composer-gh-actions" +match_max 100000 + +expect "::error title=Invalid composer.json::The composer.json file at '../fixtures/invalid-custom-composer/composer-gh-actions.json' does not validate; run 'composer validate' to check for errors" +expect eof diff --git a/tests/expect/composer_paths_13.exp b/tests/expect/composer_paths_13.exp new file mode 100755 index 0000000..bbaa573 --- /dev/null +++ b/tests/expect/composer_paths_13.exp @@ -0,0 +1,31 @@ +#!/usr/bin/env -S expect -f + +# For testing outputs variables written to GITHUB_OUTPUT +set gitHubOutputFile composer_paths_13.txt +set ::env(GITHUB_OUTPUT) $gitHubOutputFile + +set timeout 3 +spawn ../../bin/composer_paths.sh "" "../fixtures/no-lock-file-custom-composer" "" "composer-gh-actions" +match_max 100000 + +expect "::debug::Unable to find composer.lock at '../fixtures/no-lock-file-custom-composer/composer-gh-actions.lock'\r" +expect "::debug::Composer path is '*'\r +::debug::Composer version *\r +::debug::Composer cache directory found at '*'\r +::debug::File composer.json found at '../fixtures/no-lock-file-custom-composer/composer-gh-actions.json'\r +::debug::File composer.lock path computed as ''\r" +expect eof + +# Verify output variables have been set correctly. +set fp [open $gitHubOutputFile r] +set fileData [read $fp] +close $fp + +if {[regexp {^composer_command=\S*\s*cache-dir=\S*\s*json=\.\./fixtures/no-lock-file-custom-composer/composer-gh-actions\.json\s*lock=\s*$} $fileData] == 0} { + puts "\nExpected output variable does not match. Received:\n" + puts $fileData + exit 1 +} + +# Clean up +file delete $gitHubOutputFile diff --git a/tests/expect/composer_paths_14.exp b/tests/expect/composer_paths_14.exp new file mode 100755 index 0000000..26d9937 --- /dev/null +++ b/tests/expect/composer_paths_14.exp @@ -0,0 +1,31 @@ +#!/usr/bin/env -S expect -f + +# For testing outputs variables written to GITHUB_OUTPUT +set gitHubOutputFile composer_paths_14.txt +set ::env(GITHUB_OUTPUT) $gitHubOutputFile + +set timeout 3 +spawn ../../bin/composer_paths.sh "" "../fixtures/out-of-sync-lock-custom-composer" "" "composer-gh-actions" +match_max 100000 + +expect "::debug::Composer path is '*'\r +::debug::Composer version *\r +::debug::Composer cache directory found at '*'\r +::debug::File composer.json found at '../fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.json'\r +::debug::File composer.lock path computed as '../fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.lock'\r +" +expect eof + +# Verify output variables have been set correctly. +set fp [open $gitHubOutputFile r] +set fileData [read $fp] +close $fp + +if {[regexp {^composer_command=\S*\s*cache-dir=\S*\s*json=\.\./fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.json\s*lock=\.\./fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.lock\s*$} $fileData] == 0} { + puts "\nExpected output variable does not match. Received:\n" + puts $fileData + exit 1 +} + +# Clean up +file delete $gitHubOutputFile diff --git a/tests/fixtures/custom-composer/composer-gh-actions.json b/tests/fixtures/custom-composer/composer-gh-actions.json new file mode 100644 index 0000000..3cb82eb --- /dev/null +++ b/tests/fixtures/custom-composer/composer-gh-actions.json @@ -0,0 +1,18 @@ +{ + "name": "ramsey/composer-install-test-with-lock-file", + "description": "Tests composer-install when custom-composer.lock file exists.", + "license": "MIT", + "type": "project", + "authors": [ + { + "name": "Ben Ramsey", + "email": "ben@benramsey.com" + } + ], + "require": { + "ehime/hello-world": "^1.0.0" + }, + "config": { + "allow-plugins": false + } +} diff --git a/tests/fixtures/custom-composer/composer-gh-actions.lock b/tests/fixtures/custom-composer/composer-gh-actions.lock new file mode 100644 index 0000000..810366e --- /dev/null +++ b/tests/fixtures/custom-composer/composer-gh-actions.lock @@ -0,0 +1,68 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "091919c4ac368d87445398fc8c8c2696", + "packages": [ + { + "name": "ehime/hello-world", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/ehime/hello-world.git", + "reference": "b1c8cdd2c11272d8c5deec7816e51fa5374217c1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ehime/hello-world/zipball/b1c8cdd2c11272d8c5deec7816e51fa5374217c1", + "reference": "b1c8cdd2c11272d8c5deec7816e51fa5374217c1", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "mockery/mockery": "0.8.*", + "phpunit/phpunit": "4.3.5" + }, + "type": "library", + "autoload": { + "psr-0": { + "HelloWorld": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jd Daniel", + "email": "dodomeki@gmail.com" + } + ], + "description": "Sample Composer project", + "keywords": [ + "helloworld", + "sample", + "test" + ], + "support": { + "issues": "https://github.com/ehime/hello-world/issues", + "source": "https://github.com/ehime/hello-world/tree/1.0.5" + }, + "time": "2015-07-31T17:53:36+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/tests/fixtures/invalid-custom-composer/.gitignore b/tests/fixtures/invalid-custom-composer/.gitignore new file mode 100644 index 0000000..72cba13 --- /dev/null +++ b/tests/fixtures/invalid-custom-composer/.gitignore @@ -0,0 +1 @@ +composer-gh-actions.lock diff --git a/tests/fixtures/invalid-custom-composer/composer-gh-actions.json b/tests/fixtures/invalid-custom-composer/composer-gh-actions.json new file mode 100644 index 0000000..ff65c78 --- /dev/null +++ b/tests/fixtures/invalid-custom-composer/composer-gh-actions.json @@ -0,0 +1,4 @@ +{ + "name": "invalid-custom-composer.json", + "description": "Tests composer-install when custom-composer.json is invalid" +} diff --git a/tests/fixtures/no-lock-file-custom-composer/.gitignore b/tests/fixtures/no-lock-file-custom-composer/.gitignore new file mode 100644 index 0000000..72cba13 --- /dev/null +++ b/tests/fixtures/no-lock-file-custom-composer/.gitignore @@ -0,0 +1 @@ +composer-gh-actions.lock diff --git a/tests/fixtures/no-lock-file-custom-composer/composer-gh-actions.json b/tests/fixtures/no-lock-file-custom-composer/composer-gh-actions.json new file mode 100644 index 0000000..df8a2d0 --- /dev/null +++ b/tests/fixtures/no-lock-file-custom-composer/composer-gh-actions.json @@ -0,0 +1,18 @@ +{ + "name": "ramsey/composer-install-test-no-lock-file", + "description": "Tests composer-install when no custom-composer.lock file exists.", + "license": "MIT", + "type": "project", + "authors": [ + { + "name": "Ben Ramsey", + "email": "ben@benramsey.com" + } + ], + "require": { + "ehime/hello-world": "^1.0.0" + }, + "config": { + "allow-plugins": false + } +} diff --git a/tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.json b/tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.json new file mode 100644 index 0000000..357f92e --- /dev/null +++ b/tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.json @@ -0,0 +1,14 @@ +{ + "name": "ramsey/composer-install-test-out-of-sync-lock-custom-composer", + "description": "Tests composer-install when custom-composer.lock file is out of sync.", + "license": "MIT", + "authors": [ + { + "name": "Ben Ramsey", + "email": "ben@benramsey.com" + } + ], + "config": { + "platform": {} + } +} diff --git a/tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.lock b/tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.lock new file mode 100644 index 0000000..68202d6 --- /dev/null +++ b/tests/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions.lock @@ -0,0 +1,21 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "32d8479198ae031d96dd19b4332b8b0a", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "platform-overrides": { + "php": "5.6.40" + }, + "plugin-api-version": "2.2.0" +} From a09ea583a10e1afc3ae0c83e66a06283aad05467 Mon Sep 17 00:00:00 2001 From: badasukerubin Date: Wed, 11 Sep 2024 22:37:43 +0100 Subject: [PATCH 2/3] feat: custom-composer: composer_install --- action.yml | 14 ++++++++++---- bin/composer_install.sh | 5 +++++ tests/expect/composer_install_20.exp | 13 +++++++++++++ tests/expect/composer_install_21.exp | 13 +++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100755 tests/expect/composer_install_20.exp create mode 100755 tests/expect/composer_install_21.exp diff --git a/action.yml b/action.yml index 17339c4..594ec69 100644 --- a/action.yml +++ b/action.yml @@ -39,6 +39,10 @@ inputs: Require lock file for install command. required: false default: "false" + custom-composer-filename: + description: >- + The custom Composer filename to use (e.g. `composer-gh-actions`, `composer-staging`). + required: false runs: using: "composite" @@ -51,7 +55,7 @@ runs: - name: "Determine whether we should ignore caching" id: "should-cache" shell: "bash" - run: "${GITHUB_ACTION_PATH}/bin/should_cache.sh \"${{ inputs.ignore-cache }}\"" + run: '${GITHUB_ACTION_PATH}/bin/should_cache.sh "${{ inputs.ignore-cache }}"' - name: "Determine Composer paths" id: "composer" @@ -61,7 +65,8 @@ runs: ${GITHUB_ACTION_PATH}/bin/composer_paths.sh \ "" \ "${{ inputs.working-directory }}" \ - "${{ steps.php.outputs.path }}" + "${{ steps.php.outputs.path }}" \ + "${{ inputs.custom-composer-filename }}" - name: "Determine cache key" id: "cache-key" @@ -73,7 +78,7 @@ runs: "${{ steps.php.outputs.version }}" \ "${{ inputs.dependency-versions }}" \ "${{ inputs.composer-options }}" \ - "${{ hashFiles('**/composer.json', '**/composer.lock') }}" \ + "${{ hashFiles('**/${{ inputs.custom-composer-filename }}.json', '**/${{ inputs.custom-composer-filename }}.lock') }}" \ "${{ inputs.custom-cache-key }}" \ "${{ inputs.custom-cache-suffix }}" \ "${{ inputs.working-directory }}" @@ -97,4 +102,5 @@ runs: "${{ steps.php.outputs.path }}" \ "${{ steps.composer.outputs.composer_command }}" \ "${{ steps.composer.outputs.lock }}" \ - "${{ inputs.require-lock-file }}" + "${{ inputs.require-lock-file }}" \ + "${{ inputs.custom-composer-filename }}" \ diff --git a/bin/composer_install.sh b/bin/composer_install.sh index c2e9441..c686a52 100755 --- a/bin/composer_install.sh +++ b/bin/composer_install.sh @@ -7,6 +7,7 @@ php_path="${4:-$(which php)}" composer_path="${5:-$(which composer)}" composer_lock="${6}" require_lock_file="${7}" +custom_composer_filename="${8:-}" composer_command="update" composer_options=("--no-interaction" "--no-progress" "--ansi") @@ -33,6 +34,10 @@ if [ -n "${working_directory}" ]; then composer_options+=("--working-dir" "${working_directory}") fi +if [ -n "${custom_composer_filename}" ]; then + export COMPOSER="${custom_composer_filename}.json" +fi + full_command="${php_path} ${composer_path} ${composer_command} ${composer_options[*]}" echo "::debug::Using the following Composer command: '${full_command}'" $full_command diff --git a/tests/expect/composer_install_20.exp b/tests/expect/composer_install_20.exp new file mode 100755 index 0000000..a8b195d --- /dev/null +++ b/tests/expect/composer_install_20.exp @@ -0,0 +1,13 @@ +#!/usr/bin/env -S expect -f + +set timeout 3 +spawn ../../bin/composer_install.sh "" "" "../fixtures/custom-composer" "" "" "composer-gh-actions.lock" "" "composer-gh-actions" +match_max 100000 + +expect "::debug::Using the following Composer command: '*/php */composer install --no-interaction --no-progress --ansi --working-dir ../fixtures/custom-composer'" +expect "Installing dependencies" +expect "Generating autoload files" +expect eof + +# Clean up +file delete -force ../fixtures/custom-composer/vendor diff --git a/tests/expect/composer_install_21.exp b/tests/expect/composer_install_21.exp new file mode 100755 index 0000000..0b48b8c --- /dev/null +++ b/tests/expect/composer_install_21.exp @@ -0,0 +1,13 @@ +#!/usr/bin/env -S expect -f + +set timeout 3 +spawn ../../bin/composer_install.sh "" "" "../fixtures/custom-composer" "" "" "" "" "composer-gh-actions" +match_max 100000 + +expect "::debug::Using the following Composer command: '*/php */composer update --no-interaction --no-progress --ansi --working-dir ../fixtures/custom-composer'" +expect "Installing dependencies" +expect "Generating autoload files" +expect eof + +# Clean up +file delete -force ../fixtures/custom-composer/vendor From 202e35c8737cabda6696e19a75d5075b16da30b5 Mon Sep 17 00:00:00 2001 From: badasukerubin Date: Thu, 12 Sep 2024 12:27:10 +0100 Subject: [PATCH 3/3] feat: custom-composer: docs --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index c50fe8e..3dbbfc9 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,19 @@ For example: composer-options: "--ignore-platform-reqs --optimize-autoloader" ``` +#### custom-composer-filename + +If you have a custom `composer` filename, you may use the `custom-composer-filename`. For example, your `composer` file could be `composer-gh-actions.json` or `composer-staging.json` instead of the default `composer.json`. +You should specify the filename without the extension. + +For example: + +```yaml +- uses: "ramsey/composer-install@v3" + with: + custom-composer-filename: "composer-gh-actions" +``` + #### working-directory The `working-directory` input parameter allows you to specify a different