Skip to content

Commit b4e9bdc

Browse files
Replace monkeypatch + pretend with pytest-mock
1 parent 2a1b34f commit b4e9bdc

14 files changed

+222
-221
lines changed

poetry.lock

Lines changed: 19 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ watchdog = {version = "*", optional = true}
4444
[tool.poetry.group.test.dependencies]
4545
pytest = "~7.4.0"
4646
pytest-django = "~4.5.0"
47-
pretend = "~1.0.9"
4847
libsass = "~0.22.0"
4948
pytest-cov = "~4.0.0"
49+
pytest-mock = "~3.11.1"
5050
watchdog = "~3.0.0"
5151
coverage = "~7.3.0"
5252
tox = "~4.9.0"

tests/test_babel.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
import os
33

44
import pytest
5+
from pytest_mock import MockFixture
56

67
from static_precompiler import compilers, exceptions
78

89
from .test_coffeescript import clean_javascript
910

1011

11-
def test_compile_file(monkeypatch, tmpdir):
12-
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)
12+
def test_compile_file(mocker: MockFixture, tmpdir):
13+
mocker.patch("static_precompiler.settings.ROOT", tmpdir.strpath)
1314

1415
compiler = compilers.Babel()
1516

@@ -26,8 +27,8 @@ def test_compile_file(monkeypatch, tmpdir):
2627
compiler.compile_file("scripts/broken.es6")
2728

2829

29-
def test_sourcemap(monkeypatch, tmpdir):
30-
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)
30+
def test_sourcemap(mocker: MockFixture, tmpdir):
31+
mocker.patch("static_precompiler.settings.ROOT", tmpdir.strpath)
3132

3233
compiler = compilers.Babel(sourcemap_enabled=False)
3334
compiler.compile_file("scripts/test.es6")

tests/test_base_compiler.py

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import os
22

3-
import pretend
43
import pytest
54
from django.utils import encoding
5+
from pytest_mock import MockFixture
66

77
from static_precompiler import compilers, models, settings
88

99

10-
def test_is_supported(monkeypatch):
11-
monkeypatch.setattr("static_precompiler.compilers.base.BaseCompiler.input_extension", "foo")
10+
def test_is_supported(mocker: MockFixture):
11+
mocker.patch("static_precompiler.compilers.base.BaseCompiler.input_extension", "foo")
1212
compiler = compilers.BaseCompiler()
1313

1414
assert compiler.is_supported("test.foo") is True
1515
assert compiler.is_supported("test.bar") is False
1616
assert compiler.is_supported("foo.test") is False
1717

1818

19-
def test_get_output_filename(monkeypatch):
19+
def test_get_output_filename(mocker: MockFixture):
2020
compiler = compilers.BaseCompiler()
2121

22-
monkeypatch.setattr(compiler, "input_extension", "coffee")
23-
monkeypatch.setattr(compiler, "output_extension", "js")
22+
mocker.patch.object(compiler, "input_extension", "coffee")
23+
mocker.patch.object(compiler, "output_extension", "js")
2424

2525
assert compiler.get_output_filename("dummy.coffee") == "dummy.js"
2626
assert compiler.get_output_filename("dummy.coffee.coffee") == "dummy.coffee.js"
@@ -47,85 +47,87 @@ def test_get_full_source_path():
4747
)
4848

4949

50-
def test_get_output_path(monkeypatch):
50+
def test_get_output_path(mocker: MockFixture):
5151
compiler = compilers.BaseCompiler()
52-
monkeypatch.setattr(compiler, "get_output_filename", lambda source_path: source_path.replace(".coffee", ".js"))
52+
mocker.patch.object(
53+
compiler, "get_output_filename", side_effect=lambda source_path: source_path.replace(".coffee", ".js")
54+
)
5355

5456
assert compiler.get_output_path("scripts/test.coffee") == settings.OUTPUT_DIR + "/scripts/test.js"
5557
assert compiler.get_output_path("/scripts/test.coffee") == settings.OUTPUT_DIR + "/scripts/test.js"
5658

5759

58-
def test_get_full_output_path(monkeypatch):
60+
def test_get_full_output_path(mocker: MockFixture):
5961
compiler = compilers.BaseCompiler()
60-
monkeypatch.setattr(compiler, "get_output_path", lambda source_path: settings.OUTPUT_DIR + "/dummy.js")
62+
mocker.patch.object(compiler, "get_output_path", return_value=settings.OUTPUT_DIR + "/dummy.js")
6163

6264
assert compiler.get_full_output_path("dummy.coffee") == os.path.join(settings.ROOT, settings.OUTPUT_DIR, "dummy.js")
6365

6466

65-
def test_get_source_mtime(monkeypatch):
67+
def test_get_source_mtime(mocker: MockFixture):
6668
compiler = compilers.BaseCompiler()
6769

68-
monkeypatch.setattr(compiler, "get_full_source_path", lambda source_path: "dummy.coffee")
69-
monkeypatch.setattr("static_precompiler.mtime.get_mtime", lambda filename: 1)
70+
mocker.patch.object(compiler, "get_full_source_path", return_value="dummy.coffee")
71+
mocker.patch("static_precompiler.mtime.get_mtime", return_value=1)
7072

