"""
Partially borrowed from https://github.com/huggingface/transformers/blob/master/src/transformers/testing_utils.py
"""
import os
import unittest
from distutils.util import strtobool


def parse_flag_from_env(key, default=False):
    try:
        value = os.environ[key]
    except KeyError:
        # KEY isn't set, default to `default`.
        _value = default
    else:
        # KEY is set, convert it to True or False.
        try:
            _value = strtobool(value)
        except ValueError:
            # More values are supported, but let's keep the message simple.
            raise ValueError("If set, {} must be yes or no.".format(key))
    return _value


_run_slow_tests = parse_flag_from_env("RUN_SLOW", default=False)


def slow(test_case):
    """
    Decorator marking a test as slow.
    Slow tests are skipped by default. Set the RUN_SLOW environment variable
    to a truthy value to run them.
    """
    if not _run_slow_tests:
        test_case = unittest.skip("test is slow")(test_case)
    return test_case

