Skip to content

Commit 6a168db

Browse files
flagbradramsey
andauthored
fix: Improve composer.lock behavior with ignore-cache (#260)
Fixes a bug where composer.lock is not honored if ignore-cache is set to `true`. Also, the recently added require-lock-file option had not been incorporated into tests and documentation, so added a test and docs (and fixed small typo in CONTRIBUTING) Fixes #259 Co-authored-by: Ben Ramsey <ben@ramsey.dev>
1 parent 314f6c2 commit 6a168db

7 files changed

+68
-21
lines changed

CONTRIBUTING.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ When you do begin working on your feature, here are some guidelines to consider:
8181
* **Submit one feature per pull request.** If you have multiple features you
8282
wish to submit, please break them into separate pull requests.
8383
* **Write good commit messages.** This project follows the
84-
[Conventional Commits][] specification and uses Git hooks to ensure all
84+
[Conventional Commits][conv-commit] specification and uses Git hooks to ensure all
8585
commits follow this standard. Running `composer install` will set up the Git
8686
hooks, so when you run `git commit`, you'll be prompted to create a commit
8787
using the Conventional Commits rules.
@@ -117,3 +117,4 @@ composer test
117117
[issues]: https://github.com/ramsey/composer-install/issues
118118
[pull requests]: https://github.com/ramsey/composer-install/pulls
119119
[gh-flow]: https://guides.github.com/introduction/flow/
120+
[conv-commit]: https://www.conventionalcommits.org/

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ For example:
120120

121121
#### ignore-cache
122122

123+
Normally, ramsey/composer-install preserves composer's cache between jobs
124+
so that subsequent identically-invoked jobs execute faster.
123125
If you have jobs for which you wish to completely ignore the caching step, you
124126
may use the `ignore-cache` input parameter. When present, ramsey/composer-install
125127
will neither read from nor write to the cache.
@@ -175,6 +177,17 @@ even more specific, you can specify a suffix to be added to the cache key via th
175177
> [!WARNING]
176178
> Specifying a `custom-cache-key` will take precedence over the `custom-cache-suffix`.
177179

180+
181+
#### require-lock-file
182+
183+
By default, if no composer.lock file is found in the working directory
184+
ramsey/composer-install will invoke `composer update` regardless of the value of
185+
`dependency-versions`.
186+
187+
If this is set to a value of `true`, ramsey/composer-install will fail in its
188+
execution if it does not find a lock file.
189+
190+
178191
### Fork and private repositories
179192

180193
Sometimes it's needed to use the `repositories` key in your `composer.json` to pull in forks, PRs with patches or private repositories. In this case, your GitHub Action may start failing with a `Could not authenticate against github.com` error message. To solve this, you need to add a GitHub Personal Access token, and this bit to your Action configuration:

action.yml

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ runs:
5555

5656
- name: "Determine Composer paths"
5757
id: "composer"
58-
if: steps.should-cache.outputs.do-cache == 1
5958
shell: "bash"
6059
run: |
6160
${GITHUB_ACTION_PATH}/bin/composer_paths.sh \

bin/composer_paths.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if [ ! -f "${composer_lock}" ]; then
4545
composer_lock=""
4646
fi
4747

48-
composer_version="$($composer_path --version)"
48+
composer_version="$($composer_path --version 2>/dev/null)"
4949
cache_dir="$($composer_path --working-dir="${working_directory}" config cache-dir)"
5050

5151
echo "::debug::Composer path is '${composer_path}'"

tests/composer_install.bats

+20-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ teardown() {
2424
'' \
2525
'' \
2626
'' \
27-
'composer.lock'
27+
'composer.lock' \
28+
''
2829

2930
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
3031
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -43,7 +44,8 @@ teardown() {
4344
'' \
4445
'' \
4546
'' \
46-
'composer.lock'
47+
'composer.lock' \
48+
''
4749

4850
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable'$"
4951
assert_line --partial 'Updating dependencies'
@@ -62,7 +64,8 @@ teardown() {
6264
'' \
6365
'' \
6466
'' \
65-
'composer.lock'
67+
'composer.lock' \
68+
''
6669

6770
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi'$"
6871
assert_line --partial 'Updating dependencies'
@@ -81,7 +84,8 @@ teardown() {
8184
'' \
8285
'' \
8386
'' \
84-
'composer.lock'
87+
'composer.lock' \
88+
''
8589

8690
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
8791
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -100,7 +104,8 @@ teardown() {
100104
'' \
101105
'' \
102106
'' \
103-
'composer.lock'
107+
'composer.lock' \
108+
''
104109

105110
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi'$"
106111
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -119,7 +124,8 @@ teardown() {
119124
'' \
120125
'' \
121126
'' \
122-
'composer.lock'
127+
'composer.lock' \
128+
''
123129

124130
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader'$"
125131
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -138,7 +144,8 @@ teardown() {
138144
'' \
139145
'' \
140146
'' \
141-
'composer.lock'
147+
'composer.lock' \
148+
''
142149

143150
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable --ignore-platform-reqs --optimize-autoloader'$"
144151
assert_line --partial 'Updating dependencies'
@@ -157,7 +164,8 @@ teardown() {
157164
'' \
158165
'' \
159166
'' \
160-
'composer.lock'
167+
'composer.lock' \
168+
''
161169

162170
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader'$"
163171
assert_line --partial 'Updating dependencies'
@@ -176,7 +184,8 @@ teardown() {
176184
'' \
177185
'' \
178186
'' \
179-
'composer.lock'
187+
'composer.lock' \
188+
''
180189

181190
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader'$"
182191
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -195,7 +204,8 @@ teardown() {
195204
'' \
196205
'' \
197206
"$PROJECT_ROOT/tests/fixtures/composer.phar" \
198-
'composer.lock'
207+
'composer.lock' \
208+
''
199209

200210
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer\.phar install --no-interaction --no-progress --ansi'$"
201211
assert_line --partial 'Installing dependencies from lock file (including require-dev)'

tests/composer_install_with_different_working_directory.bats

+16-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ teardown() {
2020
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
2121
'' \
2222
'' \
23-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
23+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
24+
''
2425

2526
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --working-dir .*/fixtures/with-lock-file'$"
2627
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -39,7 +40,8 @@ teardown() {
3940
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
4041
'' \
4142
'' \
42-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
43+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
44+
''
4345

4446
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable --working-dir .*/fixtures/with-lock-file'$"
4547
assert_line --partial 'Updating dependencies'
@@ -58,7 +60,8 @@ teardown() {
5860
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
5961
'' \
6062
'' \
61-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
63+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
64+
''
6265

6366
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --working-dir .*/fixtures/with-lock-file'$"
6467
assert_line --partial 'Updating dependencies'
@@ -77,7 +80,8 @@ teardown() {
7780
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
7881
'' \
7982
'' \
80-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
83+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
84+
''
8185

8286
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --working-dir .*/fixtures/with-lock-file'$"
8387
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -96,7 +100,8 @@ teardown() {
96100
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
97101
'' \
98102
'' \
99-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
103+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
104+
''
100105

101106
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
102107
assert_line --partial 'Installing dependencies from lock file (including require-dev)'
@@ -115,7 +120,8 @@ teardown() {
115120
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
116121
'' \
117122
'' \
118-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
123+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
124+
''
119125

120126
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --prefer-lowest --prefer-stable --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
121127
assert_line --partial 'Updating dependencies'
@@ -134,7 +140,8 @@ teardown() {
134140
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
135141
'' \
136142
'' \
137-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
143+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
144+
''
138145

139146
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
140147
assert_line --partial 'Updating dependencies'
@@ -153,7 +160,8 @@ teardown() {
153160
"$PROJECT_ROOT/tests/fixtures/with-lock-file" \
154161
'' \
155162
'' \
156-
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock"
163+
"$PROJECT_ROOT/tests/fixtures/with-lock-file/composer.lock" \
164+
''
157165

158166
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer install --no-interaction --no-progress --ansi --ignore-platform-reqs --optimize-autoloader --working-dir .*/fixtures/with-lock-file'$"
159167
assert_line --partial 'Installing dependencies from lock file (including require-dev)'

tests/composer_install_without_lock_file.bats

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ teardown() {
2222
"$PROJECT_ROOT/tests/fixtures/no-lock-file" \
2323
'' \
2424
'' \
25+
'' \
2526
''
2627

2728
assert_line --index 0 --regexp "^::debug::Using the following Composer command: '.*/php .*/composer update --no-interaction --no-progress --ansi --working-dir .*/fixtures/no-lock-file'$"
@@ -36,3 +37,18 @@ teardown() {
3637
assert_file_exists "$PROJECT_ROOT/tests/fixtures/no-lock-file/composer.lock"
3738
}
3839

40+
@test "results in an error when lock file isn't present and require-lock-file is 'true'" {
41+
run ! composer_install.sh \
42+
'' \
43+
'' \
44+
"$PROJECT_ROOT/tests/fixtures/no-lock-file" \
45+
'' \
46+
'' \
47+
'' \
48+
'true'
49+
50+
assert_line "::error title=Composer Lock File Not Found::Unable to find 'composer.lock'"
51+
52+
assert_dir_not_exists "$PROJECT_ROOT/tests/fixtures/no-lock-file/vendor"
53+
assert_file_not_exists "$PROJECT_ROOT/tests/fixtures/no-lock-file/composer.lock"
54+
}

0 commit comments

Comments
 (0)