-
Notifications
You must be signed in to change notification settings - Fork 4.2k
299 lines (253 loc) · 13 KB
/
unit-test.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
name: Unit Tests
# Since Unit Tests are required to pass for each PR,
# we cannot disable them for documentation-only changes.
on:
pull_request:
push:
branches:
- trunk
- 'release/**'
- 'wp/**'
# Allow manually triggering the workflow.
workflow_dispatch:
# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true
jobs:
unit-js:
name: JavaScript
runs-on: ubuntu-latest
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}
strategy:
fail-fast: false
matrix:
node: ['14']
steps:
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
- name: Setup Node.js and install dependencies
uses: ./.github/setup-node
with:
node-version: ${{ matrix.node }}
- name: Npm build
# It's not necessary to run the full build, since Jest can interpret
# source files with `babel-jest`. Some packages have their own custom
# build tasks, however. These must be run.
run: npx lerna run build
- name: Running the tests
run: npm run test:unit -- --ci --maxWorkers=2 --cacheDirectory="$HOME/.jest-cache"
- name: Running the date tests
run: npm run test:unit:date -- --ci --maxWorkers=2 --cacheDirectory="$HOME/.jest-cache"
compute-previous-wordpress-version:
name: Compute previous WordPress version
runs-on: ubuntu-latest
outputs:
previous-wordpress-version: ${{ steps.get-previous-wordpress-version.outputs.previous-wordpress-version }}
steps:
- name: Get previous WordPress version
id: get-previous-wordpress-version
run: |
curl \
-H "Accept: application/json" \
-o versions.json \
"http://api.wordpress.org/core/stable-check/1.0/"
LATEST_WP_VERSION=$(jq --raw-output 'with_entries(select(.value=="latest"))|keys[]' versions.json)
IFS='.' read LATEST_WP_MAJOR LATEST_WP_MINOR LATEST_WP_PATCH <<< "${LATEST_WP_VERSION}"
if [[ ${LATEST_WP_MINOR} == "0" ]]; then
PREVIOUS_WP_SERIES="$((LATEST_WP_MAJOR - 1)).9"
else
PREVIOUS_WP_SERIES="${LATEST_WP_MAJOR}.$((LATEST_WP_MINOR - 1))"
fi
PREVIOUS_WP_VERSION=$(jq --raw-output --arg series "${PREVIOUS_WP_SERIES}" 'with_entries(select(.key|startswith($series)))|keys[-1]' versions.json)
echo "previous-wordpress-version=${PREVIOUS_WP_VERSION}" >> $GITHUB_OUTPUT
rm versions.json
test-php:
name: PHP ${{ matrix.php }}${{ matrix.multisite && ' multisite' || '' }}${{ matrix.wordpress != '' && format( ' (WP {0}) ', matrix.wordpress ) || '' }} on ubuntu-latest
needs: compute-previous-wordpress-version
runs-on: ubuntu-latest
timeout-minutes: 20
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}
strategy:
fail-fast: true
matrix:
php:
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
multisite: [false, true]
wordpress: [''] # Latest WordPress version.
include:
# Test with the previous WP version.
- php: '5.6'
wordpress: ${{ needs.compute-previous-wordpress-version.outputs.previous-wordpress-version }}
- php: '7.4'
wordpress: ${{ needs.compute-previous-wordpress-version.outputs.previous-wordpress-version }}
- php: '8.2'
wordpress: ${{ needs.compute-previous-wordpress-version.outputs.previous-wordpress-version }}
env:
WP_ENV_PHP_VERSION: ${{ matrix.php }}
WP_ENV_CORE: ${{ matrix.wordpress == '' && 'WordPress/WordPress' || format( 'https://wordpress.org/wordpress-{0}.zip', matrix.wordpress ) }}
steps:
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
- name: Setup Node.js and install dependencies
uses: ./.github/setup-node
##
# This allows Composer dependencies to be installed using a single step.
#
# Since the tests are currently run within the Docker containers where the PHP version varies,
# the same PHP version needs to be configured for the action runner machine so that the correct
# dependency versions are installed and cached.
##
- name: Set up PHP
uses: shivammathur/setup-php@d30ad8b1843ace22e6698ab99bbafaa747b6bd0d # v2.24.0
with:
php-version: '${{ matrix.php }}'
ini-file: development
coverage: none
# Ensure that Composer installs the correct versions of packages.
- name: Override PHP version in composer.json
run: composer config platform.php ${{ matrix.php }}
# The spatie/phpunit-watcher package is not compatible with PHP < 7.2.
# It must be removed before running the tests.
- name: Remove incompatible Composer packages
if: ${{ matrix.php < '7.2' }}
run: composer remove spatie/phpunit-watcher --dev --no-update
# Since Composer dependencies are installed using `composer update` and no lock file is in version control,
# passing a custom cache suffix ensures that the cache is flushed at least once per week.
- name: Install Composer dependencies
uses: ramsey/composer-install@83af392bf5f031813d25e6fe4cd626cdba9a2df6 # v2.2.0
with:
custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F")
- name: Npm build
run: npm run build
- name: Docker debug information
run: |
docker -v
docker-compose -v
- name: General debug information
run: |
npm --version
node --version
curl --version
git --version
svn --version
locale -a
- name: Start Docker environment
run: npm run wp-env start
- name: Log running Docker containers
run: docker ps -a
- name: Docker container debug information
run: |
npm run wp-env run tests-mysql mysql -- --version
npm run wp-env run tests-wordpress php -- --version
npm run wp-env run tests-wordpress php -m
npm run wp-env run tests-wordpress php -i
npm run wp-env run tests-wordpress /var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit -- --version
npm run wp-env run tests-wordpress locale -a
- name: Running single site unit tests
if: ${{ ! matrix.multisite }}
run: |
set -o pipefail
npm run test:unit:php | tee phpunit.log
- name: Running multisite unit tests
if: ${{ matrix.multisite }}
run: |
set -o pipefail
npm run test:unit:php:multisite | tee phpunit.log
# Verifies that PHPUnit actually runs in the first place. We want visibility
# into issues which can cause it to fail silently, so we check the output
# to verify that at least 500 tests have passed. This is an arbitrary
# number, but makes sure a drastic change doesn't happen without us noticing.
- name: Check number of passed tests
run: |
# Note: relies on PHPUnit execution to fail on test failure.
# Extract the number of executed tests from the log file.
if ! num_tests=$(grep -Eo 'OK \([0-9]+ tests' phpunit.log) ; then
if ! num_tests=$(grep -Eo 'Tests: [0-9]+, Assertions:' phpunit.log) ; then
echo "PHPUnit failed or did not run. Check the PHPUnit output in the previous step to debug." && exit 1
fi
fi
# Extract just the number of tests from the string.
num_tests=$(echo "$num_tests" | grep -Eo '[0-9]+')
if [ $num_tests -lt 500 ] ; then
echo "Only $num_tests tests passed, which is much fewer than expected." && exit 1
fi
echo "$num_tests tests passed."
phpcs:
name: PHP coding standards
runs-on: ubuntu-latest
timeout-minutes: 20
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}
steps:
- name: Checkout repository
uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
- name: Set up PHP
uses: shivammathur/setup-php@d30ad8b1843ace22e6698ab99bbafaa747b6bd0d # v2.24.0
with:
php-version: '7.4'
coverage: none
tools: cs2pr
# This date is used to ensure that the PHPCS cache is cleared at least once every week.
# http://man7.org/linux/man-pages/man1/date.1.html
- name: "Get last Monday's date"
id: get-date
run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> $GITHUB_OUTPUT
- name: Cache PHPCS scan cache
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1
with:
path: .cache/phpcs.json
key: ${{ runner.os }}-date-${{ steps.get-date.outputs.date }}-phpcs-cache-${{ hashFiles('**/composer.json', 'phpcs.xml.dist') }}
# Since Composer dependencies are installed using `composer update` and no lock file is in version control,
# passing a custom cache suffix ensures that the cache is flushed at least once per week.
- name: Install Composer dependencies
uses: ramsey/composer-install@83af392bf5f031813d25e6fe4cd626cdba9a2df6 # v2.2.0
with:
custom-cache-suffix: ${{ steps.get-date.outputs.date }}
- name: Make Composer packages available globally
run: echo "${PWD}/vendor/bin" >> $GITHUB_PATH
- name: Run PHPCS on all Gutenberg files
id: phpcs-gutenberg
run: phpcs --report-full --report-checkstyle=./.cache/phpcs-report.xml
- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs-gutenberg.outcome == 'failure' }}
run: cs2pr ./.cache/phpcs-report.xml
- name: Ensure version-controlled files are not modified during the tests
run: git diff --exit-code
# This job is deprecated but be present for compatibility reasons.
unit-php:
name: PHP
runs-on: ubuntu-latest
needs: [test-php, phpcs]
if: ${{ always() }}
steps:
- name: Fail the job if the PHPUnit tests fail
if: ${{ needs.test-php.result != 'success' }}
run: exit 1
- name: "Fail the job if the code doesn't conform to the coding standards"
if: ${{ needs.phpcs.result != 'success' }}
run: exit 1
- name: Mark the job as passed if all the checks pass
if: ${{ needs.test-php.result == 'success' && needs.phpcs.result == 'success' }}
run: exit 0
mobile-unit-js:
name: Mobile
runs-on: ubuntu-latest
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}
steps:
- uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
- name: Setup Node.js and install dependencies
uses: ./.github/setup-node
- name: Npm build
# It's not necessary to run the full build, since Jest can interpret
# source files with `babel-jest`. Some packages have their own custom
# build tasks, however. These must be run.
run: npx lerna run build
- name: Running the tests
run: npm run native test -- --ci --maxWorkers=2 --cacheDirectory="$HOME/.jest-cache"