Skip to content

Commit

Permalink
#410 - Fix stub filtering by prefix and delimiter (#411)
Browse files Browse the repository at this point in the history
* Add test for listing objects with path-like prefix

* #410 - Fix stub filtering by prefix and delimiter
  • Loading branch information
hbibel committed Mar 27, 2023
1 parent bb8f158 commit 2655650
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
30 changes: 22 additions & 8 deletions zio-s3/src/main/scala/zio/s3/Test.scala
Expand Up @@ -112,18 +112,32 @@ object Test {
): IO[S3Exception, S3ObjectListing] =
Files
.find(path / bucketName) {
case (p, _) if options.delimiter.nonEmpty =>
options.prefix.fold(true)((path / bucketName).relativize(p).toString().startsWith)
case (p, _) =>
options.prefix.fold(true)(p.filename.toString().startsWith)
case (_, fileAttr) =>
fileAttr.isRegularFile
}
.mapZIO { filePath =>
Files.readAttributes[BasicFileAttributes](filePath).map { attrs =>
attrs -> (path / bucketName).relativize(filePath).toString()
}
}
.filter {
case (_, relativePath) =>
options.prefix.fold(true)(relativePath.startsWith)
}
.filter {
case (_, relativePath) =>
options.delimiter.fold(true) { delim =>
relativePath
.stripPrefix(options.prefix.getOrElse(""))
.stripSuffix(delim)
.indexOf(delim) < 0
}
}
.mapZIO(p => Files.readAttributes[BasicFileAttributes](p).map(a => a -> p))
.filter { case (attr, _) => attr.isRegularFile }
.map {
case (attr, f) =>
case (attr, relativePath) =>
S3ObjectSummary(
bucketName,
(path / bucketName).relativize(f).toString(),
relativePath,
attr.lastModifiedTime().toInstant,
attr.size()
)
Expand Down
21 changes: 18 additions & 3 deletions zio-s3/src/test/scala/zio/s3/S3Test.scala
Expand Up @@ -101,17 +101,32 @@ object S3Suite {
)
)
},
test("list objects with path-like prefix") {
for {
succeed <- listObjects(bucketName, ListObjectOptions.from("dir1", 10))
} yield assert(succeed.objectSummaries.map(_.key))(
hasSameElements(List("dir1/hello.txt", "dir1/user.csv"))
)
},
test("list objects with not match prefix") {
for {
succeed <- listObjects(bucketName, ListObjectOptions.from("blah", 10))
} yield assertTrue(succeed.bucketName -> succeed.objectSummaries == bucketName -> Chunk.empty)
},
test("list objects with delimiter") {
test("list objects with prefix and delimiter") {
for {
succeed <- listObjects(bucketName, ListObjectOptions(Some("dir1/"), 10, Some("/"), None))
} yield assertTrue(
succeed.bucketName -> succeed.objectSummaries
.map(_.key) == bucketName -> Chunk("dir1/hello.txt", "dir1/user.csv")
succeed.bucketName -> succeed.objectSummaries.map(_.key) ==
bucketName -> Chunk("dir1/hello.txt", "dir1/user.csv")
)
},
test("list objects with delimiter") {
for {
succeed <- listObjects(bucketName, ListObjectOptions(None, 10, Some("/"), None))
} yield assertTrue(
succeed.bucketName -> succeed.objectSummaries.map(_.key) ==
bucketName -> Chunk("console.log")
)
},
test("list objects with startAfter dir1/hello.txt") {
Expand Down

0 comments on commit 2655650

Please sign in to comment.