diff --git a/.copier-answers.yml b/.copier-answers.yml new file mode 100644 index 0000000..6ee05b6 --- /dev/null +++ b/.copier-answers.yml @@ -0,0 +1,20 @@ +# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY +_commit: v4.2.4 +_src_path: https://github.com/jupyterlab/extension-template +author_email: datalab@tuwien.ac.at +author_name: Florian Jaeger +data_format: string +file_extension: '' +has_binder: true +has_settings: true +kind: server +labextension_name: grader-labextension +mimetype: '' +mimetype_name: '' +project_short_description: Grader Labextension is a JupyterLab extension to enable + automatic grading of assignment notebooks. +python_name: grader_labextension +repository: https://github.com/TU-Wien-dataLAB/Grader-Labextension +test: true +viewer_name: '' + diff --git a/.github/workflows/binder-on-pr.yml b/.github/workflows/binder-on-pr.yml new file mode 100644 index 0000000..87e9cd2 --- /dev/null +++ b/.github/workflows/binder-on-pr.yml @@ -0,0 +1,14 @@ +name: Binder Badge +on: + pull_request_target: + types: [opened] + +jobs: + binder: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - uses: jupyterlab/maintainer-tools/.github/actions/binder-link@v1 + with: + github_token: ${{ secrets.github_token }} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..0f865c7 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,156 @@ +name: Build + +on: + push: + branches: main + pull_request: + branches: '*' + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Base Setup + uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + + - name: Install dependencies + run: python -m pip install -U "jupyterlab>=4.0.0,<5" + + - name: Lint the extension + run: | + set -eux + jlpm + jlpm run lint:check + + - name: Test the extension + run: | + set -eux + jlpm run test + + - name: Build the extension + run: | + set -eux + python -m pip install .[test] + + pytest -vv -r ap --cov grader_labextension + jupyter server extension list + jupyter server extension list 2>&1 | grep -ie "grader_labextension.*OK" + + jupyter labextension list + jupyter labextension list 2>&1 | grep -ie "grader-labextension.*OK" + python -m jupyterlab.browser_check + + - name: Package the extension + run: | + set -eux + + pip install build + python -m build + pip uninstall -y "grader_labextension" jupyterlab + + - name: Upload extension packages + uses: actions/upload-artifact@v3 + with: + name: extension-artifacts + path: dist/grader_labextension* + if-no-files-found: error + + test_isolated: + needs: build + runs-on: ubuntu-latest + + steps: + - name: Install Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + architecture: 'x64' + - uses: actions/download-artifact@v3 + with: + name: extension-artifacts + - name: Install and Test + run: | + set -eux + # Remove NodeJS, twice to take care of system and locally installed node versions. + sudo rm -rf $(which node) + sudo rm -rf $(which node) + + pip install "jupyterlab>=4.0.0,<5" grader_labextension*.whl + + + jupyter server extension list + jupyter server extension list 2>&1 | grep -ie "grader_labextension.*OK" + + jupyter labextension list + jupyter labextension list 2>&1 | grep -ie "grader-labextension.*OK" + python -m jupyterlab.browser_check --no-browser-test + + integration-tests: + name: Integration tests + needs: build + runs-on: ubuntu-latest + + env: + PLAYWRIGHT_BROWSERS_PATH: ${{ github.workspace }}/pw-browsers + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Base Setup + uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + + - name: Download extension package + uses: actions/download-artifact@v3 + with: + name: extension-artifacts + + - name: Install the extension + run: | + set -eux + python -m pip install "jupyterlab>=4.0.0,<5" grader_labextension*.whl + + - name: Install dependencies + working-directory: ui-tests + env: + YARN_ENABLE_IMMUTABLE_INSTALLS: 0 + PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1 + run: jlpm install + + - name: Set up browser cache + uses: actions/cache@v3 + with: + path: | + ${{ github.workspace }}/pw-browsers + key: ${{ runner.os }}-${{ hashFiles('ui-tests/yarn.lock') }} + + - name: Install browser + run: jlpm playwright install chromium + working-directory: ui-tests + + - name: Execute integration tests + working-directory: ui-tests + run: | + jlpm playwright test + + - name: Upload Playwright Test report + if: always() + uses: actions/upload-artifact@v3 + with: + name: grader_labextension-playwright-tests + path: | + ui-tests/test-results + ui-tests/playwright-report + + check_links: + name: Check Links + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v3 + - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + - uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1 diff --git a/.github/workflows/check-release.yml b/.github/workflows/check-release.yml new file mode 100644 index 0000000..afb542b --- /dev/null +++ b/.github/workflows/check-release.yml @@ -0,0 +1,26 @@ +name: Check Release +on: + push: + branches: ["main"] + pull_request: + branches: ["*"] + +jobs: + check_release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Base Setup + uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + - name: Check Release + uses: jupyter-server/jupyter_releaser/.github/actions/check-release@v2 + with: + + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Upload Distributions + uses: actions/upload-artifact@v3 + with: + name: grader_labextension-releaser-dist-${{ github.run_number }} + path: .jupyter_releaser_checkout/dist diff --git a/.github/workflows/enforce-label.yml b/.github/workflows/enforce-label.yml new file mode 100644 index 0000000..725feab --- /dev/null +++ b/.github/workflows/enforce-label.yml @@ -0,0 +1,13 @@ +name: Enforce PR label + +on: + pull_request: + types: [labeled, unlabeled, opened, edited, synchronize] +jobs: + enforce-label: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - name: enforce-triage-label + uses: jupyterlab/maintainer-tools/.github/actions/enforce-label@v1 diff --git a/.github/workflows/prep-release.yml b/.github/workflows/prep-release.yml new file mode 100644 index 0000000..6f09281 --- /dev/null +++ b/.github/workflows/prep-release.yml @@ -0,0 +1,41 @@ +name: "Step 1: Prep Release" +on: + workflow_dispatch: + inputs: + version_spec: + description: "New Version Specifier" + default: "next" + required: false + branch: + description: "The branch to target" + required: false + post_version_spec: + description: "Post Version Specifier" + required: false + since: + description: "Use PRs with activity since this date or git reference" + required: false + since_last_stable: + description: "Use PRs with activity since the last stable git tag" + required: false + type: boolean +jobs: + prep_release: + runs-on: ubuntu-latest + steps: + - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + + - name: Prep Release + id: prep-release + uses: jupyter-server/jupyter_releaser/.github/actions/prep-release@v2 + with: + token: ${{ secrets.ADMIN_GITHUB_TOKEN }} + version_spec: ${{ github.event.inputs.version_spec }} + post_version_spec: ${{ github.event.inputs.post_version_spec }} + branch: ${{ github.event.inputs.branch }} + since: ${{ github.event.inputs.since }} + since_last_stable: ${{ github.event.inputs.since_last_stable }} + + - name: "** Next Step **" + run: | + echo "Optional): Review Draft Release: ${{ steps.prep-release.outputs.release_url }}" diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 0000000..cf6d905 --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -0,0 +1,57 @@ +name: "Step 2: Publish Release" +on: + workflow_dispatch: + inputs: + branch: + description: "The target branch" + required: false + release_url: + description: "The URL of the draft GitHub release" + required: false + steps_to_skip: + description: "Comma separated list of steps to skip" + required: false + +jobs: + publish_release: + runs-on: ubuntu-latest + permissions: + # This is useful if you want to use PyPI trusted publisher + # and NPM provenance + id-token: write + steps: + - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + + - name: Populate Release + id: populate-release + uses: jupyter-server/jupyter_releaser/.github/actions/populate-release@v2 + with: + token: ${{ secrets.ADMIN_GITHUB_TOKEN }} + branch: ${{ github.event.inputs.branch }} + release_url: ${{ github.event.inputs.release_url }} + steps_to_skip: ${{ github.event.inputs.steps_to_skip }} + + - name: Finalize Release + id: finalize-release + env: + # The following are needed if you use legacy PyPI set up + # PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} + # PYPI_TOKEN_MAP: ${{ secrets.PYPI_TOKEN_MAP }} + # TWINE_USERNAME: __token__ + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + uses: jupyter-server/jupyter_releaser/.github/actions/finalize-release@v2 + with: + token: ${{ secrets.ADMIN_GITHUB_TOKEN }} + release_url: ${{ steps.populate-release.outputs.release_url }} + + - name: "** Next Step **" + if: ${{ success() }} + run: | + echo "Verify the final release" + echo ${{ steps.finalize-release.outputs.release_url }} + + - name: "** Failure Message **" + if: ${{ failure() }} + run: | + echo "Failed to Publish the Draft Release Url:" + echo ${{ steps.populate-release.outputs.release_url }} diff --git a/.github/workflows/update-integration-tests.yml b/.github/workflows/update-integration-tests.yml new file mode 100644 index 0000000..7ea5a66 --- /dev/null +++ b/.github/workflows/update-integration-tests.yml @@ -0,0 +1,50 @@ +name: Update Playwright Snapshots + +on: + issue_comment: + types: [created, edited] + +permissions: + contents: write + pull-requests: write + +jobs: + update-snapshots: + if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'please update snapshots') }} + runs-on: ubuntu-latest + + steps: + - name: React to the triggering comment + run: | + gh api repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions --raw-field 'content=+1' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Checkout the branch from the PR that triggered the job + run: gh pr checkout ${{ github.event.issue.number }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Base Setup + uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 + + - name: Install dependencies + run: python -m pip install -U "jupyterlab>=4.0.0,<5" + + - name: Install extension + run: | + set -eux + jlpm + python -m pip install . + + - uses: jupyterlab/maintainer-tools/.github/actions/update-snapshots@v1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + # Playwright knows how to start JupyterLab server + start_server_script: 'null' + test_folder: ui-tests diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b14ee2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,125 @@ +*.bundle.* +lib/ +node_modules/ +*.log +.eslintcache +.stylelintcache +*.egg-info/ +.ipynb_checkpoints +*.tsbuildinfo +grader_labextension/labextension +# Version file is handled by hatchling +grader_labextension/_version.py + +# Integration tests +ui-tests/test-results/ +ui-tests/playwright-report/ + +# Created by https://www.gitignore.io/api/python +# Edit at https://www.gitignore.io/?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage/ +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# End of https://www.gitignore.io/api/python + +# OSX files +.DS_Store + +# Yarn cache +.yarn/ diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..28cefe9 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,6 @@ +node_modules +**/node_modules +**/lib +**/package.json +!/package.json +grader_labextension diff --git a/.yarnrc.yml b/.yarnrc.yml new file mode 100644 index 0000000..3186f3f --- /dev/null +++ b/.yarnrc.yml @@ -0,0 +1 @@ +nodeLinker: node-modules diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2d352af --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a699b43 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2024, Florian Jaeger +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..92248d2 --- /dev/null +++ b/README.md @@ -0,0 +1,138 @@ +# grader_labextension + +[![Github Actions Status](https://github.com/TU-Wien-dataLAB/Grader-Labextension/workflows/Build/badge.svg)](https://github.com/TU-Wien-dataLAB/Grader-Labextension/actions/workflows/build.yml)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/TU-Wien-dataLAB/Grader-Labextension/main?urlpath=lab) +Grader Labextension is a JupyterLab extension to enable automatic grading of assignment notebooks. + +This extension is composed of a Python package named `grader_labextension` +for the server extension and a NPM package named `grader-labextension` +for the frontend extension. + +## Requirements + +- JupyterLab >= 4.0.0 + +## Install + +To install the extension, execute: + +```bash +pip install grader_labextension +``` + +## Uninstall + +To remove the extension, execute: + +```bash +pip uninstall grader_labextension +``` + +## Troubleshoot + +If you are seeing the frontend extension, but it is not working, check +that the server extension is enabled: + +```bash +jupyter server extension list +``` + +If the server extension is installed and enabled, but you are not seeing +the frontend extension, check the frontend extension is installed: + +```bash +jupyter labextension list +``` + +## Contributing + +### Development install + +Note: You will need NodeJS to build the extension package. + +The `jlpm` command is JupyterLab's pinned version of +[yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use +`yarn` or `npm` in lieu of `jlpm` below. + +```bash +# Clone the repo to your local environment +# Change directory to the grader_labextension directory +# Install package in development mode +pip install -e ".[test]" +# Link your development version of the extension with JupyterLab +jupyter labextension develop . --overwrite +# Server extension must be manually installed in develop mode +jupyter server extension enable grader_labextension +# Rebuild extension Typescript source after making changes +jlpm build +``` + +You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension. + +```bash +# Watch the source directory in one terminal, automatically rebuilding when needed +jlpm watch +# Run JupyterLab in another terminal +jupyter lab +``` + +With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt). + +By default, the `jlpm build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command: + +```bash +jupyter lab build --minimize=False +``` + +### Development uninstall + +```bash +# Server extension must be manually disabled in develop mode +jupyter server extension disable grader_labextension +pip uninstall grader_labextension +``` + +In development mode, you will also need to remove the symlink created by `jupyter labextension develop` +command. To find its location, you can run `jupyter labextension list` to figure out where the `labextensions` +folder is located. Then you can remove the symlink named `grader-labextension` within that folder. + +### Testing the extension + +#### Server tests + +This extension is using [Pytest](https://docs.pytest.org/) for Python code testing. + +Install test dependencies (needed only once): + +```sh +pip install -e ".[test]" +# Each time you install the Python package, you need to restore the front-end extension link +jupyter labextension develop . --overwrite +``` + +To execute them, run: + +```sh +pytest -vv -r ap --cov grader_labextension +``` + +#### Frontend tests + +This extension is using [Jest](https://jestjs.io/) for JavaScript code testing. + +To execute them, execute: + +```sh +jlpm +jlpm test +``` + +#### Integration tests + +This extension uses [Playwright](https://playwright.dev/docs/intro) for the integration tests (aka user level tests). +More precisely, the JupyterLab helper [Galata](https://github.com/jupyterlab/jupyterlab/tree/master/galata) is used to handle testing the extension in JupyterLab. + +More information are provided within the [ui-tests](./ui-tests/README.md) README. + +### Packaging the extension + +See [RELEASE](RELEASE.md) diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..e7238e9 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,111 @@ +# Making a new release of grader_labextension + +The extension can be published to `PyPI` and `npm` manually or using the [Jupyter Releaser](https://github.com/jupyter-server/jupyter_releaser). + +## Manual release + +### Python package + +This extension can be distributed as Python packages. All of the Python +packaging instructions are in the `pyproject.toml` file to wrap your extension in a +Python package. Before generating a package, you first need to install some tools: + +```bash +pip install build twine hatch +``` + +Bump the version using `hatch`. By default this will create a tag. +See the docs on [hatch-nodejs-version](https://github.com/agoose77/hatch-nodejs-version#semver) for details. + +```bash +hatch version +``` + +Make sure to clean up all the development files before building the package: + +```bash +jlpm clean:all +``` + +You could also clean up the local git repository: + +```bash +git clean -dfX +``` + +To create a Python source package (`.tar.gz`) and the binary package (`.whl`) in the `dist/` directory, do: + +```bash +python -m build +``` + +> `python setup.py sdist bdist_wheel` is deprecated and will not work for this package. + +Then to upload the package to PyPI, do: + +```bash +twine upload dist/* +``` + +### NPM package + +To publish the frontend part of the extension as a NPM package, do: + +```bash +npm login +npm publish --access public +``` + +## Automated releases with the Jupyter Releaser + +The extension repository should already be compatible with the Jupyter Releaser. + +Check out the [workflow documentation](https://jupyter-releaser.readthedocs.io/en/latest/get_started/making_release_from_repo.html) for more information. + +Here is a summary of the steps to cut a new release: + +- Add tokens to the [Github Secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) in the repository: + - `ADMIN_GITHUB_TOKEN` (with "public_repo" and "repo:status" permissions); see the [documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) + - `NPM_TOKEN` (with "automation" permission); see the [documentation](https://docs.npmjs.com/creating-and-viewing-access-tokens) +- Set up PyPI + +
Using PyPI trusted publisher (modern way) + +- Set up your PyPI project by [adding a trusted publisher](https://docs.pypi.org/trusted-publishers/adding-a-publisher/) + - The _workflow name_ is `publish-release.yml` and the _environment_ should be left blank. +- Ensure the publish release job as `permissions`: `id-token : write` (see the [documentation](https://docs.pypi.org/trusted-publishers/using-a-publisher/)) + +
+ +
Using PyPI token (legacy way) + +- If the repo generates PyPI release(s), create a scoped PyPI [token](https://packaging.python.org/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/#saving-credentials-on-github). We recommend using a scoped token for security reasons. + +- You can store the token as `PYPI_TOKEN` in your fork's `Secrets`. + + - Advanced usage: if you are releasing multiple repos, you can create a secret named `PYPI_TOKEN_MAP` instead of `PYPI_TOKEN` that is formatted as follows: + + ```text + owner1/repo1,token1 + owner2/repo2,token2 + ``` + + If you have multiple Python packages in the same repository, you can point to them as follows: + + ```text + owner1/repo1/path/to/package1,token1 + owner1/repo1/path/to/package2,token2 + ``` + +
+ +- Go to the Actions panel +- Run the "Step 1: Prep Release" workflow +- Check the draft changelog +- Run the "Step 2: Publish Release" workflow + +## Publishing to `conda-forge` + +If the package is not on conda forge yet, check the documentation to learn how to add it: https://conda-forge.org/docs/maintainer/adding_pkgs.html + +Otherwise a bot should pick up the new version publish to PyPI, and open a new PR on the feedstock repository automatically. diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..8b5c764 --- /dev/null +++ b/babel.config.js @@ -0,0 +1 @@ +module.exports = require('@jupyterlab/testutils/lib/babel.config'); diff --git a/binder/environment.yml b/binder/environment.yml new file mode 100644 index 0000000..bc7a901 --- /dev/null +++ b/binder/environment.yml @@ -0,0 +1,21 @@ +# a mybinder.org-ready environment for demoing grader_labextension +# this environment may also be used locally on Linux/MacOS/Windows, e.g. +# +# conda env update --file binder/environment.yml +# conda activate grader-labextension-demo +# +name: grader-labextension-demo + +channels: + - conda-forge + +dependencies: + # runtime dependencies + - python >=3.10,<3.11.0a0 + - jupyterlab >=4.0.0,<5 + # labextension build dependencies + - nodejs >=18,<19 + - pip + - wheel + # additional packages for demos + # - ipywidgets diff --git a/binder/postBuild b/binder/postBuild new file mode 100755 index 0000000..fef7ff2 --- /dev/null +++ b/binder/postBuild @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +""" perform a development install of grader_labextension + + On Binder, this will run _after_ the environment has been fully created from + the environment.yml in this directory. + + This script should also run locally on Linux/MacOS/Windows: + + python3 binder/postBuild +""" +import subprocess +import sys +from pathlib import Path + + +ROOT = Path.cwd() + +def _(*args, **kwargs): + """ Run a command, echoing the args + + fails hard if something goes wrong + """ + print("\n\t", " ".join(args), "\n") + return_code = subprocess.call(args, **kwargs) + if return_code != 0: + print("\nERROR", return_code, " ".join(args)) + sys.exit(return_code) + +# verify the environment is self-consistent before even starting +_(sys.executable, "-m", "pip", "check") + +# install the labextension +_(sys.executable, "-m", "pip", "install", "-e", ".") +_(sys.executable, "-m", "jupyter", "labextension", "develop", "--overwrite", ".") +_( + sys.executable, + "-m", + "jupyter", + "server", + "extension", + "enable", + "grader_labextension", +) + +# verify the environment the extension didn't break anything +_(sys.executable, "-m", "pip", "check") + +# list the extensions +_("jupyter", "server", "extension", "list") + +# initially list installed extensions to determine if there are any surprises +_("jupyter", "labextension", "list") + + +print("JupyterLab with grader_labextension is ready to run with:\n") +print("\tjupyter lab\n") diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..63e4c54 --- /dev/null +++ b/conftest.py @@ -0,0 +1,8 @@ +import pytest + +pytest_plugins = ("pytest_jupyter.jupyter_server", ) + + +@pytest.fixture +def jp_server_config(jp_server_config): + return {"ServerApp": {"jpserver_extensions": {"grader_labextension": True}}} diff --git a/grader_labextension/__init__.py b/grader_labextension/__init__.py new file mode 100644 index 0000000..d038806 --- /dev/null +++ b/grader_labextension/__init__.py @@ -0,0 +1,36 @@ +try: + from ._version import __version__ +except ImportError: + # Fallback when using the package in dev mode without installing + # in editable mode with pip. It is highly recommended to install + # the package from a stable release or in editable mode: https://pip.pypa.io/en/stable/topics/local-project-installs/#editable-installs + import warnings + warnings.warn("Importing 'grader_labextension' outside a proper installation.") + __version__ = "dev" +from .handlers import setup_handlers + + +def _jupyter_labextension_paths(): + return [{ + "src": "labextension", + "dest": "grader-labextension" + }] + + +def _jupyter_server_extension_points(): + return [{ + "module": "grader_labextension" + }] + + +def _load_jupyter_server_extension(server_app): + """Registers the API handler to receive HTTP requests from the frontend extension. + + Parameters + ---------- + server_app: jupyterlab.labapp.LabApp + JupyterLab application instance + """ + setup_handlers(server_app.web_app) + name = "grader_labextension" + server_app.log.info(f"Registered {name} server extension") diff --git a/grader_labextension/handlers.py b/grader_labextension/handlers.py new file mode 100644 index 0000000..ceb2c06 --- /dev/null +++ b/grader_labextension/handlers.py @@ -0,0 +1,24 @@ +import json + +from jupyter_server.base.handlers import APIHandler +from jupyter_server.utils import url_path_join +import tornado + +class RouteHandler(APIHandler): + # The following decorator should be present on all verb methods (head, get, post, + # patch, put, delete, options) to ensure only authorized user can request the + # Jupyter server + @tornado.web.authenticated + def get(self): + self.finish(json.dumps({ + "data": "This is /grader-labextension/get-example endpoint!" + })) + + +def setup_handlers(web_app): + host_pattern = ".*$" + + base_url = web_app.settings["base_url"] + route_pattern = url_path_join(base_url, "grader-labextension", "get-example") + handlers = [(route_pattern, RouteHandler)] + web_app.add_handlers(host_pattern, handlers) diff --git a/grader_labextension/tests/__init__.py b/grader_labextension/tests/__init__.py new file mode 100644 index 0000000..d10f976 --- /dev/null +++ b/grader_labextension/tests/__init__.py @@ -0,0 +1 @@ +"""Python unit tests for grader_labextension.""" diff --git a/grader_labextension/tests/test_handlers.py b/grader_labextension/tests/test_handlers.py new file mode 100644 index 0000000..c976385 --- /dev/null +++ b/grader_labextension/tests/test_handlers.py @@ -0,0 +1,13 @@ +import json + + +async def test_get_example(jp_fetch): + # When + response = await jp_fetch("grader-labextension", "get-example") + + # Then + assert response.code == 200 + payload = json.loads(response.body) + assert payload == { + "data": "This is /grader-labextension/get-example endpoint!" + } \ No newline at end of file diff --git a/install.json b/install.json new file mode 100644 index 0000000..eee5636 --- /dev/null +++ b/install.json @@ -0,0 +1,5 @@ +{ + "packageManager": "python", + "packageName": "grader_labextension", + "uninstallInstructions": "Use your Python package manager (pip, conda, etc.) to uninstall the package grader_labextension" +} diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..b0471e6 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,28 @@ +const jestJupyterLab = require('@jupyterlab/testutils/lib/jest-config'); + +const esModules = [ + '@codemirror', + '@jupyter/ydoc', + '@jupyterlab/', + 'lib0', + 'nanoid', + 'vscode-ws-jsonrpc', + 'y-protocols', + 'y-websocket', + 'yjs' +].join('|'); + +const baseConfig = jestJupyterLab(__dirname); + +module.exports = { + ...baseConfig, + automock: false, + collectCoverageFrom: [ + 'src/**/*.{ts,tsx}', + '!src/**/*.d.ts', + '!src/**/.ipynb_checkpoints/*' + ], + coverageReporters: ['lcov', 'text'], + testRegex: 'src/.*/.*.spec.ts[x]?$', + transformIgnorePatterns: [`/node_modules/(?!${esModules}).+`] +}; diff --git a/jupyter-config/server-config/grader_labextension.json b/jupyter-config/server-config/grader_labextension.json new file mode 100644 index 0000000..afd2044 --- /dev/null +++ b/jupyter-config/server-config/grader_labextension.json @@ -0,0 +1,7 @@ +{ + "ServerApp": { + "jpserver_extensions": { + "grader_labextension": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..12d486a --- /dev/null +++ b/package.json @@ -0,0 +1,209 @@ +{ + "name": "grader-labextension", + "version": "0.1.0", + "description": "Grader Labextension is a JupyterLab extension to enable automatic grading of assignment notebooks.", + "keywords": [ + "jupyter", + "jupyterlab", + "jupyterlab-extension" + ], + "homepage": "https://github.com/TU-Wien-dataLAB/Grader-Labextension", + "bugs": { + "url": "https://github.com/TU-Wien-dataLAB/Grader-Labextension/issues" + }, + "license": "BSD-3-Clause", + "author": { + "name": "Florian Jaeger", + "email": "datalab@tuwien.ac.at" + }, + "files": [ + "lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}", + "style/**/*.{css,js,eot,gif,html,jpg,json,png,svg,woff2,ttf}", + "schema/*.json" + ], + "main": "lib/index.js", + "types": "lib/index.d.ts", + "style": "style/index.css", + "repository": { + "type": "git", + "url": "https://github.com/TU-Wien-dataLAB/Grader-Labextension.git" + }, + "scripts": { + "build": "jlpm build:lib && jlpm build:labextension:dev", + "build:prod": "jlpm clean && jlpm build:lib:prod && jlpm build:labextension", + "build:labextension": "jupyter labextension build .", + "build:labextension:dev": "jupyter labextension build --development True .", + "build:lib": "tsc --sourceMap", + "build:lib:prod": "tsc", + "clean": "jlpm clean:lib", + "clean:lib": "rimraf lib tsconfig.tsbuildinfo", + "clean:lintcache": "rimraf .eslintcache .stylelintcache", + "clean:labextension": "rimraf grader_labextension/labextension grader_labextension/_version.py", + "clean:all": "jlpm clean:lib && jlpm clean:labextension && jlpm clean:lintcache", + "eslint": "jlpm eslint:check --fix", + "eslint:check": "eslint . --cache --ext .ts,.tsx", + "install:extension": "jlpm build", + "lint": "jlpm stylelint && jlpm prettier && jlpm eslint", + "lint:check": "jlpm stylelint:check && jlpm prettier:check && jlpm eslint:check", + "prettier": "jlpm prettier:base --write --list-different", + "prettier:base": "prettier \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\"", + "prettier:check": "jlpm prettier:base --check", + "stylelint": "jlpm stylelint:check --fix", + "stylelint:check": "stylelint --cache \"style/**/*.css\"", + "test": "jest --coverage", + "watch": "run-p watch:src watch:labextension", + "watch:src": "tsc -w --sourceMap", + "watch:labextension": "jupyter labextension watch ." + }, + "dependencies": { + "@jupyterlab/application": "^4.0.0", + "@jupyterlab/settingregistry": "^4.0.0", + "@jupyterlab/coreutils": "^6.0.0", + "@jupyterlab/services": "^7.0.0" + }, + "devDependencies": { + "@jupyterlab/builder": "^4.0.0", + "@jupyterlab/testutils": "^4.0.0", + "@types/jest": "^29.2.0", + "@types/json-schema": "^7.0.11", + "@types/react": "^18.0.26", + "@types/react-addons-linked-state-mixin": "^0.14.22", + "@typescript-eslint/eslint-plugin": "^6.1.0", + "@typescript-eslint/parser": "^6.1.0", + "css-loader": "^6.7.1", + "eslint": "^8.36.0", + "eslint-config-prettier": "^8.8.0", + "eslint-plugin-prettier": "^5.0.0", + "jest": "^29.2.0", + "mkdirp": "^1.0.3", + "npm-run-all": "^4.1.5", + "prettier": "^3.0.0", + "rimraf": "^5.0.1", + "source-map-loader": "^1.0.2", + "style-loader": "^3.3.1", + "stylelint": "^15.10.1", + "stylelint-config-recommended": "^13.0.0", + "stylelint-config-standard": "^34.0.0", + "stylelint-csstree-validator": "^3.0.0", + "stylelint-prettier": "^4.0.0", + "typescript": "~5.0.2", + "yjs": "^13.5.0" + }, + "sideEffects": [ + "style/*.css", + "style/index.js" + ], + "styleModule": "style/index.js", + "publishConfig": { + "access": "public" + }, + "jupyterlab": { + "discovery": { + "server": { + "managers": [ + "pip" + ], + "base": { + "name": "grader_labextension" + } + } + }, + "extension": true, + "outputDir": "grader_labextension/labextension", + "schemaDir": "schema" + }, + "eslintIgnore": [ + "node_modules", + "dist", + "coverage", + "**/*.d.ts", + "tests", + "**/__tests__", + "ui-tests" + ], + "eslintConfig": { + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended", + "plugin:prettier/recommended" + ], + "parser": "@typescript-eslint/parser", + "parserOptions": { + "project": "tsconfig.json", + "sourceType": "module" + }, + "plugins": [ + "@typescript-eslint" + ], + "rules": { + "@typescript-eslint/naming-convention": [ + "error", + { + "selector": "interface", + "format": [ + "PascalCase" + ], + "custom": { + "regex": "^I[A-Z]", + "match": true + } + } + ], + "@typescript-eslint/no-unused-vars": [ + "warn", + { + "args": "none" + } + ], + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-namespace": "off", + "@typescript-eslint/no-use-before-define": "off", + "@typescript-eslint/quotes": [ + "error", + "single", + { + "avoidEscape": true, + "allowTemplateLiterals": false + } + ], + "curly": [ + "error", + "all" + ], + "eqeqeq": "error", + "prefer-arrow-callback": "error" + } + }, + "prettier": { + "singleQuote": true, + "trailingComma": "none", + "arrowParens": "avoid", + "endOfLine": "auto", + "overrides": [ + { + "files": "package.json", + "options": { + "tabWidth": 4 + } + } + ] + }, + "stylelint": { + "extends": [ + "stylelint-config-recommended", + "stylelint-config-standard", + "stylelint-prettier/recommended" + ], + "plugins": [ + "stylelint-csstree-validator" + ], + "rules": { + "csstree/validator": true, + "property-no-vendor-prefix": null, + "selector-class-pattern": "^([a-z][A-z\\d]*)(-[A-z\\d]+)*$", + "selector-no-vendor-prefix": null, + "value-no-vendor-prefix": null + } + } +} diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..7540ecb --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,88 @@ +[build-system] +requires = ["hatchling>=1.5.0", "jupyterlab>=4.0.0,<5", "hatch-nodejs-version>=0.3.2"] +build-backend = "hatchling.build" + +[project] +name = "grader_labextension" +readme = "README.md" +license = { file = "LICENSE" } +requires-python = ">=3.8" +classifiers = [ + "Framework :: Jupyter", + "Framework :: Jupyter :: JupyterLab", + "Framework :: Jupyter :: JupyterLab :: 4", + "Framework :: Jupyter :: JupyterLab :: Extensions", + "Framework :: Jupyter :: JupyterLab :: Extensions :: Prebuilt", + "License :: OSI Approved :: BSD License", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", +] +dependencies = [ + "jupyter_server>=2.0.1,<3" +] +dynamic = ["version", "description", "authors", "urls", "keywords"] + +[project.optional-dependencies] +test = [ + "coverage", + "pytest", + "pytest-asyncio", + "pytest-cov", + "pytest-jupyter[server]>=0.6.0" +] + +[tool.hatch.version] +source = "nodejs" + +[tool.hatch.metadata.hooks.nodejs] +fields = ["description", "authors", "urls"] + +[tool.hatch.build.targets.sdist] +artifacts = ["grader_labextension/labextension"] +exclude = [".github", "binder"] + +[tool.hatch.build.targets.wheel.shared-data] +"grader_labextension/labextension" = "share/jupyter/labextensions/grader-labextension" +"install.json" = "share/jupyter/labextensions/grader-labextension/install.json" +"jupyter-config/server-config" = "etc/jupyter/jupyter_server_config.d" + +[tool.hatch.build.hooks.version] +path = "grader_labextension/_version.py" + +[tool.hatch.build.hooks.jupyter-builder] +dependencies = ["hatch-jupyter-builder>=0.5"] +build-function = "hatch_jupyter_builder.npm_builder" +ensured-targets = [ + "grader_labextension/labextension/static/style.js", + "grader_labextension/labextension/package.json", +] +skip-if-exists = ["grader_labextension/labextension/static/style.js"] + +[tool.hatch.build.hooks.jupyter-builder.build-kwargs] +build_cmd = "build:prod" +npm = ["jlpm"] + +[tool.hatch.build.hooks.jupyter-builder.editable-build-kwargs] +build_cmd = "install:extension" +npm = ["jlpm"] +source_dir = "src" +build_dir = "grader_labextension/labextension" + +[tool.jupyter-releaser.options] +version_cmd = "hatch version" + +[tool.jupyter-releaser.hooks] +before-build-npm = [ + "python -m pip install 'jupyterlab>=4.0.0,<5'", + "jlpm", + "jlpm build:prod" +] +before-build-python = ["jlpm clean:all"] + +[tool.check-wheel-contents] +ignore = ["W002"] diff --git a/schema/plugin.json b/schema/plugin.json new file mode 100644 index 0000000..3f7867a --- /dev/null +++ b/schema/plugin.json @@ -0,0 +1,8 @@ +{ + "jupyter.lab.shortcuts": [], + "title": "grader-labextension", + "description": "grader-labextension settings.", + "type": "object", + "properties": {}, + "additionalProperties": false +} diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..aefdf20 --- /dev/null +++ b/setup.py @@ -0,0 +1 @@ +__import__("setuptools").setup() diff --git a/src/__tests__/grader_labextension.spec.ts b/src/__tests__/grader_labextension.spec.ts new file mode 100644 index 0000000..e42e54e --- /dev/null +++ b/src/__tests__/grader_labextension.spec.ts @@ -0,0 +1,9 @@ +/** + * Example of [Jest](https://jestjs.io/docs/getting-started) unit tests + */ + +describe('grader-labextension', () => { + it('should be tested', () => { + expect(1 + 1).toEqual(2); + }); +}); diff --git a/src/handler.ts b/src/handler.ts new file mode 100644 index 0000000..de17ee9 --- /dev/null +++ b/src/handler.ts @@ -0,0 +1,46 @@ +import { URLExt } from '@jupyterlab/coreutils'; + +import { ServerConnection } from '@jupyterlab/services'; + +/** + * Call the API extension + * + * @param endPoint API REST end point for the extension + * @param init Initial values for the request + * @returns The response body interpreted as JSON + */ +export async function requestAPI( + endPoint = '', + init: RequestInit = {} +): Promise { + // Make request to Jupyter API + const settings = ServerConnection.makeSettings(); + const requestUrl = URLExt.join( + settings.baseUrl, + 'grader-labextension', // API Namespace + endPoint + ); + + let response: Response; + try { + response = await ServerConnection.makeRequest(requestUrl, init, settings); + } catch (error) { + throw new ServerConnection.NetworkError(error as any); + } + + let data: any = await response.text(); + + if (data.length > 0) { + try { + data = JSON.parse(data); + } catch (error) { + console.log('Not a JSON response body.', response); + } + } + + if (!response.ok) { + throw new ServerConnection.ResponseError(response, data.message || data); + } + + return data; +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..7fe361f --- /dev/null +++ b/src/index.ts @@ -0,0 +1,44 @@ +import { + JupyterFrontEnd, + JupyterFrontEndPlugin +} from '@jupyterlab/application'; + +import { ISettingRegistry } from '@jupyterlab/settingregistry'; + +import { requestAPI } from './handler'; + +/** + * Initialization data for the grader-labextension extension. + */ +const plugin: JupyterFrontEndPlugin = { + id: 'grader-labextension:plugin', + description: 'Grader Labextension is a JupyterLab extension to enable automatic grading of assignment notebooks.', + autoStart: true, + optional: [ISettingRegistry], + activate: (app: JupyterFrontEnd, settingRegistry: ISettingRegistry | null) => { + console.log('JupyterLab extension grader-labextension is activated!'); + + if (settingRegistry) { + settingRegistry + .load(plugin.id) + .then(settings => { + console.log('grader-labextension settings loaded:', settings.composite); + }) + .catch(reason => { + console.error('Failed to load settings for grader-labextension.', reason); + }); + } + + requestAPI('get-example') + .then(data => { + console.log(data); + }) + .catch(reason => { + console.error( + `The grader_labextension server extension appears to be missing.\n${reason}` + ); + }); + } +}; + +export default plugin; diff --git a/style/base.css b/style/base.css new file mode 100644 index 0000000..e11f457 --- /dev/null +++ b/style/base.css @@ -0,0 +1,5 @@ +/* + See the JupyterLab Developer Guide for useful CSS Patterns: + + https://jupyterlab.readthedocs.io/en/stable/developer/css.html +*/ diff --git a/style/index.css b/style/index.css new file mode 100644 index 0000000..8a7ea29 --- /dev/null +++ b/style/index.css @@ -0,0 +1 @@ +@import url('base.css'); diff --git a/style/index.js b/style/index.js new file mode 100644 index 0000000..a028a76 --- /dev/null +++ b/style/index.js @@ -0,0 +1 @@ +import './base.css'; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..9897917 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "composite": true, + "declaration": true, + "esModuleInterop": true, + "incremental": true, + "jsx": "react", + "module": "esnext", + "moduleResolution": "node", + "noEmitOnError": true, + "noImplicitAny": true, + "noUnusedLocals": true, + "preserveWatchOutput": true, + "resolveJsonModule": true, + "outDir": "lib", + "rootDir": "src", + "strict": true, + "strictNullChecks": true, + "target": "ES2018" + }, + "include": ["src/*"] +} diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000..1de37fd --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "types": ["jest"] + } +} diff --git a/ui-tests/README.md b/ui-tests/README.md new file mode 100644 index 0000000..8d377fc --- /dev/null +++ b/ui-tests/README.md @@ -0,0 +1,167 @@ +# Integration Testing + +This folder contains the integration tests of the extension. + +They are defined using [Playwright](https://playwright.dev/docs/intro) test runner +and [Galata](https://github.com/jupyterlab/jupyterlab/tree/main/galata) helper. + +The Playwright configuration is defined in [playwright.config.js](./playwright.config.js). + +The JupyterLab server configuration to use for the integration test is defined +in [jupyter_server_test_config.py](./jupyter_server_test_config.py). + +The default configuration will produce video for failing tests and an HTML report. + +> There is a new experimental UI mode that you may fall in love with; see [that video](https://www.youtube.com/watch?v=jF0yA-JLQW0). + +## Run the tests + +> All commands are assumed to be executed from the root directory + +To run the tests, you need to: + +1. Compile the extension: + +```sh +jlpm install +jlpm build:prod +``` + +> Check the extension is installed in JupyterLab. + +2. Install test dependencies (needed only once): + +```sh +cd ./ui-tests +jlpm install +jlpm playwright install +cd .. +``` + +3. Execute the [Playwright](https://playwright.dev/docs/intro) tests: + +```sh +cd ./ui-tests +jlpm playwright test +``` + +Test results will be shown in the terminal. In case of any test failures, the test report +will be opened in your browser at the end of the tests execution; see +[Playwright documentation](https://playwright.dev/docs/test-reporters#html-reporter) +for configuring that behavior. + +## Update the tests snapshots + +> All commands are assumed to be executed from the root directory + +If you are comparing snapshots to validate your tests, you may need to update +the reference snapshots stored in the repository. To do that, you need to: + +1. Compile the extension: + +```sh +jlpm install +jlpm build:prod +``` + +> Check the extension is installed in JupyterLab. + +2. Install test dependencies (needed only once): + +```sh +cd ./ui-tests +jlpm install +jlpm playwright install +cd .. +``` + +3. Execute the [Playwright](https://playwright.dev/docs/intro) command: + +```sh +cd ./ui-tests +jlpm playwright test -u +``` + +> Some discrepancy may occurs between the snapshots generated on your computer and +> the one generated on the CI. To ease updating the snapshots on a PR, you can +> type `please update playwright snapshots` to trigger the update by a bot on the CI. +> Once the bot has computed new snapshots, it will commit them to the PR branch. + +## Create tests + +> All commands are assumed to be executed from the root directory + +To create tests, the easiest way is to use the code generator tool of playwright: + +1. Compile the extension: + +```sh +jlpm install +jlpm build:prod +``` + +> Check the extension is installed in JupyterLab. + +2. Install test dependencies (needed only once): + +```sh +cd ./ui-tests +jlpm install +jlpm playwright install +cd .. +``` + +3. Start the server: + +```sh +cd ./ui-tests +jlpm start +``` + +4. Execute the [Playwright code generator](https://playwright.dev/docs/codegen) in **another terminal**: + +```sh +cd ./ui-tests +jlpm playwright codegen localhost:8888 +``` + +## Debug tests + +> All commands are assumed to be executed from the root directory + +To debug tests, a good way is to use the inspector tool of playwright: + +1. Compile the extension: + +```sh +jlpm install +jlpm build:prod +``` + +> Check the extension is installed in JupyterLab. + +2. Install test dependencies (needed only once): + +```sh +cd ./ui-tests +jlpm install +jlpm playwright install +cd .. +``` + +3. Execute the Playwright tests in [debug mode](https://playwright.dev/docs/debug): + +```sh +cd ./ui-tests +jlpm playwright test --debug +``` + +## Upgrade Playwright and the browsers + +To update the web browser versions, you must update the package `@playwright/test`: + +```sh +cd ./ui-tests +jlpm up "@playwright/test" +jlpm playwright install +``` diff --git a/ui-tests/jupyter_server_test_config.py b/ui-tests/jupyter_server_test_config.py new file mode 100644 index 0000000..f2a9478 --- /dev/null +++ b/ui-tests/jupyter_server_test_config.py @@ -0,0 +1,12 @@ +"""Server configuration for integration tests. + +!! Never use this configuration in production because it +opens the server to the world and provide access to JupyterLab +JavaScript objects through the global window variable. +""" +from jupyterlab.galata import configure_jupyter_server + +configure_jupyter_server(c) + +# Uncomment to set server log level to debug level +# c.ServerApp.log_level = "DEBUG" diff --git a/ui-tests/package.json b/ui-tests/package.json new file mode 100644 index 0000000..e7472d1 --- /dev/null +++ b/ui-tests/package.json @@ -0,0 +1,15 @@ +{ + "name": "grader-labextension-ui-tests", + "version": "1.0.0", + "description": "JupyterLab grader-labextension Integration Tests", + "private": true, + "scripts": { + "start": "jupyter lab --config jupyter_server_test_config.py", + "test": "jlpm playwright test", + "test:update": "jlpm playwright test --update-snapshots" + }, + "devDependencies": { + "@jupyterlab/galata": "^5.0.5", + "@playwright/test": "^1.37.0" + } +} diff --git a/ui-tests/playwright.config.js b/ui-tests/playwright.config.js new file mode 100644 index 0000000..9ece6fa --- /dev/null +++ b/ui-tests/playwright.config.js @@ -0,0 +1,14 @@ +/** + * Configuration for Playwright using default from @jupyterlab/galata + */ +const baseConfig = require('@jupyterlab/galata/lib/playwright-config'); + +module.exports = { + ...baseConfig, + webServer: { + command: 'jlpm start', + url: 'http://localhost:8888/lab', + timeout: 120 * 1000, + reuseExistingServer: !process.env.CI + } +}; diff --git a/ui-tests/tests/grader_labextension.spec.ts b/ui-tests/tests/grader_labextension.spec.ts new file mode 100644 index 0000000..4d2dab2 --- /dev/null +++ b/ui-tests/tests/grader_labextension.spec.ts @@ -0,0 +1,21 @@ +import { expect, test } from '@jupyterlab/galata'; + +/** + * Don't load JupyterLab webpage before running the tests. + * This is required to ensure we capture all log messages. + */ +test.use({ autoGoto: false }); + +test('should emit an activation console message', async ({ page }) => { + const logs: string[] = []; + + page.on('console', message => { + logs.push(message.text()); + }); + + await page.goto(); + + expect( + logs.filter(s => s === 'JupyterLab extension grader-labextension is activated!') + ).toHaveLength(1); +}); diff --git a/ui-tests/yarn.lock b/ui-tests/yarn.lock new file mode 100644 index 0000000..e69de29