Miscellaneous utilities to assist with testing

class datalad.tests.utils.HTTPPath(path)[source]

Bases: object

Serve the content of a path via an HTTP URL.

This class can be used as a context manager, in which case it returns the URL.

Alternatively, the start and stop methods can be called directly.

Parameters: path (str) – Directory with content to serve.
start()[source]

Start serving path via HTTP.

stop()[source]

Stop serving path.

class datalad.tests.utils.SilentHTTPHandler(*args, **kwargs)[source]

Bases: SimpleHTTPServer.SimpleHTTPRequestHandler

A little adapter to silence the handler

log_message(format, *args)[source]
datalad.tests.utils.assert_dict_equal(d1, d2)[source]
datalad.tests.utils.assert_in_results(results, **kwargs)[source]

Verify that the particular combination of keys and values is found in one of the results

datalad.tests.utils.assert_is_generator(gen)
datalad.tests.utils.assert_message(message, results)[source]

Verify that each status dict in the results has a message

This only tests the message template string, and not a formatted message with args expanded.

datalad.tests.utils.assert_no_errors_logged(func, skip_re=None)[source]

Decorator around function to assert that no errors logged during its execution

datalad.tests.utils.assert_not_in_results(results, **kwargs)[source]

Verify that the particular combination of keys and values is not in any of the results

datalad.tests.utils.assert_re_in(regex, c, flags=0, match=True, msg=None)[source]

Assert that container (list, str, etc) contains entry matching the regex

datalad.tests.utils.assert_result_count(results, n, **kwargs)[source]

Verify specific number of results (matching criteria, if any)

datalad.tests.utils.assert_result_values_cond(results, prop, cond)[source]

Verify that the values of all results for a given key in the status dicts fullfill condition cond.

Parameters: results – prop (str) – cond (callable) –
datalad.tests.utils.assert_result_values_equal(results, prop, values)[source]

Verify that the values of all results for a given key in the status dicts match the given sequence

datalad.tests.utils.assert_status(label, results)[source]

Verify that each status dict in the results has a given status label

label can be a sequence, in which case status must be one of the items in this sequence.

datalad.tests.utils.assert_str_equal(s1, s2)[source]

Helper to compare two lines

datalad.tests.utils.check_not_generatorfunction(func)[source]

Internal helper to verify that we are not decorating generator tests

datalad.tests.utils.clone_url(url)[source]
datalad.tests.utils.get_datasets_topdir()[source]

Delayed parsing so it could be monkey patched etc

datalad.tests.utils.get_most_obscure_supported_name(*arg, **kw)[source]

Return the most obscure filename that the filesystem would support under TEMPDIR

TODO: we might want to use it as a function where we would provide tdir

datalad.tests.utils.get_mtimes_and_digests(target_path)[source]

Return digests (md5) and mtimes for all the files under target_path

datalad.tests.utils.ignore_nose_capturing_stdout(func)[source]

Decorator workaround for nose’s behaviour with redirecting sys.stdout

Needed for tests involving the runner and nose redirecting stdout. Counter-intuitively, that means it needed for nosetests without ‘-s’. See issue reported here: https://code.google.com/p/python-nose/issues/detail?id=243&can=1&sort=-id&colspec=ID%20Type%20Status%20Priority%20Stars%20Milestone%20Owner%20Summary

datalad.tests.utils.integration(f)[source]

Mark test as an “integration” test which generally is not needed to be run

Generally tend to be slower

datalad.tests.utils.known_failure(func)[source]

Test decorator marking a test as known to fail

This combines probe_known_failure and skip_known_failure giving the skipping precedence over the probing.

datalad.tests.utils.known_failure_direct_mode(func)[source]

Test decorator marking a test as known to fail in a direct mode test run

If datalad.repo.direct is set to True behaves like known_failure. Otherwise the original (undecorated) function is returned.

datalad.tests.utils.known_failure_v6(func)

Test decorator marking a test as known to fail in a v6+ test run

If datalad.repo.version is set to 6 or later behaves like known_failure. Otherwise the original (undecorated) function is returned.

datalad.tests.utils.known_failure_v6_or_later(func)[source]

Test decorator marking a test as known to fail in a v6+ test run

