File: test_posix.py

package info (click to toggle)
shellingham 1.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 204 kB
  • sloc: python: 460; sh: 6; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os

import pytest

from shellingham import posix
from shellingham.posix._core import Process


class EnvironManager(object):

    def __init__(self):
        self.backup = {}
        self.changed = set()

    def patch(self, **kwargs):
        self.backup.update({
            k: os.environ[k] for k in kwargs if k in os.environ
        })
        self.changed.update(kwargs.keys())
        os.environ.update(kwargs)

    def unpatch(self):
        for k in self.changed:
            try:
                v = self.backup[k]
            except KeyError:
                os.environ.pop(k, None)
            else:
                os.environ[k] = v


@pytest.fixture()
def environ(request):
    """Provide environment variable override, and restore on finalize.
    """
    manager = EnvironManager()
    request.addfinalizer(manager.unpatch)
    return manager


MAPPING_EXAMPLE_KEEGANCSMITH = [
    Process(
        args=(
            "/Applications/iTerm.app/Contents/MacOS/iTerm2",
            "--server",
            "login",
            "-fp",
            "keegan",
        ),
        pid="1480",
        ppid="1477",
    ),
    Process(args=("-bash",), pid="1482", ppid="1481"),
    Process(args=("screen",), pid="1556", ppid="1482"),
    Process(args=("-/usr/local/bin/bash",), pid="1558", ppid="1557"),
    Process(
        args=(
            "/Applications/Emacs.app/Contents/MacOS/Emacs-x86_64-10_10",
            "-nw",
        ),
        pid="1706",
        ppid="1558",
    ),
    Process(
        args=("/usr/local/bin/aspell", "-a", "-m", "-B", "--encoding=utf-8"),
        pid="77061",
        ppid="1706",
    ),
    Process(args=("-/usr/local/bin/bash",), pid="1562", ppid="1557"),
    Process(args=("-/usr/local/bin/bash",), pid="87033", ppid="1557"),
    Process(args=("-/usr/local/bin/bash",), pid="84732", ppid="1557"),
    Process(args=("-/usr/local/bin/bash",), pid="89065", ppid="1557"),
    Process(args=("-/usr/local/bin/bash",), pid="80216", ppid="1557"),
]


@pytest.mark.parametrize('mapping, result', [
    (   # Based on pypa/pipenv#2496, provided by @keegancsmith.
        MAPPING_EXAMPLE_KEEGANCSMITH, ('bash', '==MOCKED=LOGIN=SHELL==/bash'),
    ),
])
def test_get_shell(mocker, environ, mapping, result):
    environ.patch(SHELL="==MOCKED=LOGIN=SHELL==/bash")
    mocker.patch.object(posix, "_iter_process_parents", return_value=mapping)
    assert posix.get_shell(pid=77061) == result