From 59d725cabf44b53be32ab4402e59f89e52b51f26 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 16 Jan 2017 10:07:02 +0000 Subject: FileSpec: Fix PrependPathComponent("/") Summary: PrependPathComponent was unconditionally inserting path separators between the path components. This is not correct if the prepended path is "/", which caused problems down the line. Fix the function to use the same algorithm as AppendPathComponent and add a test. This fixes one part of llvm.org/pr31611. Reviewers: clayborg, zturner Subscribers: lldb-commits Differential Revision: https://reviews.llvm.org/D28677 llvm-svn: 292100 --- lldb/unittests/Host/FileSpecTest.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'lldb/unittests/Host/FileSpecTest.cpp') diff --git a/lldb/unittests/Host/FileSpecTest.cpp b/lldb/unittests/Host/FileSpecTest.cpp index 55a66b3b37e..dece83c2c7c 100644 --- a/lldb/unittests/Host/FileSpecTest.cpp +++ b/lldb/unittests/Host/FileSpecTest.cpp @@ -109,6 +109,28 @@ TEST(FileSpecTest, CopyByAppendingPathComponent) { EXPECT_STREQ("bar", fs.GetFilename().GetCString()); } +TEST(FileSpecTest, PrependPathComponent) { + FileSpec fs_posix("foo", false, FileSpec::ePathSyntaxPosix); + fs_posix.PrependPathComponent("/bar"); + EXPECT_STREQ("/bar/foo", fs_posix.GetCString()); + + FileSpec fs_posix_2("foo/bar", false, FileSpec::ePathSyntaxPosix); + fs_posix_2.PrependPathComponent("/baz"); + EXPECT_STREQ("/baz/foo/bar", fs_posix_2.GetCString()); + + FileSpec fs_windows("baz", false, FileSpec::ePathSyntaxWindows); + fs_windows.PrependPathComponent("F:\\bar"); + EXPECT_STREQ("F:\\bar\\baz", fs_windows.GetCString()); + + FileSpec fs_posix_root("bar", false, FileSpec::ePathSyntaxPosix); + fs_posix_root.PrependPathComponent("/"); + EXPECT_STREQ("/bar", fs_posix_root.GetCString()); + + FileSpec fs_windows_root("bar", false, FileSpec::ePathSyntaxWindows); + fs_windows_root.PrependPathComponent("F:\\"); + EXPECT_STREQ("F:\\bar", fs_windows_root.GetCString()); +} + static void Compare(const FileSpec &one, const FileSpec &two, bool full_match, bool remove_backup_dots, bool result) { EXPECT_EQ(result, FileSpec::Equal(one, two, full_match, remove_backup_dots)) @@ -283,4 +305,4 @@ TEST(FileSpecTest, FormatFileSpec) { EXPECT_EQ("foo", llvm::formatv("{0}", F).str()); EXPECT_EQ("foo", llvm::formatv("{0:F}", F).str()); EXPECT_EQ("(empty)", llvm::formatv("{0:D}", F).str()); -} \ No newline at end of file +} -- cgit v1.2.3