If datalad.repo.version is set to 6 or later behaves like known_failure. Otherwise the original (undecorated) function is returned.

datalad.tests.utils.known_failure_windows(func)[source]

Test decorator marking a test as known to fail on windows

On Windows behaves like known_failure. Otherwise the original (undecorated) function is returned.

datalad.tests.utils.nok_startswith(s, prefix)[source]
datalad.tests.utils.ok_annex_get(ar, files, network=True)[source]

Helper to run .get decorated checking for correct operation

get passes through stderr from the ar to the user, which pollutes screen while running tests

Note: Currently not true anymore, since usage of –json disables progressbars

datalad.tests.utils.ok_archives_caches(repopath, n=1, persistent=None)[source]

Given a path to repository verify number of archives

Parameters: repopath (str) – Path to the repository n (int, optional) – Number of archives directories to expect persistent (bool or None, optional) – If None – both persistent and not count.
datalad.tests.utils.ok_clean_git(path, annex=None, head_modified=[], index_modified=[], untracked=[], ignore_submodules=False)[source]

Verify that under given path there is a clean git repository

it exists, .git exists, nothing is uncommitted/dirty/staged

Note

Parameters head_modified and index_modified currently work in pure git or indirect mode annex only. If they are given, no test of modification of known repo content is performed.

Parameters: path (str or Repo) – in case of a str: path to the repository’s base dir; Note, that passing a Repo instance prevents detecting annex. This might be useful in case of a non-initialized annex, a GitRepo is pointing to. annex (bool or None) – explicitly set to True or False to indicate, that an annex is (not) expected; set to None to autodetect, whether there is an annex. Default: None. ignore_submodules (bool) – if True, submodules are not inspected
datalad.tests.utils.ok_endswith(s, suffix)[source]
datalad.tests.utils.ok_exists(path)[source]
datalad.tests.utils.ok_file_has_content(path, content, strip=False, re_=False, decompress=False, **kwargs)[source]

Verify that file exists and has expected content

datalad.tests.utils.ok_file_under_git(path, filename=None, annexed=False)[source]

Test if file is present and under git/annex control

If relative path provided, then test from current directory

datalad.tests.utils.ok_generator(gen)[source]
datalad.tests.utils.ok_git_config_not_empty(ar)[source]

Helper to verify that nothing rewritten the config file

datalad.tests.utils.ok_startswith(s, prefix)[source]

Checks whether path is either a working or broken symlink

datalad.tests.utils.patch_config(vars)[source]

Patch our config with custom settings. Returns mock.patch cm

datalad.tests.utils.probe_known_failure(func)[source]

Test decorator allowing the test to pass when it fails and vice versa

Setting config datalad.tests.knownfailures.probe to True tests, whether or not the test is still failing. If it’s not, an AssertionError is raised in order to indicate that the reason for failure seems to be gone.

datalad.tests.utils.put_file_under_git(path, filename=None, content=None, annexed=False)[source]

Place file under git/annex and return used Repo

datalad.tests.utils.set_date(*args, **kwds)[source]

Temporarily override environment variables for git/git-annex dates.

Parameters: timestamp (int) – Unix timestamp.
datalad.tests.utils.skip_httpretty_on_problematic_pythons(func)[source]

As discovered some httpretty bug causes a side-effect on other tests on some Pythons. So we skip the test if such problematic combination detected

datalad.tests.utils.skip_if_no_module(module)[source]
datalad.tests.utils.skip_if_no_network(func=None)[source]

Skip test completely in NONETWORK settings

If not used as a decorator, and just a function, could be used at the module level

datalad.tests.utils.skip_if_on_windows(func=None)[source]

Skip test completely under Windows

datalad.tests.utils.skip_if_scrapy_without_selector()[source]

A little helper to skip some tests which require recent scrapy

datalad.tests.utils.skip_if_url_is_not_available(url, regex=None)[source]
datalad.tests.utils.skip_ssh(func)[source]

Skips SSH tests if on windows or if environment variable DATALAD_TESTS_SSH was not set

datalad.tests.utils.slow(f)[source]

Mark test as a slow, although not necessarily integration or usecase test

datalad.tests.utils.usecase(f)[source]

Mark test as a usecase user ran into and which (typically) caused bug report to be filed/troubleshooted