Skip to content

Commit

Permalink
new tests
Browse files Browse the repository at this point in the history
basic globbing support
  • Loading branch information
sandorfr committed Jul 5, 2016
1 parent ca7481a commit 690bc1c
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 17 deletions.
29 changes: 21 additions & 8 deletions src/GeekLearning.Storage.Azure/AzureStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,23 @@ public async Task<IFileReference[]> ListAsync(string path, bool recursive)

public async Task<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive)
{
if (string.IsNullOrWhiteSpace(path))
{
path = null;
}
else
{
if (!path.EndsWith("/"))
{
path = path + "/";
}
}

string prefix = path;
var firstWildCard = searchPattern.IndexOf('*');
if (firstWildCard >= 0)
{
path += searchPattern.Substring(0, firstWildCard);
prefix += searchPattern.Substring(0, firstWildCard);
searchPattern = searchPattern.Substring(firstWildCard);
}

Expand All @@ -162,19 +175,19 @@ public async Task<IFileReference[]> ListAsync(string path, string searchPattern,
List<IListBlobItem> results = new List<IListBlobItem>();
do
{
var response = await this.container.Value.ListBlobsSegmentedAsync(path, recursive, BlobListingDetails.None, null, continuationToken, new BlobRequestOptions(), new OperationContext());
var response = await this.container.Value.ListBlobsSegmentedAsync(prefix, recursive, BlobListingDetails.None, null, continuationToken, new BlobRequestOptions(), new OperationContext());
continuationToken = response.ContinuationToken;
results.AddRange(response.Results);
}
while (continuationToken != null);

var pathMap = results.Select(blob => new Internal.AzureFileReference(blob)).ToDictionary(x => x.Path);
throw new NotSupportedException();
//var filteredResults = matcher.Execute(
// new Internal.AzureListDirectoryWrapper(path,
// pathMap.Values));
var pathMap = results.OfType<ICloudBlob>().Select(blob => new Internal.AzureFileReference(blob)).ToDictionary(x => x.Path);

var filteredResults = matcher.Execute(
new Internal.AzureListDirectoryWrapper(path,
pathMap));

//return filteredResults.Files.Select(x => pathMap[x.Path]).ToArray();
return filteredResults.Files.Select(x => pathMap[path + x.Path]).ToArray();
}

public async Task DeleteAsync(IPrivateFileReference file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,33 @@ namespace GeekLearning.Storage.Azure.Internal
{
public class AzureListDirectoryWrapper : DirectoryInfoBase
{
private string name;
private string fullName;
private string path;
private Dictionary<string, AzureFileReference> files;

public AzureListDirectoryWrapper(FileSystemInfoBase childrens)
{
this.fullName = "root";
this.ParentDirectory = null;
}

public AzureListDirectoryWrapper(string path, Dictionary<string, AzureFileReference> files)
{
this.path = path;
this.files = files;
this.fullName = path;
var lastSlash = path.LastIndexOf('/');
if (lastSlash >= 0)
{
this.name = path.Substring(lastSlash + 1);
}
else
{
this.name = path;
}
}

public AzureListDirectoryWrapper(CloudBlobDirectory blobDirectory, AzureListDirectoryWrapper parent = null)
{
this.ParentDirectory = parent;
Expand All @@ -35,7 +54,7 @@ public override string Name
{
get
{
return fullName;
return name;
}
}

Expand All @@ -46,7 +65,7 @@ public override DirectoryInfoBase ParentDirectory

public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
{
throw new NotImplementedException();
return this.files.Values.Select(file => new AzureListFileWrapper(file.CloudBlob, this));
}

public override DirectoryInfoBase GetDirectory(string path)
Expand All @@ -56,7 +75,7 @@ public override DirectoryInfoBase GetDirectory(string path)

public override FileInfoBase GetFile(string path)
{
throw new NotImplementedException();
return new AzureListFileWrapper(this.files[path].CloudBlob, this);
}
}
}
20 changes: 16 additions & 4 deletions src/GeekLearning.Storage.Azure/Internal/AzureListFileWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ namespace GeekLearning.Storage.Azure.Internal
{
public class AzureListFileWrapper : FileInfoBase
{
private CloudBlob blob;
private ICloudBlob blob;
private string name;
private AzureListDirectoryWrapper parent;

public AzureListFileWrapper(CloudBlob blob)
public AzureListFileWrapper(ICloudBlob blob, AzureListDirectoryWrapper parent)
{
this.blob = blob;
var lastSlash = blob.Name.LastIndexOf('/');
if (lastSlash >= 0)
{
this.name = blob.Name.Substring(lastSlash + 1);
}
else
{
this.name = blob.Name;
}
this.parent = parent;
}

public override string FullName
Expand All @@ -28,15 +40,15 @@ public override string Name
{
get
{
return this.blob.Name;
return name;
}
}

public override DirectoryInfoBase ParentDirectory
{
get
{
throw new NotImplementedException();
return this.parent;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/GeekLearning.Storage.FileSystem/FileSystemStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public Task<IFileReference[]> ListAsync(string path, string searchPattern, bool
Microsoft.Extensions.FileSystemGlobbing.Matcher matcher = new Microsoft.Extensions.FileSystemGlobbing.Matcher(StringComparison.Ordinal);
matcher.AddInclude(searchPattern);

var results = matcher.Execute(new Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoWrapper(new DirectoryInfo(path)));
var results = matcher.Execute(new Microsoft.Extensions.FileSystemGlobbing.Abstractions.DirectoryInfoWrapper(new DirectoryInfo(directoryPath)));

return Task.FromResult(results.Files
.Select(match => (IFileReference)new Internal.FileSystemFileReference(match.Path, match.Path.Replace(this.absolutePath, "").Trim('/', '\\')))
.Select(match => (IFileReference)new Internal.FileSystemFileReference(Path.Combine(directoryPath, match.Path), Path.Combine(path, match.Path).Trim('/', '\\')))
.ToArray());
}

Expand Down
38 changes: 38 additions & 0 deletions tests/GeekLearning.Integration.Test/ListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,43 @@ public async Task ListSubDirectoryFilesWithTrailingSlash(string storeName)
Assert.Empty(missingFiles);
Assert.Empty(unexpectedFiles);
}

[Theory(DisplayName = nameof(ExtensionGlobbing)), InlineData("azure"), InlineData("filesystem")]
public async Task ExtensionGlobbing(string storeName)
{
var storageFactory = this.storeFixture.Services.GetRequiredService<IStorageFactory>();

var store = storageFactory.GetStore(storeName);

var expected = new string[] { "Globbing/template.hbs", "Globbing/template-header.hbs" };

var results = await store.ListAsync("Globbing", "*.hbs");

var missingFiles = expected.Except(results.Select(f => f.Path)).ToArray();

var unexpectedFiles = results.Select(f => f.Path).Except(expected).ToArray();

Assert.Empty(missingFiles);
Assert.Empty(unexpectedFiles);
}

[Theory(DisplayName = nameof(FileNameGlobbing)), InlineData("azure"), InlineData("filesystem")]
public async Task FileNameGlobbing(string storeName)
{
var storageFactory = this.storeFixture.Services.GetRequiredService<IStorageFactory>();

var store = storageFactory.GetStore(storeName);

var expected = new string[] { "Globbing/template.hbs", "Globbing/template.mustache" };

var results = await store.ListAsync("Globbing", "template.*");

var missingFiles = expected.Except(results.Select(f => f.Path)).ToArray();

var unexpectedFiles = results.Select(f => f.Path).Except(expected).ToArray();

Assert.Empty(missingFiles);
Assert.Empty(unexpectedFiles);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template.header.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template.header.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
template.mustache

0 comments on commit 690bc1c

Please sign in to comment.