summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDanny Sepler <dannysepler@gmail.com>2021-05-16 23:45:06 -0400
committerDavid Lord <davidism@gmail.com>2021-05-18 10:30:11 -0700
commit06c646df2d7bde401c5a4bfc73794e74d612beab (patch)
tree33c64389c42a94694dba9e582a319af20ccca16e /tests
parent9f5db9a6d904a26f1dab777427b904a85d418e7a (diff)
downloadjinja2-06c646df2d7bde401c5a4bfc73794e74d612beab.tar.gz
Use pathlib in some test places
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py6
-rw-r--r--tests/test_api.py9
-rw-r--r--tests/test_loader.py41
-rw-r--r--tests/test_utils.py17
4 files changed, 27 insertions, 46 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index ddcacc2..e225ab9 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,4 +1,4 @@
-import os
+from pathlib import Path
import pytest
@@ -27,8 +27,8 @@ def package_loader():
@pytest.fixture
def filesystem_loader():
"""returns FileSystemLoader initialized to res/templates directory"""
- here = os.path.dirname(os.path.abspath(__file__))
- return loaders.FileSystemLoader(here + "/res/templates")
+ here = Path(__file__).parent.resolve()
+ return loaders.FileSystemLoader(here / "res" / "templates")
@pytest.fixture
diff --git a/tests/test_api.py b/tests/test_api.py
index eda04a9..774bb3c 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1,6 +1,6 @@
-import os
import shutil
import tempfile
+from pathlib import Path
import pytest
@@ -242,13 +242,12 @@ class TestStreaming:
assert not stream.buffered
def test_dump_stream(self, env):
- tmp = tempfile.mkdtemp()
+ tmp = Path(tempfile.mkdtemp())
try:
tmpl = env.from_string("\u2713")
stream = tmpl.stream()
- stream.dump(os.path.join(tmp, "dump.txt"), "utf-8")
- with open(os.path.join(tmp, "dump.txt"), "rb") as f:
- assert f.read() == b"\xe2\x9c\x93"
+ stream.dump(str(tmp / "dump.txt"), "utf-8")
+ assert (tmp / "dump.txt").read_bytes() == b"\xe2\x9c\x93"
finally:
shutil.rmtree(tmp)
diff --git a/tests/test_loader.py b/tests/test_loader.py
index 8ca1289..63fc39b 100644
--- a/tests/test_loader.py
+++ b/tests/test_loader.py
@@ -8,6 +8,7 @@ import sys
import tempfile
import time
import weakref
+from pathlib import Path
import pytest
@@ -32,8 +33,7 @@ class TestLoaders:
pytest.raises(TemplateNotFound, env.get_template, "missing.html")
def test_filesystem_loader_overlapping_names(self, filesystem_loader):
- res = os.path.dirname(filesystem_loader.searchpath[0])
- t2_dir = os.path.join(res, "templates2")
+ t2_dir = Path(filesystem_loader.searchpath[0]) / ".." / "templates2"
# Make "foo" show up before "foo/test.html".
filesystem_loader.searchpath.insert(0, t2_dir)
e = Environment(loader=filesystem_loader)
@@ -118,9 +118,7 @@ class TestLoaders:
class TestFileSystemLoader:
- searchpath = os.path.join(
- os.path.dirname(os.path.abspath(__file__)), "res", "templates"
- )
+ searchpath = (Path(__file__) / ".." / "res" / "templates").resolve()
@staticmethod
def _test_common(env):
@@ -131,24 +129,20 @@ class TestFileSystemLoader:
pytest.raises(TemplateNotFound, env.get_template, "missing.html")
def test_searchpath_as_str(self):
- filesystem_loader = loaders.FileSystemLoader(self.searchpath)
+ filesystem_loader = loaders.FileSystemLoader(str(self.searchpath))
env = Environment(loader=filesystem_loader)
self._test_common(env)
def test_searchpath_as_pathlib(self):
- import pathlib
-
- searchpath = pathlib.Path(self.searchpath)
- filesystem_loader = loaders.FileSystemLoader(searchpath)
+ filesystem_loader = loaders.FileSystemLoader(self.searchpath)
env = Environment(loader=filesystem_loader)
self._test_common(env)
def test_searchpath_as_list_including_pathlib(self):
- import pathlib
-
- searchpath = pathlib.Path(self.searchpath)
- filesystem_loader = loaders.FileSystemLoader(["/tmp/templates", searchpath])
+ filesystem_loader = loaders.FileSystemLoader(
+ ["/tmp/templates", self.searchpath]
+ )
env = Environment(loader=filesystem_loader)
self._test_common(env)
@@ -160,7 +154,7 @@ class TestFileSystemLoader:
tmpl2 = env.get_template("test.html")
assert tmpl1 is tmpl2
- os.utime(os.path.join(self.searchpath, "test.html"), (time.time(), time.time()))
+ os.utime(self.searchpath / "test.html", (time.time(), time.time()))
tmpl3 = env.get_template("test.html")
assert tmpl1 is not tmpl3
@@ -282,10 +276,7 @@ class TestModuleLoader:
self.compile_down(prefix_loader)
mod_path = self.mod_env.loader.module.__path__[0]
-
- import pathlib
-
- mod_loader = loaders.ModuleLoader(pathlib.Path(mod_path))
+ mod_loader = loaders.ModuleLoader(Path(mod_path))
self.mod_env = Environment(loader=mod_loader)
self._test_common()
@@ -294,10 +285,7 @@ class TestModuleLoader:
self.compile_down(prefix_loader)
mod_path = self.mod_env.loader.module.__path__[0]
-
- import pathlib
-
- mod_loader = loaders.ModuleLoader([pathlib.Path(mod_path), "/tmp/templates"])
+ mod_loader = loaders.ModuleLoader([Path(mod_path), "/tmp/templates"])
self.mod_env = Environment(loader=mod_loader)
self._test_common()
@@ -305,7 +293,7 @@ class TestModuleLoader:
@pytest.fixture()
def package_dir_loader(monkeypatch):
- monkeypatch.syspath_prepend(os.path.dirname(__file__))
+ monkeypatch.syspath_prepend(Path(__file__).parent)
return PackageLoader("res")
@@ -327,9 +315,8 @@ def test_package_dir_list(package_dir_loader):
@pytest.fixture()
def package_zip_loader(monkeypatch):
- monkeypatch.syspath_prepend(
- os.path.join(os.path.dirname(__file__), "res", "package.zip")
- )
+ package_zip = (Path(__file__) / ".." / "res" / "package.zip").resolve()
+ monkeypatch.syspath_prepend(package_zip)
return PackageLoader("t_pack")
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 3e24166..7b58af1 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -23,22 +23,17 @@ class TestLRUCache:
d["c"] = 3
d["a"]
d["d"] = 4
- assert len(d) == 3
- assert "a" in d and "c" in d and "d" in d and "b" not in d
+ assert d.keys() == ["d", "a", "c"]
- def test_itervalues(self):
+ def test_values(self):
cache = LRUCache(3)
cache["b"] = 1
cache["a"] = 2
- values = [v for v in cache.values()]
- assert len(values) == 2
- assert 1 in values
- assert 2 in values
+ assert cache.values() == [2, 1]
- def test_itervalues_empty(self):
+ def test_values_empty(self):
cache = LRUCache(2)
- values = [v for v in cache.values()]
- assert len(values) == 0
+ assert cache.values() == []
def test_pickleable(self):
cache = LRUCache(2)
@@ -61,7 +56,7 @@ class TestLRUCache:
assert copy._queue == cache._queue
copy["c"] = 3
assert copy._queue != cache._queue
- assert "a" not in copy and "b" in copy and "c" in copy
+ assert copy.keys() == ["c", "b"]
def test_clear(self):
d = LRUCache(3)