language: python
dist: bionic

addons:
  hosts:
    - test_site
    - test_site_producer
  mariadb: 10.3
  postgresql: 9.5
  chrome: stable

services:
  - xvfb
  - mysql

git:
  depth: 1

cache:
  pip: true
  npm: true
  yarn: true
  directories:
    # we also need to cache folder with Cypress binary
    # https://docs.cypress.io/guides/guides/continuous-integration.html#Caching
    - ~/.cache


matrix:
  include:
  - name: "Python 3.7 MariaDB"
    python: 3.7
    env: DB=mariadb TYPE=server
    script: bench --verbose --site test_site run-tests --coverage

  - name: "Python 3.7 PostgreSQL"
    python: 3.7
    env: DB=postgres TYPE=server
    script: bench --verbose --site test_site run-tests --coverage

  - name: "Cypress"
    python: 3.7
    env: DB=mariadb TYPE=ui
    before_script:
    - bench --site test_site execute frappe.utils.install.complete_setup_wizard
    script: bench --site test_site run-ui-tests frappe --headless

before_install:
 # do we really want to run travis?
  - |
    python ./.travis/roulette.py
    if [[ $? != 2 ]];then
      exit;
    fi

 # install wkhtmltopdf
  - wget -O /tmp/wkhtmltox.tar.xz https://github.com/frappe/wkhtmltopdf/raw/master/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
  - tar -xf /tmp/wkhtmltox.tar.xz -C /tmp
  - sudo mv /tmp/wkhtmltox/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
  - sudo chmod o+x /usr/local/bin/wkhtmltopdf

  # install cups
  - sudo apt-get install libcups2-dev

install:
  - cd ~
  - source ./.nvm/nvm.sh
  - nvm install 12

  - pip install frappe-bench

  - bench init frappe-bench --skip-assets --python $(which python) --frappe-path $TRAVIS_BUILD_DIR

  - mkdir ~/frappe-bench/sites/test_site
  - cp $TRAVIS_BUILD_DIR/.travis/consumer_db/$DB.json ~/frappe-bench/sites/test_site/site_config.json

  - if [ $TYPE == "server" ]; then
      mkdir ~/frappe-bench/sites/test_site_producer;
      cp $TRAVIS_BUILD_DIR/.travis/producer_db/$DB.json ~/frappe-bench/sites/test_site_producer/site_config.json;
    fi

  - if [ $DB == "mariadb" ];then
      mysql -u root -e "SET GLOBAL character_set_server = 'utf8mb4'";
      mysql -u root -e "SET GLOBAL collation_server = 'utf8mb4_unicode_ci'";

      mysql -u root -e "CREATE DATABASE test_frappe_consumer";
      mysql -u root -e "CREATE USER 'test_frappe_consumer'@'localhost' IDENTIFIED BY 'test_frappe_consumer'";
      mysql -u root -e "GRANT ALL PRIVILEGES ON \`test_frappe_consumer\`.* TO 'test_frappe_consumer'@'localhost'";

      mysql -u root -e "CREATE DATABASE test_frappe_producer";
      mysql -u root -e "CREATE USER 'test_frappe_producer'@'localhost' IDENTIFIED BY 'test_frappe_producer'";
      mysql -u root -e "GRANT ALL PRIVILEGES ON \`test_frappe_producer\`.* TO 'test_frappe_producer'@'localhost'";

      mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('travis') WHERE User='root'";
      mysql -u root -e "FLUSH PRIVILEGES";
    fi

  - if [ $DB == "postgres" ];then
      psql -c "CREATE DATABASE test_frappe_consumer" -U postgres;
      psql -c "CREATE USER test_frappe_consumer WITH PASSWORD 'test_frappe'" -U postgres;

      psql -c "CREATE DATABASE test_frappe_producer" -U postgres;
      psql -c "CREATE USER test_frappe_producer WITH PASSWORD 'test_frappe'" -U postgres;
    fi

  - cd ./frappe-bench

  - sed -i 's/^watch:/# watch:/g' Procfile
  - sed -i 's/^schedule:/# schedule:/g' Procfile

  - if [ $TYPE == "server" ]; then sed -i 's/^socketio:/# socketio:/g' Procfile; fi
  - if [ $TYPE == "server" ]; then sed -i 's/^redis_socketio:/# redis_socketio:/g' Procfile; fi

  - if [ $TYPE == "ui" ]; then bench setup requirements --node; fi

  # install node-sass which is required for website theme test
  - cd ./apps/frappe
  - yarn add node-sass@4.13.1
  - cd ../..

  - bench start &
  - bench --site test_site reinstall --yes
  - if [ $TYPE == "server" ]; then bench --site test_site_producer reinstall --yes; fi
  - bench build --app frappe

after_script:
  - pip install coverage==4.5.4
  - pip install python-coveralls
  - coveralls -b apps/frappe -d ../../sites/.coverage
