Skip to content

Commit 690bc1c

Browse files
committed
new tests
basic globbing support
1 parent ca7481a commit 690bc1c

File tree

9 files changed

+103
-17
lines changed

9 files changed

+103
-17
lines changed

src/GeekLearning.Storage.Azure/AzureStore.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,23 @@ public async Task<IFileReference[]> ListAsync(string path, bool recursive)
147147

148148
public async Task<IFileReference[]> ListAsync(string path, string searchPattern, bool recursive)
149149
{
150+
if (string.IsNullOrWhiteSpace(path))
151+
{
152+
path = null;
153+
}
154+
else
155+
{
156+
if (!path.EndsWith("/"))
157+
{
158+
path = path + "/";
159+
}
160+
}
161+
162+
string prefix = path;
150163
var firstWildCard = searchPattern.IndexOf('*');
151164
if (firstWildCard >= 0)
152165
{
153-
path += searchPattern.Substring(0, firstWildCard);
166+
prefix += searchPattern.Substring(0, firstWildCard);
154167
searchPattern = searchPattern.Substring(firstWildCard);
155168
}
156169

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

171-
var pathMap = results.Select(blob => new Internal.AzureFileReference(blob)).ToDictionary(x => x.Path);
172-
throw new NotSupportedException();
173-
//var filteredResults = matcher.Execute(
174-
// new Internal.AzureListDirectoryWrapper(path,
175-
// pathMap.Values));
184+
var pathMap = results.OfType<ICloudBlob>().Select(blob => new Internal.AzureFileReference(blob)).ToDictionary(x => x.Path);
185+
186+
var filteredResults = matcher.Execute(
187+
new Internal.AzureListDirectoryWrapper(path,
188+
pathMap));
176189

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

180193
public async Task DeleteAsync(IPrivateFileReference file)

src/GeekLearning.Storage.Azure/Internal/AzureListDirectoryWrapper.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,33 @@ namespace GeekLearning.Storage.Azure.Internal
99
{
1010
public class AzureListDirectoryWrapper : DirectoryInfoBase
1111
{
12+
private string name;
1213
private string fullName;
14+
private string path;
15+
private Dictionary<string, AzureFileReference> files;
1316

1417
public AzureListDirectoryWrapper(FileSystemInfoBase childrens)
1518
{
1619
this.fullName = "root";
1720
this.ParentDirectory = null;
1821
}
1922

23+
public AzureListDirectoryWrapper(string path, Dictionary<string, AzureFileReference> files)
24+
{
25+
this.path = path;
26+
this.files = files;
27+
this.fullName = path;
28+
var lastSlash = path.LastIndexOf('/');
29+
if (lastSlash >= 0)
30+
{
31+
this.name = path.Substring(lastSlash + 1);
32+
}
33+
else
34+
{
35+
this.name = path;
36+
}
37+
}
38+
2039
public AzureListDirectoryWrapper(CloudBlobDirectory blobDirectory, AzureListDirectoryWrapper parent = null)
2140
{
2241
this.ParentDirectory = parent;
@@ -35,7 +54,7 @@ public override string Name
3554
{
3655
get
3756
{
38-
return fullName;
57+
return name;
3958
}
4059
}
4160

@@ -46,7 +65,7 @@ public override DirectoryInfoBase ParentDirectory
4665

4766
public override IEnumerable<FileSystemInfoBase> EnumerateFileSystemInfos()
4867
{
49-
throw new NotImplementedException();
68+
return this.files.Values.Select(file => new AzureListFileWrapper(file.CloudBlob, this));
5069
}
5170

5271
public override DirectoryInfoBase GetDirectory(string path)
@@ -56,7 +75,7 @@ public override DirectoryInfoBase GetDirectory(string path)
5675

5776
public override FileInfoBase GetFile(string path)
5877
{
59-
throw new NotImplementedException();
78+
return new AzureListFileWrapper(this.files[path].CloudBlob, this);
6079
}
6180
}
6281
}

src/GeekLearning.Storage.Azure/Internal/AzureListFileWrapper.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ namespace GeekLearning.Storage.Azure.Internal
99
{
1010
public class AzureListFileWrapper : FileInfoBase
1111
{
12-
private CloudBlob blob;
12+
private ICloudBlob blob;
13+
private string name;
14+
private AzureListDirectoryWrapper parent;
1315

14-
public AzureListFileWrapper(CloudBlob blob)
16+
public AzureListFileWrapper(ICloudBlob blob, AzureListDirectoryWrapper parent)
1517
{
1618
this.blob = blob;
19+
var lastSlash = blob.Name.LastIndexOf('/');
20+
if (lastSlash >= 0)
21+
{
22+
this.name = blob.Name.Substring(lastSlash + 1);
23+
}
24+
else
25+
{
26+
this.name = blob.Name;
27+
}
28+
this.parent = parent;
1729
}
1830

1931
public override string FullName
@@ -28,15 +40,15 @@ public override string Name
2840
{
2941
get
3042
{
31-
return this.blob.Name;
43+
return name;
3244
}
3345
}
3446

3547
public override DirectoryInfoBase ParentDirectory
3648
{
3749
get
3850
{
39-
throw new NotImplementedException();
51+
return this.parent;
4052
}
4153
}
4254
}

src/GeekLearning.Storage.FileSystem/FileSystemStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ public Task<IFileReference[]> ListAsync(string path, string searchPattern, bool
7373
Microsoft.Extensions.FileSystemGlobbing.Matcher matcher = new Microsoft.Extensions.FileSystemGlobbing.Matcher(StringComparison.Ordinal);
7474
matcher.AddInclude(searchPattern);
7575

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

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

tests/GeekLearning.Integration.Test/ListTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,43 @@ public async Task ListSubDirectoryFilesWithTrailingSlash(string storeName)
9494
Assert.Empty(missingFiles);
9595
Assert.Empty(unexpectedFiles);
9696
}
97+
98+
[Theory(DisplayName = nameof(ExtensionGlobbing)), InlineData("azure"), InlineData("filesystem")]
99+
public async Task ExtensionGlobbing(string storeName)
100+
{
101+
var storageFactory = this.storeFixture.Services.GetRequiredService<IStorageFactory>();
102+
103+
var store = storageFactory.GetStore(storeName);
104+
105+
var expected = new string[] { "Globbing/template.hbs", "Globbing/template-header.hbs" };
106+
107+
var results = await store.ListAsync("Globbing", "*.hbs");
108+
109+
var missingFiles = expected.Except(results.Select(f => f.Path)).ToArray();
110+
111+
var unexpectedFiles = results.Select(f => f.Path).Except(expected).ToArray();
112+
113+
Assert.Empty(missingFiles);
114+
Assert.Empty(unexpectedFiles);
115+
}
116+
117+
[Theory(DisplayName = nameof(FileNameGlobbing)), InlineData("azure"), InlineData("filesystem")]
118+
public async Task FileNameGlobbing(string storeName)
119+
{
120+
var storageFactory = this.storeFixture.Services.GetRequiredService<IStorageFactory>();
121+
122+
var store = storageFactory.GetStore(storeName);
123+
124+
var expected = new string[] { "Globbing/template.hbs", "Globbing/template.mustache" };
125+
126+
var results = await store.ListAsync("Globbing", "template.*");
127+
128+
var missingFiles = expected.Except(results.Select(f => f.Path)).ToArray();
129+
130+
var unexpectedFiles = results.Select(f => f.Path).Except(expected).ToArray();
131+
132+
Assert.Empty(missingFiles);
133+
Assert.Empty(unexpectedFiles);
134+
}
97135
}
98136
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
template.header.hbs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
template.header.mustache
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
template.hbs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
template.mustache

0 commit comments

Comments
 (0)