From 618ba2fb9a371af8f2c8262746ab65092478a570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Sat, 3 Jul 2021 00:02:58 +0200 Subject: [PATCH] createSymlinkIfSupportedByFilesystem(): Add support for directory symlinks used on Windows --- test/FilesystemUtils.cpp | 13 +++++++++---- test/FilesystemUtils.h | 8 ++++++-- test/libsolutil/CommonIO.cpp | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/test/FilesystemUtils.cpp b/test/FilesystemUtils.cpp index cdad58e62..c016f7fd1 100644 --- a/test/FilesystemUtils.cpp +++ b/test/FilesystemUtils.cpp @@ -39,7 +39,7 @@ void solidity::test::createFilesWithParentDirs(set cons } } -void solidity::test::createFileWithContent(boost::filesystem::path const& _path, string const& content) +void solidity::test::createFileWithContent(boost::filesystem::path const& _path, string const& _content) { if (boost::filesystem::is_regular_file(_path)) BOOST_THROW_EXCEPTION(runtime_error("File already exists: \"" + _path.string() + "\".")); \ @@ -49,16 +49,21 @@ void solidity::test::createFileWithContent(boost::filesystem::path const& _path, if (newFile.fail() || !boost::filesystem::is_regular_file(_path)) BOOST_THROW_EXCEPTION(runtime_error("Failed to create a file: \"" + _path.string() + "\".")); \ - newFile << content; + newFile << _content; } bool solidity::test::createSymlinkIfSupportedByFilesystem( boost::filesystem::path const& _targetPath, - boost::filesystem::path const& _linkName + boost::filesystem::path const& _linkName, + bool _directorySymlink ) { boost::system::error_code symlinkCreationError; - boost::filesystem::create_symlink(_targetPath, _linkName, symlinkCreationError); + + if (_directorySymlink) + boost::filesystem::create_directory_symlink(_targetPath, _linkName, symlinkCreationError); + else + boost::filesystem::create_symlink(_targetPath, _linkName, symlinkCreationError); if (!symlinkCreationError) return true; diff --git a/test/FilesystemUtils.h b/test/FilesystemUtils.h index 291188114..f993ce03d 100644 --- a/test/FilesystemUtils.h +++ b/test/FilesystemUtils.h @@ -35,16 +35,20 @@ void createFilesWithParentDirs(std::set const& _paths, /// Creates a file with the exact content specified in the second argument. /// Throws an exception if the file already exists or if the parent directory of the file does not. -void createFileWithContent(boost::filesystem::path const& _path, std::string const& content); +void createFileWithContent(boost::filesystem::path const& _path, std::string const& _content); /// Creates a symlink between two paths. /// The target does not have to exist. +/// If @p directorySymlink is true, indicate to the operating system that this is a directory +/// symlink. On some systems (e.g. Windows) it's possible to create a non-directory symlink pointing +/// at a directory, which makes such a symlinks unusable. /// @returns true if the symlink has been successfully created, false if the filesystem does not /// support symlinks. /// Throws an exception of the operation fails for a different reason. bool createSymlinkIfSupportedByFilesystem( boost::filesystem::path const& _targetPath, - boost::filesystem::path const& _linkName + boost::filesystem::path const& _linkName, + bool _directorySymlink ); } diff --git a/test/libsolutil/CommonIO.cpp b/test/libsolutil/CommonIO.cpp index 0f3cde036..57fea38de 100644 --- a/test/libsolutil/CommonIO.cpp +++ b/test/libsolutil/CommonIO.cpp @@ -58,7 +58,7 @@ BOOST_AUTO_TEST_CASE(readFileAsString_symlink) TemporaryDirectory tempDir("common-io-test-"); createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n"); - if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt")) + if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt", false)) return; BOOST_TEST(readFileAsString(tempDir.path() / "symlink.txt") == "ABC\ndef\n");