From b4a6f34f1cb8c780f00a103d6a1746526f66b085 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 26 Nov 2020 14:48:05 +0100 Subject: [PATCH 1/7] fix(src) allow for the TRIC_HOST env var to override the default hostname lookup --- changelog.md | 5 +++++ src/tric.php | 1 + src/utils.php | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/changelog.md b/changelog.md index 04d86b2..a7231e7 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.5.16] - 2020-11-26 +### Changed + +- Add support for the `TRIC_HOST` environment variable. This will override the default host machine IP address lookup `tric` would perform on Linux or the hard-wired `host.docker.internal` hostname `tric` would use on Windows and Mac host to set the default `xdebug.remote_host` value. + ## [0.5.15] - 2020-11-24 ### Changed diff --git a/src/tric.php b/src/tric.php index 8a9ff07..b53357d 100644 --- a/src/tric.php +++ b/src/tric.php @@ -587,6 +587,7 @@ function tric_info() { 'TRIC_CURRENT_PROJECT', 'TRIC_CURRENT_PROJECT_RELATIVE_PATH', 'TRIC_CURRENT_PROJECT_SUBDIR', + 'TRIC_HOST', 'TRIC_PLUGINS', 'TRIC_THEMES', 'TRIC_GIT_DOMAIN', diff --git a/src/utils.php b/src/utils.php index 89ca591..561e911 100644 --- a/src/utils.php +++ b/src/utils.php @@ -299,6 +299,7 @@ function the_fatality() { * Returns the host machine IP address as reachable from the containers. * * The way the host machine IP address is fetched will vary depending on the Operating System the function runs on. + * If the `TRIC_HOST` environment variable is set, then that will be used without any further check. * * @param string $os The operating system to get the host machine IP address for. * @@ -306,6 +307,10 @@ function the_fatality() { * an empty string to indicate the host machine IP address could not be obtained. */ function host_ip( $os = 'Linux' ) { + if ( $env_set_host = getenv( 'TRIC_HOST' ) ) { + return $env_set_host; + } + if ( $os === 'Linux' ) { // Depending on the distribution being used either one, or both, these commands might yield a result. $commands = [ From 53525f728bf43687e82333e22495e7ceb2fb1ff0 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 26 Nov 2020 15:10:41 +0100 Subject: [PATCH 2/7] fix(src) set xdebug.remote_host from XDH env var if set --- changelog.md | 1 + src/docker.php | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index a7231e7..d6e5b79 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Add support for the `TRIC_HOST` environment variable. This will override the default host machine IP address lookup `tric` would perform on Linux or the hard-wired `host.docker.internal` hostname `tric` would use on Windows and Mac host to set the default `xdebug.remote_host` value. +- Default to the host machin IP address to set `xdebug.remote_host` only if the host has not been set by means of a call to `tric xdebug host ` or by setting the `XDH` environment variable explicitly. ## [0.5.15] - 2020-11-24 ### Changed diff --git a/src/docker.php b/src/docker.php index e03409f..ec93d27 100644 --- a/src/docker.php +++ b/src/docker.php @@ -54,7 +54,8 @@ function docker_compose( array $options = [] ) { if ( ! empty( $host_ip ) ) { // Set the host IP address on Linux machines. - $command = 'XDH=' . host_ip() . ' ' . $command; + $xdebug_remote_host = (string) getenv( 'XDH' ) ?: host_ip(); + $command = 'XDH=' . $xdebug_remote_host . ' ' . $command; } if ( ! empty( $is_ci ) ) { @@ -188,7 +189,8 @@ function docker_compose_process( array $options = [], $is_realtime = true ) { if ( ! empty( $host_ip ) ) { // Set the host IP address on Linux machines. - $command = 'XDH=' . host_ip() . ' ' . $command; + $xdebug_remote_host = (string) getenv( 'XDH' ) ?: host_ip(); + $command = 'XDH=' . $xdebug_remote_host . ' ' . $command; } if ( ! empty( $is_ci ) ) { From e081c39fc286f0ba2cc367775866c922061be0ca Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Thu, 26 Nov 2020 15:23:35 +0100 Subject: [PATCH 3/7] fix(src) update the run config on xdebug set, avoid reset --- changelog.md | 1 + src/tric.php | 2 +- src/utils.php | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index d6e5b79..62022ca 100644 --- a/changelog.md +++ b/changelog.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add support for the `TRIC_HOST` environment variable. This will override the default host machine IP address lookup `tric` would perform on Linux or the hard-wired `host.docker.internal` hostname `tric` would use on Windows and Mac host to set the default `xdebug.remote_host` value. - Default to the host machin IP address to set `xdebug.remote_host` only if the host has not been set by means of a call to `tric xdebug host ` or by setting the `XDH` environment variable explicitly. +- Fix an issue that would reset run settings stored in the `.env.tric.run` file when using the `tric xdebug ` command. ## [0.5.15] - 2020-11-24 ### Changed diff --git a/src/tric.php b/src/tric.php index b53357d..faa539b 100644 --- a/src/tric.php +++ b/src/tric.php @@ -808,7 +808,7 @@ function tric_handle_xdebug( callable $args ) { if ( array_key_exists( $toggle, $map ) ) { $var = $args( 'value' ); echo colorize( "Setting {$map[$toggle]}={$var}" ) . PHP_EOL . PHP_EOL; - write_env_file( $run_settings_file, [ $map[ $toggle ] => $var ] ); + write_env_file( $run_settings_file, [ $map[ $toggle ] => $var ], true ); echo PHP_EOL . PHP_EOL . colorize( "Tear down the stack with down and restar it to apply the new settings!\n" ); return; diff --git a/src/utils.php b/src/utils.php index 561e911..7937ed4 100644 --- a/src/utils.php +++ b/src/utils.php @@ -411,7 +411,7 @@ function root( $path = '' ) { function write_env_file( $file, array $lines = [], $update = false ) { $existing_lines = []; - if ( $update && file_exists( $file ) ) { + if ( $update && is_file( $file ) ) { $existing_lines = read_env_file( $file ); } @@ -422,7 +422,7 @@ function write_env_file( $file, array $lines = [], $update = false ) { }, array_keys( $new_lines ), $new_lines ) ); // If this is the first time creating the .env.tric.run file, assume this is the first run and place the CLI version in `.build-version`. - if ( false !== strpos( $file, '.env.tric.run' ) && ! file_exists( $file ) ) { + if ( false !== strpos( $file, '.env.tric.run' ) && ! is_file( $file ) ) { write_build_version(); } From 8c62f87495729b4ae8b25d1ff9383862948df29f Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 30 Nov 2020 10:18:12 +0100 Subject: [PATCH 4/7] fix(containers/wordpress) correctly detect user group and id for creation --- containers/wordpress/Dockerfile.base | 4 ++-- containers/wordpress/Dockerfile.debug | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/containers/wordpress/Dockerfile.base b/containers/wordpress/Dockerfile.base index b2aca61..017f7ca 100644 --- a/containers/wordpress/Dockerfile.base +++ b/containers/wordpress/Dockerfile.base @@ -6,6 +6,6 @@ FROM wordpress:${WORDPRESS_IMAGE_VERSION} # This will allow setting the `APACHE_RUN_USER`, and with it deal with filemode issues, correctly. ARG DOCKER_RUN_UID=0 ARG DOCKER_RUN_GID=0 -RUN if [ "${DOCKER_RUN_GID}" > 0 ]; then addgroup --gid ${DOCKER_RUN_GID} docker; fi; \ - if [ "${DOCKER_RUN_UID}" > 0 ]; then adduser --uid ${DOCKER_RUN_UID} --gid ${DOCKER_RUN_GID} --home /home/docker \ +RUN if [ "${DOCKER_RUN_GID}" != 0 ]; then addgroup --gid ${DOCKER_RUN_GID} docker; fi; \ + if [ "${DOCKER_RUN_UID}" != 0 ]; then adduser --uid ${DOCKER_RUN_UID} --gid ${DOCKER_RUN_GID} --home /home/docker \ --shell /bin/sh --disabled-password --gecos "" docker; fi diff --git a/containers/wordpress/Dockerfile.debug b/containers/wordpress/Dockerfile.debug index ea6e11d..5f9bf3d 100644 --- a/containers/wordpress/Dockerfile.debug +++ b/containers/wordpress/Dockerfile.debug @@ -6,8 +6,8 @@ FROM wordpress:${WORDPRESS_IMAGE_VERSION} # This will allow setting the `APACHE_RUN_USER`, and with it deal with filemode issues, correctly. ARG DOCKER_RUN_UID=0 ARG DOCKER_RUN_GID=0 -RUN if [ "${DOCKER_RUN_GID}" > 0 ]; then addgroup --gid ${DOCKER_RUN_GID} docker; fi; \ - if [ "${DOCKER_RUN_UID}" > 0 ]; then adduser --uid ${DOCKER_RUN_UID} --gid ${DOCKER_RUN_GID} --home /home/docker \ +RUN if [ "${DOCKER_RUN_GID}" != 0 ]; then addgroup --gid ${DOCKER_RUN_GID} docker; fi; \ + if [ "${DOCKER_RUN_UID}" != 0 ]; then adduser --uid ${DOCKER_RUN_UID} --gid ${DOCKER_RUN_GID} --home /home/docker \ --shell /bin/sh --disabled-password --gecos "" docker; fi # Pull in an helper library to install PHP extensions. From 918c46fd3d8a11e98c1686ef34502354cc10c1a9 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Wed, 2 Dec 2020 11:08:17 +0100 Subject: [PATCH 5/7] fix(tric.php) handle zombie containers preventing network rm --- src/tric.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tric.php b/src/tric.php index faa539b..edd4d44 100644 --- a/src/tric.php +++ b/src/tric.php @@ -982,7 +982,16 @@ function build_command_pool( $base_command, array $command, array $sub_directori $network_name = "tric{$subnet}"; $status = tric_passive()( array_merge( [ '-p', $network_name, 'run', '--rm', $base_command ], $command ), $prefix ); - shell_exec( "docker network rm {$network_name}_tric {$network_name}_default" ); + do { + /* + * Some containers might take time to terminate after yielding control back to the Docker daemon (zombies). + * If we try to remote the network when zombie containers are attached to it, we'll get the following error: + * "error while removing network: network id has active endpoints". + * When this happens, the return status of the command will be a `1`. + * We iterate until the status is a `0`. + */ + $network_rm_status = (int) process( "docker network rm {$network_name}_tric {$network_name}_default" )( 'status' ); + } while ( $network_rm_status !== 0 ); if ( 'target' !== $target ) { tric_switch_target( $using ); From 06877c841819965766e8bc1f3045a90d5b5cc125 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Wed, 2 Dec 2020 12:03:17 +0100 Subject: [PATCH 6/7] feat(commands/run.php) support just run --- src/commands/run.php | 28 ++++++++++++++++++--- src/tric.php | 59 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/commands/run.php b/src/commands/run.php index db1cf2b..8e2d5ba 100644 --- a/src/commands/run.php +++ b/src/commands/run.php @@ -10,16 +10,18 @@ * command. * See the `init` command for more details. * - * @var bool $is_help Whether we're handling an `help` request on this command or not. - * @var \Closure $args The argument map closure, as produced by the `args` function. + * @var string $cli_name The current name of the CLI tool, usually `tric`. + * @var bool $is_help Whether we're handling an `help` request on this command or not. + * @var \Closure $args The argument map closure, as produced by the `args` function. */ namespace Tribe\Test; if ( $is_help ) { - echo colorize( "Runs a Codeception test in the stack, the equivalent of 'codecept run ...'.\n" ); + echo colorize( "Runs a Codeception test in the stack, the equivalent of 'codecept run ...', or all the tests.\n" ); echo PHP_EOL; echo colorize( "This command requires a use target set using the use command.\n" ); + echo colorize( "usage: {$cli_name} run\n" ); echo colorize( "usage: {$cli_name} run [...]\n" ); echo colorize( "example: {$cli_name} run wpunit" ); @@ -86,7 +88,25 @@ // Add tric configuration file, if existing. $run_configuration = array_merge( [ 'run', '--rm', 'codeception', 'run' ], $config_files ); +$run_args = $args( '...' ); +$run_suites = []; + +if ( empty( $run_args ) ) { + $run_suites = collect_target_suites(); +} + // Finally run the command. -$status = tric_realtime()( array_merge( $run_configuration, $args( '...' ) ) ); +if ( empty( $run_suites ) ) { + // Run the command as per user input. + $status = tric_realtime()( array_merge( $run_configuration, $run_args ) ); +} else { + // Run all the suites sequentially, stop at first error. + foreach ( $run_suites as $suite ) { + $status = tric_realtime()( array_merge( $run_configuration, [ $suite ] ) ); + if ( $status !== 0 ) { + exit( $status ); + } + } +} exit( $status ); diff --git a/src/tric.php b/src/tric.php index edd4d44..c6d0dfc 100644 --- a/src/tric.php +++ b/src/tric.php @@ -982,16 +982,18 @@ function build_command_pool( $base_command, array $command, array $sub_directori $network_name = "tric{$subnet}"; $status = tric_passive()( array_merge( [ '-p', $network_name, 'run', '--rm', $base_command ], $command ), $prefix ); - do { - /* - * Some containers might take time to terminate after yielding control back to the Docker daemon (zombies). - * If we try to remote the network when zombie containers are attached to it, we'll get the following error: - * "error while removing network: network id has active endpoints". - * When this happens, the return status of the command will be a `1`. - * We iterate until the status is a `0`. - */ - $network_rm_status = (int) process( "docker network rm {$network_name}_tric {$network_name}_default" )( 'status' ); - } while ( $network_rm_status !== 0 ); + if ( ! empty( $subnet ) ) { + do { + /* + * Some containers might take time to terminate after yielding control back to the Docker daemon (zombies). + * If we try to remote the network when zombie containers are attached to it, we'll get the following error: + * "error while removing network: network id has active endpoints". + * When this happens, the return status of the command will be a `1`. + * We iterate until the status is a `0`. + */ + $network_rm_status = (int) process( "docker network rm {$network_name}_tric {$network_name}_default" )( 'status' ); + } while ( $network_rm_status !== 0 ); + } if ( 'target' !== $target ) { tric_switch_target( $using ); @@ -1326,3 +1328,40 @@ function tric_target_or_fail( $reason = null ) { return $target; } + +/** + * Returns the absolute path to the current target. + * + * @param null|string $append_path A relative path to append to the target absolute path. + * + * @return string The absolute path to the current target. + */ +function absolute_plugin_target_path( $append_path = null ) { + $full_target_path = rtrim( getenv( 'TRIC_HERE_DIR' ), '\\/' ) . '/' . trim( tric_target(), '\\/' ); + if ( empty( $append_path ) ) { + return $full_target_path; + } + + return $full_target_path . '/' . ltrim( $append_path, '\\/' ); +} + +/** + * Compiles a list of the current target Codeception suites. The available suites are inferred, as Codeception does, + * from the available suite configuration files. + * + * @return array A list of the available target suites. + */ +function collect_target_suites() { + // If the command is just `run`, without arguments, then collect the available suites and run them separately. + $dir_iterator = new \DirectoryIterator( absolute_plugin_target_path( 'tests' ) ); + $suitesFilter = new \CallbackFilterIterator( $dir_iterator, static function ( \SplFileInfo $file ) { + return $file->isFile() && preg_match( '/^.*\\.suite(\\.dist)?\\.yml$/', $file->getBasename() ); + } ); + $suites = []; + foreach ( $suitesFilter as $f ) { + $suites[] = preg_replace( '/^([\\w-]+)\\.suite(\\.dist)?\\.yml$/u', '$1', $f->getBasename() ); + } + + return $suites; +} + From 50ce95700a7803093b4c33a2fc5661d0deaa6959 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Wed, 2 Dec 2020 12:08:45 +0100 Subject: [PATCH 7/7] doc(changelog.md) add changelog entries --- changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changelog.md b/changelog.md index 62022ca..419c39d 100644 --- a/changelog.md +++ b/changelog.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add support for the `TRIC_HOST` environment variable. This will override the default host machine IP address lookup `tric` would perform on Linux or the hard-wired `host.docker.internal` hostname `tric` would use on Windows and Mac host to set the default `xdebug.remote_host` value. - Default to the host machin IP address to set `xdebug.remote_host` only if the host has not been set by means of a call to `tric xdebug host ` or by setting the `XDH` environment variable explicitly. - Fix an issue that would reset run settings stored in the `.env.tric.run` file when using the `tric xdebug ` command. +- Correctly handle Docker network removal in parallel tasks to avoid "error while removing network" errors. +- Add support for `tric run` to run all the avaiable Codeception test suites from the target one after another. ## [0.5.15] - 2020-11-24 ### Changed