Use pathlib in some test places
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Danny Sepler · committed by David Lord · parent 9f5db9a · 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
Provenance
- Commit
06c646d- Tree
33c6438- Author
Danny Sepler <dannysepler@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
9f5db9a
4 files changed+27 added−46 removed
modified:tests/conftest.py+3 added-3 removed
1-import os1+from pathlib import Path2 3 import pytest4 5 from jinja2 import loaders20 unmodified lines26 27 @pytest.fixture28 def 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")32 33 34 @pytest.fixture35 def function_loader():14 unmodified linesmodified:tests/test_api.py+4 added-5 removed
1-import os1 import shutil2 import tempfile3+from pathlib import Path4 5 import pytest6 7 from jinja2 import ChainableUndefined233 unmodified lines241 stream.disable_buffering()242 assert not stream.buffered243 244 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)253 254 179 unmodified linesmodified:tests/test_loader.py+14 added-27 removed
6 unmodified lines7 import sys8 import tempfile9 import time10 import weakref11+from pathlib import Path12 13 import pytest14 15 from jinja2 import Environment16 unmodified lines32 assert tmpl.render().strip() == "BAR"33 pytest.raises(TemplateNotFound, env.get_template, "missing.html")34 35 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 lines117 pytest.raises(TemplateNotFound, split_template_path, "../foo")118 119 120 class TestFileSystemLoader:121- searchpath = os.path.join(122- os.path.dirname(os.path.abspath(__file__)), "res", "templates"123- )121+ searchpath = (Path(__file__) / ".." / "res" / "templates").resolve()122 123 @staticmethod124 def _test_common(env):125 tmpl = env.get_template("test.html")2 unmodified lines128 assert tmpl.render().strip() == "FOO"129 pytest.raises(TemplateNotFound, env.get_template, "missing.html")130 131 def test_searchpath_as_str(self):134- filesystem_loader = loaders.FileSystemLoader(self.searchpath)132+ filesystem_loader = loaders.FileSystemLoader(str(self.searchpath))133 134 env = Environment(loader=filesystem_loader)135 self._test_common(env)136 137 def test_searchpath_as_pathlib(self):140- import pathlib141-142- 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)141 142 def test_searchpath_as_list_including_pathlib(self):148- import pathlib149-150- 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)148 149 def test_caches_template_based_on_mtime(self):3 unmodified lines153 tmpl1 = env.get_template("test.html")154 tmpl2 = env.get_template("test.html")155 assert tmpl1 is tmpl2156 163- 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 tmpl3160 161 @pytest.mark.parametrize(113 unmodified lines275 def test_path_as_pathlib(self, prefix_loader):276 self.compile_down(prefix_loader)277 278 mod_path = self.mod_env.loader.module.__path__[0]285-286- import pathlib287-288- mod_loader = loaders.ModuleLoader(pathlib.Path(mod_path))279+ mod_loader = loaders.ModuleLoader(Path(mod_path))280 self.mod_env = Environment(loader=mod_loader)281 282 self._test_common()283 284 def test_supports_pathlib_in_list_of_paths(self, prefix_loader):285 self.compile_down(prefix_loader)286 287 mod_path = self.mod_env.loader.module.__path__[0]297-298- import pathlib299-300- 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)290 291 self._test_common()292 293 294 @pytest.fixture()295 def package_dir_loader(monkeypatch):308- monkeypatch.syspath_prepend(os.path.dirname(__file__))296+ monkeypatch.syspath_prepend(Path(__file__).parent)297 return PackageLoader("res")298 299 300 @pytest.mark.parametrize(13 unmodified lines314 315 316 @pytest.fixture()317 def 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")321 322 323 @pytest.mark.parametrize(47 unmodified linesmodified:tests/test_utils.py+6 added-11 removed
21 unmodified lines22 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"]27 29- 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]33 38- 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() == []37 38 def test_pickleable(self):39 cache = LRUCache(2)40 cache["foo"] = 4214 unmodified lines55 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"]60 61 def test_clear(self):62 d = LRUCache(3)63 d["a"] = 1122 unmodified lines