From f2d6b3e6b444c61e269935188a6799c0f01d71d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 13 Oct 2021 03:10:55 +0200 Subject: [PATCH] run tests without using 'nose' run_tests.sh -> run_tests.py --- .github/workflows/tests.yml | 1 - Makefile | 2 +- gallery_dl/cache.py | 21 ++++++++++------- scripts/run_tests.py | 46 +++++++++++++++++++++++++++++++++++++ scripts/run_tests.sh | 24 ------------------- test/test_cache.py | 1 + 6 files changed, 61 insertions(+), 34 deletions(-) create mode 100755 scripts/run_tests.py delete mode 100755 scripts/run_tests.sh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b818cd97..23566a43 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,6 @@ jobs: - name: Run tests run: | - pip install nose make test - name: Test autogeneration of man pages, bash/zsh completion, etc diff --git a/Makefile b/Makefile index 334a1b3d..1ab7e06d 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ release: man completion supportedsites scripts/release.sh test: - scripts/run_tests.sh + scripts/run_tests.py executable: scripts/pyinstaller.py diff --git a/gallery_dl/cache.py b/gallery_dl/cache.py index 7a49b61a..923ed32c 100644 --- a/gallery_dl/cache.py +++ b/gallery_dl/cache.py @@ -211,13 +211,18 @@ def _path(): return os.path.join(cachedir, "cache.sqlite3") -try: - dbfile = _path() +def _init(): + try: + dbfile = _path() - # restrict access permissions for new db files - os.close(os.open(dbfile, os.O_CREAT | os.O_RDONLY, 0o600)) + # restrict access permissions for new db files + os.close(os.open(dbfile, os.O_CREAT | os.O_RDONLY, 0o600)) - DatabaseCacheDecorator.db = sqlite3.connect( - dbfile, timeout=60, check_same_thread=False) -except (OSError, TypeError, sqlite3.OperationalError): - cache = memcache # noqa: F811 + DatabaseCacheDecorator.db = sqlite3.connect( + dbfile, timeout=60, check_same_thread=False) + except (OSError, TypeError, sqlite3.OperationalError): + global cache + cache = memcache + + +_init() diff --git a/scripts/run_tests.py b/scripts/run_tests.py new file mode 100755 index 00000000..d1fd1f14 --- /dev/null +++ b/scripts/run_tests.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Copyright 2021 Mike Fährmann +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. + +import os +import sys +import unittest + +TEST_DIRECTORY = os.path.join( + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "test") + +sys.path.insert(0, TEST_DIRECTORY) + +if len(sys.argv) <= 1: + TESTS = [ + file.rpartition(".")[0] + for file in os.listdir(TEST_DIRECTORY) + if file.startswith("test_") and file != "test_results.py" + ] +else: + TESTS = [ + name if name.startswith("test_") else "test_" + name + for name in sys.argv[1:] + ] + + +suite = unittest.TestSuite() + +for test in TESTS: + try: + module = __import__(test) + except ImportError: + print("unable to import", test) + else: + tests = unittest.defaultTestLoader.loadTestsFromModule(module) + suite.addTests(tests) + +if __name__ == "__main__": + result = unittest.TextTestRunner(verbosity=2).run(suite) + if result.errors or result.failures: + sys.exit(1) diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh deleted file mode 100755 index 3a21ddb8..00000000 --- a/scripts/run_tests.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - -TESTS_CORE=(cache config cookies downloader extractor formatter job oauth output postprocessor text util) -TESTS_RESULTS=(results) - - -# select tests -case "${1:-${GALLERYDL_TESTS:-core}}" in - core) TESTS=( ${TESTS_CORE[@]} );; - results) TESTS=( ${TESTS_RESULTS[@]} );; - *) TESTS=( );; -esac - - -# transform each array element to test_###.py -TESTS=( ${TESTS[@]/#/test_} ) -TESTS=( ${TESTS[@]/%/.py} ) - - -# run 'nosetests' with selected tests -# (or all tests if ${TESTS} is empty) -nosetests --verbose -w "${DIR}/../test" ${TESTS[@]} diff --git a/test/test_cache.py b/test/test_cache.py index ecf482ca..9b3623af 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -20,6 +20,7 @@ from gallery_dl import config, util # noqa E402 dbpath = tempfile.mkstemp()[1] config.set(("cache",), "file", dbpath) from gallery_dl import cache # noqa E402 +cache._init() # def tearDownModule():