Skip to content

Commit

Permalink
inject IFileSystem into AntDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Modylevsky authored and WichardRiezebos committed Sep 21, 2018
1 parent adf1963 commit 870b0f9
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 37 deletions.
112 changes: 94 additions & 18 deletions AntPathMatching.Tests/AntDirectoryTests.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,126 @@
using NUnit.Framework;
using System;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using FluentAssertions;

namespace AntPathMatching
{
[TestFixture]
public class AntDirectoryTests
{
[TestCase("/assets/**/*.txt", true)]
[TestCase("/assets/**/*.xml", false)]
[TestCase("/assets/dir1/*/file?.txt", true)]
[TestCase("/assets/dir1/*/file???.txt", false)]
[TestCase("/assets/**/*.*", true)]
public void SearchRecursively_FromTable_ReturnsExpected(
string pattern,
bool expected)
[Test]
public void SearchRecursively_RecursiveFilesExist_ReturnsTwoFiles()
{
// Arrange
var mockFileSystem = CreateMockFileSystem();

var ant = new AntDirectory(new Ant("/assets/**/*.txt"), mockFileSystem);

// Act
var files = ant.SearchRecursively(@"C:\").ToList();

// Assert
files.Should().HaveCount(2);
files.Should().Contain(f => f == @"assets\dir1\dir1_1\file1.txt");
files.Should().Contain(f => f == @"assets\dir1\dir1_1\file2.txt");
}

[Test]
public void SearchRecursively_RecursiveFilesDoNotExist_ReturnsEmpty()
{
// Arrange
var mockFileSystem = CreateMockFileSystem();

var ant = new AntDirectory(new Ant("/assets/**/*.xml"), mockFileSystem);

// Act
var files = ant.SearchRecursively(@"C:\");

// Assert
files.Should().BeEmpty();
}

[Test]
public void SearchRecursively_PatternWithQuestionMark_ReturnsTwoFiles()
{
var ant = new AntDirectory(new Ant(pattern));
var match = ant.SearchRecursively(AppDomain.CurrentDomain.BaseDirectory);
// Arrange
var mockFileSystem = CreateMockFileSystem();

var ant = new AntDirectory(new Ant("/assets/dir1/*/file?.txt"), mockFileSystem);

Assert.That(match, expected ? Is.Not.Empty : Is.Empty);
// Act
var files = ant.SearchRecursively(@"C:\").ToList();

// Assert
files.Should().HaveCount(2);
files.Should().Contain(f => f == @"assets\dir1\dir1_1\file1.txt");
files.Should().Contain(f => f == @"assets\dir1\dir1_1\file2.txt");
}

[Test]
public void SearchRecursively_PatternWithThreeQuestionMarks_ReturnsEmpty()
{
// Arrange
var mockFileSystem = CreateMockFileSystem();

var ant = new AntDirectory(new Ant("/assets/dir1/*/file???.txt"), mockFileSystem);

// Act
var files = ant.SearchRecursively(@"C:\");

// Assert
files.Should().BeEmpty();
}

[Test]
public void SearchRecursively_PatternRecursiveWithWildcard_ReturnsThreeFiles()
{
// Arrange
var mockFileSystem = CreateMockFileSystem();

var ant = new AntDirectory(new Ant("/assets/**/*.*"), mockFileSystem);

// Act
var files = ant.SearchRecursively(@"C:\").ToList();

// Assert
files.Should().HaveCount(3);
files.Should().Contain(f => f == @"assets\dir1\dir1_1\file1.txt");
files.Should().Contain(f => f == @"assets\dir1\dir1_1\file2.txt");
files.Should().Contain(f => f == @"assets\dir2\file2.zip");
}

private static MockFileSystem CreateMockFileSystem()
{
var mockFileSystem = new MockFileSystem();
mockFileSystem.AddFile(@"C:\assets\dir1\dir1_1\file1.txt", new MockFileData("data"));
mockFileSystem.AddFile(@"C:\assets\dir1\dir1_1\file2.txt", new MockFileData("data"));
mockFileSystem.AddFile(@"C:\assets\dir2\file2.zip", new MockFileData("data"));
return mockFileSystem;
}

[TestCase(true), TestCase(false)]
public void SearchRecursively_WithDirectoryPathSetting_ReturnsExpectedValue(
bool includeDirectoryPath)
{
var dirPath = AppDomain.CurrentDomain.BaseDirectory;

var ant = new AntDirectory(new Ant("/assets/dir1/*/file?.txt"));
var match = ant.SearchRecursively(dirPath, includeDirectoryPath);
var ant = new AntDirectory(new Ant("/assets/dir1/*/file?.txt"), CreateMockFileSystem());
var match = ant.SearchRecursively(@"C:\", includeDirectoryPath);

if (includeDirectoryPath)
{
Assert.That(match.FirstOrDefault(), Does.Contain(dirPath));
match.First().Should().StartWith(@"C:\");
}
else
{
Assert.That(match.FirstOrDefault(), Does.Not.Contain(dirPath));
match.First().Should().NotStartWith(@"C:\");
}
}

[Test]
public void SearchRecursively_WhenDirectoryNotFound_DoesNotThrow()
{
var ant = new AntDirectory(new Ant("*.txt"));
var ant = new AntDirectory(new Ant("*.txt"), new MockFileSystem());

Assert.DoesNotThrow(() => ant.SearchRecursively(@"C:\Octopus\Applications\production\Containers").ToList());
}
Expand Down
34 changes: 23 additions & 11 deletions AntPathMatching.Tests/AntPathMatching.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AntPathMatching</RootNamespace>
<AssemblyName>AntPathMatching.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -29,30 +30,41 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="FluentAssertions, Version=5.4.1.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\packages\FluentAssertions.5.4.1\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="nunit.framework, Version=3.6.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.6.0\lib\net40\nunit.framework.dll</HintPath>
<HintPath>..\packages\NUnit.3.6.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Abstractions, Version=2.1.0.228, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.2.1.0.228\lib\net40\System.IO.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Abstractions.TestingHelpers, Version=2.1.0.228, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.TestingHelpers.2.1.0.228\lib\net40\System.IO.Abstractions.TestingHelpers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="AntDirectoryTests.cs" />
<Compile Include="AntTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="assets\dir1\dir1_1\file1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="assets\dir1\dir1_1\file12.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="assets\dir2\file2.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion AntPathMatching.Tests/assets/dir1/dir1_1/file1.txt

This file was deleted.

1 change: 0 additions & 1 deletion AntPathMatching.Tests/assets/dir1/dir1_1/file12.txt

This file was deleted.

1 change: 0 additions & 1 deletion AntPathMatching.Tests/assets/dir2/file2.zip

This file was deleted.

6 changes: 5 additions & 1 deletion AntPathMatching.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.6.0" targetFramework="net40" />
<package id="FluentAssertions" version="5.4.1" targetFramework="net45" />
<package id="NUnit" version="3.6.0" targetFramework="net45" />
<package id="System.IO.Abstractions" version="2.1.0.228" targetFramework="net40" />
<package id="System.IO.Abstractions.TestingHelpers" version="2.1.0.228" targetFramework="net40" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net45" />
</packages>
10 changes: 7 additions & 3 deletions AntPathMatching/AntDirectory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;

namespace AntPathMatching
{
Expand All @@ -10,17 +11,20 @@ namespace AntPathMatching
public class AntDirectory : IAntDirectory
{
private readonly IAnt ant;
private IFileSystem fileSystem;

/// <summary>
/// Initializes a new <see cref="AntDirectory"/>.
/// </summary>
/// <param name="ant">Ant pattern used for directory-searching.</param>
/// <param name="fileSystem">File system to be used</param>
/// <exception cref="ArgumentNullException">Throw when <paramref name="ant"/> is null.</exception>
public AntDirectory(IAnt ant)
public AntDirectory(IAnt ant, IFileSystem fileSystem = null)
{
if (ant == null) throw new ArgumentNullException(nameof(ant));

this.ant = ant;
this.fileSystem = fileSystem ?? new FileSystem();
}

/// <summary>
Expand All @@ -32,8 +36,8 @@ public AntDirectory(IAnt ant)
/// <inheritDoc />
public IEnumerable<string> SearchRecursively(string directory, bool includeDirectoryPath = false)
{
var files = Directory.Exists(directory) ?
Directory.GetFiles(directory, "*", SearchOption.AllDirectories)
var files = fileSystem.Directory.Exists(directory) ?
fileSystem.Directory.GetFiles(directory, "*", SearchOption.AllDirectories)
: new string[0];

foreach (var file in files)
Expand Down
10 changes: 9 additions & 1 deletion AntPathMatching/AntPathMatching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AntPathMatching</RootNamespace>
<AssemblyName>AntPathMatching</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
Expand All @@ -33,7 +33,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.IO.Abstractions, Version=2.1.0.228, Culture=neutral, PublicKeyToken=96bf224d23c43e59, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Abstractions.2.1.0.228\lib\net40\System.IO.Abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Ant.cs" />
Expand All @@ -44,6 +49,9 @@
<Compile Include="IAntFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
3 changes: 3 additions & 0 deletions AntPathMatching/AntPathMatching.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<projectUrl>https://github.com/WichardRiezebos/ant-path-matching</projectUrl>
<iconUrl>https://raw.githubusercontent.com/WichardRiezebos/ant-path-matching/master/icon.png</iconUrl>
<tags>ant path matching directory regex search recursive files</tags>
<dependencies>
<dependency id="System.IO.Abstractions" version="[2.1.0.228,3.0.0.0)" />
</dependencies>
</metadata>
</package>
4 changes: 4 additions & 0 deletions AntPathMatching/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.IO.Abstractions" version="2.1.0.228" targetFramework="net40" />
</packages>

0 comments on commit 870b0f9

Please sign in to comment.