diff --git a/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala b/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala index 6b9d0da..5871ce5 100644 --- a/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala +++ b/src/main/scala/net/kemitix/s3thorp/S3MetaDataEnricher.scala @@ -8,7 +8,10 @@ import cats.effect.IO trait S3MetaDataEnricher extends S3Client { def generateKey(c: Config)(file: File): String = { - s"${c.prefix}/${c.source.toPath.relativize(file.toPath)}" + val otherPath = file.toPath.toAbsolutePath + val sourcePath = c.source.toPath + val relativePath = sourcePath.relativize(otherPath) + s"${c.prefix}/$relativePath" } def enrichWithS3MetaData(c: Config): File => Stream[IO, Either[File, S3MetaData]] = { diff --git a/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala b/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala index e33c6d7..a614ef4 100644 --- a/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala +++ b/src/test/scala/net/kemitix/s3thorp/S3MetaDataEnricherSuite.scala @@ -2,17 +2,20 @@ package net.kemitix.s3thorp import java.io.File import java.nio.file.Paths +import java.time.Instant +import cats.effect.IO import org.scalatest.FunSpec class S3MetaDataEnricherSuite extends FunSpec { + private val sourcePath = "/root/from/here/" + private val source = Paths.get(sourcePath).toFile + private val prefix = "prefix" + private val config = Config("bucket", prefix, source) + new S3MetaDataEnricher { describe("key generator") { - val path = "/root/from/here" - val source = Paths.get(path).toFile - val prefix = "prefix" - val config = Config("bucket", prefix, source) val subject = generateKey(config)_ def resolve(subdir: String): File = { @@ -35,4 +38,22 @@ class S3MetaDataEnricherSuite extends FunSpec { } override def objectHead(bucket: String, key: String) = ??? } + + describe("enrich with metadata") { + describe("when remote exists") { + val hash = "hash" + val lastModified = Instant.now() + new S3MetaDataEnricher { + override def objectHead(bucket: String, key: String) = IO(Some((hash, lastModified))) + it("returns metadata") { + val local = "localFile" + val localFile = new File(sourcePath + local) + val expectedMetadata = S3MetaData(localFile, s"$prefix/$local", hash, lastModified) + + val result: Either[File, S3MetaData] = + enrichWithS3MetaData(config)(localFile).compile.toList.unsafeRunSync().head + assertResult(Right(expectedMetadata))(result) + } + } + } }