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

fix: Retry on move #398

Merged
merged 1 commit into from
Dec 30, 2024
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
20 changes: 10 additions & 10 deletions io/src/main/scala/sbt/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1116,17 +1116,17 @@ object IO {
/**
* Moves the contents of `a` to the location specified by `b`.
* This method deletes any content already at `b` and creates any parent directories of `b` if they do not exist.
* It will first try `File.renameTo` and if that fails, resort to copying and then deleting the original file.
* In either case, the original File will not exist on successful completion of this method.
*/
def move(a: File, b: File): Unit = {
if (b.exists)
delete(b)
createDirectory(b.getParentFile)
if (!a.renameTo(b)) {
copyFile(a, b, true)
delete(a)
}
def move(a: File, b: File): Unit = move(a.toPath(), b.toPath())

/**
* Moves the contents of `a` to the location specified by `b`.
* This method deletes any content already at `b` and creates any parent directories of `b` if they do not exist.
*/
def move(a: NioPath, b: NioPath): Unit = {
createDirectory(b.getParent().toFile())
Retry(Files.move(a, b, StandardCopyOption.REPLACE_EXISTING))
()
}

/**
Expand Down
8 changes: 8 additions & 0 deletions io/src/test/scala/sbt/io/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ class IOSpec extends AnyFunSuite {
}
}

test("move should overwrite") {
IO.withTemporaryDirectory { dir =>
IO.touch(dir / "a.txt")
IO.touch(dir / "b.txt")
IO.move(dir / "a.txt", dir / "b.txt")
}
}

test("it should create valid jar files") {
IO.withTemporaryDirectory { tmpdir =>
import java.util.jar.Manifest
Expand Down
Loading