Skip to content

Commit

Permalink
Modify tests so skipped tests are marked as such.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Mar 30, 2022
1 parent 1e9744b commit 0fabcae
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 34 deletions.
41 changes: 41 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Add missing dependency command line options to pytest command.

import os
import pytest
from inform import Error, Info as CmdLineOpts
from shlib import Run, set_prefs
set_prefs(use_inform=True)

# add command line options used to signal missing dependencies to pytest
def pytest_addoption(parser):
#parser.addoption(
# "--borg-version", action="store", default="99.99.99", help="version number of borg"
#)
parser.addoption(
"--no-fuse", action="store_true", default=None, help="fuse is not available"
)

# process the command line options
@pytest.fixture(scope="session")
def dependency_options(request):
options = CmdLineOpts()

# run borg and determine its version number
try:
borg = Run(["borg", "--version"], modes="sOEW")
except Error as e:
e.report()
raise SystemExit
borg_version = borg.stdout
borg_version = borg_version.split()[-1]
borg_version = tuple(int(i) for i in borg_version.split('.'))
options.borg_version = borg_version

# determine whether FUSE is available
# Can specify the --no-fuse command line option or set the
# MISSING_DEPENDENCIES environment to 'fuse'.
options.fuse_is_missing = request.config.getvalue("--no-fuse")
if 'fuse' in os.environ.get('MISSING_DEPENDENCIES', '').lower().split():
options.fuse_is_missing = True

return options
60 changes: 33 additions & 27 deletions tests/test_emborg.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Test Emborg
#
# These tests require BorgBackup to be available.
# You can set MISSING_DEPENDENCIES environment turn off tests that depend on
# features you do not have. Currently there are two such features, “fuse” and
# “borg1.2”. Simply list each dependency you do not have in this variable
# separated by spaces. For example:
# These tests require Borg Backup (borg) to be available.
#
# export MISSING_DEPENDENCIES="fuse borg1.2"
# Some tests require FUSE (Filesystem in User Space). Pass “--no-fuse” as
# command line option to pytest to skip those tests if FUSE is not available.
#
# Some tests require a specific version borg version or newer to be available.
# Add “--borg-version=❬version❭” to indicate your version. Typically this is
# done using “--borg-version=$(borg --version)” (bash) or “--borg-version="`borg
# --version`"”. If you do not specify “--borg-version”, the latest version is
# assumed.
#
# When using tox, you can pass these options through tox using:
# tox -- --borg-version=❬version❭ --no-fuse
# Anything after the first -- is passed to pytest.

# Imports {{{1
import arrow
Expand Down Expand Up @@ -62,14 +69,6 @@ def name_from_dict_keys(cases):
Optional('dependencies', default=""): str,
}, required=True)

# missing dependencies {{{2
missing_dependencies = set(
os.environ.get('MISSING_DEPENDENCIES', '').lower().split()
)
def skip_test(dependencies):
return set(dependencies.lower().split()) & missing_dependencies


# EmborgTester class {{{1
class EmborgTester(object):
# constructor {{{2
Expand Down Expand Up @@ -174,10 +173,12 @@ def get_result(self):


# Setup {{{1
# Test fixture {{{2
# initialize {{{3
# dependency_options fixture {{{3
# This fixture is defined at the top-level in ../conftest.py.

# initialize ixture {{{3
@pytest.fixture(scope="session")
def initialize():
def initialize(dependency_options):
with cd(tests_dir):
rm("configs .config .local repositories configs.symlink".split())
cp("CONFIGS", "configs")
Expand All @@ -186,11 +187,11 @@ def initialize():
ln("~/.local/lib", ".local/lib")
ln("configs", "configs.symlink")
os.environ["HOME"] = str(cwd())
return dependency_options


# initialize_configs {{{3
# initialize_configs fixture {{{3
@pytest.fixture(scope="session")
def initialize_configs(initialize):
def initialize_configs(initialize, dependency_options):
with cd(tests_dir):
cp("CONFIGS", ".config/emborg")
rm(".config/emborg/subdir")
Expand All @@ -199,10 +200,19 @@ def initialize_configs(initialize):
contents = contents.replace('⟪EMBORG⟫', emborg_dir)
p.write_text(contents)
touch(".config/.nobackup")
return dependency_options

# missing dependencies {{{2
def skip_test_if_missing_dependencies(cmdline_options, dependencies):
if cmdline_options.borg_version < (1, 2, 0):
if 'borg1.2' in dependencies:
pytest.skip("test requires borg 1.2 or higher")
if cmdline_options.fuse_is_missing and 'fuse' in dependencies:
pytest.skip("test requires fuse")

# Tests {{{1
# test_emborg_without_configs{{{2
#@pytest.fixture(scope='session')
@parametrize(
path = f'{tests_dir}/test-cases.nt',
key = 'emborg without configs',
Expand All @@ -212,8 +222,7 @@ def test_emborg_without_configs(
initialize,
name, args, expected, expected_type, cmp_dirs, remove, dependencies
):
if skip_test(dependencies):
return
skip_test_if_missing_dependencies(initialize, dependencies)
with cd(tests_dir):
tester = EmborgTester(args, expected, expected_type, cmp_dirs, remove)
passes = tester.run()
Expand All @@ -233,8 +242,7 @@ def test_emborg_with_configs(
initialize_configs,
name, args, expected, expected_type, cmp_dirs, remove, dependencies
):
if skip_test(dependencies):
return
skip_test_if_missing_dependencies(initialize_configs, dependencies)
with cd(tests_dir):
tester = EmborgTester(args, expected, expected_type, cmp_dirs, remove)
passes = tester.run()
Expand All @@ -254,8 +262,7 @@ def test_emborg_overdue(
initialize,
name, conf, args, expected, expected_type, dependencies
):
if skip_test(dependencies):
return
skip_test_if_missing_dependencies(initialize, dependencies)
with cd(tests_dir):
if conf:
with open('.config/overdue.conf', 'w') as f:
Expand Down Expand Up @@ -288,7 +295,6 @@ def test_emborg_api(initialize):
args = ['--json', emborg.destination()]
)
response = json.loads(borg.stdout)
print(response)
archive = response['archives'][-1]['archive']

# list files in latest archive
Expand Down
8 changes: 1 addition & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ deps =
voluptuous

[testenv:pytest]
setenv =
MISSING_DEPENDENCIES = borg1.2
# this is necessary because the OS that GitHub actions uses does not have
# borg1.2. It should be set in the GitHub actions workflow configuration
# file, but I cannot get it to work. SHIT! This does not work either. How
# can such a simple common thing not work in two very popular tools?
commands = py.test -vvs --cov {posargs}
commands = py.test -vv --cov {posargs}

[testenv:tests_dir]
# this does not seem to work, tox cannot find the coverage results
Expand Down

0 comments on commit 0fabcae

Please sign in to comment.