summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/Path.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-03-10 17:39:21 +0000
committerZachary Turner <zturner@google.com>2017-03-10 17:39:21 +0000
commite48ace6a65514a4f71669c50fd5d4ead67acdc7f (patch)
treef16ef818e20451ed8031e0fe16248b273043dfd8 /llvm/unittests/Support/Path.cpp
parent62e0759d56b34c0b044a180e1bc89c419531cf57 (diff)
downloadbcm5719-llvm-e48ace6a65514a4f71669c50fd5d4ead67acdc7f.tar.gz
bcm5719-llvm-e48ace6a65514a4f71669c50fd5d4ead67acdc7f.zip
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
Diffstat (limited to 'llvm/unittests/Support/Path.cpp')
-rw-r--r--llvm/unittests/Support/Path.cpp36
1 files changed, 36 insertions, 0 deletions
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;
OpenPOWER on IntegriCloud