From e48ace6a65514a4f71669c50fd5d4ead67acdc7f Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Fri, 10 Mar 2017 17:39:21 +0000 Subject: Add llvm::sys::fs::real_path. LLVM already has real_path like functionality, but it is cumbersome to use and involves clean up after (e.g. you have to call openFileForRead, then close the resulting FD). Furthermore, on Windows it doesn't work for directories since opening a directory and opening a file require slightly different flags. So I add a simple function `real_path` which works for all paths on all platforms and has a simple to use interface. In doing so, I add the ability to opt in to resolving tilde expressions (e.g. ~/foo), which are normally handled by the shell. Differential Revision: https://reviews.llvm.org/D30668 llvm-svn: 297483 --- llvm/unittests/Support/Path.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'llvm/unittests/Support/Path.cpp') diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 3ad7e70abf4..571edaafbb6 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -499,6 +499,42 @@ TEST_F(FileSystemTest, Unique) { ASSERT_NO_ERROR(fs::remove(TempPath)); } +TEST_F(FileSystemTest, RealPath) { + ASSERT_NO_ERROR( + fs::create_directories(Twine(TestDirectory) + "/test1/test2/test3")); + ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/test1/test2/test3")); + + SmallString<64> RealBase; + SmallString<64> Expected; + SmallString<64> Actual; + + // TestDirectory itself might be under a symlink or have been specified with + // a different case than the existing temp directory. In such cases real_path + // on the concatenated path will differ in the TestDirectory portion from + // how we specified it. Make sure to compare against the real_path of the + // TestDirectory, and not just the value of TestDirectory. + ASSERT_NO_ERROR(fs::real_path(TestDirectory, RealBase)); + path::native(Twine(RealBase) + "/test1/test2", Expected); + + ASSERT_NO_ERROR(fs::real_path( + Twine(TestDirectory) + "/././test1/../test1/test2/./test3/..", Actual)); + + EXPECT_EQ(Expected, Actual); + + SmallString<64> HomeDir; + ASSERT_TRUE(llvm::sys::path::home_directory(HomeDir)); + ASSERT_NO_ERROR(fs::real_path(HomeDir, Expected)); + ASSERT_NO_ERROR(fs::real_path("~", Actual, true)); + EXPECT_EQ(Expected, Actual); + ASSERT_NO_ERROR(fs::real_path("~/", Actual, true)); + EXPECT_EQ(Expected, Actual); + + fs::real_path(Twine(TestDirectory) + "/does_not_exist", Actual); + EXPECT_EQ("", Actual); + + ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1")); +} + TEST_F(FileSystemTest, TempFiles) { // Create a temp file. int FileDescriptor; -- cgit v1.2.3