forked from It4innovations/hyperqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.py
More file actions
44 lines (35 loc) · 1.29 KB
/
mock.py
File metadata and controls
44 lines (35 loc) · 1.29 KB
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
import contextlib
import os
import sys
from pathlib import Path
from typing import Dict
class ProgramMock:
def __init__(self, directory: str):
self.directory = Path(os.path.abspath(directory))
os.makedirs(self.directory, exist_ok=True)
def update_env(self, env: Dict[str, str]):
path = str(self.directory)
if "PATH" in env:
path += f":{env['PATH']}"
env["PATH"] = path
def redirect_program_to_binary(self, mocked_program: str, target_binary: Path):
"""
Mocks `mocked_program` so that when you try to execute it, `target_binary` will be executed
instead.
"""
link_path = self.directory / mocked_program
link_path.unlink(missing_ok=True)
os.symlink(dst=link_path, src=target_binary)
# Make the link executable
os.chmod(link_path, 0o700)
@contextlib.contextmanager
def mock_program_with_code(self, name: str, code: str):
import textwrap
content = f"#!{sys.executable}\n{textwrap.dedent(code)}"
program_path = self.directory / name
assert not program_path.is_file()
with open(program_path, "w") as f:
f.write(content)
os.chmod(program_path, 0o700)
yield program_path
os.unlink(program_path)