forked from TestableIO/System.IO.Abstractions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
139fdaa
commit df30d1b
Showing
49 changed files
with
5,224 additions
and
5,204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
# Disable LF normalization for all files | ||
* -text | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
|
||
# Custom for Visual Studio | ||
*.cs diff=csharp | ||
*.sln merge=union | ||
*.csproj merge=union | ||
*.vbproj merge=union | ||
*.fsproj merge=union | ||
*.dbproj merge=union | ||
|
||
# Standard to msysgit | ||
*.doc diff=astextplain | ||
*.DOC diff=astextplain | ||
*.docx diff=astextplain | ||
*.DOCX diff=astextplain | ||
*.dot diff=astextplain | ||
*.DOT diff=astextplain | ||
*.pdf diff=astextplain | ||
*.PDF diff=astextplain | ||
*.rtf diff=astextplain | ||
*.RTF diff=astextplain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
Microsoft Public License (Ms-PL) | ||
This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software. | ||
1. Definitions | ||
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law. | ||
A "contribution" is the original software, or any additions or changes to the software. | ||
A "contributor" is any person that distributes its contribution under this license. | ||
"Licensed patents" are a contributor's patent claims that read directly on its contribution. | ||
2. Grant of Rights | ||
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create. | ||
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software. | ||
3. Conditions and Limitations | ||
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. | ||
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically. | ||
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software. | ||
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license. | ||
Microsoft Public License (Ms-PL) | ||
|
||
This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software. | ||
|
||
1. Definitions | ||
|
||
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law. | ||
|
||
A "contribution" is the original software, or any additions or changes to the software. | ||
|
||
A "contributor" is any person that distributes its contribution under this license. | ||
|
||
"Licensed patents" are a contributor's patent claims that read directly on its contribution. | ||
|
||
2. Grant of Rights | ||
|
||
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create. | ||
|
||
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software. | ||
|
||
3. Conditions and Limitations | ||
|
||
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. | ||
|
||
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically. | ||
|
||
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software. | ||
|
||
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license. | ||
|
||
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
@echo off | ||
setlocal | ||
set tempscript=%temp%\%~n0.%random%.ps1 | ||
echo $ErrorActionPreference="Stop" >"%tempscript%" | ||
echo ^& "%~dpn0.ps1" %* >>"%tempscript%" | ||
powershell.exe -command "& \"%tempscript%\"" | ||
set errlvl=%ERRORLEVEL% | ||
del "%tempscript%" | ||
@echo off | ||
setlocal | ||
set tempscript=%temp%\%~n0.%random%.ps1 | ||
echo $ErrorActionPreference="Stop" >"%tempscript%" | ||
echo ^& "%~dpn0.ps1" %* >>"%tempscript%" | ||
powershell.exe -command "& \"%tempscript%\"" | ||
set errlvl=%ERRORLEVEL% | ||
del "%tempscript%" | ||
exit /b %errlvl% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,48 @@ | ||
param ( | ||
[Parameter(Mandatory=$true)] | ||
[ValidatePattern("\d\.\d\.\d\.\d")] | ||
[string] | ||
$ReleaseVersionNumber, | ||
|
||
[switch]$Push | ||
) | ||
|
||
$ErrorActionPreference = "Stop" | ||
|
||
$PSScriptFilePath = (Get-Item $MyInvocation.MyCommand.Path).FullName | ||
|
||
$SolutionRoot = Split-Path -Path $PSScriptFilePath -Parent | ||
$NuGetExe = Join-Path $SolutionRoot -ChildPath ".nuget/nuget.exe" | ||
|
||
# Build the NuGet package | ||
$ProjectPath = Join-Path -Path $SolutionRoot -ChildPath "System.IO.Abstractions\System.IO.Abstractions.csproj" | ||
& $NuGetExe pack $ProjectPath -Prop Configuration=Release -OutputDirectory $SolutionRoot | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
|
||
$ProjectPath = Join-Path -Path $SolutionRoot -ChildPath "TestingHelpers\TestingHelpers.csproj" | ||
& $NuGetExe pack $ProjectPath -Prop Configuration=Release -OutputDirectory $SolutionRoot | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
|
||
# Upload the NuGet package | ||
if ($Push) | ||
{ | ||
$NuPkgPath = Join-Path -Path $SolutionRoot -ChildPath "System.IO.Abstractions.$ReleaseVersionNumber.nupkg" | ||
& $NuGetExe push $NuPkgPath | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
|
||
$NuPkgPath = Join-Path -Path $SolutionRoot -ChildPath "System.IO.Abstractions.TestingHelpers.$ReleaseVersionNumber.nupkg" | ||
& $NuGetExe push $NuPkgPath | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
param ( | ||
[Parameter(Mandatory=$true)] | ||
[ValidatePattern("\d\.\d\.\d\.\d")] | ||
[string] | ||
$ReleaseVersionNumber, | ||
|
||
[switch]$Push | ||
) | ||
|
||
$ErrorActionPreference = "Stop" | ||
|
||
$PSScriptFilePath = (Get-Item $MyInvocation.MyCommand.Path).FullName | ||
|
||
$SolutionRoot = Split-Path -Path $PSScriptFilePath -Parent | ||
$NuGetExe = Join-Path $SolutionRoot -ChildPath ".nuget/nuget.exe" | ||
|
||
# Build the NuGet package | ||
$ProjectPath = Join-Path -Path $SolutionRoot -ChildPath "System.IO.Abstractions\System.IO.Abstractions.csproj" | ||
& $NuGetExe pack $ProjectPath -Prop Configuration=Release -OutputDirectory $SolutionRoot | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
|
||
$ProjectPath = Join-Path -Path $SolutionRoot -ChildPath "TestingHelpers\TestingHelpers.csproj" | ||
& $NuGetExe pack $ProjectPath -Prop Configuration=Release -OutputDirectory $SolutionRoot | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
|
||
# Upload the NuGet package | ||
if ($Push) | ||
{ | ||
$NuPkgPath = Join-Path -Path $SolutionRoot -ChildPath "System.IO.Abstractions.$ReleaseVersionNumber.nupkg" | ||
& $NuGetExe push $NuPkgPath | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
|
||
$NuPkgPath = Join-Path -Path $SolutionRoot -ChildPath "System.IO.Abstractions.TestingHelpers.$ReleaseVersionNumber.nupkg" | ||
& $NuGetExe push $NuPkgPath | ||
if (-not $?) | ||
{ | ||
throw "The NuGet process returned an error code." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,85 @@ | ||
Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access! | ||
|
||
NuGet only: | ||
|
||
Install-Package System.IO.Abstractions | ||
|
||
and/or: | ||
|
||
Install-Package System.IO.Abstractions.TestingHelpers | ||
|
||
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. | ||
|
||
```csharp | ||
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."); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The library also ships with a series of test helpers to save you from having to mock out every call: | ||
|
||
```csharp | ||
[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.AreEqual("We can't go on together. It's not me, it's you.", ex.Message); | ||
return; | ||
} | ||
|
||
Assert.Fail("The expected exception was not thrown."); | ||
} | ||
``` | ||
We even support casting from the .NET Framework's untestable types to our testable wrappers: | ||
|
||
```csharp | ||
FileInfo SomeBadApiMethodThatReturnsFileInfo() | ||
{ | ||
return new FileInfo("a"); | ||
} | ||
|
||
void MyFancyMethod() | ||
{ | ||
var testableFileInfo = (FileInfoBase)SomeBadApiMethodThatReturnsFileInfo(); | ||
... | ||
} | ||
Just like System.Web.Abstractions, but for System.IO. Yay for testable IO access! | ||
|
||
NuGet only: | ||
|
||
Install-Package System.IO.Abstractions | ||
|
||
and/or: | ||
|
||
Install-Package System.IO.Abstractions.TestingHelpers | ||
|
||
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. | ||
|
||
```csharp | ||
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."); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The library also ships with a series of test helpers to save you from having to mock out every call: | ||
|
||
```csharp | ||
[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.AreEqual("We can't go on together. It's not me, it's you.", ex.Message); | ||
return; | ||
} | ||
|
||
Assert.Fail("The expected exception was not thrown."); | ||
} | ||
``` | ||
We even support casting from the .NET Framework's untestable types to our testable wrappers: | ||
|
||
```csharp | ||
FileInfo SomeBadApiMethodThatReturnsFileInfo() | ||
{ | ||
return new FileInfo("a"); | ||
} | ||
|
||
void MyFancyMethod() | ||
{ | ||
var testableFileInfo = (FileInfoBase)SomeBadApiMethodThatReturnsFileInfo(); | ||
... | ||
} | ||
``` |
Oops, something went wrong.