fix createFile

This commit is contained in:
ansgarz 2022-01-28 19:17:51 +01:00
parent c5052ce483
commit 579bbd42f8
3 changed files with 48 additions and 12 deletions

View file

@ -88,17 +88,10 @@ fun Prov.createFile(
cmd(withSudo + "install -m $posixFilePermission /dev/null $fullyQualifiedFilename") cmd(withSudo + "install -m $posixFilePermission /dev/null $fullyQualifiedFilename")
} }
if (text != null) { if (text != null) {
if (sudo) {
cmd( cmd(
"printf " + text.escapeProcentForPrintf() "printf '%s' " + text
.escapeAndEncloseByDoubleQuoteForShell() + " | sudo tee $fullyQualifiedFilename > /dev/null" .escapeAndEncloseByDoubleQuoteForShell() + " | ${if (sudo) "sudo" else ""} tee $fullyQualifiedFilename > /dev/null"
) )
} else {
cmd(
"printf " + text.escapeProcentForPrintf()
.escapeAndEncloseByDoubleQuoteForShell() + " > $fullyQualifiedFilename"
)
}
} else { } else {
cmd(withSudo + "touch $fullyQualifiedFilename") cmd(withSudo + "touch $fullyQualifiedFilename")
} }

View file

@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base
import org.domaindrivenarchitecture.provs.test.defaultTestContainer import org.domaindrivenarchitecture.provs.test.defaultTestContainer
import org.domaindrivenarchitecture.provs.test.tags.ContainerTest import org.domaindrivenarchitecture.provs.test.tags.ContainerTest
import org.domaindrivenarchitecture.provs.test.testLocal
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.io.File import java.io.File
@ -9,6 +10,49 @@ import java.io.File
internal class FilesystemKtTest { internal class FilesystemKtTest {
val testtext = "tabs \t\t\t triple quotes \"\"\"" + """
\\ \\\ \\\\ \\\\\
'\t%s \\ " ""
' "${'$'}arg")"
'%s' "${'$'}@" | 's/\([][!#${'$'}%&()*;<=>?\_`{|}]\)/\\\1/g;'
"${'$'}@" | sed -e 's/"/\\"/g'
apostrophe's ' " \" \' and special chars ${'$'} {} ${'$'}\{something}!§${'$'}%[]\\ äöüß ${'$'}\notakotlinvariable ${'$'}notakotlinvariable and tabs and \t are should be handled correctly
"""
@Test
fun test_createFile_locally() {
// given
val prov = testLocal()
// when
val filename = "tmp/testfile9"
val res2 = prov.createFile(filename, testtext)
val textFromFile = prov.fileContent(filename)
prov.deleteFile(filename)
// then
assertTrue(res2.success)
assertEquals(testtext, textFromFile)
}
@Test
@ContainerTest
fun test_createFile_in_container() {
// given
val prov = defaultTestContainer()
val filename = "testfile8"
// when
val res = prov.createFile(filename, testtext)
val res2 = prov.createFile("sudo$filename", testtext, sudo = true)
// then
assertTrue(res.success)
assertTrue(res2.success)
assertEquals(testtext, prov.fileContent(filename))
assertEquals(testtext, prov.fileContent("sudo$filename"))
}
@Test @Test
@ContainerTest @ContainerTest
fun checkingCreatingDeletingFile() { fun checkingCreatingDeletingFile() {

View file

@ -30,7 +30,6 @@ internal class NetworkKtTest {
// assertTrue(res.success) -- netplan is not working in an unprivileged container - see also https://askubuntu.com/questions/813588/systemctl-failed-to-connect-to-bus-docker-ubuntu16-04-container // assertTrue(res.success) -- netplan is not working in an unprivileged container - see also https://askubuntu.com/questions/813588/systemctl-failed-to-connect-to-bus-docker-ubuntu16-04-container
// check file content snippet // check file content snippet
assertTrue(res.success)
assertTrue(p.fileContainsText("/etc/netplan/99-loopback.yaml", content = "- 192.168.5.1/32", sudo = true)) assertTrue(p.fileContainsText("/etc/netplan/99-loopback.yaml", content = "- 192.168.5.1/32", sudo = true))
} }
} }