Skip to content

Commit d3d8e4c

Browse files
raise error when symlink already exists but points to a different path, and add test for it
1 parent 64f96e1 commit d3d8e4c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

easybuild/tools/filetools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,12 @@ def symlink(source_path, symlink_path, use_abspath_source=True):
298298
if use_abspath_source:
299299
source_path = os.path.abspath(source_path)
300300

301-
if os.path.exists(symlink_path) and os.path.abspath(source_path) == os.path.abspath(os.readlink(symlink_path)):
301+
if os.path.exists(symlink_path):
302+
abs_source_path = os.path.abspath(source_path)
303+
symlink_target_path = os.path.abspath(os.readlink(symlink_path))
304+
if abs_source_path != symlink_target_path:
305+
raise EasyBuildError("Trying to symlink %s to %s, but the symlink already exists and points to %s.",
306+
source_path, symlink_path, symlink_target_path)
302307
_log.info("Skipping symlinking %s to %s, link already exists", source_path, symlink_path)
303308
else:
304309
try:

test/framework/filetools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,17 @@ def test_symlink_resolve_path(self):
574574

575575
self.assertTrue(os.path.samefile(os.path.join(self.test_prefix, 'test', 'test.txt'), link))
576576

577+
# test symlink when it already exists and points to the same path
578+
ft.symlink(test_file, link)
579+
580+
# test symlink when it already exists but points to a different path
581+
test_file2 = os.path.join(link_dir, 'test2.txt')
582+
ft.write_file(test_file, "test123")
583+
self.assertErrorRegex(EasyBuildError,
584+
"Trying to symlink %s to %s, but the symlink already exists and points to %s." %
585+
(test_file2, link, test_file),
586+
ft.symlink, test_file2, link)
587+
577588
# test resolve_path
578589
self.assertEqual(test_dir, ft.resolve_path(link_dir))
579590
self.assertEqual(os.path.join(os.path.realpath(self.test_prefix), 'test', 'test.txt'), ft.resolve_path(link))

0 commit comments

Comments
 (0)