From 15c9ba87f9ece69bf9f36aebb1e576c427e893c5 Mon Sep 17 00:00:00 2001 From: Timo Reichl Date: Sun, 8 Oct 2023 20:08:05 +0000 Subject: [PATCH] Add tests Signed-off-by: Timo Reichl --- .gitignore | 3 + scripts/test.sh | 15 +++++ tests/__init__.py | 0 tests/constants.py | 124 +++++++++++++++++++++++++++++++++++++ tests/helpers.py | 35 +++++++++++ tests/test_00_general.py | 13 ++++ tests/test_10_xdeb.py | 14 +++++ tests/test_20_providers.py | 12 ++++ tests/test_30_sync.py | 20 ++++++ tests/test_40_search.py | 42 +++++++++++++ tests/test_50_install.py | 37 +++++++++++ tests/test_60_clean.py | 13 ++++ 12 files changed, 328 insertions(+) create mode 100755 scripts/test.sh create mode 100644 tests/__init__.py create mode 100644 tests/constants.py create mode 100644 tests/helpers.py create mode 100644 tests/test_00_general.py create mode 100644 tests/test_10_xdeb.py create mode 100644 tests/test_20_providers.py create mode 100644 tests/test_30_sync.py create mode 100644 tests/test_40_search.py create mode 100644 tests/test_50_install.py create mode 100644 tests/test_60_clean.py diff --git a/.gitignore b/.gitignore index ba077a4..f0c36fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ bin +html +**/.pytest_cache +**/__pycache__ diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100755 index 0000000..ef20d36 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -e + +# note: tests will run with 'latest' version of xdeb for now +# because tests/test_xdeb.py installs 'latest' last + +mkdir -p html +PYTEST_TEST_REPORT_ARGS="--html=html/test-report.html --self-contained-html" + +if [ "${1}" = "html" ]; then + pytest ${PYTEST_TEST_REPORT_ARGS} +else + pytest +fi diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/constants.py b/tests/constants.py new file mode 100644 index 0000000..1712e52 --- /dev/null +++ b/tests/constants.py @@ -0,0 +1,124 @@ +from pathlib import Path + + +DEB_NONEXISTENT_PACKAGE = "DEFINITELY-NON-EXISTENT-PACKAGE" + + +XDEB_BINARY_PATH = Path("/usr/local/bin/xdeb") +XDEB_RELEASES = ( + "1.0", + "1.1", + "1.2", + "1.3" +) + +XDEB_INSTALL_BINARY_PATH = Path(__file__).parent.parent.joinpath("bin", "xdeb-install-linux-x86_64") + +XDEB_INSTALL_PROVIDERS = ( + "debian.org", + "linuxmint.com", + "ubuntu.com", + "microsoft.com", + "google.com", +) + +XDEB_INSTALL_HAVE_PACKAGE = { + "speedcrunch": { + "debian.org": { + "any": True, + "distributions": { + "bookworm": True, + "bookworm-backports": False, + "bullseye": True, + "bullseye-backports": False, + "buster": True, + "buster-backports": False, + "sid": True, + "testing": True, + "testing-backports": False + }, + }, + "linuxmint.com": { + "any": False + }, + "ubuntu.com": { + "any": True, + "distributions": { + "bionic": True, + "focal": True, + "jammy": True + } + }, + "microsoft.com": { + "any": False + }, + "google.com": { + "any": False + }, + }, + "vscode": { + "debian.org": { + "any": False + }, + "linuxmint.com": { + "any": False + }, + "ubuntu.com": { + "any": False + }, + "microsoft.com": { + "any": True, + "distributions": { + "current": True + } + }, + "google.com": { + "any": False + } + }, + "google-chrome": { + "debian.org": { + "any": False + }, + "linuxmint.com": { + "any": False + }, + "ubuntu.com": { + "any": False + }, + "microsoft.com": { + "any": False + }, + "google.com": { + "any": True, + "distributions": { + "current": True + } + } + }, + "google-chrome-unstable": { + "debian.org": { + "any": False + }, + "linuxmint.com": { + "any": False + }, + "ubuntu.com": { + "any": False + }, + "microsoft.com": { + "any": False + }, + "google.com": { + "any": True, + "distributions": { + "current": True + } + } + } +} + +XDEB_INSTALL_PACKAGE_MAP = { + "vscode": "code", + "google-chrome": "google-chrome-stable" +} diff --git a/tests/helpers.py b/tests/helpers.py new file mode 100644 index 0000000..1ffd9fb --- /dev/null +++ b/tests/helpers.py @@ -0,0 +1,35 @@ +import subprocess + +from . import constants + + +def assert_xdeb_install_command(*args): + subprocess.check_call([constants.XDEB_INSTALL_BINARY_PATH, *args]) + + +def assert_command_assume_yes(returncode: int, *args): + process = subprocess.run(*args, input="yes\n".encode(), stdout=subprocess.PIPE) + assert process.returncode == returncode + + +def assert_xdeb_install_xbps(returncode: int, *args): + assert_command_assume_yes(returncode, [constants.XDEB_INSTALL_BINARY_PATH, *args]) + + if returncode == 0: + package = args[-1] if args[-1] not in constants.XDEB_INSTALL_PACKAGE_MAP else constants.XDEB_INSTALL_PACKAGE_MAP[args[-1]] + assert_command_assume_yes(0, ["sudo", "xbps-remove", package]) + assert_command_assume_yes(0, ["sudo", "xbps-remove", "-Oo"]) + + +def assert_xdeb_installation(version: str = None): + # remove any previous xdeb binary + subprocess.check_call(["sudo", "rm", "-rf", constants.XDEB_BINARY_PATH]) + + # install xdeb version + args = ["xdeb"] + + if version is not None: + args.append(version) + + assert_xdeb_install_command(*args) + subprocess.check_call([constants.XDEB_BINARY_PATH, "-h"]) diff --git a/tests/test_00_general.py b/tests/test_00_general.py new file mode 100644 index 0000000..fc31e36 --- /dev/null +++ b/tests/test_00_general.py @@ -0,0 +1,13 @@ +import pytest + +from . import helpers + + +@pytest.mark.order(0) +def test_version(): + helpers.assert_xdeb_install_command("--version") + + +@pytest.mark.order(1) +def test_help(): + helpers.assert_xdeb_install_command("--help") diff --git a/tests/test_10_xdeb.py b/tests/test_10_xdeb.py new file mode 100644 index 0000000..b9fe41a --- /dev/null +++ b/tests/test_10_xdeb.py @@ -0,0 +1,14 @@ +import pytest + +from . import constants +from . import helpers + + +@pytest.mark.order(10) +def test_xdeb(): + helpers.assert_xdeb_installation() + + for release in constants.XDEB_RELEASES: + helpers.assert_xdeb_installation(release) + + helpers.assert_xdeb_installation("latest") diff --git a/tests/test_20_providers.py b/tests/test_20_providers.py new file mode 100644 index 0000000..4ad928b --- /dev/null +++ b/tests/test_20_providers.py @@ -0,0 +1,12 @@ +import pytest +from . import helpers + + +@pytest.mark.order(20) +def test_providers(): + helpers.assert_xdeb_install_command("providers") + + +@pytest.mark.order(21) +def test_providers_details(): + helpers.assert_xdeb_install_command("providers", "--details") diff --git a/tests/test_30_sync.py b/tests/test_30_sync.py new file mode 100644 index 0000000..c053f9a --- /dev/null +++ b/tests/test_30_sync.py @@ -0,0 +1,20 @@ +import pytest + +from . import constants +from . import helpers + + +@pytest.mark.order(30) +def test_sync(): + helpers.assert_xdeb_install_command("sync") + + +@pytest.mark.order(31) +def test_sync_single(): + for provider in constants.XDEB_INSTALL_PROVIDERS: + helpers.assert_xdeb_install_command("sync", provider) + + +@pytest.mark.order(32) +def test_sync_each(): + helpers.assert_xdeb_install_command("sync", *constants.XDEB_INSTALL_PROVIDERS) diff --git a/tests/test_40_search.py b/tests/test_40_search.py new file mode 100644 index 0000000..429039d --- /dev/null +++ b/tests/test_40_search.py @@ -0,0 +1,42 @@ +import subprocess +import pytest + +from . import constants +from . import helpers + + +@pytest.mark.order(40) +def test_search_nothing(): + with pytest.raises(subprocess.CalledProcessError): + helpers.assert_xdeb_install_command("search") + + +@pytest.mark.order(41) +def test_search_nonexistent(): + with pytest.raises(subprocess.CalledProcessError): + helpers.assert_xdeb_install_command("search", constants.DEB_NONEXISTENT_PACKAGE) + + +@pytest.mark.order(42) +def test_search_speedcrunch(): + helpers.assert_xdeb_install_command("search", "speedcrunch") + + +@pytest.mark.order(43) +def test_search_packages(): + for package, provider_data in constants.XDEB_INSTALL_HAVE_PACKAGE.items(): + for provider, data in provider_data.items(): + if not data["any"]: + with pytest.raises(subprocess.CalledProcessError): + helpers.assert_xdeb_install_command("search", "--provider", provider, package) + continue + + helpers.assert_xdeb_install_command("search", "--provider", provider, package) + + for distribution, available in data["distributions"].items(): + if not available: + with pytest.raises(subprocess.CalledProcessError): + helpers.assert_xdeb_install_command("search", "--provider", provider, "--distribution", distribution, package) + continue + + helpers.assert_xdeb_install_command("search", "--provider", provider, "--distribution", distribution, package) diff --git a/tests/test_50_install.py b/tests/test_50_install.py new file mode 100644 index 0000000..b861ca4 --- /dev/null +++ b/tests/test_50_install.py @@ -0,0 +1,37 @@ +import pytest + +from . import constants +from . import helpers + + +@pytest.mark.order(51) +def test_install_nothing(): + helpers.assert_xdeb_install_xbps(1) + + +@pytest.mark.order(52) +def test_install_nonexistent(): + helpers.assert_xdeb_install_xbps(1, constants.DEB_NONEXISTENT_PACKAGE) + + +@pytest.mark.order(53) +def test_install_speedcrunch(): + helpers.assert_xdeb_install_xbps(0, "speedcrunch") + + +@pytest.mark.order(54) +def test_install_packages(): + for package, provider_data in constants.XDEB_INSTALL_HAVE_PACKAGE.items(): + for provider, data in provider_data.items(): + if not data["any"]: + helpers.assert_xdeb_install_xbps(1, "--provider", provider, package) + continue + + helpers.assert_xdeb_install_xbps(0, "--provider", provider, package) + + for distribution, available in data["distributions"].items(): + if not available: + helpers.assert_xdeb_install_xbps(1, "--provider", provider, "--distribution", distribution, package) + continue + + helpers.assert_xdeb_install_xbps(0, "--provider", provider, "--distribution", distribution, package) diff --git a/tests/test_60_clean.py b/tests/test_60_clean.py new file mode 100644 index 0000000..31204cd --- /dev/null +++ b/tests/test_60_clean.py @@ -0,0 +1,13 @@ +import pytest + +from . import helpers + + +@pytest.mark.order(60) +def test_clean(): + helpers.assert_xdeb_install_command("clean") + + +@pytest.mark.order(61) +def test_clean_lists(): + helpers.assert_xdeb_install_command("clean", "--lists")