Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pathlib in some test places #1442

Merged
merged 1 commit into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/conftest.py
@@ -1,4 +1,4 @@
import os
from pathlib import Path

import pytest

Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions tests/test_api.py
@@ -1,6 +1,6 @@
import os
import shutil
import tempfile
from pathlib import Path

import pytest

Expand Down Expand Up @@ -242,13 +242,12 @@ def test_streaming_behavior(self, env):
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)

Expand Down
41 changes: 14 additions & 27 deletions tests/test_loader.py
Expand Up @@ -8,6 +8,7 @@
import tempfile
import time
import weakref
from pathlib import Path

import pytest

Expand All @@ -32,8 +33,7 @@ def test_package_loader(self, package_loader):
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)
Expand Down Expand Up @@ -118,9 +118,7 @@ def test_split_template_path(self):


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):
Expand All @@ -131,24 +129,20 @@ def _test_common(env):
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)

Expand All @@ -160,7 +154,7 @@ def test_caches_template_based_on_mtime(self):
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

Expand Down Expand Up @@ -282,10 +276,7 @@ def test_path_as_pathlib(self, prefix_loader):
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()
Expand All @@ -294,18 +285,15 @@ def test_supports_pathlib_in_list_of_paths(self, prefix_loader):
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()


@pytest.fixture()
def package_dir_loader(monkeypatch):
monkeypatch.syspath_prepend(os.path.dirname(__file__))
monkeypatch.syspath_prepend(Path(__file__).parent)
return PackageLoader("res")


Expand All @@ -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")


Expand Down
17 changes: 6 additions & 11 deletions tests/test_utils.py
Expand Up @@ -23,22 +23,17 @@ def test_simple(self):
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):
davidism marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -61,7 +56,7 @@ def test_copy(self, copy_func):
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)
Expand Down