name: Tests + Code Analysis

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

jobs:
  analyze:
    name: Analyze Code
    runs-on: ubuntu-latest
    permissions:
      actions: read
      contents: read
      security-events: write

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Initialize CodeQL
      uses: github/codeql-action/init@v3
      with:
        languages: python

    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@v3

  tests-linux:
    name: Unit Tests (Linux)
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    
    strategy:
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
        
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        
    - name: Fonts Cache
      id: cache-fonts-linux
      uses: pat-s/always-upload-cache@v2.1.5
      with:
        path: ~/.fonts/opentype
        key: fonts-linux

    - name: Download Fonts
      if: steps.cache-fonts-linux.outputs.cache-hit != 'true'
      run: |
        echo "Downloading xml2rfc-fonts"
        mkdir -p ~/.fonts/opentype ~/fonts
        wget -q -O fonts.tar.gz https://github.com/ietf-tools/xml2rfc-fonts/archive/refs/tags/3.22.0.tar.gz
        tar zxf fonts.tar.gz -C ~/fonts
        mv ~/fonts/*/noto/* ~/.fonts/opentype/
        mv ~/fonts/*/roboto_mono/* ~/.fonts/opentype/
        mkdir -p /usr/share/fonts/truetype
        ln -sf ~/.fonts/opentype/*.[to]tf /usr/share/fonts/truetype/
        echo "Reloading Font Cache..."
        fc-cache -f -v
        
    - name: Link Fonts
      if: steps.cache-fonts-linux.outputs.cache-hit == 'true'
      run: |
        echo "Linking Fonts..."
        mkdir -p /usr/share/fonts/truetype/
        ln -sf ~/.fonts/opentype/*.[to]tf /usr/share/fonts/truetype/
        echo "Reloading Font Cache..."
        fc-cache -f -v
        
    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -qy groff html2text python3-cffi python3-brotli libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 libxml2-dev libxml2-utils libxslt-dev
        echo "Installing pip + wheel..."
        python -m pip install --upgrade pip wheel
        echo "Installing requirements.txt + test dependencies..."
        python -m pip install -r requirements.txt tox tox-gh-actions certifi decorator dict2xml pyflakes "pypdf>=4.1.0" "weasyprint==61.2" "pydyf<0.10.0"
        
    - name: Generate Valid Tests
      run: |
        make yestests || true
        cp -af tests/out/. tests/valid
        
    - name: Test with tox
      env:
        PLATFORM: ubuntu-latest
      run: tox

  tests-macos:
    name: Unit Tests (macOS)
    runs-on: macos-12
    if: github.event_name == 'pull_request'
    
    strategy:
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
        
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        
    - name: Fonts Cache
      id: cache-fonts-mac
      uses: pat-s/always-upload-cache@v2.1.5
      with:
        path: ~/fonts
        key: fonts-macos

    - name: Download Fonts
      run: |
        echo "Downloading xml2rfc-fonts"
        mkdir -p ~/fonts /Library/Fonts
        wget -q -O fonts.tar.gz https://github.com/ietf-tools/xml2rfc-fonts/archive/refs/tags/3.22.0.tar.gz
        tar zxf fonts.tar.gz -C ~/fonts
        mv ~/fonts/*/noto/* /Library/Fonts/
        mv ~/fonts/*/roboto_mono/* /Library/Fonts/
        
    - name: Install dependencies
      run: |
        brew install pango pillow libffi groff make diffutils
        echo "Installing pip + wheel..."
        python -m pip install --upgrade pip wheel
        echo "Installing requirements.txt + test dependencies..."
        python -m pip install -r requirements.txt tox tox-gh-actions certifi decorator dict2xml pyflakes "pypdf>=4.1.0" "weasyprint==61.2" "pydyf<0.10.0"
        
    - name: Generate Valid Tests
      run: |
        echo "/usr/local/opt/make/libexec/gnubin" >> $GITHUB_PATH
        make yestests || true
        cp -af tests/out/. tests/valid
        
    - name: Test with tox
      env:
        PLATFORM: macos-latest
      run: |
        echo "/usr/local/opt/make/libexec/gnubin" >> $GITHUB_PATH
        tox
      
#   tests-windows:
#     name: Unit Tests (Windows)
#     runs-on: windows-latest
#     if: github.event_name == 'pull_request'
    
#     strategy:
#       matrix:
#         python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
        
#     steps:
#     - name: Checkout repository
#       uses: actions/checkout@v4
      
#     - name: Setup Python ${{ matrix.python-version }}
#       uses: actions/setup-python@v5
#       with:
#         python-version: ${{ matrix.python-version }}
        
#     - name: Install Fonts
#       shell: pwsh
#       working-directory: .github/scripts
#       run: |
#         Invoke-WebRequest -Uri 'https://github.com/ietf-tools/common/raw/main/scripts/font-install/font-install.exe' -OutFile font-install.exe
#         & ./font-install.exe -debug https://fonts.google.com/download?family=Roboto%20Mono
#         & ./font-install.exe -debug https://noto-website-2.storage.googleapis.com/pkgs/Noto-unhinted.zip
        
#     - name: Install dependencies
#       shell: pwsh
#       run: |
#         Invoke-WebRequest -Uri 'https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases/download/2022-01-04/gtk3-runtime-3.24.31-2022-01-04-ts-win64.exe' -OutFile gtk.exe
#         & ./gtk.exe /S
#         choco install make
#         echo "Installing pip + wheel..."
#         python -m pip install --upgrade pip wheel
#         echo "Installing requirements.txt + test dependencies..."
#         python -m pip install -r requirements.txt tox tox-gh-actions certifi decorator dict2xml pyflakes "pypdf>=4.1.0" "weasyprint==61.2" "pydyf<0.10.0"
#     - name: Generate Valid Tests
#       run: |
#         make yestests || true
#         cp tests/out/. tests/valid
        
#     - name: Test with tox
#       env:
#         PLATFORM: windows-latest
#       run: tox
