diff --git a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt index 9329e05..ca7e650 100644 --- a/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt +++ b/src/main/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/Filesystem.kt @@ -96,7 +96,7 @@ fun Prov.createFile( val file = File(fullyQualifiedFilename) if (createDirIfMissing) { - createParentDir(file, sudo) + createParentDirs(file, sudo) } posixFilePermission?.let { @@ -324,8 +324,10 @@ fun Prov.createDirs( fun Prov.deleteDir(dir: String, path: String, sudo: Boolean = false): ProvResult { - if ("" == path) + // parameter "path" must not be empty in order to prevent accidental deletion of a directory in the wrong path + if ("" == path) { throw RuntimeException("In deleteDir: path must not be empty.") + } val cmd = "cd $path && rmdir $dir" return if (!sudo) { cmd(cmd) @@ -348,7 +350,7 @@ fun Prov.createSymlink( createLinkDirIfMissing: Boolean = true, ): ProvResult = task { if (createLinkDirIfMissing) { - createParentDir(link, sudo) + createParentDirs(link, sudo) } val overwriteFlag = if (overwriteIfExisting) "f" else "" cmd("ln -s$overwriteFlag $originalFile $link", sudo = sudo) @@ -376,11 +378,17 @@ fun Prov.userHome(): String { /** * Creates the parent-dir (parent path) of the specified file if parent-dir id not yet existing */ -internal fun Prov.createParentDir(file: File, sudo: Boolean = false) = task { +internal fun Prov.createParentDirs(file: File, sudo: Boolean = false) { + // This method is not defined itself as a Prov task as its main purpose is to check if the parent dir needs to be created, + // and this check needs neither to be included in the overall result nor being listed in the results report. + // But if directories need to be created, the creation itself is placed within the task "createParentDirs" + // in order to be included in the results. val dir = file.parent?.toString() if (dir != null && dir != "" && !checkDir(dir, sudo = sudo)) { - createDirs(dir, sudo = sudo) + task("createParentDirs") { + createDirs(dir, sudo = sudo) + } } } diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt index 9587af5..4741e4d 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/framework/ubuntu/filesystem/base/FilesystemKtTest.kt @@ -396,21 +396,42 @@ internal class FilesystemKtTest { // given val prov = defaultTestContainer() val filename = "parent_dir/test/file" + prov.deleteDir("test", "parent_dir") + prov.deleteDir("parent_dir", "~/") // when - val res = prov.createParentDir(File(filename)) + prov.createParentDirs(File(filename)) val dirExists = prov.checkDir("parent_dir/test") - val res2 = prov.createParentDir(File(filename)) // test idempotence + prov.createParentDirs(File(filename)) // test idempotence val dirExists2 = prov.checkDir("parent_dir/test") // then - assertTrue(res.success) assertTrue(dirExists) - assertTrue(res2.success) assertTrue(dirExists2) } + @ContainerTest + fun test_createParentDir_with_sudo() { + // given + val prov = defaultTestContainer() + val filename = "/parent_dir_sudo/test_sudo/file" + + prov.deleteDir("test_sudo", "/parent_dir_sudo", sudo = true) + prov.deleteDir("parent_dir_sudo", "/", sudo = true) + + // when + prov.task { createParentDirs(File(filename), sudo = true) } + val dirExists = prov.checkDir("/parent_dir_sudo/test_sudo", sudo = true) + // test idempotence + prov.task { createParentDirs(File(filename), sudo = true) } // include in task in order to output a result summary + val dirExists2 = prov.checkDir("/parent_dir_sudo/test_sudo", sudo = true) + + // then + assertTrue(dirExists) + assertTrue(dirExists2) + } + @ContainerTest fun test_createLink_without_dir() { // given