-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#323] added Files.createTemp* Managed methods; added Files.deleteRec… (
#327) * [#323] added Files.createTemp* Managed methods; added Files.deleteRecursive method * [#323] added default arguments to createTempFileManaged Co-authored-by: Vitalii Honta <[email protected]> Co-authored-by: Lachlan O'Dea <[email protected]>
- Loading branch information
1 parent
45331a2
commit fbbf5d9
Showing
3 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
target | ||
.sbtopts | ||
project/.sbt | ||
*.tmp | ||
# if you are here to add your IDE's files please read this instead: | ||
# https://stackoverflow.com/questions/7335420/global-git-ignore#22885996 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package zio.nio.file | ||
|
||
import zio.{ Chunk, Ref } | ||
import zio.nio.BaseSpec | ||
import zio.nio.core.file.Path | ||
import zio.test._ | ||
import zio.test.Assertion._ | ||
|
||
object FilesSpec extends BaseSpec { | ||
|
||
override def spec = | ||
suite("FilesSpec")( | ||
testM("createTempFileInManaged cleans up temp file") { | ||
val sampleFileContent = Chunk.fromArray("createTempFileInManaged works!".getBytes) | ||
for { | ||
pathRef <- Ref.make[Option[Path]](None) | ||
readBytes <- Files | ||
.createTempFileInManaged(dir = Path(".")) | ||
.use { tmpFile => | ||
pathRef.set(Some(tmpFile)) *> writeAndThenRead(tmpFile)(sampleFileContent) | ||
} | ||
Some(tmpFilePath) <- pathRef.get | ||
tmpFileExistsAfterUsage <- Files.exists(tmpFilePath) | ||
} yield assert(readBytes)(equalTo(sampleFileContent)) && | ||
assert(tmpFileExistsAfterUsage)(isFalse) | ||
}, | ||
testM("createTempFileManaged cleans up temp file") { | ||
val sampleFileContent = Chunk.fromArray("createTempFileManaged works!".getBytes) | ||
for { | ||
pathRef <- Ref.make[Option[Path]](None) | ||
readBytes <- Files | ||
.createTempFileManaged() | ||
.use { tmpFile => | ||
pathRef.set(Some(tmpFile)) *> writeAndThenRead(tmpFile)(sampleFileContent) | ||
} | ||
Some(tmpFilePath) <- pathRef.get | ||
tmpFileExistsAfterUsage <- Files.exists(tmpFilePath) | ||
} yield assert(readBytes)(equalTo(sampleFileContent)) && | ||
assert(tmpFileExistsAfterUsage)(isFalse) | ||
}, | ||
testM("createTempDirectoryManaged cleans up temp dir") { | ||
val sampleFileContent = Chunk.fromArray("createTempDirectoryManaged works!".getBytes) | ||
for { | ||
pathRef <- Ref.make[Option[Path]](None) | ||
readBytes <- Files | ||
.createTempDirectoryManaged( | ||
prefix = None, | ||
fileAttributes = Nil | ||
) | ||
.use { tmpDir => | ||
val sampleFile = tmpDir / "createTempDirectoryManaged" | ||
pathRef.set(Some(tmpDir)) *> createAndWriteAndThenRead(sampleFile)(sampleFileContent) | ||
} | ||
Some(tmpFilePath) <- pathRef.get | ||
tmpFileExistsAfterUsage <- Files.exists(tmpFilePath) | ||
} yield assert(readBytes)(equalTo(sampleFileContent)) && | ||
assert(tmpFileExistsAfterUsage)(isFalse) | ||
}, | ||
testM("createTempDirectoryManaged (dir) cleans up temp dir") { | ||
val sampleFileContent = Chunk.fromArray("createTempDirectoryManaged(dir) works!".getBytes) | ||
for { | ||
pathRef <- Ref.make[Option[Path]](None) | ||
readBytes <- Files | ||
.createTempDirectoryManaged( | ||
dir = Path("."), | ||
prefix = None, | ||
fileAttributes = Nil | ||
) | ||
.use { tmpDir => | ||
val sampleFile = tmpDir / "createTempDirectoryManaged2" | ||
pathRef.set(Some(tmpDir)) *> createAndWriteAndThenRead(sampleFile)(sampleFileContent) | ||
} | ||
Some(tmpFilePath) <- pathRef.get | ||
tmpFileExistsAfterUsage <- Files.exists(tmpFilePath) | ||
} yield assert(readBytes)(equalTo(sampleFileContent)) && | ||
assert(tmpFileExistsAfterUsage)(isFalse) | ||
} | ||
) | ||
|
||
private def createAndWriteAndThenRead(file: Path)(bytes: Chunk[Byte]) = | ||
Files.createFile(file) *> writeAndThenRead(file)(bytes) | ||
|
||
private def writeAndThenRead(file: Path)(bytes: Chunk[Byte]) = | ||
Files.writeBytes(file, bytes) *> Files.readAllBytes(file) | ||
} |