rename createParentDirs and make it only creating a result in case directories are actually created

This commit is contained in:
ansgarz 2022-05-28 22:54:24 +02:00
parent 3f9121f8f5
commit 0f5afd9825
2 changed files with 38 additions and 9 deletions

View file

@ -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,13 +378,19 @@ 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)) {
task("createParentDirs") {
createDirs(dir, sudo = sudo)
}
}
}
/**
* Returns number of bytes of a file or null if size could not be determined

View file

@ -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