MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / getAbsolutePath

Method getAbsolutePath

test/testpath.cpp:502–583  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

500 }
501
502 void getAbsolutePath() const {
503 const std::string cwd = Path::getCurrentPath();
504
505 ScopedFile file("testabspath.txt", "");
506 std::string expected = Path::toNativeSeparators(Path::join(cwd, "testabspath.txt"));
507
508 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("testabspath.txt"));
509 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("./testabspath.txt"));
510 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath(".\\testabspath.txt"));
511 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("test/../testabspath.txt"));
512 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("test\\..\\testabspath.txt"));
513 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("./test/../testabspath.txt"));
514 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath(".\\test\\../testabspath.txt"));
515
516 ASSERT_EQUALS(expected, Path::getAbsoluteFilePath(Path::join(cwd, "testabspath.txt")));
517
518 std::string cwd_up = Path::getPathFromFilename(cwd);
519 cwd_up.pop_back(); // remove trailing slash
520 ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "..")));
521 ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "../")));
522 ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "..\\")));
523 ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "./../")));
524 ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, ".\\..\\")));
525
526 ASSERT_EQUALS(cwd, Path::getAbsoluteFilePath("."));
527#ifndef _WIN32
528 TODO_ASSERT_EQUALS(cwd, "", Path::getAbsoluteFilePath("./"));
529 TODO_ASSERT_EQUALS(cwd, "", Path::getAbsoluteFilePath(".\\"));
530#else
531 ASSERT_EQUALS(cwd, Path::getAbsoluteFilePath("./"));
532 ASSERT_EQUALS(cwd, Path::getAbsoluteFilePath(".\\"));
533#endif
534
535 ASSERT_EQUALS("", Path::getAbsoluteFilePath(""));
536
537#ifndef _WIN32
538 // the underlying realpath() call only returns something if the path actually exists
539 ASSERT_THROW_EQUALS(Path::getAbsoluteFilePath("testabspath2.txt"), std::runtime_error, "path 'testabspath2.txt' does not exist");
540#else
541 ASSERT_EQUALS(Path::toNativeSeparators(Path::join(cwd, "testabspath2.txt")), Path::getAbsoluteFilePath("testabspath2.txt"));
542#endif
543
544#ifdef _WIN32
545 // determine an existing drive letter
546 std::string drive = Path::getCurrentPath().substr(0, 2);
547 ASSERT_EQUALS(drive + "", Path::getAbsoluteFilePath(drive + "\\"));
548 ASSERT_EQUALS(drive + "\\path", Path::getAbsoluteFilePath(drive + "\\path"));
549 ASSERT_EQUALS(drive + "\\path", Path::getAbsoluteFilePath(drive + "\\path\\"));
550 ASSERT_EQUALS(drive + "\\path\\files.txt", Path::getAbsoluteFilePath(drive + "\\path\\files.txt"));
551 ASSERT_EQUALS(drive + "", Path::getAbsoluteFilePath(drive + "//"));
552 ASSERT_EQUALS(drive + "\\path", Path::getAbsoluteFilePath(drive + "//path"));
553 ASSERT_EQUALS(drive + "\\path", Path::getAbsoluteFilePath(drive + "//path/"));
554 ASSERT_EQUALS(drive + "\\path\\files.txt", Path::getAbsoluteFilePath(drive + "//path/files.txt"));
555 ASSERT_EQUALS(drive + "\\path", Path::getAbsoluteFilePath(drive + "\\\\path"));
556 ASSERT_EQUALS(drive + "", Path::getAbsoluteFilePath(drive + "/"));
557 ASSERT_EQUALS(drive + "\\path", Path::getAbsoluteFilePath(drive + "/path"));
558
559 drive[0] = static_cast<char>(toupper(drive[0]));

Callers

nothing calls this directly

Calls 1

joinFunction · 0.85

Tested by

no test coverage detected