Skip to content

Commit 3561d3d

Browse files
committed
Use the value of COMPOSER env var, if it exists
Fixes #264
1 parent 705380e commit 3561d3d

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

bin/composer_install.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ php_path="${4:-$(which php)}"
77
composer_path="${5:-$(which composer)}"
88
composer_lock="${6:-}"
99
require_lock_file="${7:-}"
10-
composer_filename="${8:-composer}"
10+
composer_filename="${8:-}"
1111

1212
composer_command="update"
1313
composer_options=("--no-interaction" "--no-progress" "--ansi")
@@ -34,7 +34,11 @@ if [ -n "${working_directory}" ]; then
3434
composer_options+=("--working-dir" "${working_directory}")
3535
fi
3636

37-
COMPOSER="${composer_filename}.json"
37+
if [ -n "${composer_filename}" ]; then
38+
COMPOSER="${composer_filename}.json"
39+
elif [ -z "${COMPOSER}" ]; then
40+
COMPOSER="composer.json"
41+
fi
3842
export COMPOSER
3943

4044
full_command="${php_path} ${composer_path} ${composer_command} ${composer_options[*]}"

bin/composer_paths.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
composer_path="${1:-$(which composer)}"
44
working_directory="${2:-.}"
55
php_path="${3:-$(which php)}"
6-
composer_filename="${4:-composer}"
6+
composer_filename="${4:-}"
77

88
function test_composer {
99
"${php_path}" "${composer_path}" --version > /dev/null 2>&1
@@ -18,6 +18,15 @@ if ! test_composer; then
1818
exit 1
1919
fi
2020

21+
if [ -z "${composer_filename}" ]; then
22+
if [ -n "${COMPOSER}" ]; then
23+
composer_filename="${COMPOSER##*/}"
24+
composer_filename="${composer_filename%.*}"
25+
else
26+
composer_filename="composer"
27+
fi
28+
fi
29+
2130
composer_json="${composer_filename}.json"
2231
composer_lock="${composer_filename}.lock"
2332

tests/composer_install_with_custom_composer_file.bats

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,41 @@ teardown() {
6060

6161
assert_dir_exists "$PROJECT_ROOT/tests/fixtures/custom-composer/vendor"
6262
}
63+
64+
@test 'installs dependencies using custom composer file via COMPOSER env var and specified lock file' {
65+
export COMPOSER='composer-gh-actions.json'
66+
67+
run -0 composer_install.sh \
68+
'' \
69+
'' \
70+
'' \
71+
'' \
72+
'' \
73+
'composer-gh-actions.lock'
74+
75+
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
76+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
77+
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
78+
assert_line --partial 'Verifying lock file contents can be installed on current platform'
79+
assert_line --partial 'Generating autoload files'
80+
81+
refute_line --partial 'Updating dependencies'
82+
83+
assert_dir_exists "$PROJECT_ROOT/tests/fixtures/custom-composer/vendor"
84+
}
85+
86+
@test 'installs dependencies using custom composer file via COMPOSER env var without specifying lock file' {
87+
export COMPOSER='composer-gh-actions.json'
88+
89+
run -0 composer_install.sh
90+
91+
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi'$"
92+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
93+
assert_line --partial 'Updating dependencies'
94+
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
95+
assert_line --partial 'Generating autoload files'
96+
97+
refute_line --partial 'Verifying lock file contents can be installed on current platform'
98+
99+
assert_dir_exists "$PROJECT_ROOT/tests/fixtures/custom-composer/vendor"
100+
}

tests/composer_paths.bats

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,70 @@ test_composer_paths() {
203203
assert_line --regexp '^json=.*/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.json$'
204204
assert_line --regexp '^lock=.*/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.lock$'
205205
}
206+
207+
@test 'generates composer paths for custom composer file via COMPOSER env var' {
208+
export COMPOSER='composer-gh-actions.json'
209+
210+
run -0 test_composer_paths \
211+
'' \
212+
"${PROJECT_ROOT}/tests/fixtures/custom-composer"
213+
214+
assert_line --regexp "^::debug::Composer path is '.*/composer'$"
215+
assert_line --regexp "^::debug::Composer version .*$"
216+
assert_line --regexp "^::debug::Composer cache directory found at '.*'$"
217+
assert_line --regexp "^::debug::File composer.json found at '.*/fixtures/custom-composer/composer-gh-actions\.json'$"
218+
assert_line --regexp "^::debug::File composer.lock path computed as '.*/fixtures/custom-composer/composer-gh-actions\.lock'$"
219+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
220+
assert_line --regexp "^composer_command=.*/composer$"
221+
assert_line --regexp "^cache-dir=.*$"
222+
assert_line --regexp '^json=.*/fixtures/custom-composer/composer-gh-actions\.json$'
223+
assert_line --regexp '^lock=.*/fixtures/custom-composer/composer-gh-actions\.lock$'
224+
}
225+
226+
@test 'prints error when custom composer file via COMPOSER env var is not valid' {
227+
export COMPOSER='composer-gh-actions.json'
228+
229+
run ! test_composer_paths \
230+
'' \
231+
"${PROJECT_ROOT}/tests/fixtures/invalid-custom-composer"
232+
233+
assert_line --regexp "^::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$"
234+
}
235+
236+
@test 'generates composer paths for custom composer file via COMPOSER env var without a lock file' {
237+
export COMPOSER='composer-gh-actions.json'
238+
239+
run -0 test_composer_paths \
240+
'' \
241+
"${PROJECT_ROOT}/tests/fixtures/no-lock-file-custom-composer"
242+
243+
assert_line --regexp "^::debug::Composer path is '.*/composer'$"
244+
assert_line --regexp "^::debug::Composer version .*$"
245+
assert_line --regexp "^::debug::Composer cache directory found at '.*'$"
246+
assert_line --regexp "^::debug::File composer.json found at '.*/fixtures/no-lock-file-custom-composer/composer-gh-actions\.json'$"
247+
assert_line --regexp "^::debug::File composer.lock path computed as ''$"
248+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
249+
assert_line --regexp "^composer_command=.*/composer$"
250+
assert_line --regexp "^cache-dir=.*$"
251+
assert_line --regexp '^json=.*/fixtures/no-lock-file-custom-composer/composer-gh-actions\.json$'
252+
assert_line 'lock='
253+
}
254+
255+
@test 'generates composer paths for custom composer file via COMPOSER env var with out-of-sync lock file' {
256+
export COMPOSER='composer-gh-actions.json'
257+
258+
run -0 test_composer_paths \
259+
'' \
260+
"${PROJECT_ROOT}/tests/fixtures/out-of-sync-lock-custom-composer"
261+
262+
assert_line --regexp "^::debug::Composer path is '.*/composer'$"
263+
assert_line --regexp "^::debug::Composer version .*$"
264+
assert_line --regexp "^::debug::Composer cache directory found at '.*'$"
265+
assert_line --regexp "^::debug::File composer.json found at '.*/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.json'$"
266+
assert_line --regexp "^::debug::File composer.lock path computed as '.*/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.lock'$"
267+
assert_line "::debug::The COMPOSER environment variable is 'composer-gh-actions.json'"
268+
assert_line --regexp "^composer_command=.*/composer$"
269+
assert_line --regexp "^cache-dir=.*$"
270+
assert_line --regexp '^json=.*/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.json$'
271+
assert_line --regexp '^lock=.*/fixtures/out-of-sync-lock-custom-composer/composer-gh-actions\.lock$'
272+
}

0 commit comments

Comments
 (0)