Skip to content

Commit 83fe525

Browse files
authored
Merge pull request #2 from bencvdb/wfInfoAdd
Unite test files.
2 parents 4a14efa + ecaa063 commit 83fe525

File tree

3 files changed

+67
-72
lines changed

3 files changed

+67
-72
lines changed

test/test_client_util.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import os
55
import logging
66
import subprocess
7+
import sys
78

8-
from wes_client.util import expand_globs
9+
pkg_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) # noqa
10+
sys.path.insert(0, pkg_root) # noqa
11+
12+
from wes_client.util import expand_globs, wf_info
913

1014
logging.basicConfig(level=logging.INFO)
1115

@@ -35,5 +39,63 @@ def test_expand_globs(self):
3539
assert set(files) == glob_files, '\n' + str(set(files)) + '\n' + str(glob_files)
3640

3741

42+
class WorkflowInfoTest(unittest.TestCase):
43+
44+
local = {'cwl': 'file://' + os.path.join(os.getcwd() + '/testdata/md5sum.cwl'),
45+
'wdl': 'file://' + os.path.join(os.getcwd() + '/testdata/md5sum.wdl'),
46+
'py': 'file://' + os.path.join(os.getcwd() + '/test/test_integration.py'),
47+
'unsupported': 'fake.txt'}
48+
49+
remote = {'cwl': 'https://raw.githubusercontent.com/common-workflow-language/workflow-service/master/testdata/md5sum.cwl',
50+
'wdl': 'https://raw.githubusercontent.com/common-workflow-language/workflow-service/master/testdata/md5sum.wdl',
51+
'py': 'https://raw.githubusercontent.com/common-workflow-language/workflow-service/master/test/test_integration.py',
52+
'unsupported': 'gs://topmed_workflow_testing/topmed_aligner/small_test_files_sbg/example_human_known_snp.py', # TODO: find real external file of .py, .cwl, .wdl
53+
'unreachable': 'https://fake.py'}
54+
55+
expected = {'cwl': ('v1.0', 'CWL'),
56+
'wdl': ('draft-2', 'WDL'),
57+
'py': ('2.7', 'PY'),
58+
'pyWithPrefix': ('2.7', 'PY')}
59+
60+
def testSupportedFormatChecking(self):
61+
"""
62+
Check that non-wdl, -python, -cwl files are rejected.
63+
64+
This test is run only on local files to avoid downloading and removing a new file.
65+
"""
66+
67+
for format, location in self.local.items():
68+
if format != 'unsupported':
69+
# Tests the behavior after receiving supported file types with and without the 'file://' prefix
70+
self.assertEquals(wf_info(location), self.expected[format])
71+
self.assertEquals(wf_info(location[7:]), self.expected[format])
72+
73+
else:
74+
# Tests behavior after recieveing a non supported file type.
75+
with self.assertRaises(TypeError):
76+
wf_info(location)
77+
78+
def testFileLocationChecking(self):
79+
"""
80+
Check that the function rejects unsupported file locations.
81+
82+
This test needs to be run on remote files to test the location checking functionality of wf_info().
83+
"""
84+
85+
for format, location in self.remote.items():
86+
if format == 'unsupported':
87+
# Tests behavior after receiving a file hosted at an unsupported location.
88+
with self.assertRaises(NotImplementedError):
89+
wf_info(location)
90+
91+
elif format == 'unreachable':
92+
# Tests behavior after receiving a non-existent file.
93+
with self.assertRaises(IOError):
94+
wf_info(location)
95+
96+
else:
97+
self.assertEquals(wf_info(location), self.expected[format])
98+
self.assertFalse(os.path.isfile(os.path.join(os.getcwd(), 'fetchedFromRemote.' + format)))
99+
38100
if __name__ == '__main__':
39101
unittest.main() # run all tests

test/test_wesclient_utils.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

wes_client/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from urllib import urlopen
1313

1414

15-
def _two_seven_compatible(filePath):
15+
def two_seven_compatible(filePath):
1616
"""Determines if a python file is 2.7 compatible by seeing if it compiles in a subprocess"""
1717
try:
1818
check_call(['python2', '-m', 'py_compile', filePath], stderr=DEVNULL)
@@ -21,9 +21,9 @@ def _two_seven_compatible(filePath):
2121
return True
2222

2323

24-
def _get_version(extension, workflow_file):
24+
def get_version(extension, workflow_file):
2525
'''Determines the version of a .py, .wdl, or .cwl file.'''
26-
if extension == 'py' and _two_seven_compatible(workflow_file):
26+
if extension == 'py' and two_seven_compatible(workflow_file):
2727
return '2.7'
2828
elif extension == 'cwl':
2929
return yaml.load(open(workflow_file))['cwlVersion']
@@ -50,7 +50,7 @@ def wf_info(workflow_path):
5050

5151
if file_type in supported_formats:
5252
if workflow_path.startswith('file://'):
53-
version = _get_version(file_type, workflow_path[7:])
53+
version = get_version(file_type, workflow_path[7:])
5454
elif workflow_path.startswith('https://') or workflow_path.startswith('http://'):
5555
# If file not local go fetch it.
5656
html = urlopen(workflow_path).read()

0 commit comments

Comments
 (0)