System.IO.Abstractions 21.1.7

System.IO.Abstractions NuGet Continuous Integration Codacy Badge Renovate enabled FOSSA Status

At the core of the library is IFileSystem and FileSystem. Instead of calling methods like File.ReadAllText directly, use IFileSystem.File.ReadAllText. We have exactly the same API, except that ours is injectable and testable.

Usage

dotnet add package TestableIO.System.IO.Abstractions.Wrappers

Note: This NuGet package is also published as System.IO.Abstractions but we suggest to use the prefix to make clear that this is not an official .NET package.

public class MyComponent
{
    readonly IFileSystem fileSystem;

    // <summary>Create MyComponent with the given fileSystem implementation</summary>
    public MyComponent(IFileSystem fileSystem)
    {
        this.fileSystem = fileSystem;
    }
    /// <summary>Create MyComponent</summary>
    public MyComponent() : this(
        fileSystem: new FileSystem() //use default implementation which calls System.IO
    )
    {
    }

    public void Validate()
    {
        foreach (var textFile in fileSystem.Directory.GetFiles(@"c:\", "*.txt", SearchOption.TopDirectoryOnly))
        {
            var text = fileSystem.File.ReadAllText(textFile);
            if (text != "Testing is awesome.")
                throw new NotSupportedException("We can't go on together. It's not me, it's you.");
        }
    }
}

Test helpers

The library also ships with a series of test helpers to save you from having to mock out every call, for basic scenarios. They are not a complete copy of a real-life file system, but they'll get you most of the way there.

dotnet add package TestableIO.System.IO.Abstractions.TestingHelpers

Note: This NuGet package is also published as System.IO.Abstractions.TestingHelpers but we suggest to use the prefix to make clear that this is not an official .NET package.

[Test]
public void MyComponent_Validate_ShouldThrowNotSupportedExceptionIfTestingIsNotAwesome()
{
    // Arrange
    var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
    {
        { @"c:\myfile.txt", new MockFileData("Testing is meh.") },
        { @"c:\demo\jQuery.js", new MockFileData("some js") },
        { @"c:\demo\image.gif", new MockFileData(new byte[] { 0x12, 0x34, 0x56, 0xd2 }) }
    });
    var component = new MyComponent(fileSystem);

    try
    {
        // Act
        component.Validate();
    }
    catch (NotSupportedException ex)
    {
        // Assert
        Assert.That(ex.Message, Is.EqualTo("We can't go on together. It's not me, it's you."));
        return;
    }

    Assert.Fail("The expected exception was not thrown.");
}

We even support casting from the .NET Framework's untestable types to our testable wrappers:

FileInfo SomeApiMethodThatReturnsFileInfo()
{
    return new FileInfo("a");
}

void MyFancyMethod()
{
    var testableFileInfo = (FileInfoBase)SomeApiMethodThatReturnsFileInfo();
    ...
}

Mock support

Since version 4.0 the top-level APIs expose interfaces instead of abstract base classes (these still exist, though), allowing you to completely mock the file system. Here's a small example, using Moq:

[Test]
public void Test1()
{
    var watcher = Mock.Of<IFileSystemWatcher>();
    var file = Mock.Of<IFile>();

    Mock.Get(file).Setup(f => f.Exists(It.IsAny<string>())).Returns(true);
    Mock.Get(file).Setup(f => f.ReadAllText(It.IsAny<string>())).Throws<OutOfMemoryException>();

    var unitUnderTest = new SomeClassUsingFileSystemWatcher(watcher, file);

    Assert.Throws<OutOfMemoryException>(() => {
        Mock.Get(watcher).Raise(w => w.Created += null, new System.IO.FileSystemEventArgs(System.IO.WatcherChangeTypes.Created, @"C:\Some\Directory", "Some.File"));
    });

    Mock.Get(file).Verify(f => f.Exists(It.IsAny<string>()), Times.Once);

    Assert.True(unitUnderTest.FileWasCreated);
}

public class SomeClassUsingFileSystemWatcher
{
    private readonly IFileSystemWatcher _watcher;
    private readonly IFile _file;

    public bool FileWasCreated { get; private set; }

    public SomeClassUsingFileSystemWatcher(IFileSystemWatcher watcher, IFile file)
    {
        this._file = file;
        this._watcher = watcher;
        this._watcher.Created += Watcher_Created;
    }

