From 7ad56fbca2962e5edafb1ac726916ec37a7c3d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Arnauts?= Date: Tue, 9 Feb 2021 21:09:58 +0100 Subject: [PATCH] Cleanup CI test (#72) --- .github/workflows/addon-check.yml | 46 +++++++++-------------- .github/workflows/ci.yml | 62 ++++++++++++++++--------------- requirements.txt | 3 +- resources/lib/addon.py | 2 +- resources/lib/service.py | 2 +- tests/test_service.py | 45 ++++++++++++++++++++++ 6 files changed, 99 insertions(+), 61 deletions(-) create mode 100644 tests/test_service.py diff --git a/.github/workflows/addon-check.yml b/.github/workflows/addon-check.yml index c6229a3..c3eafd9 100644 --- a/.github/workflows/addon-check.yml +++ b/.github/workflows/addon-check.yml @@ -1,39 +1,27 @@ name: Kodi on: - - pull_request - - push + # Run action when pushed to master, or for commits in a pull request. + push: + branches: + - master + pull_request: + branches: + - master jobs: - tests: + kodi-addon-checker: name: Addon checker runs-on: ubuntu-latest strategy: fail-fast: false matrix: - kodi-branch: [leia, matrix] + kodi-version: [ leia, matrix ] steps: - - uses: actions/checkout@v2 - with: - path: ${{ github.repository }} - - name: Set up Python 3.8 - uses: actions/setup-python@v1 - with: - python-version: 3.8 - - name: Install dependencies - run: | - sudo apt-get install xmlstarlet - python -m pip install --upgrade pip - # FIXME: Requires changes from xbmc/addon-check#217 - #pip install kodi-addon-checker - pip install git+git://github.com/xbmc/addon-check.git@master - - name: Remove unwanted files - run: awk '/export-ignore/ { print $1 }' .gitattributes | xargs rm -rf -- - working-directory: ${{ github.repository }} - - name: Rewrite addon.xml for Matrix - run: | - xmlstarlet ed -L -u '/addon/requires/import[@addon="xbmc.python"]/@version' -v "3.0.0" addon.xml - version=$(xmlstarlet sel -t -v 'string(/addon/@version)' addon.xml) - xmlstarlet ed -L -u '/addon/@version' -v "${version}+matrix.99" addon.xml - working-directory: ${{ github.repository }} - if: matrix.kodi-branch == 'matrix' + - name: Check out ${{ github.sha }} from repository ${{ github.repository }} + uses: actions/checkout@v2 + - name: Run kodi-addon-checker - run: kodi-addon-checker --branch=${{ matrix.kodi-branch }} ${{ github.repository }}/ \ No newline at end of file + uses: xbmc/action-kodi-addon-checker@v1.2 + with: + kodi-version: ${{ matrix.kodi-version }} + rewrite-for-matrix: true + addon-id: ${{ github.event.repository.name }} \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 287d643..bf2c873 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,26 +1,27 @@ name: CI on: - - pull_request - - push + # Run action when pushed to master, or for commits in a pull request. + push: + branches: + - master + pull_request: + branches: + - master jobs: tests: name: Add-on testing runs-on: ${{ matrix.os }} - env: - PYTHONIOENCODING: utf-8 - PYTHONPATH: ${{ github.workspace }}/resources/lib - KODI_HOME: ${{ github.workspace }}/tests/home - KODI_INTERACTIVE: 0 strategy: fail-fast: false matrix: - os: [ubuntu-latest] - python-version: [2.7, 3.5, 3.6, 3.7, 3.8, 3.9] + os: [ ubuntu-latest ] + python-version: [ 2.7, 3.5, 3.6, 3.7, 3.8, 3.9 ] include: - # Kodi Leia on Windows uses a bundled Python 2.7 + # Kodi Leia on Windows uses a bundled Python 2.7. - os: windows-latest python-version: 2.7 - # Kodi Matrix on Windows uses a bundled Python 3.8 + + # Kodi Matrix on Windows uses a bundled Python 3.8, but we test 3.9 also to be sure. - os: windows-latest python-version: 3.8 - os: windows-latest @@ -28,36 +29,39 @@ jobs: steps: - name: Check out ${{ github.sha }} from repository ${{ github.repository }} uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v1 with: python-version: ${{ matrix.python-version }} - - name: Install dependencies (linux) - if: matrix.os == 'ubuntu-latest' - run: | - sudo apt-get install gettext - sudo pip install coverage --install-option="--install-scripts=/usr/bin" + - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt + run: pip install -r requirements.txt + - name: Run pylint run: make check-pylint + - name: Check translations - if: matrix.os == 'ubuntu-latest' - run: make check-translations + if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.9' + run: sudo apt-get install gettext && make check-translations + - name: Run unit tests - run: coverage run -m unittest discover env: - ADDON_PASSWORD: ${{ secrets.ADDON_PASSWORD }} ADDON_USERNAME: ${{ secrets.ADDON_USERNAME }} - - name: Run addon service for 10 seconds - if: matrix.os == 'ubuntu-latest' - run: timeout --preserve-status -s SIGINT 10 coverage run -a service_entry.py - env: ADDON_PASSWORD: ${{ secrets.ADDON_PASSWORD }} - ADDON_USERNAME: ${{ secrets.ADDON_USERNAME }} + PYTHONIOENCODING: utf-8 + KODI_HOME: ${{ github.workspace }}/tests/home + KODI_INTERACTIVE: 0 + KODI_STUB_RPC_RESPONSES: ${{ github.workspace }}/tests/rpc + HTTP_PROXY: ${{ secrets.HTTP_PROXY }} + run: pytest -v --cov=./ --cov-report=xml tests + - name: Upload code coverage to CodeCov - if: matrix.os == 'ubuntu-latest' uses: codecov/codecov-action@v1 continue-on-error: true + env: + OS: ${{ matrix.os }} + PYTHON: ${{ matrix.python-version }} + with: + flags: unittests + env_vars: OS,PYTHON \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 35cb6ba..5559ba9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ -coverage polib pylint pytest +pytest-cov +pytest-timeout python-dateutil requests git+git://github.com/dagwieers/kodi-plugin-routing.git@setup#egg=routing diff --git a/resources/lib/addon.py b/resources/lib/addon.py index 2d2b638..e0d6afd 100644 --- a/resources/lib/addon.py +++ b/resources/lib/addon.py @@ -9,7 +9,6 @@ from routing import Plugin from resources.lib import kodilogging -kodilogging.config() routing = Plugin() # pylint: disable=invalid-name _LOGGER = logging.getLogger(__name__) @@ -190,4 +189,5 @@ def iptv_epg(): def run(params): """ Run the routing plugin """ + kodilogging.config() routing.run(params) diff --git a/resources/lib/service.py b/resources/lib/service.py index f18b65a..67f063f 100644 --- a/resources/lib/service.py +++ b/resources/lib/service.py @@ -13,7 +13,6 @@ from xbmc import Monitor, Player, getInfoLabel from resources.lib import kodilogging, kodiutils from resources.lib.viervijfzes.auth import AuthApi -kodilogging.config() _LOGGER = logging.getLogger(__name__) @@ -180,4 +179,5 @@ class KodiPlayer(Player): def run(): """ Run the BackgroundService """ + kodilogging.config() BackgroundService().run() diff --git a/tests/test_service.py b/tests/test_service.py new file mode 100644 index 0000000..664fb5a --- /dev/null +++ b/tests/test_service.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +""" Tests for background service """ + +# pylint: disable=invalid-name,missing-docstring + +from __future__ import absolute_import, division, print_function, unicode_literals + +import os +import signal +import sys +import threading +import time +import unittest + +import pytest + +from resources.lib import addon, kodiutils +from resources.lib.service import BackgroundService + +routing = addon.routing + + +@unittest.skipIf(sys.platform.startswith("win"), 'Skipping on Windows.') +@unittest.skipUnless(kodiutils.get_setting('username') and kodiutils.get_setting('password'), 'Skipping since we have no credentials.') +class TestService(unittest.TestCase): + """ Tests for the background service """ + + @staticmethod + @pytest.mark.timeout(timeout=10, method='thread') + def test_service(): + """ Run the background service for 5 seconds. It will raise an error when it doesn't stop after 10 seconds. """ + + def terminate_service(seconds=5): + """ Sleep a bit, and send us a SIGINT signal. """ + time.sleep(seconds) + os.kill(os.getpid(), signal.SIGINT) + + threading.Thread(target=terminate_service).start() + + service = BackgroundService() + service.run() + + +if __name__ == '__main__': + unittest.main()