@@ -49,6 +49,8 @@ class TestPath : public TestFixture {
4949 TEST_CASE (identifyWithCppProbe);
5050 TEST_CASE (is_header);
5151 TEST_CASE (simplifyPath);
52+ TEST_CASE (getAbsolutePath);
53+ TEST_CASE (exists);
5254 }
5355
5456 void removeQuotationMarks () const {
@@ -437,6 +439,101 @@ class TestPath : public TestFixture {
437439 ASSERT_EQUALS (" //home/file.cpp" , Path::simplifyPath (" \\\\ home\\ test\\ ..\\ file.cpp" ));
438440 ASSERT_EQUALS (" //file.cpp" , Path::simplifyPath (" \\\\ home\\ ..\\ test\\ ..\\ file.cpp" ));
439441 }
442+
443+ void getAbsolutePath () const {
444+ const std::string cwd = Path::getCurrentPath ();
445+
446+ ScopedFile file (" testabspath.txt" , " " );
447+ std::string expected = Path::toNativeSeparators (Path::join (cwd, " testabspath.txt" ));
448+
449+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" testabspath.txt" ));
450+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" ./testabspath.txt" ));
451+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" .\\ testabspath.txt" ));
452+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" test/../testabspath.txt" ));
453+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" test\\ ..\\ testabspath.txt" ));
454+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" ./test/../testabspath.txt" ));
455+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (" .\\ test\\ ../testabspath.txt" ));
456+
457+ ASSERT_EQUALS (expected, Path::getAbsoluteFilePath (Path::join (cwd, " testabspath.txt" )));
458+
459+ std::string cwd_up = Path::getPathFromFilename (cwd);
460+ cwd_up.pop_back (); // remove trailing slash
461+ ASSERT_EQUALS (cwd_up, Path::getAbsoluteFilePath (Path::join (cwd, " .." )));
462+ ASSERT_EQUALS (cwd_up, Path::getAbsoluteFilePath (Path::join (cwd, " ../" )));
463+ ASSERT_EQUALS (cwd_up, Path::getAbsoluteFilePath (Path::join (cwd, " ..\\ " )));
464+ ASSERT_EQUALS (cwd_up, Path::getAbsoluteFilePath (Path::join (cwd, " ./../" )));
465+ ASSERT_EQUALS (cwd_up, Path::getAbsoluteFilePath (Path::join (cwd, " .\\ ..\\ " )));
466+
467+ ASSERT_EQUALS (cwd, Path::getAbsoluteFilePath (" ." ));
468+ #ifndef _WIN32
469+ TODO_ASSERT_EQUALS (cwd, " " , Path::getAbsoluteFilePath (" ./" ));
470+ TODO_ASSERT_EQUALS (cwd, " " , Path::getAbsoluteFilePath (" .\\ " ));
471+ #else
472+ ASSERT_EQUALS (cwd, Path::getAbsoluteFilePath (" ./" ));
473+ ASSERT_EQUALS (cwd, Path::getAbsoluteFilePath (" .\\ " ));
474+ #endif
475+
476+ ASSERT_EQUALS (" " , Path::getAbsoluteFilePath (" " ));
477+
478+ #ifndef _WIN32
479+ // the underlying realpath() call only returns something if the path actually exists
480+ ASSERT_THROW_EQUALS_2 (Path::getAbsoluteFilePath (" testabspath2.txt" ), std::runtime_error, " path 'testabspath2.txt' does not exist" );
481+ #else
482+ ASSERT_EQUALS (Path::toNativeSeparators (Path::join (cwd, " testabspath2.txt" )), Path::getAbsoluteFilePath (" testabspath2.txt" ));
483+ #endif
484+
485+ #ifdef _WIN32
486+ // determine an existing drive letter
487+ std::string drive = Path::getCurrentPath ().substr (0 , 2 );
488+ ASSERT_EQUALS (drive + " " , Path::getAbsoluteFilePath (drive + " \\ " ));
489+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " \\ path" ));
490+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " \\ path\\ " ));
491+ ASSERT_EQUALS (drive + " \\ path\\ files.txt" , Path::getAbsoluteFilePath (drive + " \\ path\\ files.txt" ));
492+ ASSERT_EQUALS (drive + " " , Path::getAbsoluteFilePath (drive + " //" ));
493+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " //path" ));
494+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " //path/" ));
495+ ASSERT_EQUALS (drive + " \\ path\\ files.txt" , Path::getAbsoluteFilePath (drive + " //path/files.txt" ));
496+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " \\\\ path" ));
497+ ASSERT_EQUALS (drive + " " , Path::getAbsoluteFilePath (drive + " /" ));
498+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " /path" ));
499+
500+ drive[0 ] = static_cast <char >(toupper (drive[0 ]));
501+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " \\ path" ));
502+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " /path" ));
503+
504+ drive[0 ] = static_cast <char >(tolower (drive[0 ]));
505+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " \\ path" ));
506+ ASSERT_EQUALS (drive + " \\ path" , Path::getAbsoluteFilePath (drive + " /path" ));
507+
508+ ASSERT_EQUALS (" 1:\\ path\\ files.txt" , Path::getAbsoluteFilePath (" 1:\\ path\\ files.txt" )); // treated as valid drive
509+ ASSERT_EQUALS (
510+ Path::toNativeSeparators (Path::join (Path::getCurrentPath (), " CC:\\ path\\ files.txt" )),
511+ Path::getAbsoluteFilePath (" CC:\\ path\\ files.txt" )); // treated as filename
512+ ASSERT_EQUALS (" 1:\\ path\\ files.txt" , Path::getAbsoluteFilePath (" 1:/path/files.txt" )); // treated as valid drive
513+ ASSERT_EQUALS (
514+ Path::toNativeSeparators (Path::join (Path::getCurrentPath (), " CC:\\ path\\ files.txt" )),
515+ Path::getAbsoluteFilePath (" CC:/path/files.txt" )); // treated as filename
516+ #endif
517+
518+ #ifndef _WIN32
519+ ASSERT_THROW_EQUALS_2 (Path::getAbsoluteFilePath (" C:\\ path\\ files.txt" ), std::runtime_error, " path 'C:\\ path\\ files.txt' does not exist" );
520+ #endif
521+
522+ // TODO: test UNC paths
523+ // TODO: test with symlinks
524+ }
525+
526+ void exists () const {
527+ ScopedFile file (" testpath.txt" , " " , " testpath" );
528+ ScopedFile file2 (" testpath2.txt" , " " );
529+ ASSERT_EQUALS (true , Path::exists (" testpath" ));
530+ ASSERT_EQUALS (true , Path::exists (" testpath/testpath.txt" ));
531+ ASSERT_EQUALS (true , Path::exists (" testpath2.txt" ));
532+
533+ ASSERT_EQUALS (false , Path::exists (" testpath2" ));
534+ ASSERT_EQUALS (false , Path::exists (" testpath/testpath2.txt" ));
535+ ASSERT_EQUALS (false , Path::exists (" testpath.txt" ));
536+ }
440537};
441538
442539REGISTER_TEST (TestPath)
0 commit comments