Skip to content

Commit 0848f53

Browse files
committed
Canonicalize root paths in tests.
Since the current and initial paths on Windows may have non-canonical root paths (i.e. "c:\" instead of "C:\"), path comparisons may fail because of the case differences between the canonicalized paths and the expected paths. To avoid these spurious failures, canonicalize root paths in all expected paths.
1 parent cf92e38 commit 0848f53

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

test/issues/99_canonical_with_junction_point.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,25 @@ struct TmpDir
3434
}
3535
};
3636

37+
//! Converts root path of the argument to canonical form, i.e. "C:\" instead of "c:\".
38+
inline fs::path canonicalize_root_path(fs::path const& p)
39+
{
40+
fs::path root_path = p.root_path();
41+
if (root_path.empty())
42+
return p;
43+
root_path = fs::canonical(root_path);
44+
fs::path rel_path = p.relative_path();
45+
if (!rel_path.empty())
46+
root_path.append(rel_path);
47+
return root_path;
48+
}
49+
3750
// Test fs::canonical for various path in a Windows directory junction point
3851
// This failed before due to broken handling of absolute paths and ignored ReparseTag
3952
int main()
4053
{
41-
42-
const fs::path cwd = fs::current_path();
54+
// Note: Use cacnonical form of the root path in all subsequent paths to make path comparisons more stable
55+
const fs::path cwd = canonicalize_root_path(fs::current_path());
4356
const TmpDir tmp(cwd);
4457
const fs::path junction = tmp.path / "junction";
4558
const fs::path real = tmp.path / "real";

test/operations_test.cpp

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ inline void unsetenv_(const char* name)
138138

139139
#endif
140140

141+
//! Converts root path of the argument to canonical form, i.e. "C:\" instead of "c:\" on Windows.
142+
inline fs::path canonicalize_root_path(fs::path const& p)
143+
{
144+
fs::path root_path = p.root_path();
145+
if (root_path.empty())
146+
return p;
147+
root_path = fs::canonical(root_path);
148+
fs::path rel_path = p.relative_path();
149+
if (!rel_path.empty())
150+
root_path.append(rel_path);
151+
return root_path;
152+
}
153+
141154
#define CHECK_EXCEPTION(Functor, Expect) throws_fs_error(Functor, Expect, __LINE__)
142155

143156
namespace {
@@ -1780,17 +1793,20 @@ void canonical_basic_tests()
17801793
}
17811794
BOOST_TEST(ok);
17821795

1796+
// Note: Use cacnonical form of the root path in all paths to make path comparisons more stable
1797+
const fs::path cur_path = canonicalize_root_path(fs::current_path());
1798+
17831799
// non-symlink tests; also see canonical_symlink_tests()
1784-
BOOST_TEST_EQ(fs::canonical(""), fs::current_path());
1785-
BOOST_TEST_EQ(fs::canonical("", fs::current_path()), fs::current_path());
1786-
BOOST_TEST_EQ(fs::canonical("", ""), fs::current_path());
1787-
BOOST_TEST_EQ(fs::canonical(fs::current_path()), fs::current_path());
1788-
BOOST_TEST_EQ(fs::canonical(fs::current_path(), ""), fs::current_path());
1789-
BOOST_TEST_EQ(fs::canonical(fs::current_path(), "no-such-file"), fs::current_path());
1800+
BOOST_TEST_EQ(fs::canonical(""), cur_path);
1801+
BOOST_TEST_EQ(fs::canonical("", fs::current_path()), cur_path);
1802+
BOOST_TEST_EQ(fs::canonical("", ""), cur_path);
1803+
BOOST_TEST_EQ(fs::canonical(fs::current_path()), cur_path);
1804+
BOOST_TEST_EQ(fs::canonical(fs::current_path(), ""), cur_path);
1805+
BOOST_TEST_EQ(fs::canonical(fs::current_path(), "no-such-file"), cur_path);
17901806

1791-
BOOST_TEST_EQ(fs::canonical("."), fs::current_path());
1792-
BOOST_TEST_EQ(fs::canonical(".."), fs::current_path().parent_path());
1793-
BOOST_TEST_EQ(fs::canonical("/"), fs::current_path().root_path());
1807+
BOOST_TEST_EQ(fs::canonical("."), cur_path);
1808+
BOOST_TEST_EQ(fs::canonical(".."), cur_path.parent_path());
1809+
BOOST_TEST_EQ(fs::canonical("/"), cur_path.root_path());
17941810

17951811
fs::path relative_dir(dir.filename());
17961812
BOOST_TEST_EQ(fs::canonical(dir), dir);
@@ -1801,7 +1817,7 @@ void canonical_basic_tests()
18011817
BOOST_TEST_EQ(fs::canonical(relative_dir / "d1/../f0"), dir / "f0");
18021818

18031819
// treat parent of root as itself on both POSIX and Windows
1804-
fs::path init(fs::initial_path());
1820+
fs::path init(canonicalize_root_path(fs::initial_path()));
18051821
fs::path root(init.root_path());
18061822
fs::path::const_iterator it(init.begin());
18071823
fs::path first; // relative first non-root directory
@@ -2825,8 +2841,11 @@ void weakly_canonical_basic_tests()
28252841
cout << "weakly_canonical_basic_tests..." << endl;
28262842
cout << " dir is " << dir << endl;
28272843

2828-
BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/bar"), fs::current_path() / fs::path("no-such/foo/bar"));
2829-
BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/../bar"), fs::current_path() / fs::path("no-such/bar"));
2844+
// Note: Use cacnonical form of the root path in all paths to make path comparisons more stable
2845+
const fs::path cur_path = canonicalize_root_path(fs::current_path());
2846+
2847+
BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/bar"), cur_path / fs::path("no-such/foo/bar"));
2848+
BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/../bar"), cur_path / fs::path("no-such/bar"));
28302849
BOOST_TEST_EQ(fs::weakly_canonical(dir), dir);
28312850
BOOST_TEST_EQ(fs::weakly_canonical(dir / "no-such/foo/bar"), dir / "no-such/foo/bar");
28322851
BOOST_TEST_EQ(fs::weakly_canonical(dir / "no-such/foo/../bar"), dir / "no-such/bar");
@@ -2910,8 +2929,8 @@ int cpp_main(int argc, char* argv[])
29102929
#error neither BOOST_POSIX_API nor BOOST_WINDOWS_API is defined. See boost/system/api_config.hpp
29112930
#endif
29122931
cout << "API is " << platform << endl;
2913-
cout << "initial_path() is " << fs::initial_path() << endl;
2914-
fs::path ip = fs::initial_path();
2932+
const fs::path ip = fs::initial_path();
2933+
cout << "initial_path() is " << ip << endl;
29152934
do_the_right_thing_tests(); // compile-only tests, but call anyhow to suppress warnings
29162935

29172936
for (fs::path::const_iterator it = ip.begin(); it != ip.end(); ++it)
@@ -2922,7 +2941,8 @@ int cpp_main(int argc, char* argv[])
29222941
}
29232942
cout << endl;
29242943

2925-
dir = fs::initial_path() / temp_dir;
2944+
// Note: Use cacnonical form of the root path in all subsequent paths to make path comparisons more stable
2945+
dir = canonicalize_root_path(ip / temp_dir);
29262946

29272947
if (fs::exists(dir))
29282948
{

0 commit comments

Comments
 (0)