Skip to content

Commit 8a406d7

Browse files
authored
Run Emscripten tests in a browser (#569)
1 parent 039b3fd commit 8a406d7

File tree

6 files changed

+300
-2
lines changed

6 files changed

+300
-2
lines changed

.github/workflows/deploy-pages.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,53 @@ jobs:
145145
../
146146
fi
147147
emmake make -j ${{ env.ncpus }} check-cppinterop
148+
cd ./unittests/CppInterOp/
149+
150+
# Fresh install browsers, and run Emscripten tests in them
151+
# This is to match the Emscripten build instructions, where
152+
# we run in a fresh browser, to stop any extra installed
153+
# stuff interferring with the running of the tests
154+
# Explaination of options for emrun
155+
# --browser (name of browser on path)
156+
# --kill_exit makes it so that when emrun finishes,
157+
# that the headless browser we create is killed along with it
158+
# --timeout 60 is such that emrun is killed after 60 seconds if
159+
# still running. emrun should have finished long before then,
160+
# so if it is still running, something went wrong (such as a test
161+
# which crashed the html file). This will cause the ci to fail,
162+
# as a non 0 value of will be returned.
163+
# In the case of Chrome we have the extra --no-sandbox flag, as on
164+
# Ubuntu Chrome will refuse to run otherwise, as it expects to have
165+
# been installed with admin privileges. This flag allows it to run
166+
# in userspace.
167+
168+
# Install Firefox
169+
wget "https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US" -O Firefox-latest.dmg
170+
hdiutil attach Firefox-latest.dmg
171+
cp -r /Volumes/Firefox/Firefox.app $PWD
172+
hdiutil detach /Volumes/Firefox
173+
cd ./Firefox.app/Contents/MacOS/
174+
export PATH="$PWD:$PATH"
175+
cd -
176+
177+
# Install Google Chrome
178+
wget https://dl.google.com/chrome/mac/stable/accept_tos%3Dhttps%253A%252F%252Fwww.google.com%252Fintl%252Fen_ph%252Fchrome%252Fterms%252F%26_and_accept_tos%3Dhttps%253A%252F%252Fpolicies.google.com%252Fterms/googlechrome.pkg
179+
pkgutil --expand-full googlechrome.pkg google-chrome
180+
cd ./google-chrome/GoogleChrome.pkg/Payload/Google\ Chrome.app/Contents/MacOS/
181+
export PATH="$PWD:$PATH"
182+
cd -
183+
184+
# Run tests in browsers
185+
echo "Running CppInterOpTests in Firefox"
186+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
187+
echo "Running DynamicLibraryManagerTests in Firefox"
188+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
189+
echo "Running CppInterOpTests in Google Chrome"
190+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
191+
echo "Running DynamicLibraryManagerTests in Google Chrome"
192+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
193+
194+
cd ../../
148195
emmake make -j ${{ env.ncpus }} install
149196
150197
cd ..

.github/workflows/emscripten.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,93 @@ jobs:
608608
fi
609609
610610
emmake make -j ${{ env.ncpus }} check-cppinterop
611+
cd ./unittests/CppInterOp/
612+
# Fresh install browsers, and run Emscripten tests in them
613+
# This is to match the Emscripten build instructions, where
614+
# we run in a fresh browser, to stop any extra installed
615+
# stuff interferring with the running of the tests
616+
# Explaination of options for emrun
617+
# --browser (name of browser on path)
618+
# --kill_exit makes it so that when emrun finishes,
619+
# that the headless browser we create is killed along with it
620+
# --timeout 60 is such that emrun is killed after 60 seconds if
621+
# still running. emrun should have finished long before then,
622+
# so if it is still running, something went wrong (such as a test
623+
# which crashed the html file). This will cause the ci to fail,
624+
# as a non 0 value of will be returned.
625+
# In the case of Chrome we have the extra --no-sandbox flag, as on
626+
# Ubuntu Chrome will refuse to run otherwise, as it expects to have
627+
# been installed with admin privileges. This flag allows it to run
628+
# in userspace.
629+
os="${{ matrix.os }}"
630+
if [[ "${os}" == "macos"* ]]; then
631+
# Install Firefox
632+
wget "https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US" -O Firefox-latest.dmg
633+
hdiutil attach Firefox-latest.dmg
634+
cp -r /Volumes/Firefox/Firefox.app $PWD
635+
hdiutil detach /Volumes/Firefox
636+
cd ./Firefox.app/Contents/MacOS/
637+
export PATH="$PWD:$PATH"
638+
cd -
639+
640+
# Install Google Chrome
641+
wget https://dl.google.com/chrome/mac/stable/accept_tos%3Dhttps%253A%252F%252Fwww.google.com%252Fintl%252Fen_ph%252Fchrome%252Fterms%252F%26_and_accept_tos%3Dhttps%253A%252F%252Fpolicies.google.com%252Fterms/googlechrome.pkg
642+
pkgutil --expand-full googlechrome.pkg google-chrome
643+
cd ./google-chrome/GoogleChrome.pkg/Payload/Google\ Chrome.app/Contents/MacOS/
644+
export PATH="$PWD:$PATH"
645+
cd -
646+
647+
# Run tests in browsers
648+
echo "Running CppInterOpTests in Firefox"
649+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
650+
echo "Running DynamicLibraryManagerTests in Firefox"
651+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
652+
echo "Running CppInterOpTests in Google Chrome"
653+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
654+
echo "Running DynamicLibraryManagerTests in Google Chrome"
655+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
656+
else
657+
export ARCHITECHURE=$(uname -m)
658+
if [[ "$ARCHITECHURE" != "aarch64" ]]; then
659+
# Install Google Chrome
660+
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
661+
dpkg-deb -x google-chrome-stable_current_amd64.deb $PWD/chrome
662+
cd ./chrome/opt/google/chrome/
663+
export PATH="$PWD:$PATH"
664+
cd -
665+
666+
# Install Firefox
667+
wget https://ftp.mozilla.org/pub/firefox/releases/138.0.1/linux-x86_64/en-GB/firefox-138.0.1.tar.xz
668+
tar -xJf firefox-138.0.1.tar.xz
669+
cd ./firefox
670+
export PATH="$PWD:$PATH"
671+
cd -
672+
673+
# Run tests in browsers
674+
echo "Running CppInterOpTests in Firefox"
675+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
676+
echo "Running DynamicLibraryManagerTests in Firefox"
677+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
678+
echo "Running CppInterOpTests in Google Chrome"
679+
emrun --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
680+
echo "Running DynamicLibraryManagerTests in Google Chrome"
681+
emrun --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
682+
else
683+
# Install Firefox
684+
wget https://ftp.mozilla.org/pub/firefox/releases/138.0.1/linux-aarch64/en-GB/firefox-138.0.1.tar.xz
685+
tar -xJf firefox-138.0.1.tar.xz
686+
cd ./firefox
687+
export PATH="$PWD:$PATH"
688+
cd -
689+
690+
# Run tests in browsers
691+
echo "Running CppInterOpTests in Firefox"
692+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
693+
echo "Running DynamicLibraryManagerTests in Firefox"
694+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
695+
fi
696+
fi
697+
cd ../..
611698
emmake make -j ${{ env.ncpus }} install
612699
cd ..
613700

Emscripten-build-instructions.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ $env:CMAKE_PREFIX_PATH=$env:PREFIX
172172
$env:CMAKE_SYSTEM_PREFIX_PATH=$env:PREFIX
173173
```
174174

175-
on Windows. Now to build and test your Emscripten build of CppInterOp on Linux and osx execute the following
175+
on Windows. Now to build and test your Emscripten build of CppInterOp using node on Linux and osx execute the following
176176

177177
```bash
178178
mkdir build
@@ -189,7 +189,7 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
189189
emmake make -j $(nproc --all) check-cppinterop
190190
```
191191

192-
To build and test your Emscripten build of CppInterOp on Windows execute the following
192+
To build and test your Emscripten build of CppInterOp on using node Windows execute the following
193193

194194
```powershell
195195
emcmake cmake -DCMAKE_BUILD_TYPE=Release `
@@ -206,9 +206,85 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release `
206206
emmake make -j $(nproc --all) check-cppinterop
207207
```
208208

209+
It is possible to run the Emscripten tests in a headless browser on Linux and osx (in future we plan to include instructions on how to run the tests in a browser on Windows too). To do this we will first move to the tests directory
210+
211+
212+
```bash
213+
cd ./unittests/CppInterOp/
214+
```
215+
216+
We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this on MacOS execute the following
217+
218+
```bash
219+
wget "https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US" -O Firefox-latest.dmg
220+
hdiutil attach Firefox-latest.dmg
221+
cp -r /Volumes/Firefox/Firefox.app $PWD
222+
hdiutil detach /Volumes/Firefox
223+
cd ./Firefox.app/Contents/MacOS/
224+
export PATH="$PWD:$PATH"
225+
cd -
226+
227+
wget https://dl.google.com/chrome/mac/stable/accept_tos%3Dhttps%253A%252F%252Fwww.google.com%252Fintl%252Fen_ph%252Fchrome%252Fterms%252F%26_and_accept_tos%3Dhttps%253A%252F%252Fpolicies.google.com%252Fterms/googlechrome.pkg
228+
pkgutil --expand-full googlechrome.pkg google-chrome
229+
cd ./google-chrome/GoogleChrome.pkg/Payload/Google\ Chrome.app/Contents/MacOS/
230+
export PATH="$PWD:$PATH"
231+
cd -
232+
233+
echo "Running CppInterOpTests in Firefox"
234+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
235+
echo "Running DynamicLibraryManagerTests in Firefox"
236+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
237+
echo "Running CppInterOpTests in Google Chrome"
238+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
239+
echo "Running DynamicLibraryManagerTests in Google Chrome"
240+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
241+
```
242+
243+
To do this on Ubuntu x86 execute the following
244+
245+
```bash
246+
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
247+
dpkg-deb -x google-chrome-stable_current_amd64.deb $PWD/chrome
248+
cd ./chrome/opt/google/chrome/
249+
export PATH="$PWD:$PATH"
250+
cd -
251+
252+
wget https://ftp.mozilla.org/pub/firefox/releases/138.0.1/linux-x86_64/en-GB/firefox-138.0.1.tar.xz
253+
tar -xJf firefox-138.0.1.tar.xz
254+
cd ./firefox
255+
export PATH="$PWD:$PATH"
256+
cd -
257+
258+
echo "Running CppInterOpTests in Firefox"
259+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
260+
echo "Running DynamicLibraryManagerTests in Firefox"
261+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
262+
echo "Running CppInterOpTests in Google Chrome"
263+
emrun --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
264+
echo "Running DynamicLibraryManagerTests in Google Chrome"
265+
emrun --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
266+
```
267+
268+
and on Ubuntu Arm execute the following (Google Chrome is not available on Ubuntu arm,
269+
so we currently only run in Firefox on this platform, unlike other plaforms)
270+
271+
```bash
272+
wget https://ftp.mozilla.org/pub/firefox/releases/138.0.1/linux-aarch64/en-GB/firefox-138.0.1.tar.xz
273+
tar -xJf firefox-138.0.1.tar.xz
274+
cd ./firefox
275+
export PATH="$PWD:$PATH"
276+
cd -
277+
278+
echo "Running CppInterOpTests in Firefox"
279+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
280+
echo "Running DynamicLibraryManagerTests in Firefox"
281+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
282+
```
283+
209284
Assuming it passes all test you can install by executing the following
210285

211286
```bash
287+
cd ../..
212288
emmake make -j $(nproc --all) install
213289
```
214290

docs/Emscripten-build-instructions.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,80 @@ To build and test your Emscripten build of CppInterOp on Windows execute the fol
230230
..\
231231
emmake make -j $(nproc --all) check-cppinterop
232232
233+
It is possible to run the Emscripten tests in a headless browser on Linux and osx (in future we plan to include instructions on how to run the tests in a browser on Windows too). To do this we will first move to the tests directory
234+
235+
.. code:: bash
236+
237+
cd ./unittests/CppInterOp/
238+
239+
We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this on MacOS execute the following
240+
241+
.. code:: bash
242+
243+
wget "https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US" -O Firefox-latest.dmg
244+
hdiutil attach Firefox-latest.dmg
245+
cp -r /Volumes/Firefox/Firefox.app $PWD
246+
hdiutil detach /Volumes/Firefox
247+
cd ./Firefox.app/Contents/MacOS/
248+
export PATH="$PWD:$PATH"
249+
cd -
250+
251+
wget https://dl.google.com/chrome/mac/stable/accept_tos%3Dhttps%253A%252F%252Fwww.google.com%252Fintl%252Fen_ph%252Fchrome%252Fterms%252F%26_and_accept_tos%3Dhttps%253A%252F%252Fpolicies.google.com%252Fterms/googlechrome.pkg
252+
pkgutil --expand-full googlechrome.pkg google-chrome
253+
cd ./google-chrome/GoogleChrome.pkg/Payload/Google\ Chrome.app/Contents/MacOS/
254+
export PATH="$PWD:$PATH"
255+
cd -
256+
257+
echo "Running CppInterOpTests in Firefox"
258+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
259+
echo "Running DynamicLibraryManagerTests in Firefox"
260+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
261+
echo "Running CppInterOpTests in Google Chrome"
262+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
263+
echo "Running DynamicLibraryManagerTests in Google Chrome"
264+
emrun --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
265+
266+
To do this on Ubuntu x86 execute the following
267+
268+
.. code:: bash
269+
270+
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
271+
dpkg-deb -x google-chrome-stable_current_amd64.deb $PWD/chrome
272+
cd ./chrome/opt/google/chrome/
273+
export PATH="$PWD:$PATH"
274+
cd -
275+
276+
wget https://ftp.mozilla.org/pub/firefox/releases/138.0.1/linux-x86_64/en-GB/firefox-138.0.1.tar.xz
277+
tar -xJf firefox-138.0.1.tar.xz
278+
cd ./firefox
279+
export PATH="$PWD:$PATH"
280+
cd -
281+
282+
echo "Running CppInterOpTests in Firefox"
283+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
284+
echo "Running DynamicLibraryManagerTests in Firefox"
285+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
286+
echo "Running CppInterOpTests in Google Chrome"
287+
emrun --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" CppInterOpTests.html
288+
echo "Running DynamicLibraryManagerTests in Google Chrome"
289+
emrun --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" DynamicLibraryManagerTests.html
290+
291+
and on Ubuntu Arm execute the following (Google Chrome is not available on Ubuntu arm,
292+
so we currently only run the tests using Firefox on this platform, unlike other plaforms)
293+
294+
.. code:: bash
295+
296+
wget https://ftp.mozilla.org/pub/firefox/releases/138.0.1/linux-aarch64/en-GB/firefox-138.0.1.tar.xz
297+
tar -xJf firefox-138.0.1.tar.xz
298+
cd ./firefox
299+
export PATH="$PWD:$PATH"
300+
cd -
301+
302+
echo "Running CppInterOpTests in Firefox"
303+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" CppInterOpTests.html
304+
echo "Running DynamicLibraryManagerTests in Firefox"
305+
emrun --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" DynamicLibraryManagerTests.html
306+
233307
Assuming it passes all test you can install by executing the following.
234308

235309
.. code:: bash

unittests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ if(WIN32)
3434
else()
3535
target_link_libraries(${name} PRIVATE ${ARG_LIBRARIES} ${gtest_libs} ${link_pthreads_lib})
3636
endif()
37+
if(EMSCRIPTEN)
38+
# Without this cmake will try and get node to run the html file.
39+
# This guarantees that it runs the js file, and uses emsdks node.
40+
add_test(NAME cppinterop-${name} COMMAND $ENV{EMSDK_NODE} ${name}.js)
41+
else()
3742
add_test(NAME cppinterop-${name} COMMAND ${name})
43+
endif()
3844
set_tests_properties(cppinterop-${name} PROPERTIES
3945
TIMEOUT "${TIMEOUT_VALUE}"
4046
ENVIRONMENT "CPLUS_INCLUDE_PATH=${CMAKE_BINARY_DIR}/etc"

unittests/CppInterOp/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
if (EMSCRIPTEN)
2+
# So we create a html file, as well as the javascript file
3+
set(CMAKE_EXECUTABLE_SUFFIX ".html")
24
# Omitting CUDATest.cpp since Emscripten build currently has no GPU support
35
# For Emscripten builds linking to gtest_main will not suffice for gtest to run
46
# the tests and an explicitly main.cpp is needed
@@ -42,6 +44,10 @@ if(EMSCRIPTEN)
4244
#
4345
# --preload-file ${SYSROOT_PATH}/include@/include:
4446
# Preloads the system include directory into the Emscripten virtual filesystem to make headers accessible at runtime.
47+
#
48+
# --emrun
49+
# Makes it so that we run the html file created by this target, that we can capture the standard output
50+
# and output to the terminal
4551
target_link_options(CppInterOpTests
4652
PUBLIC "SHELL: -fexceptions"
4753
PUBLIC "SHELL: -s MAIN_MODULE=1"
@@ -50,6 +56,7 @@ if(EMSCRIPTEN)
5056
PUBLIC "SHELL: -s STACK_SIZE=32mb"
5157
PUBLIC "SHELL: -s INITIAL_MEMORY=128mb"
5258
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
59+
PUBLIC "SHELL: --emrun"
5360
)
5461
endif()
5562

@@ -97,6 +104,7 @@ if(EMSCRIPTEN)
97104
PUBLIC "SHELL: -s STACK_SIZE=32mb"
98105
PUBLIC "SHELL: -s INITIAL_MEMORY=128mb"
99106
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
107+
PUBLIC "SHELL: --emrun"
100108
PUBLIC "SHELL: --preload-file ${CMAKE_CURRENT_BINARY_DIR}/TestSharedLib/unittests/bin/Release/libTestSharedLib.so@/libTestSharedLib.so"
101109
)
102110
endif()

0 commit comments

Comments
 (0)