-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtest_subprocess.py
More file actions
89 lines (62 loc) · 2.05 KB
/
test_subprocess.py
File metadata and controls
89 lines (62 loc) · 2.05 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
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
86
87
88
89
from __future__ import annotations
import pathlib
import pytest
from coverage_comment import subprocess
def test_run__ok():
assert subprocess.run("echo", "yay", path=pathlib.Path(".")).strip() == "yay"
def test_run__path():
assert subprocess.run("pwd", path=pathlib.Path("/")).strip() == "/"
def test_run__kwargs():
assert "A=B" in subprocess.run("env", env={"A": "B"}, path=pathlib.Path("."))
def test_run__error():
with pytest.raises(subprocess.SubProcessError):
subprocess.run("false", path=pathlib.Path("."))
@pytest.fixture
def environ(mocker):
return mocker.patch("os.environ", {})
def test_git(mocker, environ):
run = mocker.patch("coverage_comment.subprocess.run")
git = subprocess.Git()
git.cwd = pathlib.Path("/tmp")
environ["A"] = "B"
git.clone("https://some_address.git", "--depth", "1", text=True)
git.add("some_file")
run.assert_has_calls(
[
mocker.call(
"git",
"clone",
"https://some_address.git",
"--depth",
"1",
path=pathlib.Path("/tmp"),
text=True,
env=mocker.ANY,
),
mocker.call(
"git",
"add",
"some_file",
path=pathlib.Path("/tmp"),
env=mocker.ANY,
),
]
)
assert run.call_args_list[0].kwargs["env"]["A"] == "B"
def test_git_env(mocker, environ):
run = mocker.patch("coverage_comment.subprocess.run")
git = subprocess.Git()
environ.update({"A": "B", "C": "D"})
git.commit(env={"C": "E", "F": "G"})
_, _kwargs = run.call_args_list[0]
env = run.call_args_list[0].kwargs["env"]
assert env["A"] == "B"
assert env["C"] == "E"
assert env["F"] == "G"
def test_git__error(mocker):
mocker.patch(
"coverage_comment.subprocess.run", side_effect=subprocess.SubProcessError
)
git = subprocess.Git()
with pytest.raises(subprocess.GitError):
git.add("some_file")