Skip to content

Commit

Permalink
fixup! FileReader: Normalize base path and strip it from normalized s…
Browse files Browse the repository at this point in the history
…ource paths
  • Loading branch information
cameel committed Jul 16, 2021
1 parent 9eb0e61 commit f6fde4e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
5 changes: 1 addition & 4 deletions libsolidity/interface/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ namespace solidity::frontend

void FileReader::setBasePath(boost::filesystem::path const& _path)
{
if (_path.empty())
m_basePath = "";
else
m_basePath = normalizeCLIPathForVFS(_path);
m_basePath = (_path.empty() ? "" : normalizeCLIPathForVFS(_path));
}

void FileReader::setSource(boost::filesystem::path const& _path, SourceCode _source)
Expand Down
10 changes: 5 additions & 5 deletions libsolidity/interface/FileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class FileReader
/// The path does not have to actually exist.
static boost::filesystem::path normalizeCLIPathForVFS(boost::filesystem::path const& _path);

/// Returns true if all the path components of @a _prefix are present at the beginning of @a _path.
/// @returns true if all the path components of @a _prefix are present at the beginning of @a _path.
/// Both paths must be absolute (or have slash as root) and normalized (no . or .. segments, no
/// multiple consecutive slashes).
/// Paths are treated as case-sensitive. Does not require the path to actually exist in the
Expand All @@ -101,11 +101,11 @@ class FileReader
static bool isPathPrefix(boost::filesystem::path _prefix, boost::filesystem::path const& _path);

/// If @a _prefix is actually a prefix of @p _path, removes it from @a _path to make it relative.
/// Otherwise returns @a _path unchanged.
/// Returns '.' if @a _path and @_prefix are identical.
/// @returns The path without the prefix or unchanged path if there is not prefix.
/// If @a _path and @_prefix are identical, the result is '.'.
static boost::filesystem::path stripPrefixIfPresent(boost::filesystem::path _prefix, boost::filesystem::path const& _path);

// Returns true if the specified path is an UNC path.
// @returns true if the specified path is an UNC path.
// UNC paths start with // followed by a name (on Windows they can also start with \\).
// They are used for network shares on Windows. On UNIX systems they do not have the same
// functionality but usually they are still recognized and treated in a special way.
Expand All @@ -117,7 +117,7 @@ class FileReader
/// absolute (or have slash as root).
static boost::filesystem::path absoluteDotDotPrefix(boost::filesystem::path const& _path);

/// Returns true if the path contains any .. segments.
/// @returns true if the path contains any .. segments.
static bool hasDotDotSegments(boost::filesystem::path const& _path);

/// Base path, used for resolving relative paths in imports.
Expand Down
56 changes: 28 additions & 28 deletions test/libsolidity/interface/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,37 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_relative_path)
boost::filesystem::create_directories(tempDir.path() / "x/y/z");
TemporaryWorkingDirectory tempWorkDir(tempDir.path() / "x/y/z");

// NOTE: If path to work dir contains symlinks (of then the case on macOS), boost might resolve
// NOTE: If path to work dir contains symlinks (often the case on macOS), boost might resolve
// them, making the path different from tempDirPath.
boost::filesystem::path expectedWorkDir = boost::filesystem::current_path().parent_path().parent_path().parent_path();
boost::filesystem::path expectedPrefix = boost::filesystem::current_path().parent_path().parent_path().parent_path();
// On Windows tempDir.path() normally contains the drive letter while the normalized path should not.
expectedWorkDir = "/" / expectedWorkDir.relative_path();
soltestAssert(expectedWorkDir.is_absolute() || expectedWorkDir.root_path() == "/", "");
expectedPrefix = "/" / expectedPrefix.relative_path();
soltestAssert(expectedPrefix.is_absolute() || expectedPrefix.root_path() == "/", "");

BOOST_TEST(FileReader::normalizeCLIPathForVFS(".") == expectedWorkDir / "x/y/z/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./") == expectedWorkDir / "x/y/z/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("../") == expectedWorkDir / "x/y/");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("a") == expectedWorkDir / "x/y/z/a");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/") == expectedWorkDir / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/.") == expectedWorkDir / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a") == expectedWorkDir / "x/y/z/a");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a/") == expectedWorkDir / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a/.") == expectedWorkDir / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b") == expectedWorkDir / "x/y/z/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b/") == expectedWorkDir / "x/y/z/a/b/");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("../a/b") == expectedWorkDir / "x/y/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("../../a/b") == expectedWorkDir / "x/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a/b") == expectedWorkDir / "x/y/z/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("././a/b") == expectedWorkDir / "x/y/z/a/b");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/./b/") == expectedWorkDir / "x/y/z/a/b/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/../a/b/") == expectedWorkDir / "x/y/z/a/b/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b/c/..") == expectedWorkDir / "x/y/z/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b/c/../") == expectedWorkDir / "x/y/z/a/b/");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("../../a/.././../p/../q/../a/b") == expectedWorkDir / "a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS(".") == expectedPrefix / "x/y/z/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./") == expectedPrefix / "x/y/z/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("../") == expectedPrefix / "x/y/");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("a") == expectedPrefix / "x/y/z/a");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/") == expectedPrefix / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/.") == expectedPrefix / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a") == expectedPrefix / "x/y/z/a");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a/") == expectedPrefix / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a/.") == expectedPrefix / "x/y/z/a/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b") == expectedPrefix / "x/y/z/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b/") == expectedPrefix / "x/y/z/a/b/");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("../a/b") == expectedPrefix / "x/y/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("../../a/b") == expectedPrefix / "x/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("./a/b") == expectedPrefix / "x/y/z/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("././a/b") == expectedPrefix / "x/y/z/a/b");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/./b/") == expectedPrefix / "x/y/z/a/b/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/../a/b/") == expectedPrefix / "x/y/z/a/b/");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b/c/..") == expectedPrefix / "x/y/z/a/b");
BOOST_TEST(FileReader::normalizeCLIPathForVFS("a/b/c/../") == expectedPrefix / "x/y/z/a/b/");

BOOST_TEST(FileReader::normalizeCLIPathForVFS("../../a/.././../p/../q/../a/b") == expectedPrefix / "a/b");

}

Expand Down

0 comments on commit f6fde4e

Please sign in to comment.