[Support] Add sys::fs::set_current_path() (aka chdir)
Summary:
This adds a cross-platform way of setting the current working directory
analogous to the existing current_path() function used for retrieving
it. The function will be used in lldb.
Reviewers: rafael, silvas, zturner
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29035
llvm-svn: 292907
diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp
index 30eaa8b..fb61cc7 100644
--- a/llvm/unittests/Support/Path.cpp
+++ b/llvm/unittests/Support/Path.cpp
@@ -1135,4 +1135,23 @@
::close(FileDescriptor);
}
+
+TEST_F(FileSystemTest, set_current_path) {
+ SmallString<128> path;
+
+ ASSERT_NO_ERROR(fs::current_path(path));
+ ASSERT_NE(TestDirectory, path);
+
+ struct RestorePath {
+ SmallString<128> path;
+ RestorePath(const SmallString<128> &path) : path(path) {}
+ ~RestorePath() { fs::set_current_path(path); }
+ } restore_path(path);
+
+ ASSERT_NO_ERROR(fs::set_current_path(TestDirectory));
+
+ ASSERT_NO_ERROR(fs::current_path(path));
+ ASSERT_EQ(TestDirectory, path);
+}
+
} // anonymous namespace