Skip to content

Commit 762b89e

Browse files
authored
Merge pull request #60 from elixir-cloud-aai/update-test-filer
Update tests filer
2 parents 6eb9f44 + 2bf9998 commit 762b89e

2 files changed

Lines changed: 48 additions & 43 deletions

File tree

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
]
1818
TEST_DEPS = [ 'pytest',
1919
'pyfakefs',
20-
'pytest-mock'
21-
, 'fs',
20+
'pytest-mock',
21+
'fsspec',
2222
'moto<5',
2323
'pytest-localftpserver'
2424
]

tests/test_filer.py

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unittest
22
import logging
33
import os
4+
import fsspec
5+
import shutil
46
from tesk_core.filer import newTransput, FTPTransput, HTTPTransput, FileTransput,\
57
process_file, logConfig, getPath, copyDir, copyFile, ftp_check_directory,\
68
subfolders_in
@@ -9,28 +11,37 @@
911
from tesk_core.path import containerPath
1012
from tesk_core.filer_s3 import S3Transput
1113
from assertThrows import AssertThrowsMixin
12-
from fs.opener import open_fs
1314
from io import StringIO
1415
from unittest.mock import patch
1516

1617

17-
18-
19-
20-
21-
2218
def getTree(rootDir):
23-
strio = StringIO()
24-
with open_fs(rootDir) as dst1_fs:
25-
dst1_fs.tree(file=strio)
26-
treeTxt = strio.getvalue()
27-
strio.close()
28-
return treeTxt
29-
30-
31-
def stripLines(txt):
32-
return '\n'.join([line.strip() for line in txt.splitlines()[1:]])
33-
19+
fs, base_path = fsspec.core.url_to_fs(rootDir)
20+
out = StringIO()
21+
22+
for root, dirs, files in fs.walk(base_path):
23+
out.write(f"{root or base_path}\n")
24+
for d in dirs:
25+
out.write(f"{d}/\n")
26+
for f in files:
27+
out.write(f"{f}\n")
28+
29+
return out.getvalue()
30+
31+
def normalize_tree(tree_str, abs_root, prefix):
32+
"""Convert absolute paths from getTree into relative paths."""
33+
lines = []
34+
for line in tree_str.splitlines():
35+
if line.startswith(abs_root):
36+
stripped = prefix + line[len(abs_root):]
37+
else:
38+
stripped = line
39+
stripped = stripped.lstrip("/")
40+
lines.append(stripped)
41+
return "\n".join(lines)
42+
43+
def rmDir(d):
44+
shutil.rmtree(d, ignore_errors=True)
3445

3546
@patch('tesk_core.path.HOST_BASE_PATH', '/home/tfga/workspace/cwl-tes')
3647
@patch('tesk_core.path.CONTAINER_BASE_PATH', '/transfer')
@@ -133,9 +144,6 @@ def test_upload_file_glob(self, copyFileMock, copyDirMock):
133144

134145

135146
def test_copyDir(self):
136-
def rmDir(d):
137-
os.system('rm -r {}'.format(d))
138-
139147
baseDir = 'tests/resources/copyDirTest/'
140148
src = os.path.join(baseDir, 'src')
141149
dst1 = os.path.join(baseDir, 'dst1')
@@ -155,33 +163,30 @@ def rmDir(d):
155163

156164
# Let's try to copy
157165
copyDir(src, dst1)
166+
tree = getTree(dst1)
167+
abs_dst1 = os.path.abspath(dst1)
168+
normalizedTree = normalize_tree(tree, abs_dst1, "dist1")
169+
170+
expected = "dist1\na/\n3.txt\ndist1/a\n2.txt\n1.txt".strip()
158171

172+
normalized_lines = sorted(normalizedTree.splitlines())
173+
expected_lines = sorted(expected.splitlines())
159174

160-
self.assertEqual(getTree(dst1),
161-
stripLines('''
162-
|-- a
163-
| |-- 1.txt
164-
| `-- 2.txt
165-
`-- 3.txt
166-
'''
167-
)
168-
)
175+
self.assertEqual(normalized_lines, expected_lines)
169176

170177
# Copying to non-existing dst -----------------------------------------
171-
self.assertFalse(os.path.exists(dst2)) # dst2 should not exist
172-
173-
# Let's try to copy
178+
# # Let's try to copy
174179
copyDir(src, dst2)
180+
tree = getTree(dst2)
181+
abs_dst2 = os.path.abspath(dst2)
182+
normalizedTree = normalize_tree(tree, abs_dst2, "dist2")
175183

176-
self.assertEqual(getTree(dst2),
177-
stripLines('''
178-
|-- a
179-
| |-- 1.txt
180-
| `-- 2.txt
181-
`-- 3.txt
182-
'''
183-
)
184-
)
184+
expected = "dist2\na/\n3.txt\ndist2/a\n2.txt\n1.txt".strip()
185+
186+
normalized_lines = sorted(normalizedTree.splitlines())
187+
expected_lines = sorted(expected.splitlines())
188+
189+
self.assertEqual(normalized_lines, expected_lines)
185190

186191
def test_getPath(self):
187192

0 commit comments

Comments
 (0)