Skip to content

Run Emscripten tests in a browser #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mcbarton
Copy link
Collaborator

@mcbarton mcbarton commented May 1, 2025

Description

Please include a summary of changes, motivation and context for this PR.

This PR adds the ability to run the Emscripten tests in a browser

Fixes # (issue)

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Added/removed dependencies
  • Required documentation updates

@codecov-commenter
Copy link

codecov-commenter commented May 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.00%. Comparing base (66510a4) to head (d72f58a).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #294   +/-   ##
=======================================
  Coverage   82.00%   82.00%           
=======================================
  Files          20       20           
  Lines         950      950           
  Branches       87       87           
=======================================
  Hits          779      779           
  Misses        171      171           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcbarton mcbarton force-pushed the Run-emscripten-tests-in-browser-as-well-as-node branch 4 times, most recently from 32c6236 to 93dd06f Compare May 1, 2025 18:03
@@ -69,6 +71,8 @@ if(EMSCRIPTEN)
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
PUBLIC "SHELL: --preload-file ../${XEUS_CPP_DATA_DIR}@/share/xeus-cpp"
PUBLIC "SHELL: --preload-file ../${XEUS_CPP_CONF_DIR}@/etc/xeus-cpp"
PUBLIC "SHELL: --emrun"
PUBLIC "SHELL: -s MINIFY_HTML=0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this MINIFY_HTML=0 flag you end up with the situation where the creation of the test_xeus_cpp.html file breaks on MacOS, but not Ubuntu as can be seen here https://github.com/compiler-research/xeus-cpp/actions/runs/14780201517

@mcbarton
Copy link
Collaborator Author

mcbarton commented May 1, 2025

Running the tests makes use of emrun which comes with Emscripten. The documentation for emrun can be found here https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html

@mcbarton mcbarton force-pushed the Run-emscripten-tests-in-browser-as-well-as-node branch from 944d734 to d72f58a Compare May 1, 2025 18:18
@mcbarton mcbarton requested review from vgvassilev and anutosh491 May 1, 2025 18:59
micromamba activate xeus-lite-host
cd build/test
os="${{ matrix.os }}"
if [[ "${os}" == "macos"* ]]; then
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we install a fresh install of Firefox and Chome is to follow what is done in the Emscripten repo (for example here with Firefox https://github.com/emscripten-core/emscripten/blob/38e16464cffc8f886364fe4919a712131a4c9456/.circleci/config.yml#L363 ). It has the added advantage by not using the users installed versions of these browsers (if they have them installed), that there are no add ons, etc that could be causing issues with the tests running. I have made the instructions, such that a developer without admin privileges on there machine can install them.


# Run tests in browsers
echo "Running test_xeus_cpp in Firefox"
python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given emrun is not on our PATH, I have to reference the exact path to emrun.py . I could add ${{ env.BUILD_PREFIX }}/bin/ to PATH, but not sure if this would be wanted.

--kill_exit makes it so that when emrun finishes, that the headless browser we create is killed along with it
--timeout 60 is such that emrun is killed after 60 seconds if still running. emrun should have finished long before then, so if it is still running, something went wrong (such as a test which crashed the html file). This will cause the ci to fail, as a non 0 value of will be returned.

python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html
echo "Running test_xeus_cpp in Google Chrome"
python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html
fi
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of Chrome we have the extra --no-sandbox flag, as on Ubuntu Chrome will refuse to run otherwise, as it expects to have been installed with admin privileges. This flag allows it to run in userspace.

@@ -69,6 +71,8 @@ if(EMSCRIPTEN)
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
PUBLIC "SHELL: --preload-file ../${XEUS_CPP_DATA_DIR}@/share/xeus-cpp"
PUBLIC "SHELL: --preload-file ../${XEUS_CPP_CONF_DIR}@/etc/xeus-cpp"
PUBLIC "SHELL: --emrun"
Copy link
Collaborator Author

@mcbarton mcbarton May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebuild your Emscripten application and add the --emrun [linker flag](https://emscripten.org/docs/tools_reference/emcc.html#emcc-emrun).

This flag injects code into the generated [Module object](https://emscripten.org/docs/api_reference/module.html#module) to enable capture of stdout, stderr and exit().

Note

If you skip this step, you can still run any .html file with emrun, but the capture will not work.

The above is taken from emruns documentation https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html


set_target_properties(test_xeus_cpp PROPERTIES
SUFFIX ".html"
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we create a html file, as well as the javascript file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants