Skip to content
Myr

Merge pull request #1442 from dannysepler/use-pathlib-in-places

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Use pathlib in some test places

authored by David Lord · committed by GitHub · parents 9f5db9a, 06c646d · Browse files · 4 files+27 added−46 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+27 added−46 removed

  • tests:+27 added−46 removed

4 files+27 added−46 removed

modified:tests/conftest.py+3 added-3 removed
1import os1from pathlib import Path23import pytest45from jinja2 import loaders
20 unmodified lines
2627@pytest.fixture28def filesystem_loader():29 """returns FileSystemLoader initialized to res/templates directory"""30 here = os.path.dirname(os.path.abspath(__file__))31 return loaders.FileSystemLoader(here + "/res/templates")30 here = Path(__file__).parent.resolve()31 return loaders.FileSystemLoader(here / "res" / "templates")323334@pytest.fixture35def function_loader():
14 unmodified lines
modified:tests/test_api.py+4 added-5 removed
1import os1import shutil2import tempfile3from pathlib import Path45import pytest67from jinja2 import ChainableUndefined
233 unmodified lines
241 stream.disable_buffering()242 assert not stream.buffered243244 def test_dump_stream(self, env):245 tmp = tempfile.mkdtemp()245 tmp = Path(tempfile.mkdtemp())246 try:247 tmpl = env.from_string("\u2713")248 stream = tmpl.stream()249 stream.dump(os.path.join(tmp, "dump.txt"), "utf-8")250 with open(os.path.join(tmp, "dump.txt"), "rb") as f:251 assert f.read() == b"\xe2\x9c\x93"249 stream.dump(str(tmp / "dump.txt"), "utf-8")250 assert (tmp / "dump.txt").read_bytes() == b"\xe2\x9c\x93"251 finally:252 shutil.rmtree(tmp)253254
179 unmodified lines
modified:tests/test_loader.py+14 added-27 removed
6 unmodified lines
7import sys8import tempfile9import time10import weakref11from pathlib import Path1213import pytest1415from jinja2 import Environment
16 unmodified lines
32 assert tmpl.render().strip() == "BAR"33 pytest.raises(TemplateNotFound, env.get_template, "missing.html")3435 def test_filesystem_loader_overlapping_names(self, filesystem_loader):35 res = os.path.dirname(filesystem_loader.searchpath[0])36 t2_dir = os.path.join(res, "templates2")36 t2_dir = Path(filesystem_loader.searchpath[0]) / ".." / "templates2"37 # Make "foo" show up before "foo/test.html".38 filesystem_loader.searchpath.insert(0, t2_dir)39 e = Environment(loader=filesystem_loader)40 e.get_template("foo")
76 unmodified lines
117 pytest.raises(TemplateNotFound, split_template_path, "../foo")118119120class TestFileSystemLoader:121 searchpath = os.path.join(122 os.path.dirname(os.path.abspath(__file__)), "res", "templates"123 )121 searchpath = (Path(__file__) / ".." / "res" / "templates").resolve()122123 @staticmethod124 def _test_common(env):125 tmpl = env.get_template("test.html")
2 unmodified lines
128 assert tmpl.render().strip() == "FOO"129 pytest.raises(TemplateNotFound, env.get_template, "missing.html")130131 def test_searchpath_as_str(self):134 filesystem_loader = loaders.FileSystemLoader(self.searchpath)132 filesystem_loader = loaders.FileSystemLoader(str(self.searchpath))133134 env = Environment(loader=filesystem_loader)135 self._test_common(env)136137 def test_searchpath_as_pathlib(self):140 import pathlib141142 searchpath = pathlib.Path(self.searchpath)143 filesystem_loader = loaders.FileSystemLoader(searchpath)138 filesystem_loader = loaders.FileSystemLoader(self.searchpath)139 env = Environment(loader=filesystem_loader)140 self._test_common(env)141142 def test_searchpath_as_list_including_pathlib(self):148 import pathlib149150 searchpath = pathlib.Path(self.searchpath)151 filesystem_loader = loaders.FileSystemLoader(["/tmp/templates", searchpath])143 filesystem_loader = loaders.FileSystemLoader(144 ["/tmp/templates", self.searchpath]145 )146 env = Environment(loader=filesystem_loader)147 self._test_common(env)148149 def test_caches_template_based_on_mtime(self):
3 unmodified lines
153 tmpl1 = env.get_template("test.html")154 tmpl2 = env.get_template("test.html")155 assert tmpl1 is tmpl2156163 os.utime(os.path.join(self.searchpath, "test.html"), (time.time(), time.time()))157 os.utime(self.searchpath / "test.html", (time.time(), time.time()))158 tmpl3 = env.get_template("test.html")159 assert tmpl1 is not tmpl3160161 @pytest.mark.parametrize(
113 unmodified lines
275 def test_path_as_pathlib(self, prefix_loader):276 self.compile_down(prefix_loader)277278 mod_path = self.mod_env.loader.module.__path__[0]285286 import pathlib287288 mod_loader = loaders.ModuleLoader(pathlib.Path(mod_path))279 mod_loader = loaders.ModuleLoader(Path(mod_path))280 self.mod_env = Environment(loader=mod_loader)281282 self._test_common()283284 def test_supports_pathlib_in_list_of_paths(self, prefix_loader):285 self.compile_down(prefix_loader)286287 mod_path = self.mod_env.loader.module.__path__[0]297298 import pathlib299300 mod_loader = loaders.ModuleLoader([pathlib.Path(mod_path), "/tmp/templates"])288 mod_loader = loaders.ModuleLoader([Path(mod_path), "/tmp/templates"])289 self.mod_env = Environment(loader=mod_loader)290291 self._test_common()292293294@pytest.fixture()295def package_dir_loader(monkeypatch):308 monkeypatch.syspath_prepend(os.path.dirname(__file__))296 monkeypatch.syspath_prepend(Path(__file__).parent)297 return PackageLoader("res")298299300@pytest.mark.parametrize(
13 unmodified lines
314315316@pytest.fixture()317def package_zip_loader(monkeypatch):330 monkeypatch.syspath_prepend(331 os.path.join(os.path.dirname(__file__), "res", "package.zip")332 )318 package_zip = (Path(__file__) / ".." / "res" / "package.zip").resolve()319 monkeypatch.syspath_prepend(package_zip)320 return PackageLoader("t_pack")321322323@pytest.mark.parametrize(
47 unmodified lines
modified:tests/test_utils.py+6 added-11 removed
21 unmodified lines
22 d["b"] = 223 d["c"] = 324 d["a"]25 d["d"] = 426 assert len(d) == 327 assert "a" in d and "c" in d and "d" in d and "b" not in d26 assert d.keys() == ["d", "a", "c"]2729 def test_itervalues(self):28 def test_values(self):29 cache = LRUCache(3)30 cache["b"] = 131 cache["a"] = 233 values = [v for v in cache.values()]34 assert len(values) == 235 assert 1 in values36 assert 2 in values32 assert cache.values() == [2, 1]3338 def test_itervalues_empty(self):34 def test_values_empty(self):35 cache = LRUCache(2)40 values = [v for v in cache.values()]41 assert len(values) == 036 assert cache.values() == []3738 def test_pickleable(self):39 cache = LRUCache(2)40 cache["foo"] = 42
14 unmodified lines
55 copy = copy_func(cache)56 assert copy._queue == cache._queue57 copy["c"] = 358 assert copy._queue != cache._queue64 assert "a" not in copy and "b" in copy and "c" in copy59 assert copy.keys() == ["c", "b"]6061 def test_clear(self):62 d = LRUCache(3)63 d["a"] = 1
122 unmodified lines