createFile also creates a potentially missing directory, dependent on parameter createDirIfMissing
This commit is contained in:
parent
00043d2828
commit
7ca6723b4d
4 changed files with 64 additions and 24 deletions
|
@ -1,5 +1,5 @@
|
||||||
<component name="ProjectRunConfigurationManager">
|
<component name="ProjectRunConfigurationManager">
|
||||||
<configuration default="false" name="test" type="JUnit" factoryName="JUnit">
|
<configuration default="false" name="testquick" type="JUnit" factoryName="JUnit">
|
||||||
<module name="provs.test" />
|
<module name="provs.test" />
|
||||||
<option name="PACKAGE_NAME" value="org" />
|
<option name="PACKAGE_NAME" value="org" />
|
||||||
<option name="MAIN_CLASS_NAME" value="" />
|
<option name="MAIN_CLASS_NAME" value="" />
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
package org.domaindrivenarchitecture.provs.framework.core.platforms
|
package org.domaindrivenarchitecture.provs.framework.core.platforms
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.ProgressType
|
import org.domaindrivenarchitecture.provs.framework.core.*
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.Prov
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.ProvResult
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.escapeAndEncloseByDoubleQuoteForShell
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.processors.LocalProcessor
|
import org.domaindrivenarchitecture.provs.framework.core.processors.LocalProcessor
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.processors.Processor
|
import org.domaindrivenarchitecture.provs.framework.core.processors.Processor
|
||||||
|
|
||||||
|
@ -52,9 +49,13 @@ class UbuntuProv internal constructor(
|
||||||
|
|
||||||
private fun commandWithDirAndSudo(cmd: String, dir: String?, sudo: Boolean): String {
|
private fun commandWithDirAndSudo(cmd: String, dir: String?, sudo: Boolean): String {
|
||||||
val cmdWithDir = if (dir == null) cmd else "cd $dir && $cmd"
|
val cmdWithDir = if (dir == null) cmd else "cd $dir && $cmd"
|
||||||
return if (sudo) cmdWithDir.sudoize() else cmdWithDir
|
return if (sudo) cmdWithDir.sudoizeCommand() else cmdWithDir
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.sudoize(): String {
|
/**
|
||||||
return "sudo " + SHELL + " -c " + this.escapeAndEncloseByDoubleQuoteForShell()
|
* Returns a command encapsulated in a shell command and executed with sudo.
|
||||||
|
* For simple cases consider using sudo as prefix if the command instead.
|
||||||
|
*/
|
||||||
|
internal fun String.sudoizeCommand(): String {
|
||||||
|
return "sudo -E " + SHELL + " -c " + this.escapeAndEncloseByDoubleQuoteForShell()
|
||||||
}
|
}
|
|
@ -1,8 +1,7 @@
|
||||||
package org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base
|
package org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base
|
||||||
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.platforms.SHELL
|
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.*
|
import org.domaindrivenarchitecture.provs.framework.core.*
|
||||||
import org.domaindrivenarchitecture.provs.framework.core.getLocalFileContent
|
import org.domaindrivenarchitecture.provs.framework.core.platforms.sudoizeCommand
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
@ -89,10 +88,17 @@ fun Prov.createFile(
|
||||||
text: String?,
|
text: String?,
|
||||||
posixFilePermission: String? = null,
|
posixFilePermission: String? = null,
|
||||||
sudo: Boolean = false,
|
sudo: Boolean = false,
|
||||||
overwriteIfExisting: Boolean = true
|
overwriteIfExisting: Boolean = true,
|
||||||
|
createDirIfMissing: Boolean = true,
|
||||||
): ProvResult = taskWithResult {
|
): ProvResult = taskWithResult {
|
||||||
val maxBlockSize = 50000
|
val maxBlockSize = 50000
|
||||||
val withSudo = if (sudo) "sudo " else ""
|
val withSudo = if (sudo) "sudo " else ""
|
||||||
|
val file = File(fullyQualifiedFilename)
|
||||||
|
val dir = file.parent?.toString()
|
||||||
|
|
||||||
|
if (dir != null && dir != "" && createDirIfMissing && !checkDir(dir, sudo = sudo)) {
|
||||||
|
createDirs(dir, sudo = sudo)
|
||||||
|
}
|
||||||
|
|
||||||
posixFilePermission?.let {
|
posixFilePermission?.let {
|
||||||
ensureValidPosixFilePermission(posixFilePermission)
|
ensureValidPosixFilePermission(posixFilePermission)
|
||||||
|
@ -358,17 +364,6 @@ private fun ensureValidPosixFilePermission(posixFilePermission: String) {
|
||||||
if (!Regex("^[0-7]{3}$").matches(posixFilePermission)) throw IllegalArgumentException("Wrong file permission ($posixFilePermission), permission must consist of 3 digits as e.g. 664")
|
if (!Regex("^[0-7]{3}$").matches(posixFilePermission)) throw IllegalArgumentException("Wrong file permission ($posixFilePermission), permission must consist of 3 digits as e.g. 664")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a command encapsulated in a shell command and executed with sudo.
|
|
||||||
* For simple cases consider sudo as prefix instead.
|
|
||||||
* @see prefixWithSudo
|
|
||||||
*/
|
|
||||||
private fun String.sudoizeCommand(): String {
|
|
||||||
return "sudo " + SHELL + " -c " + this.escapeAndEncloseByDoubleQuoteForShell()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns path with a trailing fileSeparator if path not empty
|
* Returns path with a trailing fileSeparator if path not empty
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,7 +23,7 @@ internal class FilesystemKtTest {
|
||||||
prov.createDir("tmp")
|
prov.createDir("tmp")
|
||||||
|
|
||||||
// when
|
// when
|
||||||
val filename = "tmp/testfile9"
|
val filename = "tmp/provstestfile123"
|
||||||
val res2 = prov.createFile(filename, testtext)
|
val res2 = prov.createFile(filename, testtext)
|
||||||
val textFromFile = prov.fileContent(filename)
|
val textFromFile = prov.fileContent(filename)
|
||||||
prov.deleteFile(filename)
|
prov.deleteFile(filename)
|
||||||
|
@ -35,7 +35,7 @@ internal class FilesystemKtTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ContainerTest
|
@ContainerTest
|
||||||
fun createFile_in_container() {
|
fun createFile_successfully() {
|
||||||
// given
|
// given
|
||||||
val prov = defaultTestContainer()
|
val prov = defaultTestContainer()
|
||||||
val filename = "testfile8"
|
val filename = "testfile8"
|
||||||
|
@ -51,6 +51,50 @@ internal class FilesystemKtTest {
|
||||||
assertEquals(testtext, prov.fileContent("sudo$filename"))
|
assertEquals(testtext, prov.fileContent("sudo$filename"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@ContainerTest
|
||||||
|
fun createFile_with_dir_successfully() {
|
||||||
|
// given
|
||||||
|
val prov = defaultTestContainer()
|
||||||
|
val filename = "dir1/dir2/testfile-with-dir"
|
||||||
|
|
||||||
|
// when
|
||||||
|
val res1 = prov.createFile(filename, testtext)
|
||||||
|
val res2 = prov.createFile("sudo$filename", testtext, sudo = true)
|
||||||
|
// check idempotence
|
||||||
|
val res1b = prov.createFile(filename, testtext)
|
||||||
|
val res2b = prov.createFile("sudo$filename", testtext, sudo = true)
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertTrue(res1.success)
|
||||||
|
assertTrue(prov.checkDir("dir1/dir2"))
|
||||||
|
assertTrue(res1b.success)
|
||||||
|
|
||||||
|
assertTrue(res2.success)
|
||||||
|
assertTrue(prov.checkDir("sudodir1/dir2", sudo = true))
|
||||||
|
assertTrue(res2b.success)
|
||||||
|
assertEquals(testtext, prov.fileContent(filename))
|
||||||
|
assertEquals(testtext, prov.fileContent("sudo$filename"))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@ContainerTest
|
||||||
|
fun createFile_with_dir_fails_if_createDirIfMissing_is_false() {
|
||||||
|
// given
|
||||||
|
val prov = defaultTestContainer()
|
||||||
|
val filename = "dirDoesNotExist/dir2/testfile-with-dir"
|
||||||
|
|
||||||
|
// when
|
||||||
|
val res = prov.createFile(filename, testtext, createDirIfMissing = false)
|
||||||
|
val res2 = prov.createFile("sudo$filename", testtext, sudo = true, createDirIfMissing = false)
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertFalse(res.success)
|
||||||
|
assertFalse(res2.success)
|
||||||
|
assertFalse(prov.checkDir("dirDoesNotExist"))
|
||||||
|
assertFalse(prov.checkDir("sudodirDoesNotExist", sudo = true))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ContainerTest
|
@ContainerTest
|
||||||
fun create_large_file_in_container() {
|
fun create_large_file_in_container() {
|
||||||
|
|
Loading…
Reference in a new issue