Skip to content

Commit 1664291

Browse files
Merge pull request #168 from wp-cli/travis-matrix-extra
Allow package to append/overwrite stuff in .travis.yml.
2 parents a97a546 + 81a3114 commit 1664291

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed

features/scaffold-package-tests.feature

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,252 @@ Feature: Scaffold the test suite for an existing package
146146
Warning: File already exists
147147
"""
148148
And the return code should be 0
149+
150+
Scenario: Scaffolds .travis.yml configuration file with travis[-<tag>[-append]].yml append/override files.
151+
Given a community-command/travis-cache-append.yml file:
152+
"""
153+
- $HOME/my-append-cache
154+
"""
155+
And a community-command/travis-env-append.yml file:
156+
"""
157+
- MY_APPEND_ENV="my-append-env"
158+
"""
159+
And a community-command/travis-matrix-append.yml file:
160+
"""
161+
- php: 99.97
162+
env: WP_VERSION=9997.9997
163+
"""
164+
And a community-command/travis-before_install-append.yml file:
165+
"""
166+
- bash bin/my-append-before_install.sh
167+
- php -m
168+
"""
169+
And a community-command/travis-install-append.yml file:
170+
"""
171+
- bash bin/my-append-install.sh
172+
"""
173+
And a community-command/travis-before_script-append.yml file:
174+
"""
175+
- bash bin/my-append-before_script.sh
176+
"""
177+
And a community-command/travis-script-append.yml file:
178+
"""
179+
- bash bin/my-append-script.sh
180+
"""
181+
And a community-command/travis-append.yml file:
182+
"""
183+
184+
addons:
185+
apt:
186+
packages:
187+
- ghostscript
188+
"""
189+
190+
When I run `wp scaffold package-tests community-command`
191+
Then STDOUT should not be empty
192+
And the community-command/.travis.yml file should exist
193+
And the community-command/.travis.yml file should contain:
194+
"""
195+
- $HOME/.composer/cache
196+
"""
197+
And the community-command/.travis.yml file should contain:
198+
"""
199+
- $HOME/my-append-cache
200+
201+
env:
202+
"""
203+
And the community-command/.travis.yml file should contain:
204+
"""
205+
- PATH="$TRAVIS_BUILD_DIR/vendor/bin:$PATH"
206+
"""
207+
And the community-command/.travis.yml file should contain:
208+
"""
209+
- MY_APPEND_ENV="my-append-env"
210+
211+
matrix:
212+
"""
213+
And the community-command/.travis.yml file should contain:
214+
"""
215+
- php: 7.2
216+
"""
217+
And the community-command/.travis.yml file should contain:
218+
"""
219+
- php: 99.97
220+
env: WP_VERSION=9997.9997
221+
222+
before_install:
223+
"""
224+
And the community-command/.travis.yml file should contain:
225+
"""
226+
# Remove Xdebug
227+
"""
228+
And the community-command/.travis.yml file should contain:
229+
"""
230+
- bash bin/my-append-before_install.sh
231+
- php -m
232+
233+
install:
234+
"""
235+
And the community-command/.travis.yml file should contain:
236+
"""
237+
- bash bin/install-package-tests.sh
238+
"""
239+
And the community-command/.travis.yml file should contain:
240+
"""
241+
- bash bin/my-append-install.sh
242+
243+
before_script:
244+
"""
245+
And the community-command/.travis.yml file should contain:
246+
"""
247+
- composer validate
248+
"""
249+
And the community-command/.travis.yml file should contain:
250+
"""
251+
- bash bin/my-append-before_script.sh
252+
253+
script:
254+
"""
255+
And the community-command/.travis.yml file should contain:
256+
"""
257+
- bash bin/test.sh
258+
- bash bin/my-append-script.sh
259+
"""
260+
And the community-command/.travis.yml file should contain:
261+
"""
262+
263+
addons:
264+
apt:
265+
packages:
266+
- ghostscript
267+
"""
268+
269+
Given a community-command/travis-cache.yml file:
270+
"""
271+
cache:
272+
directories:
273+
- $HOME/my-overwrite-cache
274+
"""
275+
And a community-command/travis-env.yml file:
276+
"""
277+
env:
278+
global:
279+
- MY_OVERWRITE_ENV="my-overwrite-env"
280+
"""
281+
And a community-command/travis-matrix.yml file:
282+
"""
283+
matrix:
284+
include:
285+
- php: 99.99
286+
env: WP_VERSION=9999.9999
287+
- php: 99.98
288+
env: WP_VERSION=9999.9998
289+
"""
290+
And a community-command/travis-before_install.yml file:
291+
"""
292+
before_install:
293+
- bash bin/my-overwrite-before_install.sh
294+
"""
295+
And a community-command/travis-install.yml file:
296+
"""
297+
install:
298+
- bash bin/my-overwrite-install.sh
299+
- bash bin/my-overwrite-install2.sh
300+
"""
301+
And a community-command/travis-before_script.yml file:
302+
"""
303+
before_script:
304+
- bash bin/my-overwrite-before_script.sh
305+
"""
306+
And a community-command/travis-script.yml file:
307+
"""
308+
script:
309+
- bash bin/my-overwrite-script.sh
310+
"""
311+
312+
When I try `wp scaffold package-tests community-command --force`
313+
Then STDOUT should not be empty
314+
And the community-command/.travis.yml file should exist
315+
And the community-command/.travis.yml file should contain:
316+
"""
317+
cache:
318+
directories:
319+
- $HOME/my-overwrite-cache
320+
321+
env:
322+
global:
323+
- MY_OVERWRITE_ENV="my-overwrite-env"
324+
325+
matrix:
326+
include:
327+
- php: 99.99
328+
env: WP_VERSION=9999.9999
329+
- php: 99.98
330+
env: WP_VERSION=9999.9998
331+
332+
before_install:
333+
- bash bin/my-overwrite-before_install.sh
334+
335+
install:
336+
- bash bin/my-overwrite-install.sh
337+
- bash bin/my-overwrite-install2.sh
338+
339+
before_script:
340+
- bash bin/my-overwrite-before_script.sh
341+
342+
script:
343+
- bash bin/my-overwrite-script.sh
344+
"""
345+
And the community-command/.travis.yml file should contain:
346+
"""
347+
348+
addons:
349+
apt:
350+
packages:
351+
- ghostscript
352+
"""
353+
# `travis-matrix.yml` overrides `travis-matrix-append.yml`.
354+
And the community-command/.travis.yml file should not contain:
355+
"""
356+
9997
357+
"""
358+
# `travis-<tag>.yml` overrides `travis-<tag>-append.yml`.
359+
And the community-command/.travis.yml file should not contain:
360+
"""
361+
my-append
362+
"""
363+
# `travis-cache.yml` overrides standard generated content.
364+
And the community-command/.travis.yml file should not contain:
365+
"""
366+
.composer
367+
"""
368+
# `travis-env.yml` overrides standard generated content.
369+
And the community-command/.travis.yml file should not contain:
370+
"""
371+
WP_CLI_BIN_DIR
372+
"""
373+
# `travis-matrix.yml` overrides standard generated content.
374+
And the community-command/.travis.yml file should not contain:
375+
"""
376+
7.2
377+
"""
378+
# `travis-before_install.yml` overrides standard generated content.
379+
And the community-command/.travis.yml file should not contain:
380+
"""
381+
# Remove Xdebug
382+
"""
383+
# `travis-install/before_script.yml` overrides standard generated content.
384+
And the community-command/.travis.yml file should not contain:
385+
"""
386+
composer
387+
"""
388+
# `travis-script.yml` overrides standard generated content.
389+
And the community-command/.travis.yml file should not contain:
390+
"""
391+
bin/test.sh
392+
"""
393+
And STDERR should contain:
394+
"""
395+
Warning: File already exists
396+
"""
397+
And the return code should be 0

src/ScaffoldPackageCommand.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,15 @@ public function package_github( $args, $assoc_args ) {
560560
* `$PATH`. Once you've done so, you can run the tests for a project by
561561
* calling `behat`.
562562
*
563+
* For Travis CI, specially-named files in the package directory can be
564+
* used to modify the generated `.travis.yml`, where `<tag>` is one of
565+
* 'cache', 'env', 'matrix', 'before_install', 'install', 'before_script', 'script':
566+
* * `travis-<tag>.yml` - contents used for `<tag>:` (if present following ignored)
567+
* * `travis-<tag>-append.yml` - contents appended to generated `<tag>:`
568+
*
569+
* You can also append to the generated `.travis.yml` with the file:
570+
* * `travis-append.yml` - contents appended to generated `.travis.yml`
571+
*
563572
* ## ENVIRONMENT
564573
*
565574
* The `features/bootstrap/FeatureContext.php` file expects the
@@ -641,8 +650,26 @@ public function package_tests( $args, $assoc_args ) {
641650
$copy_source[ $package_root ]['templates/load-wp-cli.feature'] = $features_dir;
642651
}
643652

653+
$travis_tags = array( 'cache', 'env', 'matrix', 'before_install', 'install', 'before_script', 'script' );
654+
$travis_tag_overwrites = $travis_tag_appends = array();
655+
$travis_append = '';
644656
if ( 'travis' === $assoc_args['ci'] ) {
645657
$copy_source[ $package_root ]['.travis.yml'] = $package_dir;
658+
659+
// Allow a package to overwrite or append to Travis tags.
660+
foreach ( $travis_tags as $travis_tag ) {
661+
if ( file_exists( $package_dir . 'travis-' . $travis_tag . '.yml' ) ) {
662+
$travis_tag_overwrites[ $travis_tag ] = file_get_contents( $package_dir . 'travis-' . $travis_tag . '.yml' );
663+
} elseif ( file_exists( $package_dir . 'travis-' . $travis_tag . '-append.yml' ) ) {
664+
$travis_tag_appends[ $travis_tag ] = file_get_contents( $package_dir . 'travis-' . $travis_tag . '-append.yml' );
665+
}
666+
}
667+
668+
// Allow a package to append to Travis.
669+
if ( file_exists( $package_dir . 'travis-append.yml' ) ) {
670+
$travis_append = file_get_contents( $package_dir . 'travis-append.yml' );
671+
}
672+
646673
} else if ( 'circle' === $assoc_args['ci'] ) {
647674
$copy_source[ $package_root ]['circle.yml'] = $package_dir;
648675
}
@@ -657,6 +684,20 @@ public function package_tests( $args, $assoc_args ) {
657684
$contents = file_get_contents( $source . "/{$file}" );
658685
$file_path = $dir . basename( $file );
659686

687+
if ( '.travis.yml' === $file ) {
688+
foreach ( $travis_tags as $travis_tag ) {
689+
if ( isset( $travis_tag_overwrites[ $travis_tag ] ) ) {
690+
// Note the contents fully overwrite, so should include the tag, eg `env:` etc (or nothing if want to remove totally).
691+
$contents = preg_replace( '/^' . $travis_tag . ':.*?(?:^$|\z)/ms', $travis_tag_overwrites[ $travis_tag ], $contents );
692+
} elseif ( isset( $travis_tag_appends[ $travis_tag ] ) ) {
693+
$contents = preg_replace( '/^' . $travis_tag . ':.*?(?:^$|\z)/ms', '\0' . $travis_tag_appends[ $travis_tag ], $contents );
694+
}
695+
}
696+
if ( $travis_append ) {
697+
$contents = $contents . $travis_append;
698+
}
699+
}
700+
660701
$force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force' );
661702
$should_write_file = $this->prompt_if_files_will_be_overwritten( $file_path, $force );
662703
if ( ! $should_write_file ) {

0 commit comments

Comments
 (0)