Skip to content

Commit 68002d0

Browse files
Integration test for ls
1 parent 43f966a commit 68002d0

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,13 @@ public DirectoryStream<Path> newDirectoryStream(Path dir, final Filter<? super P
562562
final CloudStoragePath cloudPath = CloudStorageUtil.checkPath(dir);
563563
checkNotNull(filter);
564564
String prefix = cloudPath.toString();
565-
final Iterator<Blob> blobIterator = storage.list(cloudPath.bucket(), Storage.BlobListOption.prefix(prefix), Storage.BlobListOption.fields()).iterateAll();
565+
final Iterator<Blob> blobIterator = storage.list(
566+
cloudPath.bucket(),
567+
Storage.BlobListOption.prefix(prefix),
568+
Storage.BlobListOption.currentDirectory()
569+
// this breaks the listing (bug?)
570+
//Storage.BlobListOption.fields()
571+
).iterateAll();
566572
return new DirectoryStream<Path>() {
567573
@Override
568574
public Iterator<Path> iterator() {

gcloud-java-contrib/gcloud-java-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import java.nio.ByteBuffer;
2828
import java.nio.channels.ReadableByteChannel;
2929
import java.nio.channels.SeekableByteChannel;
30+
import java.nio.file.FileSystem;
3031
import java.nio.file.Files;
3132
import java.nio.file.NoSuchFileException;
3233
import java.nio.file.Path;
3334
import java.nio.file.StandardOpenOption;
35+
import java.util.ArrayList;
3436
import java.util.Arrays;
3537
import java.util.List;
3638
import java.util.Random;
@@ -317,6 +319,29 @@ public void testCopy() throws IOException {
317319
}
318320
}
319321

322+
@Test
323+
public void testListFiles() throws IOException {
324+
try (FileSystem fs = getTestBucket()) {
325+
List<Path> goodPaths = new ArrayList<>();
326+
List<Path> paths = new ArrayList<>();
327+
goodPaths.add(fs.getPath("dir/angel"));
328+
goodPaths.add(fs.getPath("dir/alone"));
329+
paths.add(fs.getPath("dir/dir2/another_angel"));
330+
paths.add(fs.getPath("atroot"));
331+
paths.addAll(goodPaths);
332+
goodPaths.add(fs.getPath("dir/dir2/"));
333+
for (Path path : paths) {
334+
fillFile(storage, path.toString(), SML_SIZE);
335+
}
336+
337+
List<Path> got = new ArrayList<>();
338+
for (Path path : Files.newDirectoryStream(fs.getPath("dir/"))) {
339+
got.add(path);
340+
}
341+
assertThat(got).containsExactlyElementsIn(goodPaths);
342+
}
343+
}
344+
320345
private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOException {
321346
ByteBuffer buf = ByteBuffer.wrap(outputBuf);
322347
int sofar = 0;

gcloud-java-storage/src/main/java/com/google/cloud/storage/testing/FakeStorageRpc.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws Stora
9393
@Override
9494
public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?> options)
9595
throws StorageException {
96+
String delimiter = null;
9697
String preprefix = "";
9798
for (Map.Entry<Option, ?> e : options.entrySet()) {
9899
switch (e.getKey()) {
@@ -102,6 +103,9 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
102103
preprefix = preprefix.substring(1);
103104
}
104105
break;
106+
case DELIMITER:
107+
delimiter = (String) e.getValue();
108+
break;
105109
case FIELDS:
106110
// ignore and return all the fields
107111
break;
@@ -117,17 +121,7 @@ public Tuple<String, Iterable<StorageObject>> list(String bucket, Map<Option, ?>
117121
if (!so.getName().startsWith(prefix)) {
118122
continue;
119123
}
120-
int nextSlash = so.getName().indexOf("/", prefix.length());
121-
if (nextSlash >= 0) {
122-
String folderName = so.getName().substring(0, nextSlash + 1);
123-
if (folders.containsKey(folderName)) {
124-
continue;
125-
}
126-
StorageObject fakeFolder = new StorageObject();
127-
fakeFolder.setName(folderName);
128-
fakeFolder.setBucket(so.getBucket());
129-
fakeFolder.setGeneration(so.getGeneration());
130-
folders.put(folderName, fakeFolder);
124+
if (processedAsFolder(so, delimiter, prefix, folders)) {
131125
continue;
132126
}
133127
values.add(so);
@@ -333,4 +327,25 @@ private void potentiallyThrow(Map<Option, ?> options) throws UnsupportedOperatio
333327
throw new UnsupportedOperationException();
334328
}
335329
}
330+
331+
// Returns true if this is a folder. Adds it to folders if it isn't already there.
332+
private boolean processedAsFolder(StorageObject so, String delimiter, String prefix, /* inout */ Map<String, StorageObject> folders) {
333+
if (delimiter == null) {
334+
return false;
335+
}
336+
int nextSlash = so.getName().indexOf(delimiter, prefix.length());
337+
if (nextSlash < 0) {
338+
return false;
339+
}
340+
String folderName = so.getName().substring(0, nextSlash + 1);
341+
if (folders.containsKey(folderName)) {
342+
return true;
343+
}
344+
StorageObject fakeFolder = new StorageObject();
345+
fakeFolder.setName(folderName);
346+
fakeFolder.setBucket(so.getBucket());
347+
fakeFolder.setGeneration(so.getGeneration());
348+
folders.put(folderName, fakeFolder);
349+
return true;
350+
}
336351
}

0 commit comments

Comments
 (0)