diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index a08369e9c..1ef38b4fb 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -1,5 +1,6 @@ import os import sys +from pathlib import Path from unittest import mock import pytest @@ -20,13 +21,13 @@ def _assert_config_read(environment, mock_config): del os.environ[env_name] os.environ[environment] = "/MOCK" - module_dir = os.path.dirname(sys.modules["praw"].__file__) - environ_path = os.path.join( - "/MOCK", ".config" if environment == "HOME" else "", "praw.ini" + module_dir = Path(sys.modules["praw"].__file__).parent + environ_path = ( + Path("/MOCK") / (".config" if environment == "HOME" else "") / "praw.ini" ) locations = [ - os.path.join(module_dir, "praw.ini"), - environ_path, + str(module_dir / "praw.ini"), + str(environ_path), "praw.ini", ] diff --git a/tests/unit/util/test_token_manager.py b/tests/unit/util/test_token_manager.py index 2423ef205..35c5088a3 100644 --- a/tests/unit/util/test_token_manager.py +++ b/tests/unit/util/test_token_manager.py @@ -1,5 +1,6 @@ """Test praw.util.refresh_token_manager.""" import sys +from pathlib import Path from tempfile import NamedTemporaryFile from unittest import mock @@ -15,6 +16,18 @@ from ..test_reddit import DummyTokenManager +def mock_path(read_data=None): + if read_data is not None: + mock_open = mock.mock_open(read_data=read_data) + else: + mock_open = mock.mock_open() + + def mocked_open(self, *args, **kwargs): + return mock_open(self, *args, **kwargs) + + return mock.patch.object(Path, "open", mocked_open), mock_open + + class DummyAuthorizer: def __init__(self, refresh_token): self.refresh_token = refresh_token @@ -45,9 +58,9 @@ class TestFileTokenManager(UnitTest): def test_post_refresh_token_callback__writes_to_file(self): authorizer = DummyAuthorizer("token_value") manager = FileTokenManager("mock/dummy_path") - mock_open = mock.mock_open() + mock_patch, mock_open = mock_path() - with mock.patch("io.open", mock_open): + with mock_patch: manager.post_refresh_callback(authorizer) assert authorizer.refresh_token == "token_value" @@ -60,16 +73,15 @@ def test_post_refresh_token_callback__writes_to_file(self): def test_pre_refresh_token_callback__reads_from_file(self): authorizer = DummyAuthorizer(None) manager = FileTokenManager("mock/dummy_path") - mock_open = mock.mock_open(read_data="token_value") + mock_patch, mock_open = mock_path(read_data="token_value") - with mock.patch("io.open", mock_open): + with mock_patch: manager.pre_refresh_callback(authorizer) assert authorizer.refresh_token == "token_value" call = mock_open.mock_calls[0] - path, mode, *_ = call.args + path, *_ = call.args assert path.match("mock/dummy_path") - assert mode == "r" class TestSQLiteTokenManager(UnitTest): diff --git a/tox.ini b/tox.ini index d16a253bb..f5978b1fb 100644 --- a/tox.ini +++ b/tox.ini @@ -1,17 +1,12 @@ [tox] -envlist = py35,py36,py37,py38 -skip_missing_interpreters = true +envlist = py37,py38,py39,py310,py311 skipsdist = true [testenv] deps = - betamax >=0.8, <0.9 - betamax-matchers >=0.3.0, <0.5 - pytest >=2.7.3 - flake8 + .[test] commands = pytest - flake8 --exclude=.eggs,build,docs passenv = prawtest_client_id prawtest_client_secret