Skip to content

IDirectoryInfoFactory.Wrap loses part of absolute path when used with MockFileSystem #1049

Closed

Description

@koyote

Describe the bug
If you provide the MockFileSystem's DirectoryInfo.Wrap method with a DirectoryInfo that has an absolute path, the resulting IDirectoryInfo has the wrong file path.

To Reproduce

using Xunit;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;

[Fact]
public void Test()
{
    var fs = new MockFileSystem();
    var directoryInfo = new DirectoryInfo(@"C:\subfolder\file");
    var wrappedDirectoryInfo = fs.DirectoryInfo.Wrap(directoryInfo);

    Assert.Equal(directoryInfo.FullName, wrappedDirectoryInfo.FullName); // wrappedDirectoryInfo returns "C:\file"
}

Expected behaviour
The test above is expected to pass.

Actual behaviour

Assert.Equal() Failure
             ↓ (pos 3)
Expected: C:\subfolder\file
Actual:   C:\file
             ↑ (pos 3)

Additional context
Tested on Windows 10, .Net 7, System.IO.Abstractions version 19.2.69

Activity

  1. added
    type: bugIssues that describe misbehaving functionality
    on Nov 3, 2023
  2. fgreinacher commented on Jan 9, 2024

    @fgreinacher
    Contributor

    Thanks for reporting and sorry for the late response @koyote! Do you know if this has ever worked?

  3. added
    state: ready to pickIssues that are ready for being worked on
    and removed on Jan 9, 2024
  4. koyote commented on Jan 9, 2024

    @koyote
    Author

    Hi @fgreinacher, I do not, no.
    I just recently started using this library and quickly encountered the above bug.

    But just looking at the code for DirectoryInfo.Wrap:

    public IDirectoryInfo Wrap(DirectoryInfo directoryInfo)
    {
        if (directoryInfo == null)
        {
            return null;
        }
    
        return new MockDirectoryInfo(mockFileSystem, directoryInfo.Name);
    }

    It looks like it's sending in directoryInfo.Name, which is only the dir name, not the full path.
    I'd assume changing that to directoryInfo.FullName would fix it but I don't quite have time at the moment to verify that; maybe this weekend.

  5. vbreuss commented on Jan 22, 2024

    @vbreuss
    Member

    Please note, that the same issue also applies when wrapping a FileInfo. I fixed both issues with #1079.

  6. added a commit that references this issue on Jan 22, 2024
    3d517f4
  7. github-actions commented on Jan 22, 2024

    @github-actions

    This is addressed in release v20.0.15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testinghelpersIssues that address the testing helpersstate: ready to pickIssues that are ready for being worked onstate: releasedIssues that are releasedtype: 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