7173
assert compiler.get_source_mtime("dummy.coffee") == 1
7274

7375

74-
def test_get_output_mtime(monkeypatch):
76+
def test_get_output_mtime(mocker: MockFixture):
7577
compiler = compilers.BaseCompiler()
7678

77-
monkeypatch.setattr(compiler, "get_full_output_path", lambda output_path: "dummy.js")
78-
monkeypatch.setattr("os.path.exists", lambda path: False)
79+
mocker.patch.object(compiler, "get_full_output_path", return_value="dummy.js")
80+
mocker.patch("os.path.exists", return_value=False)
7981

8082
assert compiler.get_output_mtime("dummy.coffee") is None
8183

82-
monkeypatch.setattr("os.path.exists", lambda path: True)
84+
mocker.patch("os.path.exists", return_value=True)
8385

84-
monkeypatch.setattr("static_precompiler.mtime.get_mtime", lambda filename: 1)
86+
mocker.patch("static_precompiler.mtime.get_mtime", return_value=1)
8587
assert compiler.get_output_mtime("dummy.coffee") == 1
8688

8789

88-
def test_should_compile(monkeypatch):
90+
def test_should_compile(mocker: MockFixture):
8991
compiler = compilers.BaseCompiler()
9092

91-
monkeypatch.setattr(compiler, "get_dependencies", lambda source_path: ["B", "C"])
93+
mocker.patch.object(compiler, "get_dependencies", return_value=["B", "C"])
9294

9395
mtimes = {
9496
"A": 1,
9597
"B": 3,
9698
"C": 5,
9799
}
98100

99-
monkeypatch.setattr(compiler, "get_source_mtime", lambda x: mtimes[x])
100-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: None)
101+
mocker.patch.object(compiler, "get_source_mtime", side_effect=lambda x: mtimes[x])
102+
mocker.patch.object(compiler, "get_output_mtime", return_value=None)
101103

102104
assert compiler.should_compile("A") is True
103105

104-
monkeypatch.setattr(compiler, "supports_dependencies", True)
106+
mocker.patch.object(compiler, "supports_dependencies", True)
105107

106-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: 6)
108+
mocker.patch.object(compiler, "get_output_mtime", return_value=6)
107109
assert compiler.should_compile("A") is False
108110

109-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: 5)
111+
mocker.patch.object(compiler, "get_output_mtime", return_value=5)
110112
assert compiler.should_compile("A") is True
111113

112-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: 4)
114+
mocker.patch.object(compiler, "get_output_mtime", return_value=4)
113115
assert compiler.should_compile("A") is True
114116

115-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: 2)
117+
mocker.patch.object(compiler, "get_output_mtime", return_value=2)
116118
assert compiler.should_compile("A") is True
117119

118-
monkeypatch.setattr(compiler, "supports_dependencies", False)
120+
mocker.patch.object(compiler, "supports_dependencies", False)
119121

120122
assert compiler.should_compile("A") is False
121123

122-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: 1)
124+
mocker.patch.object(compiler, "get_output_mtime", return_value=1)
123125
assert compiler.should_compile("A") is True
124126

125-
monkeypatch.setattr(compiler, "get_output_mtime", lambda x: 0)
127+
mocker.patch.object(compiler, "get_output_mtime", return_value=0)
126128
assert compiler.should_compile("A") is True
127129

128-
monkeypatch.setattr("static_precompiler.settings.DISABLE_AUTO_COMPILE", True)
130+
mocker.patch("static_precompiler.settings.DISABLE_AUTO_COMPILE", True)
129131
assert compiler.should_compile("A") is False
130132

131133

@@ -140,60 +142,49 @@ def test_compile_source():
140142
compiler.compile_source("source")
141143

142144

143-
def test_compile(monkeypatch):
145+
def test_compile(mocker: MockFixture):
144146
compiler = compilers.BaseCompiler()
145147

146-
monkeypatch.setattr(compiler, "compile_file", pretend.call_recorder(lambda *args: "dummy.js"))
147-
monkeypatch.setattr(compiler, "update_dependencies", pretend.call_recorder(lambda *args: None))
148-
monkeypatch.setattr(compiler, "find_dependencies", pretend.call_recorder(lambda *args: ["A", "B"]))
149-
monkeypatch.setattr(compiler, "get_output_path", lambda *args: "dummy.js")
150-
monkeypatch.setattr(compiler, "is_supported", lambda *args: False)
151-
monkeypatch.setattr(compiler, "should_compile", lambda *args, **kwargs: True)
148+
compile_file = mocker.patch.object(compiler, "compile_file", return_value="dummy.js")
149+
update_dependencies = mocker.patch.object(compiler, "update_dependencies", return_value=None)
150+
find_dependencies = mocker.patch.object(compiler, "find_dependencies", return_value=["A", "B"])
151+
mocker.patch.object(compiler, "get_output_path", return_value="dummy.js")
152+
is_supported = mocker.patch.object(compiler, "is_supported", return_value=False)
153+
should_compile = mocker.patch.object(compiler, "should_compile", return_value=True)
152154

