From d31dc663b9425c967d13291fbd068bba0f4270f1 Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Mon, 11 Jan 2021 22:30:33 +0000 Subject: [PATCH] Split unit test over multiple jobs in GitHub Actions, lowering the total run time (#4985) * Split unit test over multiple jobs in GitHub Actions, lowering the total run time * rename FileManipulationTest to FileManipulationTestCase so it does not run as a standalone Test --- .github/workflows/ci.yml | 38 ++++++++++++++++++- bin/tests-github-actions.sh | 35 +++++++++++++++++ ...nTest.php => FileManipulationTestCase.php} | 2 +- .../ImmutableAnnotationAdditionTest.php | 2 +- .../MissingPropertyTypeTest.php | 2 +- .../MissingReturnTypeTest.php | 2 +- .../ParamNameMismatchTest.php | 2 +- .../ParamTypeManipulationTest.php | 2 +- .../PureAnnotationAdditionTest.php | 2 +- .../ReturnTypeManipulationTest.php | 2 +- .../UndefinedVariableManipulationTest.php | 2 +- ...necessaryVarAnnotationManipulationTest.php | 2 +- .../UnusedCodeManipulationTest.php | 2 +- .../UnusedVariableManipulationTest.php | 2 +- 14 files changed, 83 insertions(+), 14 deletions(-) create mode 100755 bin/tests-github-actions.sh rename tests/FileManipulation/{FileManipulationTest.php => FileManipulationTestCase.php} (97%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3032e62ee..1af30ce67 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,8 +2,42 @@ name: Run unit tests on: [push, pull_request] jobs: - build: + chunk-matrix: + name: Generate Chunk Matrix + runs-on: ubuntu-latest + env: + CHUNK_COUNT: 5 + + outputs: + count: ${{ steps.chunk-matrix.outputs.count }} + chunks: ${{ steps.chunk-matrix.outputs.chunks }} + + steps: + - id: chunk-matrix + name: Generates the Chunk Matrix + run: | + echo "::set-output name=count::$(php -r 'echo json_encode([ ${{ env.CHUNK_COUNT }} ]);')" + echo "::set-output name=chunks::$(php -r 'echo json_encode(range(1, ${{ env.CHUNK_COUNT }} ));')" + + tests: + name: "Unit Tests - ${{ matrix.chunk }}" + + runs-on: ubuntu-latest + needs: + - chunk-matrix + + strategy: + fail-fast: false + matrix: + count: ${{ fromJson(needs.chunk-matrix.outputs.count) }} + chunk: ${{ fromJson(needs.chunk-matrix.outputs.chunks) }} + + env: + CHUNK_COUNT: "${{ matrix.count }}" + CHUNK_NUMBER: "${{ matrix.chunk }}" + PARALLEL_PROCESSES: 5 + steps: - name: Set up PHP uses: shivammathur/setup-php@v2 @@ -35,4 +69,4 @@ jobs: COMPOSER_ROOT_VERSION: dev-master - name: Run unit tests - run: php vendor/bin/paratest --runner=WrapperRunner + run: bin/tests-github-actions.sh diff --git a/bin/tests-github-actions.sh b/bin/tests-github-actions.sh new file mode 100755 index 000000000..8e1f37a67 --- /dev/null +++ b/bin/tests-github-actions.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +set -eu + +function get_seeded_random() { + openssl enc -aes-256-ctr -pass pass:"vimeo/psalm" -nosalt /dev/null +} + +function run { + local -r chunk_count="$1" + local -r chunk_number="$2" + local -r parallel_processes="$3" + + local -r phpunit_cmd=' +echo "::group::{}"; +vendor/bin/phpunit --log-junit build/phpunit/logs/{_}.xml --colors=always {}; +exit_code=$?; +echo ::endgroup::; +if [[ "$exit_code" -ne 0 ]]; then + echo "::error::{}"; +fi; +exit "$exit_code"' + + mkdir -p build/parallel/ build/phpunit/logs/ + + find tests -name '*Test.php' | shuf --random-source=<(get_seeded_random) > build/tests_all + split --number="l/$chunk_number/$chunk_count" build/tests_all > build/tests_split + parallel --group -j"$parallel_processes" --rpl {_}\ s/\\//_/g --joblog build/parallel/jobs.log "$phpunit_cmd" < build/tests_split +} + +if [ -z "${CHUNK_COUNT:-}" ]; then echo "Did not find env var CHUNK_COUNT."; exit 1; fi +if [ -z "${CHUNK_NUMBER:-}" ]; then echo "Did not find env var CHUNK_NUMBER."; exit 1; fi +if [ -z "${PARALLEL_PROCESSES:-}" ]; then echo "Did not find env var PARALLEL_PROCESSES."; exit 1; fi + +run "$CHUNK_COUNT" "$CHUNK_NUMBER" "$PARALLEL_PROCESSES" \ No newline at end of file diff --git a/tests/FileManipulation/FileManipulationTest.php b/tests/FileManipulation/FileManipulationTestCase.php similarity index 97% rename from tests/FileManipulation/FileManipulationTest.php rename to tests/FileManipulation/FileManipulationTestCase.php index 36b2a3f99..631950cda 100644 --- a/tests/FileManipulation/FileManipulationTest.php +++ b/tests/FileManipulation/FileManipulationTestCase.php @@ -7,7 +7,7 @@ use Psalm\Tests\Internal\Provider; use Psalm\Tests\TestConfig; use function strpos; -abstract class FileManipulationTest extends \Psalm\Tests\TestCase +abstract class FileManipulationTestCase extends \Psalm\Tests\TestCase { /** @var \Psalm\Internal\Analyzer\ProjectAnalyzer */ protected $project_analyzer; diff --git a/tests/FileManipulation/ImmutableAnnotationAdditionTest.php b/tests/FileManipulation/ImmutableAnnotationAdditionTest.php index de9d4c11b..b31ddbc08 100644 --- a/tests/FileManipulation/ImmutableAnnotationAdditionTest.php +++ b/tests/FileManipulation/ImmutableAnnotationAdditionTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/MissingPropertyTypeTest.php b/tests/FileManipulation/MissingPropertyTypeTest.php index 4ebd2141f..84e8cff46 100644 --- a/tests/FileManipulation/MissingPropertyTypeTest.php +++ b/tests/FileManipulation/MissingPropertyTypeTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/MissingReturnTypeTest.php b/tests/FileManipulation/MissingReturnTypeTest.php index 899ae60a2..80d7f1d60 100644 --- a/tests/FileManipulation/MissingReturnTypeTest.php +++ b/tests/FileManipulation/MissingReturnTypeTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/ParamNameMismatchTest.php b/tests/FileManipulation/ParamNameMismatchTest.php index 2948251fb..c448a2c85 100644 --- a/tests/FileManipulation/ParamNameMismatchTest.php +++ b/tests/FileManipulation/ParamNameMismatchTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/ParamTypeManipulationTest.php b/tests/FileManipulation/ParamTypeManipulationTest.php index 22336e616..153657ece 100644 --- a/tests/FileManipulation/ParamTypeManipulationTest.php +++ b/tests/FileManipulation/ParamTypeManipulationTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/PureAnnotationAdditionTest.php b/tests/FileManipulation/PureAnnotationAdditionTest.php index 724bb12eb..d810e3ba1 100644 --- a/tests/FileManipulation/PureAnnotationAdditionTest.php +++ b/tests/FileManipulation/PureAnnotationAdditionTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/ReturnTypeManipulationTest.php b/tests/FileManipulation/ReturnTypeManipulationTest.php index ee83044ad..3fc19169c 100644 --- a/tests/FileManipulation/ReturnTypeManipulationTest.php +++ b/tests/FileManipulation/ReturnTypeManipulationTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/UndefinedVariableManipulationTest.php b/tests/FileManipulation/UndefinedVariableManipulationTest.php index b73aa29ae..f66fe1b1c 100644 --- a/tests/FileManipulation/UndefinedVariableManipulationTest.php +++ b/tests/FileManipulation/UndefinedVariableManipulationTest.php @@ -3,7 +3,7 @@ namespace Psalm\Tests\FileManipulation; use const PHP_VERSION; -class UndefinedVariableManipulationTest extends FileManipulationTest +class UndefinedVariableManipulationTest extends FileManipulationTestCase { /** * @return array diff --git a/tests/FileManipulation/UnnecessaryVarAnnotationManipulationTest.php b/tests/FileManipulation/UnnecessaryVarAnnotationManipulationTest.php index ef4b0b3fe..164180487 100644 --- a/tests/FileManipulation/UnnecessaryVarAnnotationManipulationTest.php +++ b/tests/FileManipulation/UnnecessaryVarAnnotationManipulationTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/UnusedCodeManipulationTest.php b/tests/FileManipulation/UnusedCodeManipulationTest.php index 39a9a7d5f..77b92dfa2 100644 --- a/tests/FileManipulation/UnusedCodeManipulationTest.php +++ b/tests/FileManipulation/UnusedCodeManipulationTest.php @@ -1,7 +1,7 @@ diff --git a/tests/FileManipulation/UnusedVariableManipulationTest.php b/tests/FileManipulation/UnusedVariableManipulationTest.php index 4d75fd1c7..2fd916197 100644 --- a/tests/FileManipulation/UnusedVariableManipulationTest.php +++ b/tests/FileManipulation/UnusedVariableManipulationTest.php @@ -1,7 +1,7 @@