    private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        FileWasCreated = true;

        if(_file.Exists(e.FullPath))
        {
            var text = _file.ReadAllText(e.FullPath);
        }
    }
}

Showing the top 20 packages that depend on System.IO.Abstractions.

Packages Downloads
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
6
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
7
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
8
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
9
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
10
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
11
KubernetesClient
Client library for the Kubernetes open source container orchestrator.
13

Version Downloads Last updated
21.1.7 2 12/24/2024
21.1.3 4 11/12/2024
21.1.2 6 11/20/2024
21.1.1 2 12/09/2024
21.0.29 4 12/07/2024
21.0.26 4 12/15/2024
21.0.22 4 12/03/2024
21.0.2 8 05/18/2024
20.0.34 9 05/15/2024
20.0.28 9 05/23/2024
20.0.15 13 04/10/2024
20.0.4 10 12/20/2023
20.0.1 14 04/09/2024
19.2.91 9 05/25/2024
19.2.87 8 05/23/2024
19.2.69 8 05/15/2024
19.2.67 6 05/19/2024
19.2.66 12 05/11/2024
19.2.64 7 05/25/2024
19.2.63 6 05/03/2024
19.2.61 9 05/24/2024
19.2.51 10 05/25/2024
19.2.50 7 05/25/2024
19.2.29 11 05/08/2024
19.2.26 6 05/25/2024
19.2.25 7 05/07/2024
19.2.22 9 05/09/2024
19.2.18 11 05/13/2024
19.2.17 9 05/25/2024
19.2.16 11 05/10/2024
19.2.15 9 05/22/2024
19.2.13 8 05/25/2024
19.2.12 11 05/11/2024
19.2.11 8 05/15/2024
19.2.9 7 05/21/2024
19.2.8 11 04/30/2024
19.2.4 8 05/17/2024
19.2.1 11 05/09/2024
19.1.18 9 05/06/2024
19.1.14 10 05/04/2024
19.1.13 6 05/25/2024
19.1.5 12 05/19/2024
19.1.1 11 05/02/2024
19.0.1 11 05/19/2024
18.0.1 8 04/22/2024
17.2.26 12 05/11/2024
17.2.3 8 05/25/2024
17.2.1 8 04/09/2024
17.1.1 8 05/25/2024
17.0.28 16 04/09/2024
17.0.24 8 05/25/2024
17.0.23 7 05/02/2024
17.0.21 7 05/25/2024
17.0.18 8 05/24/2024
17.0.15 8 05/06/2024
17.0.14 11 05/25/2024
17.0.13 10 05/24/2024
17.0.12 8 05/25/2024
17.0.11 9 05/25/2024
17.0.10 8 05/02/2024
17.0.9 12 05/23/2024
17.0.8 9 05/16/2024
17.0.7 8 05/24/2024
17.0.6 9 05/09/2024
17.0.5 5 05/25/2024
17.0.4 11 05/15/2024
17.0.3 11 05/05/2024
17.0.2 10 05/25/2024
17.0.1 10 05/25/2024
16.1.26 7 05/25/2024
16.1.22 7 05/25/2024
16.1.18 9 05/14/2024
16.1.16 7 05/01/2024
16.1.13 10 05/03/2024
16.1.12 10 05/07/2024
16.1.9 8 05/22/2024
16.1.4 6 05/23/2024
16.0.8 7 05/06/2024
16.0.7 9 05/13/2024
16.0.6 12 05/13/2024
16.0.5 10 05/19/2024
16.0.4 8 05/02/2024
16.0.3 13 05/15/2024
16.0.2 11 05/10/2024
16.0.1 7 05/21/2024
15.0.1 7 05/20/2024
14.0.13 9 05/06/2024
14.0.12 10 05/06/2024
14.0.11 12 05/10/2024
14.0.10 10 05/14/2024
14.0.9 10 05/11/2024
14.0.7 7 05/25/2024
14.0.6 8 05/10/2024
14.0.5 6 05/24/2024
14.0.4 9 05/25/2024
14.0.3 9 05/21/2024
13.2.32 8 05/25/2024
13.2.31 10 05/19/2024
13.2.9 10 05/09/2024
13.2.7 8 05/05/2024
13.2.5 7 05/08/2024
13.2.4 7 05/06/2024
13.2.3 11 05/16/2024
13.0.1 9 05/14/2024
12.2.26 6 05/25/2024
12.2.25 7 05/25/2024
12.2.7 7 05/25/2024
12.2.4 7 04/30/2024
12.2.1 9 05/25/2024
12.1.11 14 05/08/2024
12.1.2 8 05/07/2024
12.0.7 9 05/16/2024
12.0.2 9 05/25/2024
11.0.18 10 05/25/2024
11.0.17 10 05/25/2024
11.0.16 11 05/18/2024
11.0.13 7 05/31/2024
11.0.11 10 05/03/2024
11.0.9 7 05/10/2024
11.0.7 10 04/26/2022
11.0.5 8 05/25/2024
11.0.4 9 04/26/2022
11.0.2 9 05/19/2024
10.0.9 8 05/22/2024
10.0.8 11 05/01/2024
10.0.6 10 05/13/2024
10.0.5 10 05/01/2024
10.0.4 10 05/25/2024
9.0.6 12 05/18/2024
9.0.4 10 05/14/2024
9.0.3 10 05/25/2024
9.0.2 11 05/09/2024
8.1.1 10 05/23/2024
8.0.3 9 05/22/2024
7.1.4 12 05/23/2024
7.1.1 8 05/11/2024
7.0.15 6 05/21/2024
6.0.36 9 05/13/2024
6.0.34 10 05/07/2024
6.0.27 10 05/09/2024
6.0.25 11 05/25/2024
6.0.23 7 05/01/2024
6.0.21 11 05/02/2024
6.0.17 12 05/21/2024
6.0.14 9 05/25/2024
6.0.13 7 05/04/2024
6.0.7 10 05/25/2024
6.0.6 9 05/13/2024
6.0.5 12 05/19/2024
6.0.3 11 05/15/2024
6.0.1 14 04/26/2022
4.2.15 6 05/25/2024
4.2.12 9 05/08/2024
4.2.9 11 05/22/2024
4.2.8 8 05/14/2024
4.1.6 8 05/10/2024
3.1.1 9 05/25/2024
3.0.2 8 05/25/2024
2.2.10-beta 9 04/23/2024
2.2.9-beta 11 05/02/2024
2.2.4-beta 9 05/17/2024
2.2.2-beta 14 04/24/2024
2.1.0.247 12 05/25/2024
2.1.0.237 7 05/25/2024
2.1.0.236 10 05/25/2024
2.1.0.235 7 05/25/2024
2.1.0.234 8 05/25/2024
2.1.0.231 11 05/25/2024
2.1.0.230 7 05/03/2024
2.1.0.229 8 05/25/2024
2.1.0.227 6 05/25/2024
2.1.0.226 8 05/04/2024
2.1.0.217 7 05/02/2024
2.1.0.216 8 05/11/2024
2.1.0.215 9 05/20/2024
2.1.0.214 8 05/25/2024
2.1.0.213 8 05/25/2024
2.1.0.211 8 05/25/2024
2.1.0.210 9 05/06/2024
2.1.0.209 5 05/11/2024
2.1.0.208 6 05/07/2024
2.1.0.207 8 05/25/2024
2.1.0.206 6 05/25/2024
2.1.0.204 10 05/25/2024
2.1.0.203 8 05/04/2024
2.1.0.202 7 05/25/2024
2.1.0.201 8 05/25/2024
2.1.0.193 9 05/14/2024
2.1.0.189 8 04/26/2022
2.1.0.187 8 05/07/2024
2.1.0.186 8 05/25/2024
2.1.0.184 9 05/25/2024
2.1.0.183 6 05/11/2024
2.1.0.182 11 05/25/2024
2.1.0.181 9 05/25/2024
2.1.0.180 12 05/25/2024
2.1.0.179 9 05/08/2024
2.1.0.178 7 05/06/2024
2.1.0.177 9 05/02/2024
2.1.0.176 9 05/25/2024
2.1.0.175 8 05/16/2024
2.1.0.174 9 05/09/2024
2.1.0.173 7 04/26/2022
2.1.0.171 7 05/25/2024
2.1.0.170 11 05/25/2024
2.1.0.168 9 05/02/2024
2.1.0.164 9 05/02/2024
2.1.0.159 9 05/25/2024
2.0.0.144 9 05/04/2024
2.0.0.143 7 05/25/2024
2.0.0.142 7 05/25/2024
2.0.0.141 9 05/09/2024
2.0.0.140 11 05/19/2024
2.0.0.139 6 05/25/2024
2.0.0.138 7 05/25/2024
2.0.0.136 8 05/25/2024
2.0.0.124 17 04/09/2024
2.0.0.120 7 05/16/2024
2.0.0.119 9 05/21/2024
2.0.0.118 9 05/24/2024
2.0.0.117 9 05/25/2024
2.0.0.115 8 05/25/2024
2.0.0.113 10 05/25/2024
2.0.0.111 8 05/25/2024
2.0.0.110 11 04/26/2022
2.0.0.109 9 04/26/2022
2.0.0.108 10 05/25/2024
2.0.0.107 13 04/10/2024
2.0.0.106 8 05/25/2024
2.0.0.105 6 05/19/2024
2.0.0.104 10 04/26/2022
2.0.0.103 9 05/25/2024
2.0.0.102 7 05/12/2024
2.0.0.101 10 04/26/2022
2.0.0.100 17 04/09/2024
2.0.0.98 10 04/09/2024
1.4.0.93 8 05/20/2024
1.4.0.92 11 05/02/2024
1.4.0.89 8 05/25/2024
1.4.0.88 7 05/07/2024
1.4.0.87 7 05/09/2024
1.4.0.86 8 05/08/2024
1.4.0.85 8 05/01/2024
1.4.0.84 9 05/09/2024
1.4.0.83 11 05/10/2024
1.4.0.82 10 05/21/2024
1.4.0.81 9 05/21/2024
1.4.0.80 6 05/12/2024
1.4.0.79 7 05/01/2024
1.4.0.78 10 05/04/2024
1.4.0.77 10 05/25/2024
1.4.0.76 8 05/10/2024
1.4.0.75 10 05/19/2024
1.4.0.74 9 05/02/2024
1.4.0.73 9 05/25/2024
1.4.0.72 9 05/09/2024
1.4.0.71 8 05/25/2024
1.4.0.70 6 05/10/2024
1.4.0.69 8 05/25/2024
1.4.0.68 11 05/25/2024
1.4.0.67 7 05/23/2024
1.4.0.66 7 05/02/2024
1.4.0.65 8 05/25/2024
1.4.0.64 7 05/08/2024
1.4.0.63 11 05/14/2024
1.4.0.62 11 05/25/2024
1.4.0.61 9 05/07/2024
1.4.0.60 6 05/18/2024
1.4.0.59 7 05/21/2024
1.4.0.58 7 05/13/2024
1.4.0.57 7 05/18/2024
1.4.0.56 11 05/01/2024
1.4.0.55 7 05/16/2024
1.4.0.54 11 05/17/2024
1.4.0.53 5 05/11/2024
1.4.0.52 8 05/25/2024
1.4.0.51 7 05/17/2024
1.4.0.50 8 05/25/2024
1.4.0.49 9 05/25/2024
1.4.0.48 10 05/22/2024
1.4.0.47 13 05/14/2024
1.4.0.46 10 05/15/2024
1.4.0.45 11 05/06/2024
1.4.0.44 11 05/02/2024
1.4.0.43 9 05/09/2024
1.4.0.42 9 05/16/2024
1.4.0.41 9 05/07/2024
1.4.0.40 9 05/23/2024
1.4.0.39 8 05/19/2024
1.4.0.38 7 05/09/2024
1.4.0.37 11 05/25/2024
1.4.0.36 10 05/14/2024
1.4.0.35 10 05/05/2024
1.4.0.34 8 05/02/2024
1.4.0.33 9 05/23/2024
1.4.0.32 10 05/25/2024
1.4.0.31 9 05/07/2024
1.4.0.30 8 05/24/2024
1.4.0.29 9 05/25/2024
1.4.0.28 8 05/25/2024
1.4.0.27 6 05/25/2024
1.4.0.26 11 05/25/2024
1.4.0.25 9 05/15/2024
1.4.0.24 9 05/19/2024
1.4.0.23 8 05/25/2024
1.4.0.22 7 05/17/2024
1.4.0.21 11 05/24/2024
1.4.0.20 20 04/09/2024
1.4.0.19 8 05/23/2024
1.4.0.18 11 05/18/2024
1.4.0.17 9 05/06/2024
1.4.0.14 9 05/25/2024
1.4.0.12 8 05/25/2024
1.4.0.11 9 05/15/2024
1.2.0 6 05/25/2024