From 6fbe99964f3c9f18a27553e8193044a815a09a23 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Wed, 13 Aug 2025 13:22:20 +0200 Subject: [PATCH] Add CI for unit tests --- .../actions/configure-unit-tests/action.yml | 10 +++ .github/workflows/unit-tests.yml | 75 +++++++++++++++++++ tests/unit/Makefile | 1 + tests/unit/main/test_network.c | 2 +- 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 .github/actions/configure-unit-tests/action.yml create mode 100644 .github/workflows/unit-tests.yml diff --git a/.github/actions/configure-unit-tests/action.yml b/.github/actions/configure-unit-tests/action.yml new file mode 100644 index 00000000000..ef8239b7111 --- /dev/null +++ b/.github/actions/configure-unit-tests/action.yml @@ -0,0 +1,10 @@ +name: ./configure (unit tests) +description: Configure PHP with minimal settings for unit testing +runs: + using: composite + steps: + - shell: bash + run: | + set -x + ./buildconf --force + ./configure --disable-all --enable-embed=static diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 00000000000..dc52a152f7a --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,75 @@ +name: Unit Tests +on: + push: + paths: + - 'main/network.c' + - 'tests/unit/**' + - '.github/workflows/unit-tests.yml' + branches: + - master + pull_request: + paths: + - 'main/network.c' + - 'tests/unit/**' + - '.github/workflows/unit-tests.yml' + branches: + - '**' + workflow_dispatch: ~ + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.url || github.run_id }} + cancel-in-progress: true + +env: + CC: ccache gcc + CXX: ccache g++ + +jobs: + UNIT_TESTS: + if: github.repository == 'php/php-src' || github.event_name == 'pull_request' + name: UNIT_TESTS_LINUX_X64 + runs-on: ubuntu-24.04 + timeout-minutes: 20 + steps: + - name: git checkout + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + set -x + sudo apt-get update + sudo apt-get install -y \ + libcmocka-dev \ + autoconf \ + gcc \ + make \ + unzip \ + bison \ + re2c \ + locales \ + ccache + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: "unit-tests-${{hashFiles('main/php_version.h')}}" + append-timestamp: false + save: ${{ github.event_name != 'pull_request' }} + + - name: ./configure (minimal build) + uses: ./.github/actions/configure-unit-tests + + - name: make libphp.a + run: | + set -x + make -j$(/usr/bin/nproc) >/dev/null + + - name: Run unit tests + run: | + set -x + cd tests/unit + make test + diff --git a/tests/unit/Makefile b/tests/unit/Makefile index 050b05bbd21..ae8eeb8ab3e 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile @@ -2,6 +2,7 @@ CC = gcc CFLAGS = -g -Wall -I../../ -I../../Zend -I../../main -I../../TSRM -I. -I.. COMMON_LDFLAGS = ../../.libs/libphp.a -lcmocka -lpthread -lm -ldl -lresolv -lutil +# Update paths in .github/workflows/unit-tests.yml when adding new test to make it run in PR when such file changes TESTS = main/test_network main/test_network_SRC = main/test_network.c main/test_network_LDFLAGS = $(COMMON_LDFLAGS) -Wl,--wrap=connect,--wrap=poll,--wrap=getsockopt,--wrap=gettimeofday diff --git a/tests/unit/main/test_network.c b/tests/unit/main/test_network.c index 9ebdf098e41..3e0e6e37ed9 100644 --- a/tests/unit/main/test_network.c +++ b/tests/unit/main/test_network.c @@ -251,4 +251,4 @@ int main(void) { cmocka_unit_test(test_php_network_connect_socket_connect_error), }; return cmocka_run_group_tests(tests, NULL, NULL); -} \ No newline at end of file +}