Skip to content

Commit 974760a

Browse files
d-w-moorekorydraughn
authored andcommitted
[#735] remove dependency on test folder
Moved function definitions for make_session and home_collection to irods.helpers module. Made irods.test.helpers versions of those functions import and/or derive from those definitions.
1 parent 80e9128 commit 974760a

2 files changed

Lines changed: 76 additions & 72 deletions

File tree

irods/helpers/__init__.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,66 @@
11
import contextlib
2-
import re
3-
from ..test.helpers import home_collection, make_session as make_test_session
4-
from irods.message import ET, XML_Parser_Type
2+
import sys
3+
from irods import env_filename_from_keyword_args
4+
from irods.message import ET, XML_Parser_Type, IRODS_VERSION
5+
from irods.session import iRODSSession
6+
57

68
__all__ = ["make_session", "home_collection", "xml_mode"]
79

810

11+
class StopTestsException(Exception):
12+
13+
def __init__(self, *args, **kwargs):
14+
super().__init__(*args, **kwargs)
15+
if "unittest" in sys.modules.keys():
16+
print("Aborting tests [ Got : %r ]" % self, file=sys.stderr)
17+
os.abort()
18+
19+
class iRODS_Server_Too_Recent_For_Testing(StopTestsException):
20+
pass
21+
22+
23+
def _get_server_version_for_test(session, curtail_length):
24+
return session._server_version(session.GET_SERVER_VERSION_WITHOUT_AUTH)[
25+
:curtail_length
26+
]
27+
28+
29+
# Create a connection for test, based on ~/.irods environment by default.
30+
931
def make_session(test_server_version=False, **kwargs):
10-
return make_test_session(test_server_version=test_server_version, **kwargs)
32+
"""Connect to an iRODS server as determined by any client environment
33+
file present at a standard location, and by any keyword arguments given.
34+
35+
Arguments:
36+
37+
test_server_version: Of type bool; in the `irods.test.helpers` version of this
38+
function, defaults to True. A True value causes
39+
*iRODS_Server_Too_Recent* to be raised if the server
40+
connected to is more recent than the current Python iRODS
41+
client's advertised level of compatibility.
42+
43+
**kwargs: Keyword arguments. Fed directly to the iRODSSession
44+
constructor."""
45+
46+
env_file = env_filename_from_keyword_args(kwargs)
47+
session = iRODSSession(irods_env_file=env_file, **kwargs)
48+
if test_server_version:
49+
connected_version = _get_server_version_for_test(session, curtail_length=3)
50+
advertised_version = IRODS_VERSION[:3]
51+
if connected_version > advertised_version:
52+
msg = (
53+
"Connected server is {connected_version}, "
54+
"but this python-irodsclient advertises compatibility up to {advertised_version}."
55+
).format(**locals())
56+
raise iRODS_Server_Too_Recent_For_Testing(msg)
57+
58+
return session
1159

1260

13-
make_session.__doc__ = re.sub(
14-
r"(test_server_version\s*)=\s*\w+", r"\1 = False", make_test_session.__doc__
15-
)
61+
def home_collection(session):
62+
"""Return a string value for the given session's home collection."""
63+
return "/{0.zone}/home/{0.username}".format(session)
1664

1765

1866
@contextlib.contextmanager

irods/test/helpers.py

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
import os
2-
import io
3-
import tempfile
1+
import base64
42
import contextlib
5-
import shutil
3+
import io
4+
import datetime
65
import hashlib
7-
import base64
6+
import inspect
7+
import json
88
import logging
99
import math
10+
import os
11+
import shutil
1012
import socket
11-
import inspect
12-
import threading
1313
import random
14-
import datetime
15-
import json
14+
import re
1615
import sys
16+
import tempfile
17+
import threading
18+
1719
import irods.client_configuration as config
18-
import irods.rule
19-
from irods.session import iRODSSession
20+
from irods.helpers import (
21+
home_collection,
22+
make_session as _irods_helpers_make_session)
2023
from irods.message import iRODSMessage, IRODS_VERSION
2124
from irods.password_obfuscation import encode
22-
from irods import env_filename_from_keyword_args
25+
import irods.rule
26+
from irods.session import iRODSSession
2327

2428

2529
class iRODSUserLogins:
@@ -76,19 +80,6 @@ def configuration_file_exists():
7680
return True
7781

7882

79-
class StopTestsException(Exception):
80-
81-
def __init__(self, *args, **kwargs):
82-
super(StopTestsException, self).__init__(*args, **kwargs)
83-
if "unittest" in sys.modules.keys():
84-
print("Aborting tests [ Got : %r ]" % self, file=sys.stderr)
85-
os.abort()
86-
87-
88-
class iRODS_Server_Too_Recent(StopTestsException):
89-
pass
90-
91-
9283
def my_function_name():
9384
"""Returns the name of the calling function or method"""
9485
return inspect.getframeinfo(inspect.currentframe().f_back).function
@@ -172,48 +163,13 @@ def recast(k):
172163
return (config, auth)
173164

174165

175-
def get_server_version_for_test(session, curtail_length):
176-
return session._server_version(session.GET_SERVER_VERSION_WITHOUT_AUTH)[
177-
:curtail_length
178-
]
179-
180-
181-
# Create a connection for test, based on ~/.irods environment by default.
166+
def make_session(test_server_version=True, **kwargs):
167+
return _irods_helpers_make_session(test_server_version=test_server_version, **kwargs)
182168

183169

184-
def make_session(test_server_version=True, **kwargs):
185-
"""Connect to an iRODS server as determined by any client environment
186-
file present at a standard location, and by any keyword arguments given.
187-
188-
Arguments:
189-
190-
test_server_version: Of type bool; in the `irods.test.helpers` version of this
191-
function, defaults to True. A True value causes
192-
*iRODS_Server_Too_Recent* to be raised if the server
193-
connected to is more recent than the current Python iRODS
194-
client's advertised level of compatibility.
195-
196-
**kwargs: Keyword arguments. Fed directly to the iRODSSession
197-
constructor."""
198-
199-
env_file = env_filename_from_keyword_args(kwargs)
200-
session = iRODSSession(irods_env_file=env_file, **kwargs)
201-
if test_server_version:
202-
connected_version = get_server_version_for_test(session, curtail_length=3)
203-
advertised_version = IRODS_VERSION[:3]
204-
if connected_version > advertised_version:
205-
msg = (
206-
"Connected server is {connected_version}, "
207-
"but this python-irodsclient advertises compatibility up to {advertised_version}."
208-
).format(**locals())
209-
raise iRODS_Server_Too_Recent(msg)
210-
211-
return session
212-
213-
214-
def home_collection(session):
215-
"""Return a string value for the given session's home collection."""
216-
return "/{0.zone}/home/{0.username}".format(session)
170+
make_session.__doc__ = re.sub(
171+
r"(test_server_version\s*)=\s*\w+", r"\1 = True", _irods_helpers_make_session.__doc__
172+
)
217173

218174

219175
def make_object(session, path, content=None, **options):

0 commit comments

Comments
 (0)