|
| 1 | +import inspect |
| 2 | +from os import environ |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +proj_root = Path(__file__).parent.parent.parent.parent |
| 8 | + |
| 9 | + |
| 10 | +# test environment variables |
| 11 | + |
| 12 | + |
| 13 | +def test_environment(): |
| 14 | + assert environ.get("GITHUB_TOKEN") |
| 15 | + assert Path(environ.get("MODFLOW6_PATH", proj_root / "modflow6")).is_dir() |
| 16 | + |
| 17 | + |
| 18 | +# temporary directory fixtures |
| 19 | + |
| 20 | + |
| 21 | +def test_tmpdirs(tmpdir, module_tmpdir): |
| 22 | + # function-scoped temporary directory |
| 23 | + assert isinstance(tmpdir, Path) |
| 24 | + assert tmpdir.is_dir() |
| 25 | + assert inspect.currentframe().f_code.co_name in tmpdir.stem |
| 26 | + |
| 27 | + # module-scoped temp dir (accessible to other tests in the script) |
| 28 | + assert module_tmpdir.is_dir() |
| 29 | + assert "test" in module_tmpdir.stem |
| 30 | + |
| 31 | + |
| 32 | +def test_function_scoped_tmpdir(tmpdir): |
| 33 | + assert isinstance(tmpdir, Path) |
| 34 | + assert tmpdir.is_dir() |
| 35 | + assert inspect.currentframe().f_code.co_name in tmpdir.stem |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("name", ["noslash", "forward/slash", "back\\slash"]) |
| 39 | +def test_function_scoped_tmpdir_slash_in_name(tmpdir, name): |
| 40 | + assert isinstance(tmpdir, Path) |
| 41 | + assert tmpdir.is_dir() |
| 42 | + |
| 43 | + # node name might have slashes if test function is parametrized |
| 44 | + # (e.g., test_function_scoped_tmpdir_slash_in_name[a/slash]) |
| 45 | + replaced1 = name.replace("/", "_").replace("\\", "_").replace(":", "_") |
| 46 | + replaced2 = name.replace("/", "_").replace("\\", "__").replace(":", "_") |
| 47 | + assert ( |
| 48 | + f"{inspect.currentframe().f_code.co_name}[{replaced1}]" in tmpdir.stem |
| 49 | + or f"{inspect.currentframe().f_code.co_name}[{replaced2}]" |
| 50 | + in tmpdir.stem |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class TestClassScopedTmpdir: |
| 55 | + filename = "hello.txt" |
| 56 | + |
| 57 | + @pytest.fixture(autouse=True) |
| 58 | + def setup(self, class_tmpdir): |
| 59 | + with open(class_tmpdir / self.filename, "w") as file: |
| 60 | + file.write("hello, class-scoped tmpdir") |
| 61 | + |
| 62 | + def test_class_scoped_tmpdir(self, class_tmpdir): |
| 63 | + assert isinstance(class_tmpdir, Path) |
| 64 | + assert class_tmpdir.is_dir() |
| 65 | + assert self.__class__.__name__ in class_tmpdir.stem |
| 66 | + assert Path(class_tmpdir / self.filename).is_file() |
| 67 | + |
| 68 | + |
| 69 | +def test_module_scoped_tmpdir(module_tmpdir): |
| 70 | + assert isinstance(module_tmpdir, Path) |
| 71 | + assert module_tmpdir.is_dir() |
| 72 | + assert Path(inspect.getmodulename(__file__)).stem in module_tmpdir.name |
| 73 | + |
| 74 | + |
| 75 | +def test_session_scoped_tmpdir(session_tmpdir): |
| 76 | + assert isinstance(session_tmpdir, Path) |
| 77 | + assert session_tmpdir.is_dir() |
| 78 | + |
| 79 | + |
| 80 | +# test misc fixtures |
| 81 | + |
| 82 | + |
| 83 | +def test_github_api(gh_api): |
| 84 | + assert gh_api.get_user().login |
| 85 | + |
| 86 | + |
| 87 | +def test_modflow6_path(modflow6_path): |
| 88 | + assert modflow6_path.is_dir() |
| 89 | + assert (modflow6_path / "version.txt").is_file() |
0 commit comments