From 579bbd42f80aa2c597d0f0b6a3f25ca5f4e0a319 Mon Sep 17 00:00:00 2001 From: ansgarz Date: Fri, 28 Jan 2022 19:17:51 +0100 Subject: [PATCH] fix createFile --- .../ubuntu/filesystem/base/Filesystem.kt | 15 ++----- .../filesystem/base/FilesystemKtTest.kt | 44 +++++++++++++++++++ .../server/infrastructure/NetworkKtTest.kt | 1 - 3 files changed, 48 insertions(+), 12 deletions(-) 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 31a76b7..b7e68f1 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 @@ -88,17 +88,10 @@ fun Prov.createFile( cmd(withSudo + "install -m $posixFilePermission /dev/null $fullyQualifiedFilename") } if (text != null) { - if (sudo) { - cmd( - "printf " + text.escapeProcentForPrintf() - .escapeAndEncloseByDoubleQuoteForShell() + " | sudo tee $fullyQualifiedFilename > /dev/null" - ) - } else { - cmd( - "printf " + text.escapeProcentForPrintf() - .escapeAndEncloseByDoubleQuoteForShell() + " > $fullyQualifiedFilename" - ) - } + cmd( + "printf '%s' " + text + .escapeAndEncloseByDoubleQuoteForShell() + " | ${if (sudo) "sudo" else ""} tee $fullyQualifiedFilename > /dev/null" + ) } else { cmd(withSudo + "touch $fullyQualifiedFilename") } 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 36e6d45..9d6a5b4 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 @@ -2,6 +2,7 @@ package org.domaindrivenarchitecture.provs.framework.ubuntu.filesystem.base import org.domaindrivenarchitecture.provs.test.defaultTestContainer import org.domaindrivenarchitecture.provs.test.tags.ContainerTest +import org.domaindrivenarchitecture.provs.test.testLocal import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test import java.io.File @@ -9,6 +10,49 @@ import java.io.File 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 @ContainerTest fun checkingCreatingDeletingFile() { diff --git a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/NetworkKtTest.kt b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/NetworkKtTest.kt index 9589c0c..1301707 100644 --- a/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/NetworkKtTest.kt +++ b/src/test/kotlin/org/domaindrivenarchitecture/provs/server/infrastructure/NetworkKtTest.kt @@ -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 // check file content snippet - assertTrue(res.success) assertTrue(p.fileContainsText("/etc/netplan/99-loopback.yaml", content = "- 192.168.5.1/32", sudo = true)) } } \ No newline at end of file