... |
... |
@@ -72,16 +72,16 @@ def combinations(integer_range): |
72
|
72
|
|
73
|
73
|
|
74
|
74
|
def resolve_symlinks(path, root):
|
75
|
|
- """ A function to resolve symlinks which are rooted at 'root'. For example, the symlink
|
76
|
|
-
|
77
|
|
- /a/b/c/d -> /c/e
|
78
|
|
-
|
79
|
|
- should resolve with the call resolve_symlinks('/a/b/c/d', '/a/b') to '/a/b/c/e'.
|
|
75
|
+ """ A function to resolve symlinks inside 'path' components apart from the last one.
|
|
76
|
+ For example, resolve_symlinks('/a/b/c/d', '/a/b')
|
|
77
|
+ will return '/a/b/f/d' if /a/b/c is a symlink to /a/b/f. The final component of
|
|
78
|
+ 'path' is not resolved, because we typically want to inspect the symlink found
|
|
79
|
+ at that path, not its target.
|
80
|
80
|
|
81
|
81
|
"""
|
82
|
82
|
components = path.split(os.path.sep)
|
83
|
83
|
location = root
|
84
|
|
- for i in range(0, len(components)):
|
|
84
|
+ for i in range(0, len(components) - 1):
|
85
|
85
|
location = os.path.join(location, components[i])
|
86
|
86
|
if os.path.islink(location):
|
87
|
87
|
# Resolve the link, add on all the remaining components
|
... |
... |
@@ -95,9 +95,15 @@ def resolve_symlinks(path, root): |
95
|
95
|
# Relative link - relative to symlink location
|
96
|
96
|
location = os.path.join(location, target)
|
97
|
97
|
return resolve_symlinks(location, root)
|
|
98
|
+ # If we got here, no symlinks were found. Add on the final component and return.
|
|
99
|
+ location = os.path.join(location, components[-1])
|
98
|
100
|
return location
|
99
|
101
|
|
100
|
102
|
|
|
103
|
+def directory_not_empty(path):
|
|
104
|
+ return os.listdir(path)
|
|
105
|
+
|
|
106
|
+
|
101
|
107
|
@pytest.mark.parametrize("original,overlay", combinations([1, 2, 3, 4, 5]))
|
102
|
108
|
def test_cas_import(cli, tmpdir, original, overlay):
|
103
|
109
|
print("Testing import of root {} into root {}".format(overlay, original))
|
... |
... |
@@ -116,15 +122,19 @@ def test_cas_import(cli, tmpdir, original, overlay): |
116
|
122
|
realpath = resolve_symlinks(path, os.path.join(tmpdir, "output"))
|
117
|
123
|
print("resolved {} to {}".format(path, realpath))
|
118
|
124
|
if typename == 'F':
|
119
|
|
- assert os.path.isfile(realpath), "{} did not exist in the combined virtual directory".format(path)
|
120
|
|
- # Problem here - symlinks won't resolve because the root is incorrect.
|
121
|
|
- assert file_contents_are(realpath, content)
|
|
125
|
+ if os.path.isdir(realpath) and directory_not_empty(realpath):
|
|
126
|
+ # The file should not have overwritten the directory in this case.
|
|
127
|
+ pass
|
|
128
|
+ else:
|
|
129
|
+ assert os.path.isfile(realpath), "{} did not exist in the combined virtual directory".format(path)
|
|
130
|
+ assert file_contents_are(realpath, content)
|
122
|
131
|
elif typename == 'S':
|
123
|
|
- # Not currently verified.
|
124
|
|
- # assert os.path.islink(os.path.join(tmpdir, "output", path)),
|
125
|
|
- # "{} did not exist in the combined virtual directory".format(path)
|
126
|
|
- # assert os.readlink(os.path.join(tmpdir, "output", path)) == content
|
127
|
|
- pass
|
|
132
|
+ if os.path.isdir(realpath) and directory_not_empty(realpath):
|
|
133
|
+ # The symlink should not have overwritten the directory in this case.
|
|
134
|
+ pass
|
|
135
|
+ else:
|
|
136
|
+ assert os.path.islink(realpath)
|
|
137
|
+ assert os.readlink(realpath) == content
|
128
|
138
|
elif typename == 'D':
|
129
|
139
|
# Note that isdir accepts symlinks to dirs, so a symlink to a dir is acceptable.
|
130
|
140
|
assert os.path.isdir(realpath)
|