Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit e3e28c6

Browse files
authored
meson: add tests (yhirose#1044)
This integrates the "main" test suite (test/test.cc) in Meson. This allows to run the tests in the CI with the Meson-built version of the library to ensure that nothing breaks unexpectedly. It also simplifies life of downstream packagers, that do not have to write a custom build script to split the library and run tests but can instead just let Meson do that for them.
1 parent 4e05368 commit e3e28c6

File tree

7 files changed

+179
-5
lines changed

7 files changed

+179
-5
lines changed

.github/workflows/test.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,55 @@ jobs:
3939
cd test
4040
msbuild.exe test.sln /verbosity:minimal /t:Build "/p:Configuration=Release;Platform=x64"
4141
x64\Release\test.exe
42+
43+
44+
meson-build:
45+
runs-on: ${{ matrix.os }}
46+
47+
strategy:
48+
matrix:
49+
os: [macos-latest, ubuntu-latest, windows-latest]
50+
51+
steps:
52+
- name: Prepare Git for checkout on Windows
53+
if: matrix.os == 'windows-latest'
54+
run: |
55+
git config --global core.autocrlf false
56+
git config --global core.eol lf
57+
58+
- uses: actions/checkout@v2
59+
60+
- name: Install dependencies on Linux
61+
if: matrix.os == 'ubuntu-latest'
62+
run: sudo apt-get -qq update && sudo apt-get -qq install meson libssl-dev zlib1g-dev libbrotli-dev libgtest-dev
63+
64+
- name: Install dependencies on MacOS
65+
if: matrix.os == 'macos-latest'
66+
run: brew install meson openssl brotli googletest
67+
68+
- name: Setup MSVC on Windows
69+
if: matrix.os == 'windows-latest'
70+
uses: ilammy/msvc-dev-cmd@v1
71+
72+
# It is necessary to remove MinGW and StrawberryPerl as they both provide
73+
# GCC. This causes issues because CMake prefers to use MSVC, while Meson
74+
# uses GCC, if found, causing linking errors.
75+
- name: Install dependencies on Windows
76+
if: matrix.os == 'windows-latest'
77+
run: |
78+
choco uninstall mingw strawberryperl --yes --all-versions --remove-dependencies --skip-autouninstaller --no-color
79+
Remove-Item -Path C:\Strawberry -Recurse
80+
choco install pkgconfiglite --yes --skip-virus-check --no-color
81+
pip install meson ninja
82+
Invoke-WebRequest -Uri https://github.com/google/googletest/archive/refs/heads/master.zip -OutFile googletest-master.zip
83+
Expand-Archive -Path googletest-master.zip
84+
cd googletest-master\googletest-master
85+
cmake -S . -B build -DINSTALL_GTEST=ON -DBUILD_GMOCK=OFF -Dgtest_hide_internal_symbols=ON -DCMAKE_INSTALL_PREFIX=C:/googletest
86+
cmake --build build --config=Release
87+
cmake --install build --config=Release
88+
cd ..\..
89+
90+
- name: Build and test
91+
run: |
92+
meson setup build -Dcpp-httplib_test=true -Dpkg_config_path=C:\googletest\lib\pkgconfig -Db_vscrt=static_from_buildtype
93+
meson test --no-stdsplit --print-errorlogs -C build

meson.build

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ project(
77
'cpp',
88
license: 'MIT',
99
default_options: [
10+
'cpp_std=c++11',
1011
'buildtype=release',
1112
'b_ndebug=if-release',
1213
'b_lto=true',
@@ -71,7 +72,7 @@ if brotli_found_all
7172
args += '-DCPPHTTPLIB_BROTLI_SUPPORT'
7273
endif
7374

74-
cpp_httplib_dep = dependency('', required : false)
75+
cpp_httplib_dep = dependency('', required: false)
7576

7677
if get_option('cpp-httplib_compile')
7778
httplib_ch = custom_target(
@@ -106,3 +107,7 @@ endif
106107
if meson.version().version_compare('>=0.54.0')
107108
meson.override_dependency('cpp-httplib', cpp_httplib_dep)
108109
endif
110+
111+
if get_option('cpp-httplib_test')
112+
subdir('test')
113+
endif

meson_options.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
option('cpp-httplib_openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support')
6-
option('cpp-httplib_zlib', type: 'feature', value: 'auto', description: 'Enable zlib support')
7-
option('cpp-httplib_brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support')
8-
option('cpp-httplib_compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires Python 3)')
5+
option('cpp-httplib_openssl', type: 'feature', value: 'auto', description: 'Enable OpenSSL support')
6+
option('cpp-httplib_zlib', type: 'feature', value: 'auto', description: 'Enable zlib support')
7+
option('cpp-httplib_brotli', type: 'feature', value: 'auto', description: 'Enable Brotli support')
8+
option('cpp-httplib_compile', type: 'boolean', value: false, description: 'Split the header into a compilable header & source file (requires Python 3)')
9+
option('cpp-httplib_test', type: 'boolean', value: false, description: 'Build tests')

test/meson.build

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# SPDX-FileCopyrightText: 2021 Andrea Pappacoda
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
gtest_dep = dependency('gtest', main: true)
6+
openssl = find_program('openssl')
7+
test_conf = files('test.conf')
8+
9+
key_pem = custom_target(
10+
'key_pem',
11+
output: 'key.pem',
12+
command: [openssl, 'genrsa', '-out', '@OUTPUT@', '2048']
13+
)
14+
15+
temp_req = custom_target(
16+
'temp_req',
17+
input: key_pem,
18+
output: 'temp_req',
19+
command: [openssl, 'req', '-new', '-batch', '-config', test_conf, '-key', '@INPUT@', '-out', '@OUTPUT@']
20+
)
21+
22+
cert_pem = custom_target(
23+
'cert_pem',
24+
input: [temp_req, key_pem],
25+
output: 'cert.pem',
26+
command: [openssl, 'x509', '-in', '@INPUT0@', '-days', '3650', '-req', '-signkey', '@INPUT1@', '-out', '@OUTPUT@']
27+
)
28+
29+
cert2_pem = custom_target(
30+
'cert2_pem',
31+
input: key_pem,
32+
output: 'cert2.pem',
33+
command: [openssl, 'req', '-x509', '-config', test_conf, '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-out', '@OUTPUT@', '-extensions', 'SAN']
34+
)
35+
36+
rootca_key_pem = custom_target(
37+
'rootca_key_pem',
38+
output: 'rootCA.key.pem',
39+
command: [openssl, 'genrsa', '-out', '@OUTPUT@', '2048']
40+
)
41+
42+
rootca_cert_pem = custom_target(
43+
'rootca_cert_pem',
44+
input: rootca_key_pem,
45+
output: 'rootCA.cert.pem',
46+
command: [openssl, 'req', '-x509', '-new', '-batch', '-config', files('test.rootCA.conf'), '-key', '@INPUT@', '-days', '1024', '-out', '@OUTPUT@']
47+
)
48+
49+
client_key_pem = custom_target(
50+
'client_key_pem',
51+
output: 'client.key.pem',
52+
command: [openssl, 'genrsa', '-out', '@OUTPUT@', '2048']
53+
)
54+
55+
client_temp_req = custom_target(
56+
'client_temp_req',
57+
input: client_key_pem,
58+
output: 'client_temp_req',
59+
command: [openssl, 'req', '-new', '-batch', '-config', test_conf, '-key', '@INPUT@', '-out', '@OUTPUT@']
60+
)
61+
62+
client_cert_pem = custom_target(
63+
'client_cert_pem',
64+
input: [client_temp_req, rootca_cert_pem, rootca_key_pem],
65+
output: 'client.cert.pem',
66+
command: [openssl, 'x509', '-in', '@INPUT0@', '-days', '370', '-req', '-CA', '@INPUT1@', '-CAkey', '@INPUT2@', '-CAcreateserial', '-out', '@OUTPUT@']
67+
)
68+
69+
# Copy test files to the build directory
70+
configure_file(input: 'ca-bundle.crt', output: 'ca-bundle.crt', copy: true)
71+
configure_file(input: 'image.jpg', output: 'image.jpg', copy: true)
72+
subdir(join_paths('www', 'dir'))
73+
subdir(join_paths('www2', 'dir'))
74+
subdir(join_paths('www3', 'dir'))
75+
76+
test(
77+
'main',
78+
executable(
79+
'main',
80+
'test.cc',
81+
dependencies: [
82+
cpp_httplib_dep,
83+
gtest_dep
84+
]
85+
),
86+
depends: [
87+
key_pem,
88+
cert_pem,
89+
cert2_pem,
90+
rootca_key_pem,
91+
rootca_cert_pem,
92+
client_key_pem,
93+
client_cert_pem
94+
],
95+
workdir: meson.current_build_dir(),
96+
timeout: 300
97+
)

test/www/dir/meson.build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SPDX-FileCopyrightText: 2021 Andrea Pappacoda
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
configure_file(input: 'index.html', output: 'index.html', copy: true)
6+
configure_file(input: 'test.abcde', output: 'test.abcde', copy: true)
7+
configure_file(input: 'test.html', output: 'test.html', copy: true)

test/www2/dir/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-FileCopyrightText: 2021 Andrea Pappacoda
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
configure_file(input: 'index.html', output: 'index.html', copy: true)
6+
configure_file(input: 'test.html', output: 'test.html', copy: true)

test/www3/dir/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SPDX-FileCopyrightText: 2021 Andrea Pappacoda
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
configure_file(input: 'index.html', output: 'index.html', copy: true)
6+
configure_file(input: 'test.html', output: 'test.html', copy: true)

0 commit comments

Comments
 (0)