153155
with pytest.raises(ValueError):
154156
compiler.compile("dummy.coffee")
155157

156-
# noinspection PyUnresolvedReferences
157-
assert compiler.compile_file.calls == []
158-
159-
monkeypatch.setattr(compiler, "is_supported", lambda *args: True)
160-
monkeypatch.setattr(compiler, "should_compile", lambda *args, **kwargs: False)
158+
compile_file.assert_not_called()
159+
is_supported.return_value = True
160+
should_compile.return_value = False
161161

162162
assert compiler.compile("dummy.coffee") == "dummy.js"
163-
# noinspection PyUnresolvedReferences
164-
assert compiler.compile_file.calls == []
163+
compile_file.assert_not_called()
165164

166-
monkeypatch.setattr(compiler, "should_compile", lambda *args, **kwargs: True)
165+
should_compile.return_value = True
167166
assert compiler.compile("dummy.coffee") == "dummy.js"
168167

169-
# noinspection PyUnresolvedReferences
170-
assert compiler.compile_file.calls == [pretend.call("dummy.coffee")]
168+
compile_file.assert_called_once_with("dummy.coffee")
171169

172-
# noinspection PyUnresolvedReferences
173-
assert compiler.update_dependencies.calls == []
170+
update_dependencies.assert_not_called()
174171

175-
monkeypatch.setattr(compiler, "supports_dependencies", True)
176172
compiler.supports_dependencies = True
177173
compiler.compile("dummy.coffee")
178-
# noinspection PyUnresolvedReferences
179-
assert compiler.find_dependencies.calls == [pretend.call("dummy.coffee")]
180-
# noinspection PyUnresolvedReferences
181-
assert compiler.update_dependencies.calls == [pretend.call("dummy.coffee", ["A", "B"])]
174+
find_dependencies.assert_called_once_with("dummy.coffee")
175+
update_dependencies.assert_called_once_with("dummy.coffee", ["A", "B"])
182176

183177

184-
def test_compile_lazy(monkeypatch):
178+
def test_compile_lazy(mocker: MockFixture):
185179
compiler = compilers.BaseCompiler()
186180

187-
monkeypatch.setattr(compiler, "compile", pretend.call_recorder(lambda path: path))
181+
compile = mocker.patch.object(compiler, "compile", side_effect=lambda x: x)
188182

189183
lazy_compiled = compiler.compile_lazy("dummy.coffee")
190-
# noinspection PyUnresolvedReferences
191-
assert compiler.compile.calls == []
184+
compile.assert_not_called()
192185

193186
assert encoding.force_str(lazy_compiled) == "dummy.coffee"
194-
195-
# noinspection PyUnresolvedReferences
196-
assert compiler.compile.calls == [pretend.call("dummy.coffee")]
187+
compile.assert_called_once_with("dummy.coffee")
197188

198189

199190
def test_find_dependencies():
@@ -202,7 +193,7 @@ def test_find_dependencies():
202193

203194

204195
@pytest.mark.django_db
205-
def test_get_dependencies(monkeypatch):
196+
def test_get_dependencies(mocker: MockFixture):
206197
compiler = compilers.BaseCompiler()
207198

208199
assert models.Dependency.objects.exists() is False
@@ -218,18 +209,18 @@ def get_full_source_path(source_path):
218209
raise ValueError()
219210
return source_path
220211

221-
monkeypatch.setattr(compiler, "get_full_source_path", get_full_source_path)
212+
mocker.patch.object(compiler, "get_full_source_path", side_effect=get_full_source_path)
222213

223214
assert list(models.Dependency.objects.all()) == [dependency_1, dependency_2]
224215

225216
assert compiler.get_dependencies("spam.scss") == ["ham.scss"]
226217

227-
# Dependency the refers to non-existing file were removed.
218+
# The Dependency that refers to non-existing file was removed.
228219
assert list(models.Dependency.objects.all()) == [dependency_1]
229220

230221

231222
@pytest.mark.django_db
232-
def test_get_dependents(monkeypatch):
223+
def test_get_dependents(mocker: MockFixture):
233224
compiler = compilers.BaseCompiler()
234225

235226
assert not models.Dependency.objects.exists()
@@ -245,13 +236,13 @@ def get_full_source_path(source_path):
245236
raise ValueError()
246237
return source_path
247238

248-
monkeypatch.setattr(compiler, "get_full_source_path", get_full_source_path)
239+
mocker.patch.object(compiler, "get_full_source_path", side_effect=get_full_source_path)
249240

250241
assert list(models.Dependency.objects.all()) == [dependency_1, dependency_2]
251242

252243
assert compiler.get_dependents("spam.scss") == ["ham.scss"]
253244

254-
# Dependency the refers to non-existing file were removed.
245+
# The Dependency that refers to non-existing file was removed.
255246
assert list(models.Dependency.objects.all()) == [dependency_1]
256247

257248

0 commit comments

Comments
 (0)