From 485840fefda29844883e8be83a8794de22268ace Mon Sep 17 00:00:00 2001 From: Dmitry Bushev Date: Mon, 9 Dec 2024 15:16:06 +0300 Subject: [PATCH] Project Manager fails to create directory when one exists (#11804) close #11758 Changelog: - update: FileSystemService fails to create directory if one exists --- .../file/BlockingFileSystem.scala | 5 +++- .../filesystem/FileSystemServiceSpec.scala | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/file/BlockingFileSystem.scala b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/file/BlockingFileSystem.scala index 9e7898ddadcb..791a68807461 100644 --- a/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/file/BlockingFileSystem.scala +++ b/lib/scala/project-manager/src/main/scala/org/enso/projectmanager/infrastructure/file/BlockingFileSystem.scala @@ -73,7 +73,10 @@ class BlockingFileSystem[F[+_, +_]: Sync: ErrorChannel]( /** @inheritdoc */ override def createDir(path: File): F[FileSystemFailure, Unit] = Sync[F] - .blockingOp { FileUtils.forceMkdir(path) } + .blockingOp { + if (path.exists()) throw new FileExistsException() + FileUtils.forceMkdir(path) + } .mapError(toFsFailure) .timeoutFail(OperationTimeout)(ioTimeout) diff --git a/lib/scala/project-manager/src/test/scala/org/enso/projectmanager/service/filesystem/FileSystemServiceSpec.scala b/lib/scala/project-manager/src/test/scala/org/enso/projectmanager/service/filesystem/FileSystemServiceSpec.scala index 5ecb63428aad..f3905d06a0a6 100644 --- a/lib/scala/project-manager/src/test/scala/org/enso/projectmanager/service/filesystem/FileSystemServiceSpec.scala +++ b/lib/scala/project-manager/src/test/scala/org/enso/projectmanager/service/filesystem/FileSystemServiceSpec.scala @@ -110,6 +110,29 @@ class FileSystemServiceSpec FileUtils.deleteQuietly(directoryPath) } + "create directory fail when one exists with the same name" in { + val testDir = testStorageConfig.userProjectsPath + + val directoryName = "filesystem_test_create_dir_with_same_name" + val directoryPath = new File(testDir, directoryName) + + val result1 = fileSystemService + .createDirectory(directoryPath) + .unsafeRunSync() + + result1 shouldEqual Right(()) + Files.isDirectory(directoryPath.toPath) shouldEqual true + + val result2 = fileSystemService + .createDirectory(directoryPath) + .unsafeRunSync() + + result2.isLeft shouldEqual true + + // cleanup + FileUtils.deleteQuietly(directoryPath) + } + "delete directory" in { implicit val client: WsTestClient = new WsTestClient(address)