Debian Codegraph @

tests/test_posix.py

raw symbols SCIP
1 import os
2
3 import pytest
4
5 from shellingham import posix
6 from shellingham.posix._core import Process
7
8
9 class EnvironManager(object):
10
11 def __init__(self):
12 self.backup = {}
13 self.changed = set()
14
15 def patch(self, **kwargs):
16 self.backup.update({
17 k: os.environ[k] for k in kwargs if k in os.environ
18 })
19 self.changed.update(kwargs.keys())
20 os.environ.update(kwargs)
21
22 def unpatch(self):
23 for k in self.changed:
24 try:
25 v = self.backup[k]
26 except KeyError:
27 os.environ.pop(k, None)
28 else:
29 os.environ[k] = v
30
31
32 @pytest.fixture()
33 def environ(request):
34 """Provide environment variable override, and restore on finalize.
35 """
36 manager = EnvironManager()
37 request.addfinalizer(manager.unpatch)
38 return manager
39
40
41 MAPPING_EXAMPLE_KEEGANCSMITH = [
42 Process(
43 args=(
44 "/Applications/iTerm.app/Contents/MacOS/iTerm2",
45 "--server",
46 "login",
47 "-fp",
48 "keegan",
49 ),
50 pid="1480",
51 ppid="1477",
52 ),
53 Process(args=("-bash",), pid="1482", ppid="1481"),
54 Process(args=("screen",), pid="1556", ppid="1482"),
55 Process(args=("-/usr/local/bin/bash",), pid="1558", ppid="1557"),
56 Process(
57 args=(
58 "/Applications/Emacs.app/Contents/MacOS/Emacs-x86_64-10_10",
59 "-nw",
60 ),
61 pid="1706",
62 ppid="1558",
63 ),
64 Process(
65 args=("/usr/local/bin/aspell", "-a", "-m", "-B", "--encoding=utf-8"),
66 pid="77061",
67 ppid="1706",
68 ),
69 Process(args=("-/usr/local/bin/bash",), pid="1562", ppid="1557"),
70 Process(args=("-/usr/local/bin/bash",), pid="87033", ppid="1557"),
71 Process(args=("-/usr/local/bin/bash",), pid="84732", ppid="1557"),
72 Process(args=("-/usr/local/bin/bash",), pid="89065", ppid="1557"),
73 Process(args=("-/usr/local/bin/bash",), pid="80216", ppid="1557"),
74 ]
75
76
77 @pytest.mark.parametrize('mapping, result', [
78 ( # Based on pypa/pipenv#2496, provided by @keegancsmith.
79 MAPPING_EXAMPLE_KEEGANCSMITH, ('bash', '==MOCKED=LOGIN=SHELL==/bash'),
80 ),
81 ])
82 def test_get_shell(mocker, environ, mapping, result):
83 environ.patch(SHELL="==MOCKED=LOGIN=SHELL==/bash")
84 mocker.patch.object(posix, "_iter_process_parents", return_value=mapping)
85 assert posix.get_shell(pid=77061) == result