Skip to content

Commit 8962d53

Browse files
authored
Merge pull request #12701 from a3d4/fix-running-tests-from-any-drive
Fix running path-related tests from any Windows drive
2 parents 5a94a7c + 50ae21d commit 8962d53

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

libsolidity/interface/FileReader.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,8 @@ boost::filesystem::path FileReader::normalizeCLIPathForVFS(
276276
// If the path is on the same drive as the working dir, for portability we prefer not to
277277
// include the root name. Do this only for non-UNC paths - my experiments show that on Windows
278278
// when the working dir is an UNC path, / does not not actually refer to the root of the UNC path.
279-
boost::filesystem::path normalizedRootPath = normalizedPath.root_path();
280-
if (!isUNCPath(normalizedPath))
281-
{
282-
boost::filesystem::path workingDirRootPath = canonicalWorkDir.root_path();
283-
// Ignore drive letter case on Windows (C:\ <=> c:\).
284-
if (boost::filesystem::equivalent(normalizedRootPath, workingDirRootPath))
285-
normalizedRootPath = "/";
286-
}
279+
280+
boost::filesystem::path normalizedRootPath = normalizeCLIRootPathForVFS(normalizedPath, canonicalWorkDir);
287281

288282
// lexically_normal() will not squash paths like "/../../" into "/". We have to do it manually.
289283
boost::filesystem::path dotDotPrefix = absoluteDotDotPrefix(normalizedPath);
@@ -308,6 +302,27 @@ boost::filesystem::path FileReader::normalizeCLIPathForVFS(
308302
return normalizedPathNoDotDot;
309303
}
310304

305+
boost::filesystem::path FileReader::normalizeCLIRootPathForVFS(
306+
boost::filesystem::path const& _path,
307+
boost::filesystem::path const& _workDir
308+
)
309+
{
310+
solAssert(_workDir.is_absolute(), "");
311+
312+
boost::filesystem::path absolutePath = boost::filesystem::absolute(_path, _workDir);
313+
boost::filesystem::path rootPath = absolutePath.root_path();
314+
boost::filesystem::path baseRootPath = _workDir.root_path();
315+
316+
if (isUNCPath(absolutePath))
317+
return rootPath;
318+
319+
// Ignore drive letter case on Windows (C:\ <=> c:\).
320+
if (boost::filesystem::equivalent(rootPath, baseRootPath))
321+
return "/";
322+
323+
return rootPath;
324+
}
325+
311326
bool FileReader::isPathPrefix(boost::filesystem::path const& _prefix, boost::filesystem::path const& _path)
312327
{
313328
solAssert(!_prefix.empty() && !_path.empty(), "");

libsolidity/interface/FileReader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ class FileReader
115115
SymlinkResolution _symlinkResolution = SymlinkResolution::Disabled
116116
);
117117

118+
/// Normalizes a root path by excluding, in some cases, its root name.
119+
/// The function is used for better portability, and intended to omit root name
120+
/// if the path can be used without it.
121+
/// @param _path Path to normalize the root path.
122+
/// @param _workDir Current working directory path, must be absolute.
123+
/// @returns a normalized root path.
124+
static boost::filesystem::path normalizeCLIRootPathForVFS(
125+
boost::filesystem::path const& _path,
126+
boost::filesystem::path const& _workDir = boost::filesystem::current_path()
127+
);
128+
118129
/// @returns true if all the path components of @a _prefix are present at the beginning of @a _path.
119130
/// Both paths must be absolute (or have slash as root) and normalized (no . or .. segments, no
120131
/// multiple consecutive slashes).

test/libsolidity/interface/FileReader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ BOOST_AUTO_TEST_CASE(normalizeCLIPathForVFS_should_not_resolve_symlinks_unless_r
313313
if (!createSymlinkIfSupportedByFilesystem(tempDir.path() / "abc", tempDir.path() / "sym", true))
314314
return;
315315

316-
boost::filesystem::path expectedPrefixWithSymlinks = "/" / tempDir.path().relative_path();
317-
boost::filesystem::path expectedPrefixWithoutSymlinks = "/" / boost::filesystem::weakly_canonical(tempDir).relative_path();
316+
boost::filesystem::path expectedRootPath = FileReader::normalizeCLIRootPathForVFS(tempDir);
317+
boost::filesystem::path expectedPrefixWithSymlinks = expectedRootPath / tempDir.path().relative_path();
318+
boost::filesystem::path expectedPrefixWithoutSymlinks = expectedRootPath / boost::filesystem::weakly_canonical(tempDir).relative_path();
318319

319320
BOOST_CHECK_EQUAL(
320321
FileReader::normalizeCLIPathForVFS(tempDir.path() / "sym/contract.sol", SymlinkResolution::Disabled),

test/solc/CommandLineInterface.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ BOOST_AUTO_TEST_CASE(cli_input)
178178
createFilesWithParentDirs({tempDir1.path() / "input1.sol"});
179179
createFilesWithParentDirs({tempDir2.path() / "input2.sol"});
180180

181-
boost::filesystem::path expectedDir1 = "/" / tempDir1.path().relative_path();
182-
boost::filesystem::path expectedDir2 = "/" / tempDir2.path().relative_path();
181+
boost::filesystem::path expectedRootPath = FileReader::normalizeCLIRootPathForVFS(tempDir1);
182+
boost::filesystem::path expectedDir1 = expectedRootPath / tempDir1.path().relative_path();
183+
boost::filesystem::path expectedDir2 = expectedRootPath / tempDir2.path().relative_path();
183184
soltestAssert(expectedDir1.is_absolute() || expectedDir1.root_path() == "/", "");
184185
soltestAssert(expectedDir2.is_absolute() || expectedDir2.root_path() == "/", "");
185186

@@ -223,7 +224,8 @@ BOOST_AUTO_TEST_CASE(cli_ignore_missing_some_files_exist)
223224
TemporaryDirectory tempDir2(TEST_CASE_NAME);
224225
createFilesWithParentDirs({tempDir1.path() / "input1.sol"});
225226

226-
boost::filesystem::path expectedDir1 = "/" / tempDir1.path().relative_path();
227+
boost::filesystem::path expectedRootPath = FileReader::normalizeCLIRootPathForVFS(tempDir1);
228+
boost::filesystem::path expectedDir1 = expectedRootPath / tempDir1.path().relative_path();
227229
soltestAssert(expectedDir1.is_absolute() || expectedDir1.root_path() == "/", "");
228230

229231
// NOTE: Allowed paths should not be added for skipped files.

0 commit comments

Comments
 (0)