Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rename #349

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.5.1
uses: actions/checkout@v3.3.0
with:
fetch-depth: '0'
- name: Setup Scala
Expand All @@ -41,7 +41,7 @@ jobs:
if: ${{ ((github.event_name == 'release') && (github.event.action == 'published')) || (github.event_name == 'workflow_dispatch') }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.5.1
uses: actions/checkout@v3.3.0
with:
fetch-depth: '0'
- name: Setup Scala
Expand All @@ -65,7 +65,7 @@ jobs:
if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event_name == 'published')) }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.5.1
uses: actions/checkout@v3.3.0
with:
ref: ${{ github.head_ref }}
fetch-depth: '0'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.0"
libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.1"
```

## How to use it?
Expand Down
8 changes: 8 additions & 0 deletions zio-ftp/src/main/scala/zio/ftp/FtpAccessors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ trait FtpAccessors[+A] {
*/
def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R with Scope, IOException, Unit]

/**
* Renames a file/directory. If the operation failed, an error will be emitted
*
* @param oldPath absolute path of the file/directory to rename
* @param newPath absolute path of the file/directory destination.
*/
def rename(oldPath: String, newPath: String): ZIO[Any, IOException, Unit]

}
3 changes: 3 additions & 0 deletions zio-ftp/src/main/scala/zio/ftp/SecureFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ final private class SecureFtp(unsafeClient: Client) extends FtpAccessors[Client]
.flatMap(ZStream.fromIterable(_))
.map(FtpResource.fromResource)

def rename(oldPath: String, newPath: String): ZIO[Any, IOException, Unit] =
execute(_.rename(oldPath, newPath))

def lsDescendant(path: String): ZStream[Any, IOException, FtpResource] =
ZStream
.fromZIO(
Expand Down
5 changes: 5 additions & 0 deletions zio-ftp/src/main/scala/zio/ftp/TestFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,10 @@ object TestFtp {
.refineToOrDie[IOException]
.catchAll(err => ZIO.fail(new IOException(s"Path is invalid. Cannot upload data to : $path", err)))
}

override def rename(oldPath: String, newPath: String): ZIO[Any, IOException, Unit] =
Files
.move(root / ZPath(oldPath).elements.mkString("/"), root / ZPath(newPath).elements.mkString("/"))
.catchAll(err => ZIO.fail(new IOException(s"Path is invalid. Cannot rename $oldPath to $newPath", err)))
}
}
5 changes: 5 additions & 0 deletions zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ final private class UnsecureFtp(unsafeClient: Client) extends FtpAccessors[Clien
.unit
)

def rename(oldPath: String, newPath: String): ZIO[Any, IOException, Unit] =
execute(_.rename(oldPath, newPath))
.filterOrFail(identity)(InvalidPathError(s"Path is invalid. Cannot rename $oldPath to $newPath"))
.unit

override def execute[T](f: Client => T): ZIO[Any, IOException, T] =
attemptBlockingIO(f(unsafeClient))
}
Expand Down
9 changes: 9 additions & 0 deletions zio-ftp/src/main/scala/zio/ftp/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ package object ftp {

def readFile(path: String, chunkSize: Int = 2048): ZStream[Ftp, IOException, Byte] =
ZStream.serviceWithStream(_.readFile(path, chunkSize))

def rename(oldPath: String, newPath: String): ZIO[Ftp, Exception, Unit] =
ZIO.serviceWithZIO(_.rename(oldPath, newPath))
}

object SFtp {
Expand Down Expand Up @@ -99,6 +102,9 @@ package object ftp {

def readFile(path: String, chunkSize: Int = 2048): ZStream[SFtp, IOException, Byte] =
ZStream.serviceWithStream(_.readFile(path, chunkSize))

def rename(oldPath: String, newPath: String): ZIO[SFtp, Exception, Unit] =
ZIO.serviceWithZIO(_.rename(oldPath, newPath))
}

object StubFtp {
Expand Down Expand Up @@ -135,6 +141,9 @@ package object ftp {

def readFile(path: String, chunkSize: Int = 2048): ZStream[StubFtp, IOException, Byte] =
ZStream.serviceWithStream(_.readFile(path, chunkSize))

def rename(oldPath: String, newPath: String): ZIO[StubFtp, Exception, Unit] =
ZIO.serviceWithZIO(_.rename(oldPath, newPath))
}

def unsecure(settings: UnsecureFtpSettings): ZLayer[Scope, ConnectionError, Ftp] =
Expand Down
23 changes: 20 additions & 3 deletions zio-ftp/src/test/scala/zio/ftp/SecureFtpSpec.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package zio.ftp

import zio.ZIO.{ acquireRelease, attempt, attemptBlockingIO }

import java.net.{ InetSocketAddress, Proxy }
import java.nio.file.{ Files, Paths }
import zio._
import zio.ftp.SFtp._
import zio.stream.ZPipeline.utf8Decode
import zio.stream.ZStream
import zio.test.Assertion._
import zio.test._

import java.net.{ InetSocketAddress, Proxy }
import java.nio.file.{ Files, Paths }
import scala.io.Source

object Load
Expand Down Expand Up @@ -197,6 +196,24 @@ object SecureFtpSpec extends ZIOSpecDefault {
for {
version <- execute(_.version())
} yield assert(version.toString)(isNonEmptyString)
},
test("rename valid path") {
val oldPath = home.resolve("dir1/to-rename.txt")
val newPath = home.resolve("dir1/to-rename-destination.txt")
Files.createFile(oldPath)

(
for {
success <- rename("/dir1/to-rename.txt", "/dir1/to-rename-destination.txt").as(true)
oldFileExists <- attempt(Files.exists(oldPath))
newFileExists <- attempt(Files.exists(newPath))
} yield assertTrue(success && !oldFileExists && newFileExists)
) <* attempt(Files.delete(newPath))
},
test("rename fail when invalid path") {
for {
invalid <- rename("/dont-exist", "dont-exist-destination").flip.map(_.getMessage)
} yield assertTrue(invalid == "No such file")
}
).provideCustomLayerShared(sftp)
}
17 changes: 17 additions & 0 deletions zio-ftp/src/test/scala/zio/ftp/StubFtpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ object StubFtpSpec extends ZIOSpecDefault {
failure <- upload("/dont-exist/hello-world.txt", data).flip.map(_.getMessage)

} yield assertTrue(failure == "Path is invalid. Cannot upload data to : /dont-exist/hello-world.txt")
},
test("rename a file") {
val oldPath = home / "to-rename.txt"
val newPath = home / "to-rename-destination.txt"

(for {
_ <- Files.createFile(oldPath)
success <- rename("/to-rename.txt", "/to-rename-destination.txt").as(true)

oldFileExists <- Files.exists(oldPath)
newFileExists <- Files.exists(newPath)
} yield assertTrue(success && !oldFileExists && newFileExists)) <* Files.delete(newPath)
},
test("rename a file fails when old path doesn't exist") {
for {
failure <- rename("/dont-exist.txt", "/dont-exist-destination.txt").flip.map(_.getMessage)
} yield assertTrue(failure == "Path is invalid. Cannot rename /dont-exist.txt to /dont-exist-destination.txt")
}
).provideCustomLayerShared(stubFtp)
}
17 changes: 17 additions & 0 deletions zio-ftp/src/test/scala/zio/ftp/UnsecureFtpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@ object FtpSuite {
failure <- upload("/dont-exist/hello-world.txt", data).flip.map(_.getMessage)
} yield assertTrue(failure == "Path is invalid. Cannot upload data to : /dont-exist/hello-world.txt")
},
test("rename valid path") {
val oldPath = home / "to-rename.txt"
val newPath = home / "to-rename-destination.txt"

(for {
_ <- Files.createFile(oldPath)
success <- rename("/to-rename.txt", "/to-rename-destination.txt").as(true)

oldFileExists <- Files.exists(oldPath)
newFileExists <- Files.exists(newPath)
} yield assertTrue(success && !oldFileExists && newFileExists)) <* Files.delete(newPath)
},
test("rename fail when invalid path") {
for {
invalid <- rename("/dont-exist", "/dont-exist-destination").flip.map(_.getMessage)
} yield assertTrue(invalid == "Path is invalid. Cannot rename /dont-exist to /dont-exist-destination")
},
test("call noOp underlying client") {
for {
noOp <- execute(_.sendNoOp())
Expand Down