name: build

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
        jsonschema-version: ["3.0", "4.17"]
    name: py ${{ matrix.python-version }} js ${{ matrix.jsonschema-version }}
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      # - name: Set Up Chromedriver
      #   run: |
      #     sudo apt-get update
      #     sudo apt-get --only-upgrade install google-chrome-stable
      #     sudo apt-get -yqq install chromium-chromedriver
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install jsonschema==${{ matrix.jsonschema-version }}
          pip install .[dev]
          # pip install "selenium<4.3.0"
          # pip install altair_saver
      - name: Test that schema generation has no effect
        run: |
          python tools/generate_schema_wrapper.py
          # This gets the paths of all files which were either deleted, modified
          # or are not yet tracked by Git
          files=`git ls-files --deleted --modified --others --exclude-standard`
          # Depending on the shell it can happen that 'files' contains empty
          # lines which are filtered out in the for loop below
          files_cleaned=()
          for i in "${files[@]}"; do
            # Skip empty items
            if [ -z "$i" ]; then
              continue
            fi
            # Add the rest of the elements to a new array
            files_cleaned+=("${i}")
          done
          if [ ${#files_cleaned[@]} -gt 0 ]; then
              echo "The code generation modified the following files:"
              echo $files
              exit 1
          fi
      - name: Test with pytest
        run: |
          pytest --doctest-modules tests
      # - name: Selected tests without vl-convert-python
      #   run: |
      #     pip uninstall vl-convert-python --yes
      #     pytest -m save_engine --doctest-modules tests
      # - name: Selected tests without vl-convert-python and altair_saver
      #   run: |
      #     pip uninstall altair_saver --yes
      #     pytest -m save_engine --doctest-modules tests
      - name: Selected tests with vl-convert-python and without altair_saver
        run: |
          # pip install vl-convert-python
          pytest -m save_engine --doctest-modules tests
      - name: Validate Vega-Lite schema
        run: |
          # We install all 'format' dependencies of jsonschema as check-jsonschema
          # only does the 'format' checks which are installed.
          # We can always use the latest jsonschema version here.
          # uri-reference check is disabled as the URIs in the Vega-Lite schema do
          # not conform RFC 3986.
          pip install 'jsonschema[format]' check-jsonschema --upgrade
          check-jsonschema --check-metaschema altair/vegalite/v5/schema/vega-lite-schema.json --disable-formats uri-reference
