Ejemplo n.º 1
0
 def getbasetemp(self):
     """ return base temporary directory. """
     if self._basetemp is None:
         if self._given_basetemp is not None:
             basetemp = Path(self._given_basetemp)
             ensure_reset_dir(basetemp)
         else:
             from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
             temproot = Path(from_env or tempfile.gettempdir())
             user = get_user() or "unknown"
             # use a sub-directory in the temproot to speed-up
             # make_numbered_dir() call
             rootdir = temproot.joinpath(f"pyt-{user}")
             rootdir.mkdir(exist_ok=True)
             basetemp = make_numbered_dir_with_cleanup(
                 prefix="",
                 root=rootdir,
                 keep=3,
                 lock_timeout=LOCK_TIMEOUT)
         assert basetemp is not None
         self._basetemp = t = basetemp
         self._trace("new basetemp", t)
         return t
     else:
         return self._basetemp
Ejemplo n.º 2
0
def get_conf_path(filename=None):
    """Return absolute path for configuration file with specified filename"""
    # Define conf_dir
    if PYTEST:
        import py
        from _pytest.tmpdir import get_user
        conf_dir = osp.join(str(py.path.local.get_temproot()),
                            'pytest-of-{}'.format(get_user()),
                            SUBFOLDER)
    elif sys.platform.startswith('linux'):
        # This makes us follow the XDG standard to save our settings
        # on Linux, as it was requested on Issue 2629
        xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '')
        if not xdg_config_home:
            xdg_config_home = osp.join(get_home_dir(), '.config')
        if not osp.isdir(xdg_config_home):
            os.makedirs(xdg_config_home)
        conf_dir = osp.join(xdg_config_home, SUBFOLDER)
    else:
        conf_dir = osp.join(get_home_dir(), SUBFOLDER)

    # Create conf_dir
    if not osp.isdir(conf_dir):
        if PYTEST:
            os.makedirs(conf_dir)
        else:
            os.mkdir(conf_dir)
    if filename is None:
        return conf_dir
    else:
        return osp.join(conf_dir, filename)
Ejemplo n.º 3
0
 def getbasetemp(self):
     """ return base temporary directory. """
     if self._basetemp is None:
         if self._given_basetemp is not None:
             basetemp = Path(self._given_basetemp)
             ensure_reset_dir(basetemp)
         else:
             from_env = os.environ.get("PYTEST_DEBUG_TEMPROOT")
             temproot = Path(from_env or tempfile.gettempdir())
             user = get_user() or "unknown"
             # use a sub-directory in the temproot to speed-up
             # make_numbered_dir() call
             rootdir = temproot.joinpath("pyt-{}".format(user))
             rootdir.mkdir(exist_ok=True)
             basetemp = make_numbered_dir_with_cleanup(
                 prefix="",
                 root=rootdir,
                 keep=3,
                 lock_timeout=LOCK_TIMEOUT,
             )
         assert basetemp is not None
         self._basetemp = t = basetemp
         self._trace("new basetemp", t)
         return t
     else:
         return self._basetemp
Ejemplo n.º 4
0
def test_get_user_uid_not_found():
    """Test that get_user() function works even if the current process's
    user id does not correspond to a valid user (e.g. running pytest in a
    Docker container with 'docker run -u'.
    """
    from _pytest.tmpdir import get_user
    assert get_user() is None
Ejemplo n.º 5
0
def find_last_picks(num=None):
    """Find latest dumps.

    This is only for REPLing.`` test_create_ioset`` uses /pytest-current

        >>> from dumper import collect_picks, find_last_picks
        >>> collect_picks(*find_last_picks())  # doctest: +SKIP
        ...
        >>> [(name, [t for t in trans if "cmdline" not in t]) for
        ...  name, trans in collected.items()]  # doctest: +SKIP
    """
    # Doubt this works in < 3.5
    # tempfile.gettempdir()
    from _pytest.tmpdir import get_user
    from pathlib import Path

    top = Path("/tmp/pytest-of-%s" % get_user())
    for numbed in top.iterdir():  # pytest-99, pytest-98, ...
        if numbed.name.endswith("-current"):
            continue
        if num and not numbed.name.endswith(str(num)):
            continue
        picks = [
            p for p in numbed.glob("srv-*/data.pickle")
            if not p.parent.name.endswith("current")
        ]
        if picks:
            return picks
    raise RuntimeError("No picks found!")
Ejemplo n.º 6
0
def test_get_user_uid_not_found():
    """Test that get_user() function works even if the current process's
    user id does not correspond to a valid user (e.g. running pytest in a
    Docker container with 'docker run -u'.
    """
    from _pytest.tmpdir import get_user
    assert get_user() is None
Ejemplo n.º 7
0
def test_get_user(monkeypatch):
    """Test that get_user() function works even if environment variables
    required by getpass module are missing from the environment on Windows
    (#1010).
    """
    monkeypatch.delenv("USER", raising=False)
    monkeypatch.delenv("USERNAME", raising=False)
    assert get_user() is None
Ejemplo n.º 8
0
def test_get_user(monkeypatch):
    """Test that get_user() function works even if environment variables
    required by getpass module are missing from the environment on Windows
    (#1010).
    """
    from _pytest.tmpdir import get_user
    monkeypatch.delenv('USER', raising=False)
    monkeypatch.delenv('USERNAME', raising=False)
    assert get_user() is None
Ejemplo n.º 9
0
def test_get_user_uid_not_found(monkeypatch):
    """Test that get_user() function works even if the current process's
    user id does not correspond to a valid user (e.g. running pytest in a
    Docker container with 'docker run -u'.
    """
    import os
    monkeypatch.setattr(os, 'getuid', lambda: -1)
    monkeypatch.delenv('USER', raising=False)
    monkeypatch.delenv('USERNAME', raising=False)

    from _pytest.tmpdir import get_user
    assert get_user() is None
Ejemplo n.º 10
0
def test_get_user_uid_not_found(monkeypatch):
    """Test that get_user() function works even if the current process's
    user id does not correspond to a valid user (e.g. running pytest in a
    Docker container with 'docker run -u'.
    """
    import os
    monkeypatch.setattr(os, 'getuid', lambda: -1)
    monkeypatch.delenv('USER', raising=False)
    monkeypatch.delenv('USERNAME', raising=False)

    from _pytest.tmpdir import get_user
    assert get_user() is None
Ejemplo n.º 11
0
    def decorated_function(*args, **kwargs):
        access_token = request.headers.get('Authorization')  # 3)
        if access_token is not None:  # 4)
            try:
                payload = jwt.decode(access_token,
                                     current_app.config['JWT_SECRET_KEY'],
                                     'HS256')  # 5)
            except jwt.InvalidTokenError:
                payload = None  # 6)

            if payload is None: return Response(status=401)  # 7)

            user_id = payload['user_id']  # 8)
            g.user_id = user_id
            g.user = get_user(user_id) if user_id else None
        else:
            return Response(status=401)  # 9)

        return f(*args, **kwargs)