Skip to content

Path.GetRelativePath returns inconsistent results #728

@antonyoni

Description

@antonyoni

Describe the bug

On windows, when using MockFileSystem().Path.GetRelativePath and the relativeTo path does not have a drive specified, the result is inconsistent depending on what drive you run the code on.

To Reproduce

var fs = new MockFileSystem();
fs.AddDirectory("/dir/subdir");
foreach (var d in fs.Directory.GetDirectories("/dir")) {
    Console.WriteLine(d);
    var relPath = fs.Path.GetRelativePath("/dir", d);
    Console.WriteLine(relPath);
}

If I run this from the c drive (c:\temp\io-test), the output is:

C:\dir\subdir
subdir

But running it from the d drive, results in:

C:\dir\subdir
C:\dir\subdir

Expected behavior

subdir on both c and d drives.

Additional context

PathWrapper.GetRelativePath calls to the System.IO.Path.GetRelativePath which in turn calls to System.IO.Path.GetFullPath (https://github.com/dotnet/runtime/blob/01b7e73cd378145264a7cb7a09365b41ed42b240/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs#L882) which returns D:\dir for /dir.

We use paths without drive letters to make the tests compatible with both linux and windows. Guess the fix would be to implement GetRelativePath in PathWrapper, or to make MockFileSystem use the same root drive as System.IO?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked ontype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions