Documentation
name: Run tests
on: [push, pull_request]

jobs:
  msrv:
    runs-on: ubuntu-24.04
    outputs:
      msrv: ${{ steps.msrv.outputs.msrv }}
      rust_toolchains: ${{ steps.msrv.outputs.rust_toolchains }}
    steps:
      - uses: actions/checkout@v4
      - id: msrv
        name: Get minimum supported rust version in Cargo.toml
        run: |
          MSRV=$(sed -n -e 's/rust-version *= *"\(.*\)"/\1/p' Cargo.toml)
          echo "rust_toolchains=[\"stable\", \"$MSRV\"]" >> $GITHUB_OUTPUT

  tests:
    needs: msrv
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-24.04]
        rust_toolchain: ${{ fromJSON(needs.msrv.outputs.rust_toolchains) }}

    runs-on: ${{ matrix.os }}

    env:
      rust_features: aq_unstable,chrono

    services:
      oracle:
        image: gvenzl/oracle-xe:latest
        env:
          ORACLE_PASSWORD: sys_passwd
        ports:
          - 1521:1521
        options: >-
          --health-cmd healthcheck.sh
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true

      - name: Install the latest Oracle instant client
        run: |
          curl -Lo basic.zip https://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linuxx64.zip
          curl -Lo sqlplus.zip https://download.oracle.com/otn_software/linux/instantclient/instantclient-sqlplus-linuxx64.zip
          mkdir linux
          unzip basic.zip -d linux -x "META-INF/*"
          unzip sqlplus.zip -d linux -x "META-INF/*"
          IC_DIR=$PWD/$(ls -d linux/instantclient*)
          ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64 $IC_DIR/libaio.so.1
          echo LD_LIBRARY_PATH=$IC_DIR:$LD_LIBRARY_PATH >> $GITHUB_ENV
          echo $IC_DIR >> $GITHUB_PATH

      - name: Get the Oracle container IP address
        env:
          ORACLE_SERVICE_ID: ${{ job.services.oracle.id }}
        run: |
          ORACLE_IP_ADDRESS=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' $ORACLE_SERVICE_ID)
          if test -z "$ORACLE_IP_ADDRESS"; then
              echo "Cannot get ORACLE_IP_ADDRESS."
              docker inspect $ORACLE_SERVICE_ID
              exit 1
          fi
          echo TWO_TASK=//$ORACLE_IP_ADDRESS:1521/XEPDB1 >> $GITHUB_ENV
          echo ODPIC_TEST_CONNECT_STRING=//$ORACLE_IP_ADDRESS:1521/XEPDB1 >> $GITHUB_ENV
          echo NLS_LANG=AMERICAN_AMERICA.AL32UTF8 >> $GITHUB_ENV

      - name: Install rust
        run: |
          rustup install ${{ matrix.rust_toolchain }}

      - name: Downgrade dependent crates to use them with oracle crate's MSRV if the rust toolchain isn't stable
        if: matrix.rust_toolchain != 'stable'
        run: |
          cargo update -p chrono --precise 0.4.20
          cargo update -p cc --precise 1.0.94

      - name: Create Oracle users and schema
        run: |
          sqlplus sys/sys_passwd as sysdba @tests/SetupTest.sql < /dev/null

      - name: cargo test (x86_64-unknown-linux-gnu)
        run: |
          rustup run ${{ matrix.rust_toolchain }} cargo test --features ${{ env.rust_features }} -- --nocapture

      - name: cargo test on Wine (x86_64-pc-windows-gnu)
        run: |
          sudo apt-get update -q -y
          sudo apt-get install -y g++-mingw-w64-x86-64 wine binfmt-support wine-binfmt
          sudo update-binfmts --import wine
          curl -Lo basic-windows.zip https://download.oracle.com/otn_software/nt/instantclient/instantclient-basic-windows.zip
          mkdir windows
          unzip basic-windows.zip -d windows -x "META-INF/*"
          export WINEPATH=$PWD/$(ls -d windows/instantclient*)
          rustup target add x86_64-pc-windows-gnu --toolchain ${{ matrix.rust_toolchain }}
          rustup run ${{ matrix.rust_toolchain }} cargo test --target x86_64-pc-windows-gnu --features ${{ env.rust_features }} -- --nocapture