11import unittest
22import logging
33import os
4+ import fsspec
5+ import shutil
46from tesk_core .filer import newTransput , FTPTransput , HTTPTransput , FileTransput ,\
57 process_file , logConfig , getPath , copyDir , copyFile , ftp_check_directory ,\
68 subfolders_in
911from tesk_core .path import containerPath
1012from tesk_core .filer_s3 import S3Transput
1113from assertThrows import AssertThrowsMixin
12- from fs .opener import open_fs
1314from io import StringIO
1415from unittest .mock import patch
1516
1617
17-
18-
19-
20-
21-
2218def 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+ stripped = line .replace (abs_root , prefix )
36+ stripped = stripped .lstrip ("/" )
37+ lines .append (stripped )
38+ return "\n " .join (lines )
39+
40+ def rmDir (d ):
41+ shutil .rmtree (d , ignore_errors = True )
3442
3543@patch ('tesk_core.path.HOST_BASE_PATH' , '/home/tfga/workspace/cwl-tes' )
3644@patch ('tesk_core.path.CONTAINER_BASE_PATH' , '/transfer' )
@@ -133,9 +141,6 @@ def test_upload_file_glob(self, copyFileMock, copyDirMock):
133141
134142
135143 def test_copyDir (self ):
136- def rmDir (d ):
137- os .system ('rm -r {}' .format (d ))
138-
139144 baseDir = 'tests/resources/copyDirTest/'
140145 src = os .path .join (baseDir , 'src' )
141146 dst1 = os .path .join (baseDir , 'dst1' )
@@ -155,33 +160,24 @@ def rmDir(d):
155160
156161 # Let's try to copy
157162 copyDir (src , dst1 )
163+ tree = getTree (dst1 )
164+ abs_dst1 = os .path .abspath (dst1 )
165+ normalizedTree = normalize_tree (tree , abs_dst1 , "dist1" )
166+
167+ expected = "dist1\n a/\n 3.txt\n dist1/a\n 2.txt\n 1.txt" .strip ()
158168
159-
160- self .assertEqual (getTree (dst1 ),
161- stripLines ('''
162- |-- a
163- | |-- 1.txt
164- | `-- 2.txt
165- `-- 3.txt
166- '''
167- )
168- )
169+ self .assertEqual (normalizedTree , expected )
169170
170171 # Copying to non-existing dst -----------------------------------------
171- self .assertFalse (os .path .exists (dst2 )) # dst2 should not exist
172-
173- # Let's try to copy
172+ # # Let's try to copy
174173 copyDir (src , dst2 )
174+ tree = getTree (dst2 )
175+ abs_dst2 = os .path .abspath (dst2 )
176+ normalizedTree = normalize_tree (tree , abs_dst2 , "dist2" )
175177
176- self .assertEqual (getTree (dst2 ),
177- stripLines ('''
178- |-- a
179- | |-- 1.txt
180- | `-- 2.txt
181- `-- 3.txt
182- '''
183- )
184- )
178+ expected = "dist2\n a/\n 3.txt\n dist2/a\n 2.txt\n 1.txt" .strip ()
179+
180+ self .assertEqual (normalizedTree , expected )
185181
186182 def test_getPath (self ):
187183
0 commit comments