Skip to content

Commit

Permalink
Fix test on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Aug 31, 2023
1 parent 92fc197 commit 2c70bd5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
11 changes: 6 additions & 5 deletions tests/unit/test_config.py
@@ -1,5 +1,6 @@
import os
import sys
from pathlib import Path
from unittest import mock

import pytest
Expand All @@ -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",
]

Expand Down
24 changes: 18 additions & 6 deletions 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

Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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):
Expand Down
9 changes: 2 additions & 7 deletions 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
Expand Down

0 comments on commit 2c70bd5

Please sign in to comment.