Skip to content

Commit d9f6b97

Browse files
authored
fixed options + tests for that. (#74)
Signed-off-by: Alexander Piskun <[email protected]>
1 parent 81fa4c7 commit d9f6b97

File tree

7 files changed

+74
-4
lines changed

7 files changed

+74
-4
lines changed

.github/workflows/analysis-coverage.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,9 @@ jobs:
722722
- name: Generate coverage report
723723
working-directory: nc_py_api
724724
run: coverage run --data-file=.coverage.ci -m pytest && coverage combine && coverage xml && coverage html
725+
env:
726+
NPA_TIMEOUT: None
727+
NPA_TIMEOUT_DAV: None
725728

726729
- name: HTML coverage to artifacts
727730
uses: actions/upload-artifact@v3

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.0.29 - 2023-08-14]
6+
7+
### Fixed
8+
9+
- `options` error when setting timeouts with the `.env` file.
10+
511
## [0.0.28 - 2023-08-11]
612

713
### Added

docs/Options.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ With .env
2525

2626
Place **.env** file in your project's directory, and it will be automatically loaded using `dotenv <https://github.com/theskumar/python-dotenv>`_
2727

28+
`Loading occurs only once, when "nc_py_api" is imported into the Python interpreter.`
29+
2830
Modifying at module level
2931
"""""""""""""""""""""""""
3032

nc_py_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""All possible stuff for Nextcloud & NextcloudApp that can be used."""
22

3-
from . import ex_app
3+
from . import ex_app, options
44
from ._exceptions import NextcloudException, NextcloudExceptionNotFound
55
from ._version import __version__
66
from .files import FsNode

nc_py_api/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Version of nc_py_api."""
22

3-
__version__ = "0.0.28"
3+
__version__ = "0.0.29.dev0"

nc_py_api/options.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Each setting only affects newly created instances of Nextcloud or NextcloudApp class, unless otherwise specified.
44
Specifying options in **kwargs** has higher priority than this.
55
"""
6+
import typing
67
from os import environ
78

89
from dotenv import load_dotenv
@@ -12,11 +13,19 @@
1213
XDEBUG_SESSION = environ.get("XDEBUG_SESSION", "")
1314
"""Dev option, for debugging PHP code."""
1415

15-
NPA_TIMEOUT = environ.get("NPA_TIMEOUT", 50)
16+
NPA_TIMEOUT: typing.Optional[int]
1617
"""Default timeout for OCS API calls. Set to ``None`` to disable timeouts for development."""
18+
try:
19+
NPA_TIMEOUT = int(environ.get("NPA_TIMEOUT", 30))
20+
except (TypeError, ValueError):
21+
NPA_TIMEOUT = None
1722

18-
NPA_TIMEOUT_DAV = environ.get("NPA_TIMEOUT_DAV", NPA_TIMEOUT * 3 if isinstance(NPA_TIMEOUT, int) else None)
23+
NPA_TIMEOUT_DAV: typing.Optional[int]
1924
"""File operations timeout, usually it is OCS timeout multiplied by 3."""
25+
try:
26+
NPA_TIMEOUT_DAV = int(environ.get("NPA_TIMEOUT_DAV", 30 * 3))
27+
except (TypeError, ValueError):
28+
NPA_TIMEOUT_DAV = None
2029

2130
NPA_NC_CERT = environ.get("NPA_NC_CERT", True)
2231
"""Option to enable/disable Nextcloud certificate verification.

tests/options_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import sys
3+
from subprocess import PIPE, run
4+
5+
import pytest
6+
from gfixture import NC_TO_TEST
7+
8+
import nc_py_api
9+
10+
11+
def test_timeouts():
12+
project_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
13+
env_file = os.path.join(project_dir, ".env")
14+
env_backup_file = os.path.join(project_dir, ".env.backup")
15+
if os.path.exists(env_file):
16+
os.rename(env_file, env_backup_file)
17+
try:
18+
check_command = [sys.executable, "-c", "import nc_py_api\nassert nc_py_api.options.NPA_TIMEOUT is None"]
19+
with open(env_file, "w") as env_f:
20+
env_f.write("NPA_TIMEOUT=None")
21+
r = run(check_command, stderr=PIPE, env={}, cwd=project_dir, check=False)
22+
assert not r.stderr
23+
check_command = [sys.executable, "-c", "import nc_py_api\nassert nc_py_api.options.NPA_TIMEOUT == 11"]
24+
with open(env_file, "w") as env_f:
25+
env_f.write("NPA_TIMEOUT=11")
26+
r = run(check_command, stderr=PIPE, env={}, cwd=project_dir, check=False)
27+
assert not r.stderr
28+
check_command = [sys.executable, "-c", "import nc_py_api\nassert nc_py_api.options.NPA_TIMEOUT_DAV is None"]
29+
with open(env_file, "w") as env_f:
30+
env_f.write("NPA_TIMEOUT_DAV=None")
31+
r = run(check_command, stderr=PIPE, env={}, cwd=project_dir, check=False)
32+
assert not r.stderr
33+
check_command = [sys.executable, "-c", "import nc_py_api\nassert nc_py_api.options.NPA_TIMEOUT_DAV == 11"]
34+
with open(env_file, "w") as env_f:
35+
env_f.write("NPA_TIMEOUT_DAV=11")
36+
r = run(check_command, stderr=PIPE, env={}, cwd=project_dir, check=False)
37+
assert not r.stderr
38+
finally:
39+
if os.path.exists(env_backup_file):
40+
os.rename(env_backup_file, env_file)
41+
42+
43+
@pytest.mark.skipif(not NC_TO_TEST, reason="Need Nextcloud or NextcloudApp.")
44+
def test_xdebug_session():
45+
nc_py_api.options.XDEBUG_SESSION = "12345"
46+
if isinstance(NC_TO_TEST, nc_py_api.Nextcloud):
47+
new_nc = nc_py_api.Nextcloud()
48+
else:
49+
new_nc = nc_py_api.NextcloudApp()
50+
assert new_nc._session.adapter.cookies["XDEBUG_SESSION"] == "12345"

0 commit comments

Comments
 (0)